Commit e8e84d70 authored by alexpott's avatar alexpott
Browse files

Issue #2331407 by Xano, tstoeckler, damiankloip: YamlDiscovery does not handle empty files

parent c889a5ff
...@@ -48,7 +48,9 @@ public function __construct($name, array $directories) { ...@@ -48,7 +48,9 @@ public function __construct($name, array $directories) {
public function findAll() { public function findAll() {
$all = array(); $all = array();
foreach ($this->findFiles() as $provider => $file) { 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; return $all;
......
...@@ -9,6 +9,9 @@ ...@@ -9,6 +9,9 @@
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Drupal\Component\Discovery\YamlDiscovery; use Drupal\Component\Discovery\YamlDiscovery;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamWrapper;
use org\bovigo\vfs\vfsStreamDirectory;
/** /**
* YamlDiscovery component unit tests. * YamlDiscovery component unit tests.
...@@ -21,13 +24,26 @@ class YamlDiscoveryTest extends UnitTestCase { ...@@ -21,13 +24,26 @@ class YamlDiscoveryTest extends UnitTestCase {
* Tests the YAML file discovery. * Tests the YAML file discovery.
*/ */
public function testDiscovery() { 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. // Set up the directories to search.
$directories = array( $directories = array(
'test_1' => $base_path . '/test_1', 'test_1' => $url . '/test_1',
'test_2' => $base_path . '/test_2', 'test_2' => $url . '/test_1',
// Use the same directory with a different provider name. 'test_3' => $url . '/test_2',
'test_3' => $base_path . '/test_2', 'test_4' => $url . '/test_2',
); );
$discovery = new YamlDiscovery('test', $directories); $discovery = new YamlDiscovery('test', $directories);
...@@ -37,11 +53,14 @@ public function testDiscovery() { ...@@ -37,11 +53,14 @@ public function testDiscovery() {
$this->assertArrayHasKey('test_1', $data); $this->assertArrayHasKey('test_1', $data);
$this->assertArrayHasKey('test_2', $data); $this->assertArrayHasKey('test_2', $data);
$this->assertArrayHasKey('test_3', $data); $this->assertArrayHasKey('test_3', $data);
$this->assertArrayHasKey('test_4', $data);
foreach ($data as $item) { foreach (array('test_1', 'test_2', 'test_3') as $key) {
$this->assertArrayHasKey('name', $item); $this->assertArrayHasKey('name', $data[$key]);
$this->assertEquals($item['name'], 'test'); $this->assertEquals($data[$key]['name'], 'test');
} }
$this->assertSame(array(), $data['test_4']);
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment