comparison core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryCachedTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\Tests\Component\Annotation;
4
5 use Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery;
6 use Drupal\Component\FileCache\FileCacheFactory;
7 use PHPUnit\Framework\TestCase;
8
9 /**
10 * @coversDefaultClass \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery
11 * @group Annotation
12 */
13 class AnnotatedClassDiscoveryCachedTest extends TestCase {
14
15 /**
16 * {@inheritdoc}
17 */
18 protected function setUp() {
19 parent::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');
25 }
26
27 /**
28 * Test that getDefinitions() retrieves the file cache correctly.
29 *
30 * @covers ::getDefinitions
31 */
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';
37
38 $discovery = new AnnotatedClassDiscovery(['com\example' => [$discovery_path]]);
39 $this->assertEquals([
40 'discovery_test_1' => [
41 'id' => 'discovery_test_1',
42 'class' => 'com\example\PluginNamespace\DiscoveryTest1',
43 ],
44 ], $discovery->getDefinitions());
45
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, [
54 'id' => 'wrong_id',
55 'content' => serialize(['an' => 'array']),
56 ]);
57
58 // Now perform the same query and check for the cached results.
59 $this->assertEquals([
60 'wrong_id' => [
61 'an' => 'array',
62 ],
63 ], $discovery->getDefinitions());
64 }
65
66 }