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