BaseCronJobTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. public function preview(): void
  20. {
  21. }
  22. public function execute(): void
  23. {
  24. }
  25. }
  26. class BaseCronJobTest extends TestCase
  27. {
  28. public function testName(): void
  29. {
  30. $job = new TestableBaseCronJob(); // can't use a mock here, because it'll mess up with the class's name
  31. $this->assertEquals(
  32. 'testable-base-cron-job',
  33. $job->name()
  34. );
  35. }
  36. public function testSetLogger(): void
  37. {
  38. $job = $this->getMockBuilder(TestableBaseCronJob::class)
  39. ->setMethodsExcept(['setLoggerInterface', 'getLogger'])
  40. ->getMock();
  41. $logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  42. $job->setLoggerInterface($logger);
  43. $this->assertEquals(
  44. $logger,
  45. $job->getLogger()
  46. );
  47. }
  48. public function testDefaultUi(): void
  49. {
  50. $job = new TestableBaseCronJob(); // can't use a mock here, because we need the constructor to run
  51. $this->assertInstanceOf(
  52. SilentUI::class,
  53. $job->getUI()
  54. );
  55. }
  56. public function testSetUi(): void
  57. {
  58. $job = $this->getMockBuilder(TestableBaseCronJob::class)
  59. ->setMethodsExcept(['setUI', 'getUI'])
  60. ->getMock();
  61. $ui = $this->getMockBuilder(ConsoleUI::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $job->setUI($ui);
  65. $this->assertEquals(
  66. $ui,
  67. $job->getUI()
  68. );
  69. }
  70. }