YamlDiscoveryTest.php

Same filename in this branch
  1. 8.9.x core/tests/Drupal/Tests/Core/Discovery/YamlDiscoveryTest.php
  2. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryTest.php
Same filename in other branches
  1. 9 core/tests/Drupal/Tests/Core/Discovery/YamlDiscoveryTest.php
  2. 9 core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryTest.php
  3. 9 core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php
  4. 10 core/tests/Drupal/Tests/Core/Discovery/YamlDiscoveryTest.php
  5. 10 core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryTest.php
  6. 10 core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php
  7. 11.x core/tests/Drupal/Tests/Core/Discovery/YamlDiscoveryTest.php
  8. 11.x core/tests/Drupal/Tests/Core/Plugin/Discovery/YamlDiscoveryTest.php
  9. 11.x core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php

Namespace

Drupal\Tests\Component\Discovery

File

core/tests/Drupal/Tests/Component/Discovery/YamlDiscoveryTest.php

View source
<?php

namespace Drupal\Tests\Component\Discovery;

use Drupal\Component\Discovery\YamlDiscovery;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamWrapper;
use PHPUnit\Framework\TestCase;

/**
 * YamlDiscovery component unit tests.
 *
 * @group Discovery
 */
class YamlDiscoveryTest extends TestCase {
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() {
        // 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->assertEquals(count($data), count($directories));
        $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($data[$key]['name'], 'test');
        }
        $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();
    }

}

Classes

Title Deprecated Summary
YamlDiscoveryTest YamlDiscovery component unit tests.

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