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