LoggerMiddlewareTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace League\Tactician\Logger\Tests;
  3. use League\Tactician\Logger\Formatter\Formatter;
  4. use League\Tactician\Logger\LoggerMiddleware;
  5. use League\Tactician\Logger\Tests\Fixtures\RegisterUserCommand;
  6. use League\Tactician\Logger\Tests\Fixtures\UserAlreadyExistsException;
  7. use Mockery;
  8. use Mockery\MockInterface;
  9. use Psr\Log\LoggerInterface;
  10. use Psr\Log\LogLevel;
  11. class LoggerMiddlewareTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var LoggerInterface
  15. */
  16. private $logger;
  17. /**
  18. * @var LoggerMiddleware
  19. */
  20. private $middleware;
  21. /**
  22. * @var Formatter|MockInterface
  23. */
  24. private $formatter;
  25. /**
  26. * @var callable
  27. */
  28. private $mockNext;
  29. protected function setUp()
  30. {
  31. $this->logger = Mockery::mock(LoggerInterface::class);
  32. $this->formatter = Mockery::mock(Formatter::class);
  33. $this->middleware = new LoggerMiddleware($this->formatter, $this->logger);
  34. }
  35. public function testSuccessfulEventsLogWithCommandAndReturnValue()
  36. {
  37. $command = new RegisterUserCommand();
  38. $this->formatter->shouldReceive('logCommandReceived')->with($this->logger, $command)->once();
  39. $this->formatter->shouldReceive('logCommandSucceeded')->with($this->logger, $command, 'blat bart')->once();
  40. $this->middleware->execute($command, function () {
  41. return 'blat bart';
  42. });
  43. }
  44. public function testEmptyReturnValuesIsPassedAsNull()
  45. {
  46. $command = new RegisterUserCommand();
  47. $this->formatter->shouldReceive('logCommandReceived')->with($this->logger, $command)->once();
  48. $this->formatter->shouldReceive('logCommandSucceeded')->with($this->logger, $command, null)->once();
  49. $this->middleware->execute(
  50. $command,
  51. function () {
  52. // no-op
  53. }
  54. );
  55. }
  56. /**
  57. * @expectedException \League\Tactician\Logger\Tests\Fixtures\UserAlreadyExistsException
  58. */
  59. public function testFailuresMessagesAreLoggedWithException()
  60. {
  61. $command = new RegisterUserCommand();
  62. $exception = new UserAlreadyExistsException();
  63. $this->formatter->shouldReceive('logCommandReceived')->with($this->logger, $command)->once();
  64. $this->formatter->shouldReceive('logCommandFailed')->with($this->logger, $command, $exception)->once();
  65. $this->middleware->execute(
  66. $command,
  67. function () use ($exception) {
  68. throw $exception;
  69. }
  70. );
  71. }
  72. public function testNextCallableIsInvoked()
  73. {
  74. $this->logger->shouldIgnoreMissing();
  75. $this->formatter->shouldIgnoreMissing();
  76. $sentCommand = new RegisterUserCommand();
  77. $receivedSameCommand = false;
  78. $next = function ($receivedCommand) use (&$receivedSameCommand, $sentCommand) {
  79. $receivedSameCommand = ($receivedCommand === $sentCommand);
  80. };
  81. $this->middleware->execute($sentCommand, $next);
  82. $this->assertTrue($receivedSameCommand);
  83. }
  84. }