YamlTest.php in Drupal 10
File
core/tests/Drupal/Tests/Component/Serialization/YamlTest.php
View source
<?php
namespace Drupal\Tests\Component\Serialization;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Component\Serialization\Yaml;
use Drupal\Component\Serialization\YamlPecl;
use Drupal\Component\Serialization\YamlSymfony;
use PHPUnit\Framework\TestCase;
class YamlTest extends TestCase {
protected $mockParser;
protected function setUp() : void {
parent::setUp();
$this->mockParser = $this
->getMockBuilder('\\stdClass')
->addMethods([
'encode',
'decode',
'getFileExtension',
])
->getMock();
YamlParserProxy::setMock($this->mockParser);
}
protected function tearDown() : void {
YamlParserProxy::setMock(NULL);
parent::tearDown();
}
public function testDecode() {
$this->mockParser
->expects($this
->once())
->method('decode');
YamlStub::decode('test');
}
public function testGetFileExtension() {
$this->mockParser
->expects($this
->never())
->method('getFileExtension');
$this
->assertEquals('yml', YamlStub::getFileExtension());
}
public function testYamlFiles($file) {
$data = file_get_contents($file);
try {
$this
->assertEquals(YamlSymfony::decode($data), YamlPecl::decode($data), $file);
} catch (InvalidDataTypeException $e) {
$this
->fail("Exception thrown parsing {$file}:\n" . $e
->getMessage());
}
}
public function testObjectSupportDisabledPecl() {
$object = new \stdClass();
$object->foo = 'bar';
$yaml = YamlPecl::encode([
$object,
]);
$this
->assertEquals([
'O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}',
], YamlPecl::decode($yaml));
}
public function testObjectSupportDisabledSymfony() {
$this
->expectException(InvalidDataTypeException::class);
$this
->expectExceptionMessageMatches('/^Object support when parsing a YAML file has been disabled/');
$object = new \stdClass();
$object->foo = 'bar';
$yaml = YamlPecl::encode([
$object,
]);
YamlSymfony::decode($yaml);
}
public function providerYamlFilesInCore() {
$files = [];
$dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../../../../../', \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
foreach ($dirs as $dir) {
$pathname = $dir
->getPathname();
if ($dir
->getExtension() == 'yml' && strpos($pathname, '/../../../../../node_modules') === FALSE) {
if (strpos($dir
->getRealPath(), 'invalid_file') !== FALSE) {
continue;
}
$files[] = [
$dir
->getRealPath(),
];
}
}
return $files;
}
}
class YamlStub extends Yaml {
public static function getSerializer() {
return '\\Drupal\\Tests\\Component\\Serialization\\YamlParserProxy';
}
}
class YamlParserProxy implements SerializationInterface {
protected static $mock;
public static function setMock($mock) {
static::$mock = $mock;
}
public static function encode($data) {
return static::$mock
->encode($data);
}
public static function decode($raw) {
return static::$mock
->decode($raw);
}
public static function getFileExtension() {
return static::$mock
->getFileExtension();
}
}