BaseCronJobTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Tests\Unit\Service\Cron;
  3. use App\Service\Cron\BaseCronJob;
  4. use App\Service\Cron\UI\ConsoleUI;
  5. use App\Service\Cron\UI\CronUIInterface;
  6. use App\Service\Cron\UI\SilentUI;
  7. use PHPUnit\Framework\TestCase;
  8. use Psr\Log\LoggerInterface;
  9. class TestableBaseCronJob extends BaseCronJob
  10. {
  11. public function getUI(): CronUIInterface
  12. {
  13. return $this->ui;
  14. }
  15. public function getLogger(): LoggerInterface
  16. {
  17. return $this->logger;
  18. }
  19. }
  20. class BaseCronJobTest extends TestCase
  21. {
  22. public function testName(): void
  23. {
  24. $job = new TestableBaseCronJob(); // can't use a mock here, because it'll mess up with the class's name
  25. $this->assertEquals(
  26. 'testable-base-cron-job',
  27. $job->name()
  28. );
  29. }
  30. public function testSetLogger(): void
  31. {
  32. $job = $this->getMockBuilder(TestableBaseCronJob::class)
  33. ->setMethodsExcept(['setLoggerInterface', 'getLogger'])
  34. ->getMock();
  35. $logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  36. $job->setLoggerInterface($logger);
  37. $this->assertEquals(
  38. $logger,
  39. $job->getLogger()
  40. );
  41. }
  42. public function testDefaultUi(): void
  43. {
  44. $job = new TestableBaseCronJob(); // can't use a mock here, because we need the constructor to run
  45. $this->assertInstanceOf(
  46. SilentUI::class,
  47. $job->getUI()
  48. );
  49. }
  50. public function testSetUi(): void
  51. {
  52. $job = $this->getMockBuilder(TestableBaseCronJob::class)
  53. ->setMethodsExcept(['setUI', 'getUI'])
  54. ->getMock();
  55. $ui = $this->getMockBuilder(ConsoleUI::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $job->setUI($ui);
  59. $this->assertEquals(
  60. $ui,
  61. $job->getUI()
  62. );
  63. }
  64. }