AnnotatedClassDiscoveryCachedTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Drupal\Tests\Component\Annotation;
  3. use Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery;
  4. use Drupal\Component\FileCache\FileCacheFactory;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * @coversDefaultClass \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery
  8. * @group Annotation
  9. */
  10. class AnnotatedClassDiscoveryCachedTest extends TestCase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. protected function setUp() {
  15. parent::setUp();
  16. // Ensure FileCacheFactory::DISABLE_CACHE is *not* set, since we're testing
  17. // integration with the file cache.
  18. FileCacheFactory::setConfiguration([]);
  19. // Ensure that FileCacheFactory has a prefix.
  20. FileCacheFactory::setPrefix('prefix');
  21. }
  22. /**
  23. * Test that getDefinitions() retrieves the file cache correctly.
  24. *
  25. * @covers ::getDefinitions
  26. */
  27. public function testGetDefinitions() {
  28. // Path to the classes which we'll discover and parse annotation.
  29. $discovery_path = __DIR__ . '/Fixtures';
  30. // File path that should be discovered within that directory.
  31. $file_path = $discovery_path . '/PluginNamespace/DiscoveryTest1.php';
  32. $discovery = new AnnotatedClassDiscovery(['com\example' => [$discovery_path]]);
  33. $this->assertEquals([
  34. 'discovery_test_1' => [
  35. 'id' => 'discovery_test_1',
  36. 'class' => 'com\example\PluginNamespace\DiscoveryTest1',
  37. ],
  38. ], $discovery->getDefinitions());
  39. // Gain access to the file cache so we can change it.
  40. $ref_file_cache = new \ReflectionProperty($discovery, 'fileCache');
  41. $ref_file_cache->setAccessible(TRUE);
  42. /* @var $file_cache \Drupal\Component\FileCache\FileCacheInterface */
  43. $file_cache = $ref_file_cache->getValue($discovery);
  44. // The file cache is keyed by the file path, and we'll add some known
  45. // content to test against.
  46. $file_cache->set($file_path, [
  47. 'id' => 'wrong_id',
  48. 'content' => serialize(['an' => 'array']),
  49. ]);
  50. // Now perform the same query and check for the cached results.
  51. $this->assertEquals([
  52. 'wrong_id' => [
  53. 'an' => 'array',
  54. ],
  55. ], $discovery->getDefinitions());
  56. }
  57. }