function YamlDiscoveryTest::testDiscovery

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php \Drupal\Tests\Component\Discovery\YamlDiscoveryTest::testDiscovery()
  2. 10 core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php \Drupal\Tests\Component\Discovery\YamlDiscoveryTest::testDiscovery()
  3. 11.x core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php \Drupal\Tests\Component\Discovery\YamlDiscoveryTest::testDiscovery()

Tests the YAML file discovery.

File

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

Class

YamlDiscoveryTest
YamlDiscovery component unit tests.

Namespace

Drupal\Tests\Component\Discovery

Code

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']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.