BaseCronJobTest.php 1.9 KB

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