Guest User

Untitled

a guest
Feb 26th, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. <?php
  2. namespace GpxHandlers;
  3. use GpxEntityInfrastructureModelEventsSessionInvalidated;
  4. use GpxEntityInfrastructureModelPayload;
  5. use GpxHfxFrameworkMessageTransportApplicationHandlerSynchronousHandlerInterface;
  6. use GpxHfxFrameworkMessageTransportApplicationHandlerMessageHandlingContextInterface;
  7. use GpxHfxEventSourcingHfxAggregateRoot;
  8. use GpxHfxEventSourcingHfxProjectionHelper;
  9. use GpxHfxEventSourcingHfxEventMetadata;
  10. use GpxHfxEventSourcingHfxRepository;
  11. use GpxHfxMessageTransportResponseSendableResponse;
  12.  
  13. class BroadcastSessionInvalidated implements SynchronousHandlerInterface
  14. {
  15.  
  16. /** @var HfxRepository */
  17. private $repository;
  18.  
  19. /** @var HfxProjectionHelper */
  20. private $projectionHelper;
  21.  
  22.  
  23. public function __construct(HfxRepository $repository, HfxProjectionHelper $projectionHelper)
  24. {
  25. $this->repository = $repository;
  26. $this->projectionHelper = $projectionHelper;
  27. }
  28.  
  29.  
  30. public function handleSynchronousMessage(MessageHandlingContextInterface $context): SendableResponse
  31. {
  32. $content = $context->message();
  33. $header = $context->rawMessage()->header();
  34.  
  35. $metadata = HfxEventMetadata::fromHfxHeader($header);
  36. $payload = Payload::fromMessageContent($content);
  37.  
  38. /** @var HfxAggregateRoot $roleAggregate */
  39. // GpxHfxEventSourcingHfxAggregateRoot
  40. $roleAggregate = $this->repository->get($payload->id());
  41. $roleAggregate->registerEvent(SessionInvalidated::class, $payload, $metadata);
  42.  
  43. $this->repository->save($roleAggregate);
  44. $currentEvent = $roleAggregate->currentEvent();
  45. $context->sendNonBlockingAsynchronous('session_invalidated',$content);
  46.  
  47. $this->projectionHelper->updateReadModel();
  48. return SendableResponse::answerTo($context->rawMessage(), 1100, [
  49. 'responseMessage' => 'Success',
  50. 'event' => $currentEvent
  51. ]);
  52. }
  53. }
  54.  
  55. <?php
  56.  
  57. namespace GpxTestsFeature;
  58.  
  59. use RamseyUuidUuid;
  60. use GpxJsonJsonEncode;
  61. use ProphecyArgument;
  62. use PHPUnitFrameworkTestCase;
  63. use GpxHfxEventSourcingHfx;
  64. use GpxHfxEventSourcingHfxRepository;
  65. use GpxHfxFrameworkMessageTransportApplicationHandlerMessageHandlingContextInterface;
  66. use GpxHandlersBroadcastSessionInvalidated;
  67. use GpxHfxMessageTransportMessageReceivedMessage;
  68. use ProophEventSourcingAggregateRoot;
  69. use GpxHfxEventSourcingHfxAggregateRoot;
  70.  
  71.  
  72. class BroadcastSessionInvalidatedTest extends TestCase
  73. {
  74. /** @var HfxRepository */
  75. private $repository;
  76.  
  77. /** @var Hfx */
  78. private $projectionHelper;
  79.  
  80. // We have to test handleSynchronousMessage handler whether it is returning sendable response with certain properties in it.
  81. public function testHandleSynchronousMessage()
  82. {
  83.  
  84. // Expected return value of message() function of $context
  85. $expectedReturnValue = [
  86. "session_id" => "1a92-4376-a8eb-deaf208e1",
  87. "user_id" => "we",
  88. "access_jwt" => "C",
  89. "access_token" => "john@gmail.com",
  90. "refresh_token" => "C",
  91. "refresh_token_expires" => "john@gmail.com"
  92. ];
  93.  
  94. // Expected return value of rawMessage() function of $context
  95. $headerResponseExpected = [
  96. 'header' => [
  97. 'version' => '2.0',
  98. 'originId' => (string)Uuid::uuid4(),
  99. 'destination' => 'application/meta@1.0.0',
  100. 'sent' => '2017-12-19T10:12:37.941+00:00'
  101. ],
  102. 'content' => [
  103. 'session_id' => "8365526e-fb92-4376-a8eb-deaf208edf61",
  104. 'title' => "A task's title."
  105. ]
  106. ];
  107.  
  108. // Prophecy means prediction of the future object
  109.  
  110. // Prediction of $context object starts
  111. $context = $this->prophesize(MessageHandlingContextInterface::class);
  112. $context->message(Argument::any())->willReturn($expectedReturnValue);
  113.  
  114. $encodedMessage = new JsonEncode($headerResponseExpected);
  115. $rawMessage = ReceivedMessage::fromEncodedMessage($encodedMessage->asString());
  116. $context->rawMessage()->willReturn($rawMessage);
  117.  
  118. $context->sendNonBlockingAsynchronous('platform_session_initiated', Argument::type("array"))
  119. ->shouldBeCalled();
  120. // Prediction of $context object ends
  121.  
  122. // Repository Mocking Starts
  123. $ravenAggregateRoot = $this->getMockBuilder(HfxAggregateRoot::class)
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126.  
  127. $this->ravenRepository = $this->prophesize(HfxRepository::class);
  128. $this->ravenRepository->get('1a92-4376-a8eb-deaf208e1')->shouldBeCalled()->willReturn($ravenAggregateRoot);
  129. $this->ravenRepository->save(Argument::any())->shouldBeCalled();
  130. // Repository Mocking Ends
  131.  
  132. // Mocking Hfx and calling the method updateReadModel which will return the string UpdateReadModel
  133. $this->projectionHelper = $this->createMock(Hfx::class);
  134. $this->projectionHelper->method('updateReadModel')
  135. ->willReturn('UpdateReadModel');
  136.  
  137. // Actual calling
  138. $broadcastPlatformSessionInvalidated = new BroadcastSessionInvalidated($this->ravenRepository->reveal(), $this->projectionHelper);
  139. //$broadcastPlatformSessionInvalidated = new BroadcastSessionInvalidated($this->ravenRepository, $this->projectionHelper);
  140. $response = $broadcastPlatformSessionInvalidated->handleSynchronousMessage($context->reveal());
  141.  
  142. $this->assertInstanceOf('GpxHfxMessageTransportResponseSendableResponse', $response);
  143. $this->assertArrayHasKey("responseMessage", $response->content()->data());
  144. $this->assertArrayHasKey("event", $response->content()->data());
  145. $this->assertEquals("Success", $response->content()->data()['responseMessage']);
  146.  
  147.  
  148. }
  149. }
  150.  
  151. PHPUnit 6.5.4 by Sebastian Bergmann and contributors.
  152. .E 2 / 2 (100%)
  153. Time: 506 ms, Memory: 6.00MB
  154. There was 1 error:
  155. 1) GpxTestsFeatureBroadcastSessionInvalidatedTest::testHandleSynchronousMessage
  156. ProphecyExceptionCallUnexpectedCallException: Method call:
  157. - sendNonBlockingAsynchronous("session_invalidated", ["session_id" => "1a92-4376-a8eb-deaf208e1", "user_id" => "we", "access_jwt" => "C", "access_token" => "john@gmail.com", "refresh_token" => "C", "refresh_token_expires" => "john@gmail.com"])
  158. on DoubleMessageHandlingContextInterfaceP3 was not expected, expected calls were:
  159. - message(*)
  160. - rawMessage()
  161. - sendNonBlockingAsynchronous(exact("platform_session_initiated"), type(array))
  162. /vagrant/services/sessions-stream/app/src/Gpx/Handlers/BroadcastSessionInvalidated.php:54
  163. /vagrant/services/sessions-stream/app/tests/Feature/BroadcastPlatformSessionInvalidatedTest.php:107
  164. ERRORS!
  165. Tests: 2, Assertions: 6, Errors: 1.
  166.  
  167. // BroadcastSessionInvalidatedTest
  168. $context->sendNonBlockingAsynchronous('platform_session_initiated', Argument::type("array"))
  169. ->shouldBeCalled();
  170.  
  171. // BroadcastSessionInvalidated
  172. $context->sendNonBlockingAsynchronous('session_invalidated',$content);
  173.  
  174. // BroadcastSessionInvalidatedTest
  175. $context->sendNonBlockingAsynchronous('session_invalidated', Argument::type("array"))
  176. ->shouldBeCalled();
Add Comment
Please, Sign In to add comment