Same name in this branch
  1. 10 core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php \Drupal\Tests\Component\Discovery\YamlDiscoveryTest
  2. 10 core/tests/Drupal/Tests/Core/Discovery/YamlDiscoveryTest.php \Drupal\Tests\Core\Discovery\YamlDiscoveryTest
  3. 10 core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryTest.php \Drupal\Tests\Core\Plugin\Discovery\YamlDiscoveryTest
Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryTest.php \Drupal\Tests\Core\Plugin\Discovery\YamlDiscoveryTest
  2. 9 core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryTest.php \Drupal\Tests\Core\Plugin\Discovery\YamlDiscoveryTest

@coversDefaultClass \Drupal\Core\Plugin\Discovery\YamlDiscovery @group Plugin

Hierarchy

Expanded class hierarchy of YamlDiscoveryTest

File

core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryTest.php, line 16

Namespace

Drupal\Tests\Core\Plugin\Discovery
View source
class YamlDiscoveryTest extends UnitTestCase {

  /**
   * The YamlDiscovery instance to test.
   *
   * @var \Drupal\Core\Plugin\Discovery\YamlDiscovery
   */
  protected $discovery;

  /**
   * Expected provider => key mappings for testing.
   *
   * @var array
   */
  protected $expectedKeys = [
    'test_1' => 'test_1_a',
    'another_provider_1' => 'test_1_b',
    'another_provider_2' => 'test_2_a',
    'test_2' => 'test_2_b',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $base_path = __DIR__ . '/Fixtures';

    // Set up the directories to search.
    $directories = [
      'test_1' => $base_path . '/test_1',
      'test_2' => $base_path . '/test_2',
    ];
    $this->discovery = new YamlDiscovery('test', $directories);
  }

  /**
   * Tests the getDefinitions() method.
   */
  public function testGetDefinitions() {
    $definitions = $this->discovery
      ->getDefinitions();
    $this
      ->assertIsArray($definitions);
    $this
      ->assertCount(4, $definitions);
    foreach ($this->expectedKeys as $expected_key) {
      $this
        ->assertArrayHasKey($expected_key, $definitions);
    }
    foreach ($definitions as $id => $definition) {
      foreach ([
        'name',
        'id',
        'provider',
      ] as $key) {
        $this
          ->assertArrayHasKey($key, $definition);
      }
      $this
        ->assertEquals($id, $definition['id']);
      $this
        ->assertEquals(array_search($id, $this->expectedKeys), $definition['provider']);
    }
  }

  /**
   * @covers ::getDefinitions
   */
  public function testGetDefinitionsWithTranslatableDefinitions() {
    vfsStream::setup('root');
    $file_1 = <<<'EOS'
test_plugin:
  title: test title
EOS;
    $file_2 = <<<'EOS'
test_plugin2:
  title: test title2
  title_context: 'test-context'
EOS;
    vfsStream::create([
      'test_1' => [
        'test_1.test.yml' => $file_1,
      ],
      'test_2' => [
        'test_2.test.yml' => $file_2,
      ],
    ]);
    $discovery = new YamlDiscovery('test', [
      'test_1' => vfsStream::url('root/test_1'),
      'test_2' => vfsStream::url('root/test_2'),
    ]);
    $discovery
      ->addTranslatableProperty('title', 'title_context');
    $definitions = $discovery
      ->getDefinitions();
    $this
      ->assertCount(2, $definitions);
    $plugin_1 = $definitions['test_plugin'];
    $plugin_2 = $definitions['test_plugin2'];
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $plugin_1['title']);
    $this
      ->assertEquals([], $plugin_1['title']
      ->getOptions());
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $plugin_2['title']);
    $this
      ->assertEquals([
      'context' => 'test-context',
    ], $plugin_2['title']
      ->getOptions());
  }

  /**
   * Tests the getDefinition() method.
   */
  public function testGetDefinition() {
    $definitions = $this->discovery
      ->getDefinitions();

    // Test the getDefinition() method.
    foreach ($this->expectedKeys as $expected_key) {
      $this
        ->assertEquals($definitions[$expected_key], $this->discovery
        ->getDefinition($expected_key));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function
YamlDiscoveryTest::$discovery protected property The YamlDiscovery instance to test.
YamlDiscoveryTest::$expectedKeys protected property Expected provider => key mappings for testing.
YamlDiscoveryTest::setUp protected function Overrides UnitTestCase::setUp
YamlDiscoveryTest::testGetDefinition public function Tests the getDefinition() method.
YamlDiscoveryTest::testGetDefinitions public function Tests the getDefinitions() method.
YamlDiscoveryTest::testGetDefinitionsWithTranslatableDefinitions public function @covers ::getDefinitions