ClassPropertiesFormatterTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace League\Tactician\Logger\Tests\Formatter;
  3. use League\Tactician\Logger\Formatter\ClassPropertiesFormatter;
  4. use League\Tactician\Logger\PropertyNormalizer\PropertyNormalizer;
  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 ClassPropertiesFormatterTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var PropertyNormalizer|MockInterface
  15. */
  16. private $normalizer;
  17. /**
  18. * @var ClassPropertiesFormatter
  19. */
  20. protected $formatter;
  21. /**
  22. * @var LoggerInterface|Mockery\MockInterface
  23. */
  24. protected $logger;
  25. protected function setUp()
  26. {
  27. $this->normalizer = Mockery::mock(PropertyNormalizer::class);
  28. $this->normalizer->shouldReceive('normalize')->andReturn(['test' => 'data']);
  29. $this->logger = Mockery::mock(LoggerInterface::class);
  30. $this->formatter = new ClassPropertiesFormatter($this->normalizer);
  31. }
  32. public function testBasicSuccessMessageIsLogged()
  33. {
  34. $this->logger->shouldReceive('log')->with(
  35. LogLevel::DEBUG,
  36. 'Command succeeded: ' . RegisterUserCommand::class,
  37. ['command' => ['test' => 'data']]
  38. );
  39. $this->formatter->logCommandSucceeded($this->logger, new RegisterUserCommand(), null);
  40. }
  41. public function testCommandReceivedCreatesExpectedMessage()
  42. {
  43. $this->logger->shouldReceive('log')->with(
  44. LogLevel::DEBUG,
  45. 'Command received: ' . RegisterUserCommand::class,
  46. ['command' => ['test' => 'data']]
  47. );
  48. $this->formatter->logCommandReceived($this->logger, new RegisterUserCommand());
  49. }
  50. public function testCommandFailedCreatesExpectedMessage()
  51. {
  52. $exception = new UserAlreadyExistsException();
  53. $this->logger->shouldReceive('log')->with(
  54. LogLevel::ERROR,
  55. 'Command failed: ' . RegisterUserCommand::class,
  56. ['exception' => $exception]
  57. );
  58. $this->formatter->logCommandFailed($this->logger, new RegisterUserCommand(), $exception);
  59. }
  60. public function testCustomLogLevels()
  61. {
  62. $formatter = new ClassPropertiesFormatter(
  63. $this->normalizer,
  64. LogLevel::WARNING,
  65. LogLevel::NOTICE,
  66. LogLevel::EMERGENCY
  67. );
  68. $this->logger->shouldReceive('log')->with(LogLevel::WARNING, Mockery::any(), Mockery::any())->once();
  69. $formatter->logCommandReceived($this->logger, new RegisterUserCommand());
  70. $this->logger->shouldReceive('log')->with(LogLevel::NOTICE, Mockery::any(), Mockery::any())->once();
  71. $formatter->logCommandSucceeded($this->logger, new RegisterUserCommand(), null);
  72. $this->logger->shouldReceive('log')->with(LogLevel::EMERGENCY, Mockery::any(), Mockery::any())->once();
  73. $formatter->logCommandFailed($this->logger, new RegisterUserCommand(), new UserAlreadyExistsException());
  74. }
  75. }