diff --git a/core/lib/Drupal/Component/Discovery/YamlDiscovery.php b/core/lib/Drupal/Component/Discovery/YamlDiscovery.php index bf4ee15aa778b17a6bfe011b2dab9945f04e0980..1d147fd08ba1ff29acc98a36e35507f24e874b04 100644 --- a/core/lib/Drupal/Component/Discovery/YamlDiscovery.php +++ b/core/lib/Drupal/Component/Discovery/YamlDiscovery.php @@ -48,7 +48,9 @@ public function __construct($name, array $directories) { public function findAll() { $all = array(); foreach ($this->findFiles() as $provider => $file) { - $all[$provider] = Yaml::decode(file_get_contents($file)); + // If a file is empty or its contents are commented out, return an empty + // array instead of NULL for type consistency. + $all[$provider] = Yaml::decode(file_get_contents($file)) ?: []; } return $all; diff --git a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_1/test_1.test.yml b/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_1/test_1.test.yml deleted file mode 100644 index b3275362517072a94da46b9f7966cdbc97ddc670..0000000000000000000000000000000000000000 --- a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_1/test_1.test.yml +++ /dev/null @@ -1 +0,0 @@ -name: test diff --git a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_2.test.yml b/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_2.test.yml deleted file mode 100644 index b3275362517072a94da46b9f7966cdbc97ddc670..0000000000000000000000000000000000000000 --- a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_2.test.yml +++ /dev/null @@ -1 +0,0 @@ -name: test diff --git a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_3.test.yml b/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_3.test.yml deleted file mode 100644 index b3275362517072a94da46b9f7966cdbc97ddc670..0000000000000000000000000000000000000000 --- a/core/tests/Drupal/Tests/Component/Discovery/Fixtures/test_2/test_3.test.yml +++ /dev/null @@ -1 +0,0 @@ -name: test diff --git a/core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php b/core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php index a5469922a98fe9ba0295c246d255ba933c1f52b8..1849cd53a2ef86c4524526bf996e7dd32d765121 100644 --- a/core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php +++ b/core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php @@ -9,6 +9,9 @@ use Drupal\Tests\UnitTestCase; use Drupal\Component\Discovery\YamlDiscovery; +use org\bovigo\vfs\vfsStream; +use org\bovigo\vfs\vfsStreamWrapper; +use org\bovigo\vfs\vfsStreamDirectory; /** * YamlDiscovery component unit tests. @@ -21,13 +24,26 @@ class YamlDiscoveryTest extends UnitTestCase { * Tests the YAML file discovery. */ public function testDiscovery() { - $base_path = __DIR__ . '/Fixtures'; + 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 = array( - 'test_1' => $base_path . '/test_1', - 'test_2' => $base_path . '/test_2', - // Use the same directory with a different provider name. - 'test_3' => $base_path . '/test_2', + '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); @@ -37,11 +53,14 @@ public function testDiscovery() { $this->assertArrayHasKey('test_1', $data); $this->assertArrayHasKey('test_2', $data); $this->assertArrayHasKey('test_3', $data); + $this->assertArrayHasKey('test_4', $data); - foreach ($data as $item) { - $this->assertArrayHasKey('name', $item); - $this->assertEquals($item['name'], 'test'); + foreach (array('test_1', 'test_2', 'test_3') as $key) { + $this->assertArrayHasKey('name', $data[$key]); + $this->assertEquals($data[$key]['name'], 'test'); } + + $this->assertSame(array(), $data['test_4']); } }