annotate core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryCachedTest.php @ 5:12f9dff5fda9 tip

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