BaseCronJobTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. $job = $this->getMockBuilder(TestableBaseCronJob::class)
  27. ->setMethodsExcept(['setLoggerInterface', 'getLogger'])
  28. ->getMock();
  29. $logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
  30. $job->setLoggerInterface($logger);
  31. $this->assertEquals(
  32. $logger,
  33. $job->getLogger()
  34. );
  35. }
  36. public function testDefaultUi(): void {
  37. $job = new TestableBaseCronJob(); // can't use a mock here, because we need the constructor to run
  38. $this->assertInstanceOf(
  39. SilentUI::class,
  40. $job->getUI()
  41. );
  42. }
  43. public function testSetUi(): void {
  44. $job = $this->getMockBuilder(TestableBaseCronJob::class)
  45. ->setMethodsExcept(['setUI', 'getUI'])
  46. ->getMock();
  47. $ui = $this->getMockBuilder(ConsoleUI::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $job->setUI($ui);
  51. $this->assertEquals(
  52. $ui,
  53. $job->getUI()
  54. );
  55. }
  56. }