ClassNameFormatterTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace League\Tactician\Logger\Tests\Formatter;
  3. use League\Tactician\Logger\Formatter\ClassNameFormatter;
  4. use League\Tactician\Logger\Tests\Fixtures\RegisterUserCommand;
  5. use League\Tactician\Logger\Tests\Fixtures\UserAlreadyExistsException;
  6. use Psr\Log\LoggerInterface;
  7. use Mockery;
  8. use Psr\Log\LogLevel;
  9. class ClassNameFormatterTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var ClassNameFormatter
  13. */
  14. protected $formatter;
  15. /**
  16. * @var LoggerInterface|Mockery\MockInterface
  17. */
  18. protected $logger;
  19. protected function setUp()
  20. {
  21. $this->formatter = new ClassNameFormatter();
  22. $this->logger = Mockery::mock(LoggerInterface::class);
  23. }
  24. public function testBasicSuccessMessageIsLogged()
  25. {
  26. $this->logger->shouldReceive('log')->with(
  27. LogLevel::DEBUG,
  28. 'Command succeeded: ' . RegisterUserCommand::class,
  29. []
  30. );
  31. $this->formatter->logCommandSucceeded($this->logger, new RegisterUserCommand(), null);
  32. }
  33. public function testCommandReceivedCreatesExpectedMessage()
  34. {
  35. $this->logger->shouldReceive('log')->with(
  36. LogLevel::DEBUG,
  37. 'Command received: ' . RegisterUserCommand::class,
  38. []
  39. );
  40. $this->formatter->logCommandReceived($this->logger, new RegisterUserCommand());
  41. }
  42. public function testCommandFailedCreatesExpectedMessage()
  43. {
  44. $exception = new UserAlreadyExistsException();
  45. $this->logger->shouldReceive('log')->with(
  46. LogLevel::ERROR,
  47. 'Command failed: ' . RegisterUserCommand::class,
  48. ['exception' => $exception]
  49. );
  50. $this->formatter->logCommandFailed($this->logger, new RegisterUserCommand(), $exception);
  51. }
  52. public function testCustomLogLevels()
  53. {
  54. $formatter = new ClassNameFormatter(LogLevel::WARNING, LogLevel::NOTICE, LogLevel::EMERGENCY);
  55. $this->logger->shouldReceive('log')->with(LogLevel::WARNING, Mockery::any(), Mockery::any())->once();
  56. $formatter->logCommandReceived($this->logger, new RegisterUserCommand());
  57. $this->logger->shouldReceive('log')->with(LogLevel::NOTICE, Mockery::any(), Mockery::any())->once();
  58. $formatter->logCommandSucceeded($this->logger, new RegisterUserCommand(), null);
  59. $this->logger->shouldReceive('log')->with(LogLevel::EMERGENCY, Mockery::any(), Mockery::any())->once();
  60. $formatter->logCommandFailed($this->logger, new RegisterUserCommand(), new UserAlreadyExistsException());
  61. }
  62. }