ControllerFinderTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Test\Michel\Framework\Core;
  3. use Michel\Framework\Core\Routing\ControllerFinder;
  4. use Michel\UniTester\TestCase;
  5. use Test\Michel\Framework\Core\Controller\SampleControllerTest;
  6. use Test\Michel\Framework\Core\Controller\UserControllerTest;
  7. class ControllerFinderTest extends TestCase
  8. {
  9. protected function setUp(): void
  10. {
  11. // TODO: Implement setUp() method.
  12. }
  13. protected function tearDown(): void
  14. {
  15. // TODO: Implement tearDown() method.
  16. }
  17. protected function execute(): void
  18. {
  19. $this->testFound();
  20. $this->testFoundCache();
  21. }
  22. public function testFound()
  23. {
  24. if (PHP_VERSION_ID >= 80000) {
  25. $controllers = (new ControllerFinder([__DIR__ . '/Controller']))->findControllerClasses();
  26. $this->assertCount(2, $controllers);
  27. }
  28. $this->assertTrue(true);
  29. }
  30. public function testFoundCache()
  31. {
  32. if (PHP_VERSION_ID >= 80000) {
  33. $cacheDir = __DIR__ . '/cache';
  34. $targetDir = __DIR__ . '/Controller';
  35. if (!is_dir($cacheDir)) {
  36. mkdir($cacheDir, 0777, true);
  37. }
  38. $fileCache = "$cacheDir/" . md5($targetDir) . '.php';
  39. if (file_exists($fileCache)) {
  40. unlink($fileCache);
  41. }
  42. $this->assertFalse(file_exists($fileCache));
  43. $controllers = (new ControllerFinder([$targetDir], $cacheDir))->findControllerClasses();
  44. $this->assertCount(2, $controllers);
  45. $this->assertTrue(file_exists($fileCache));
  46. $this->assertEquals([
  47. SampleControllerTest::class,
  48. UserControllerTest::class,
  49. ], require $fileCache);
  50. $controllers = (new ControllerFinder([$targetDir], $cacheDir))->findControllerClasses();
  51. $this->assertCount(2, $controllers);
  52. unlink($fileCache);
  53. rmdir($cacheDir);
  54. }
  55. $this->assertTrue(true);
  56. }
  57. }