Как определить, почему фиктивный PHPUnit терпит неудачу?

Я хочу провести модульное тестирование следующего класса.

<?php
namespace Gpx\Handlers;
use Gpx\EntityInfrastructure\Model\Events\SessionInvalidated;
use Gpx\EntityInfrastructure\Model\Payload;
use Gpx\Hfx\Framework\MessageTransportApplication\Handler\SynchronousHandlerInterface;
use Gpx\Hfx\Framework\MessageTransportApplication\Handler\MessageHandlingContextInterface;
use Gpx\HfxEventSourcing\HfxAggregateRoot;
use Gpx\HfxEventSourcing\HfxProjectionHelper;
use Gpx\HfxEventSourcing\HfxEventMetadata;
use Gpx\HfxEventSourcing\HfxRepository;
use Gpx\Hfx\MessageTransport\Response\SendableResponse;

class BroadcastSessionInvalidated implements SynchronousHandlerInterface
{

/** @var HfxRepository  */
private $repository;

/** @var  HfxProjectionHelper */
private $projectionHelper;public function __construct(HfxRepository $repository, HfxProjectionHelper $projectionHelper)
{
$this->repository = $repository;
$this->projectionHelper = $projectionHelper;
}public function handleSynchronousMessage(MessageHandlingContextInterface $context): SendableResponse
{
$content = $context->message();
$header = $context->rawMessage()->header();

$metadata = HfxEventMetadata::fromHfxHeader($header);
$payload = Payload::fromMessageContent($content);

/** @var HfxAggregateRoot $roleAggregate */
// Gpx\HfxEventSourcing\HfxAggregateRoot
$roleAggregate = $this->repository->get($payload->id());
$roleAggregate->registerEvent(SessionInvalidated::class, $payload, $metadata);

$this->repository->save($roleAggregate);
$currentEvent = $roleAggregate->currentEvent();
$context->sendNonBlockingAsynchronous('session_invalidated',$content);

$this->projectionHelper->updateReadModel();
return SendableResponse::answerTo($context->rawMessage(), 1100, [
'responseMessage' => 'Success',
'event' => $currentEvent
]);
}
}

Тестовый пример, который я написал до сих пор

<?php

namespace Gpx\Tests\Feature;

use Ramsey\Uuid\Uuid;
use Gpx\Json\JsonEncode;
use Prophecy\Argument;
use PHPUnit\Framework\TestCase;
use Gpx\HfxEventSourcing\Hfx;
use Gpx\HfxEventSourcing\HfxRepository;
use Gpx\Hfx\Framework\MessageTransportApplication\Handler\MessageHandlingContextInterface;
use Gpx\Handlers\BroadcastSessionInvalidated;
use Gpx\Hfx\MessageTransport\Message\ReceivedMessage;
use Prooph\EventSourcing\AggregateRoot;
use Gpx\HfxEventSourcing\HfxAggregateRoot;class BroadcastSessionInvalidatedTest extends TestCase
{
/** @var HfxRepository */
private $repository;

/** @var  Hfx */
private $projectionHelper;

// We have to test handleSynchronousMessage handler whether it is returning sendable response with certain properties in it.
public function testHandleSynchronousMessage()
{

// Expected return value of message() function of $context
$expectedReturnValue = [
"session_id" => "1a92-4376-a8eb-deaf208e1",
"user_id" => "we",
"access_jwt" => "C",
"access_token" => "[email protected]",
"refresh_token" => "C",
"refresh_token_expires" => "[email protected]"];

// Expected return value of rawMessage() function of $context
$headerResponseExpected = [
'header' => [
'version' => '2.0',
'originId' => (string)Uuid::uuid4(),
'destination' => 'application/[email protected]',
'sent' => '2017-12-19T10:12:37.941+00:00'
],
'content' => [
'session_id' => "8365526e-fb92-4376-a8eb-deaf208edf61",
'title' => "A task's title."]
];

// Prophecy means prediction of the future object

// Prediction of $context object starts
$context = $this->prophesize(MessageHandlingContextInterface::class);
$context->message(Argument::any())->willReturn($expectedReturnValue);

$encodedMessage = new JsonEncode($headerResponseExpected);
$rawMessage = ReceivedMessage::fromEncodedMessage($encodedMessage->asString());
$context->rawMessage()->willReturn($rawMessage);

$context->sendNonBlockingAsynchronous('platform_session_initiated', Argument::type("array"))
->shouldBeCalled();
// Prediction of $context object ends

// Repository Mocking Starts
$ravenAggregateRoot = $this->getMockBuilder(HfxAggregateRoot::class)
->disableOriginalConstructor()
->getMock();

$this->ravenRepository = $this->prophesize(HfxRepository::class);
$this->ravenRepository->get('1a92-4376-a8eb-deaf208e1')->shouldBeCalled()->willReturn($ravenAggregateRoot);
$this->ravenRepository->save(Argument::any())->shouldBeCalled();
// Repository Mocking Ends

// Mocking Hfx and calling the method updateReadModel which will return the string UpdateReadModel
$this->projectionHelper = $this->createMock(Hfx::class);
$this->projectionHelper->method('updateReadModel')
->willReturn('UpdateReadModel');

// Actual calling
$broadcastPlatformSessionInvalidated = new BroadcastSessionInvalidated($this->ravenRepository->reveal(), $this->projectionHelper);
//$broadcastPlatformSessionInvalidated = new BroadcastSessionInvalidated($this->ravenRepository, $this->projectionHelper);
$response = $broadcastPlatformSessionInvalidated->handleSynchronousMessage($context->reveal());

$this->assertInstanceOf('Gpx\Hfx\MessageTransport\Response\SendableResponse', $response);
$this->assertArrayHasKey("responseMessage", $response->content()->data());
$this->assertArrayHasKey("event", $response->content()->data());
$this->assertEquals("Success", $response->content()->data()['responseMessage']);}
}

Когда я выполняю тест, он выдает ошибку

PHPUnit 6.5.4 by Sebastian Bergmann and contributors.
.E                                                                  2 / 2 (100%)
Time: 506 ms, Memory: 6.00MB
There was 1 error:
1) Gpx\Tests\Feature\BroadcastSessionInvalidatedTest::testHandleSynchronousMessage
Prophecy\Exception\Call\UnexpectedCallException: Method call:
- sendNonBlockingAsynchronous("session_invalidated", ["session_id" => "1a92-4376-a8eb-deaf208e1", "user_id" => "we", "access_jwt" => "C", "access_token" => "[email protected]", "refresh_token" => "C", "refresh_token_expires" => "[email protected]"])
on Double\MessageHandlingContextInterface\P3 was not expected, expected calls were:
- message(*)
- rawMessage()
- sendNonBlockingAsynchronous(exact("platform_session_initiated"), type(array))
/vagrant/services/sessions-stream/app/src/Gpx/Handlers/BroadcastSessionInvalidated.php:54
/vagrant/services/sessions-stream/app/tests/Feature/BroadcastPlatformSessionInvalidatedTest.php:107
ERRORS!
Tests: 2, Assertions: 6, Errors: 1.

Что я здесь не так делаю?

1

Решение

Исключение говорит вам, что не так: вы используете shouldBeCalled() сказать PHPUnit, чтобы проверить, что метод sendNonBlockingAsynchronous() вызывается с первым параметром platform_session_initiated и array во втором параметре.

// BroadcastSessionInvalidatedTest
$context->sendNonBlockingAsynchronous('platform_session_initiated', Argument::type("array"))
->shouldBeCalled();

Второе условие выполнено, но в вашем фактическом методе первый параметр session_invalidated:

// BroadcastSessionInvalidated
$context->sendNonBlockingAsynchronous('session_invalidated',$content);

Так что измените ожидание, чтобы быть

// BroadcastSessionInvalidatedTest
$context->sendNonBlockingAsynchronous('session_invalidated', Argument::type("array"))
->shouldBeCalled();

И это будет работать.

1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]