2
0

PQueueHandlerFinderTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Test\Michel\PQueue;
  3. use Michel\UniTester\TestCase;
  4. use Michel\PQueue\PQueueHandlerFinder;
  5. use LogicException;
  6. use InvalidArgumentException;
  7. use Test\Michel\PQueue\Extra\TestMessage;
  8. use Test\Michel\PQueue\Extra\TestMessageHandler;
  9. class PQueueHandlerFinderTest extends TestCase
  10. {
  11. private array $tempDirs = [];
  12. protected function setUp(): void
  13. {
  14. // Each test is self-contained.
  15. }
  16. protected function tearDown(): void
  17. {
  18. // Clean up all temporary directories created during tests.
  19. foreach ($this->tempDirs as $dir) {
  20. if (is_dir($dir)) {
  21. $this->deleteDirectory($dir);
  22. }
  23. }
  24. $this->tempDirs = [];
  25. }
  26. public function execute(): void
  27. {
  28. $this->testFindsSingleHandlerCorrectly();
  29. $this->testFailsWhenMultipleHandlersExist();
  30. $this->testCacheWorksCorrectly();
  31. $this->testFailsForInvalidDirectory();
  32. }
  33. /**
  34. * Test: The finder correctly finds a single, valid handler.
  35. */
  36. private function testFindsSingleHandlerCorrectly()
  37. {
  38. // Given: A temporary directory with ONE valid handler copied from the Extra directory.
  39. $sourceDir = $this->createTempDir();
  40. copy(__DIR__ . '/Extra/TestMessage.php', $sourceDir . '/TestMessage.php');
  41. copy(__DIR__ . '/Extra/TestMessageHandler.php', $sourceDir . '/TestMessageHandler.php');
  42. // When: We run the finder on that directory.
  43. $finder = new PQueueHandlerFinder([$sourceDir]);
  44. $handlerMap = $finder->find();
  45. // Then: The map should contain exactly one correct entry.
  46. $this->assertArrayHasKey(TestMessage::class, $handlerMap);
  47. $this->assertStrictEquals(TestMessageHandler::class, $handlerMap[TestMessage::class]);
  48. $this->assertCount(1, $handlerMap);
  49. }
  50. /**
  51. * Test: The finder throws an exception when scanning the Extra directory, which has multiple handlers.
  52. */
  53. private function testFailsWhenMultipleHandlersExist()
  54. {
  55. $this->expectException(LogicException::class, function () {
  56. // When: We run the finder on that directory.
  57. $finder = new PQueueHandlerFinder([__DIR__ . '/Extra']);
  58. $finder->find();
  59. });
  60. }
  61. /**
  62. * Test: The finder creates and uses a cache file correctly.
  63. */
  64. private function testCacheWorksCorrectly()
  65. {
  66. // Given: A temporary directory with one handler and a cache directory.
  67. $sourceDir = $this->createTempDir();
  68. $cacheDir = $this->createTempDir();
  69. $handlerFile = $sourceDir . '/TestMessageHandler.php';
  70. copy(__DIR__ . '/Extra/TestMessage.php', $sourceDir . '/TestMessage.php');
  71. copy(__DIR__ . '/Extra/TestMessageHandler.php', $handlerFile);
  72. // When: We run the finder the first time.
  73. $finder = new PQueueHandlerFinder([$sourceDir], $cacheDir);
  74. $handlerMap1 = $finder->find();
  75. // Then: The cache file must exist.
  76. $this->assertFileExists($cacheDir . '/pqueue_handler_map.php');
  77. // And When: We delete the source handler file and run the finder again.
  78. unlink($handlerFile);
  79. $finder2 = new PQueueHandlerFinder([$sourceDir], $cacheDir);
  80. $handlerMap2 = $finder2->find();
  81. // Then: The result should be identical because it was loaded from cache.
  82. $this->assertNotEmpty($handlerMap2, 'Cache should not be empty');
  83. $this->assertStrictEquals($handlerMap1, $handlerMap2, 'Result from cache should match the original');
  84. }
  85. /**
  86. * Test: The finder fails if the source directory does not exist.
  87. */
  88. private function testFailsForInvalidDirectory()
  89. {
  90. $this->expectException(InvalidArgumentException::class, function () {
  91. new PQueueHandlerFinder(['/this/directory/does/not/exist']);
  92. });
  93. }
  94. // --- UTILITY HELPERS ---
  95. private function createTempDir(): string
  96. {
  97. $dir = sys_get_temp_dir() . '/' . uniqid('pqueue_test_');
  98. mkdir($dir, 0777, true);
  99. $this->tempDirs[] = $dir;
  100. return $dir;
  101. }
  102. private function deleteDirectory(string $dir): void
  103. {
  104. if (!is_dir($dir)) return;
  105. $files = new \RecursiveIteratorIterator(
  106. new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
  107. \RecursiveIteratorIterator::CHILD_FIRST
  108. );
  109. foreach ($files as $fileinfo) {
  110. $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
  111. $todo($fileinfo->getRealPath());
  112. }
  113. rmdir($dir);
  114. }
  115. }