You are here

class YamlDiscoveryTest in Drupal 9

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

YamlDiscovery component unit tests.

@group Discovery

Hierarchy

  • class \Drupal\Tests\Component\Discovery\YamlDiscoveryTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of YamlDiscoveryTest

File

core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php, line 18

Namespace

Drupal\Tests\Component\Discovery
View source
class YamlDiscoveryTest extends TestCase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {

    // Ensure that FileCacheFactory has a prefix.
    FileCacheFactory::setPrefix('prefix');
  }

  /**
   * Tests the YAML file discovery.
   */
  public function testDiscovery() {
    vfsStreamWrapper::register();
    $root = new vfsStreamDirectory('modules');
    vfsStreamWrapper::setRoot($root);
    $url = vfsStream::url('modules');
    mkdir($url . '/test_1');
    file_put_contents($url . '/test_1/test_1.test.yml', 'name: test');
    file_put_contents($url . '/test_1/test_2.test.yml', 'name: test');
    mkdir($url . '/test_2');
    file_put_contents($url . '/test_2/test_3.test.yml', 'name: test');

    // Write an empty YAML file.
    file_put_contents($url . '/test_2/test_4.test.yml', '');

    // Set up the directories to search.
    $directories = [
      'test_1' => $url . '/test_1',
      'test_2' => $url . '/test_1',
      'test_3' => $url . '/test_2',
      'test_4' => $url . '/test_2',
    ];
    $discovery = new YamlDiscovery('test', $directories);
    $data = $discovery
      ->findAll();
    $this
      ->assertCount(4, $data);
    $this
      ->assertArrayHasKey('test_1', $data);
    $this
      ->assertArrayHasKey('test_2', $data);
    $this
      ->assertArrayHasKey('test_3', $data);
    $this
      ->assertArrayHasKey('test_4', $data);
    foreach ([
      'test_1',
      'test_2',
      'test_3',
    ] as $key) {
      $this
        ->assertArrayHasKey('name', $data[$key]);
      $this
        ->assertEquals('test', $data[$key]['name']);
    }
    $this
      ->assertSame([], $data['test_4']);
  }

  /**
   * Tests if filename is output for a broken YAML file.
   */
  public function testForBrokenYml() {
    vfsStreamWrapper::register();
    $root = new vfsStreamDirectory('modules');
    vfsStreamWrapper::setRoot($root);
    $url = vfsStream::url('modules');
    mkdir($url . '/test_broken');
    file_put_contents($url . '/test_broken/test_broken.test.yml', "broken:\n:");
    $this
      ->expectException(InvalidDataTypeException::class);
    $this
      ->expectExceptionMessage('vfs://modules/test_broken/test_broken.test.yml');
    $directories = [
      'test_broken' => $url . '/test_broken',
    ];
    $discovery = new YamlDiscovery('test', $directories);
    $discovery
      ->findAll();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
YamlDiscoveryTest::setUp protected function
YamlDiscoveryTest::testDiscovery public function Tests the YAML file discovery.
YamlDiscoveryTest::testForBrokenYml public function Tests if filename is output for a broken YAML file.