BaseCronJobTest.php 1.8 KB

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