class YamlTest

Same name in this branch
  1. 8.9.x core/tests/Drupal/Tests/Core/Serialization/YamlTest.php \Drupal\Tests\Core\Serialization\YamlTest
Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Serialization/YamlTest.php \Drupal\Tests\Core\Serialization\YamlTest
  2. 9 core/tests/Drupal/Tests/Component/Serialization/YamlTest.php \Drupal\Tests\Component\Serialization\YamlTest
  3. 10 core/tests/Drupal/Tests/Component/Serialization/YamlTest.php \Drupal\Tests\Component\Serialization\YamlTest
  4. 11.x core/tests/Drupal/Tests/Component/Serialization/YamlTest.php \Drupal\Tests\Component\Serialization\YamlTest

@coversDefaultClass \Drupal\Component\Serialization\Yaml @group Serialization

Hierarchy

  • class \Drupal\Tests\Component\Serialization\YamlTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of YamlTest

File

core/tests/Drupal/Tests/Component/Serialization/YamlTest.php, line 16

Namespace

Drupal\Tests\Component\Serialization
View source
class YamlTest extends TestCase {
    
    /**
     * @var \PHPUnit\Framework\MockObject\MockObject
     */
    protected $mockParser;
    public function setUp() {
        parent::setUp();
        $this->mockParser = $this->getMockBuilder('\\stdClass')
            ->setMethods([
            'encode',
            'decode',
            'getFileExtension',
        ])
            ->getMock();
        YamlParserProxy::setMock($this->mockParser);
    }
    public function tearDown() {
        YamlParserProxy::setMock(NULL);
        parent::tearDown();
    }
    
    /**
     * @covers ::decode
     */
    public function testDecode() {
        $this->mockParser
            ->expects($this->once())
            ->method('decode');
        YamlStub::decode('test');
    }
    
    /**
     * @covers ::getFileExtension
     */
    public function testGetFileExtension() {
        $this->mockParser
            ->expects($this->never())
            ->method('getFileExtension');
        $this->assertEquals('yml', YamlStub::getFileExtension());
    }
    
    /**
     * Tests all YAML files are decoded in the same way with Symfony and PECL.
     *
     * This test is a little bit slow but it tests that we do not have any bugs in
     * our YAML that might not be decoded correctly in any of our implementations.
     *
     * @todo This should exist as an integration test not part of our unit tests.
     *   https://www.drupal.org/node/2597730
     *
     * @requires extension yaml
     * @dataProvider providerYamlFilesInCore
     */
    public function testYamlFiles($file) {
        $data = file_get_contents($file);
        try {
            $this->assertEquals(YamlSymfony::decode($data), YamlPecl::decode($data), $file);
        } catch (InvalidDataTypeException $e) {
            // Provide file context to the failure so the exception message is useful.
            $this->fail("Exception thrown parsing {$file}:\n" . $e->getMessage());
        }
    }
    
    /**
     * Ensures that decoding php objects does not work in PECL.
     *
     * @requires extension yaml
     *
     * @see \Drupal\Tests\Component\Serialization\YamlTest::testObjectSupportDisabledSymfony()
     */
    public function testObjectSupportDisabledPecl() {
        $object = new \stdClass();
        $object->foo = 'bar';
        // In core all Yaml encoding is done via Symfony and it does not support
        // objects so in order to encode an object we have to use the PECL
        // extension.
        // @see \Drupal\Component\Serialization\Yaml::encode()
        $yaml = YamlPecl::encode([
            $object,
        ]);
        $this->assertEquals([
            'O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}',
        ], YamlPecl::decode($yaml));
    }
    
    /**
     * Ensures that decoding php objects does not work in Symfony.
     *
     * @requires extension yaml
     *
     * @see \Drupal\Tests\Component\Serialization\YamlTest::testObjectSupportDisabledPecl()
     */
    public function testObjectSupportDisabledSymfony() {
        $this->expectException(InvalidDataTypeException::class);
        $this->expectExceptionMessageRegExp('/^Object support when parsing a YAML file has been disabled/');
        $object = new \stdClass();
        $object->foo = 'bar';
        // In core all Yaml encoding is done via Symfony and it does not support
        // objects so in order to encode an object we have to use the PECL
        // extension.
        // @see \Drupal\Component\Serialization\Yaml::encode()
        $yaml = YamlPecl::encode([
            $object,
        ]);
        YamlSymfony::decode($yaml);
    }
    
    /**
     * Data provider that lists all YAML files in core.
     */
    public function providerYamlFilesInCore() {
        $files = [];
        $dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../../../../../', \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
        foreach ($dirs as $dir) {
            $pathname = $dir->getPathname();
            // Exclude core/node_modules.
            if ($dir->getExtension() == 'yml' && strpos($pathname, '/../../../../../node_modules') === FALSE) {
                if (strpos($dir->getRealPath(), 'invalid_file') !== FALSE) {
                    // There are some intentionally invalid files provided for testing
                    // library API behaviors, ignore them.
                    continue;
                }
                $files[] = [
                    $dir->getRealPath(),
                ];
            }
        }
        return $files;
    }

}

Members

Title Sort descending Modifiers Object type Summary
YamlTest::$mockParser protected property
YamlTest::providerYamlFilesInCore public function Data provider that lists all YAML files in core.
YamlTest::setUp public function
YamlTest::tearDown public function
YamlTest::testDecode public function @covers ::decode
YamlTest::testGetFileExtension public function @covers ::getFileExtension
YamlTest::testObjectSupportDisabledPecl public function Ensures that decoding php objects does not work in PECL.
YamlTest::testObjectSupportDisabledSymfony public function Ensures that decoding php objects does not work in Symfony.
YamlTest::testYamlFiles public function Tests all YAML files are decoded in the same way with Symfony and PECL.

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