3 namespace Drupal\Tests\Component\Serialization;
5 use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
6 use Drupal\Component\Serialization\SerializationInterface;
7 use Drupal\Component\Serialization\Yaml;
8 use Drupal\Component\Serialization\YamlPecl;
9 use Drupal\Component\Serialization\YamlSymfony;
10 use Drupal\Tests\UnitTestCase;
13 * @coversDefaultClass \Drupal\Component\Serialization\Yaml
14 * @group Serialization
16 class YamlTest extends UnitTestCase {
19 * @var \PHPUnit_Framework_MockObject_MockObject
21 protected $mockParser;
23 public function setUp() {
25 $this->mockParser = $this->getMockBuilder('\stdClass')
26 ->setMethods(['encode', 'decode', 'getFileExtension'])
28 YamlParserProxy::setMock($this->mockParser);
31 public function tearDown() {
32 YamlParserProxy::setMock(NULL);
39 public function testDecode() {
41 ->expects($this->once())
43 YamlStub::decode('test');
47 * @covers ::getFileExtension
49 public function testGetFileExtension() {
51 ->expects($this->never())
52 ->method('getFileExtension');
53 $this->assertEquals('yml', YamlStub::getFileExtension());
57 * Tests all YAML files are decoded in the same way with Symfony and PECL.
59 * This test is a little bit slow but it tests that we do not have any bugs in
60 * our YAML that might not be decoded correctly in any of our implementations.
62 * @todo This should exist as an integration test not part of our unit tests.
63 * https://www.drupal.org/node/2597730
65 * @requires extension yaml
66 * @dataProvider providerYamlFilesInCore
68 public function testYamlFiles($file) {
69 $data = file_get_contents($file);
71 $this->assertEquals(YamlSymfony::decode($data), YamlPecl::decode($data), $file);
73 catch (InvalidDataTypeException $e) {
74 // Provide file context to the failure so the exception message is useful.
75 $this->fail("Exception thrown parsing $file:\n" . $e->getMessage());
80 * Ensures that decoding php objects is similar for PECL and Symfony.
82 * @requires extension yaml
84 public function testObjectSupportDisabled() {
85 $object = new \stdClass();
87 // In core all Yaml encoding is done via Symfony and it does not support
88 // objects so in order to encode an object we hace to use the PECL
90 // @see \Drupal\Component\Serialization\Yaml::encode()
91 $yaml = YamlPecl::encode([$object]);
92 $this->assertEquals(['O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}'], YamlPecl::decode($yaml));
93 $this->assertEquals(['!php/object "O:8:\"stdClass\":1:{s:3:\"foo\";s:3:\"bar\";}"'], YamlSymfony::decode($yaml));
97 * Data provider that lists all YAML files in core.
99 public function providerYamlFilesInCore() {
101 $dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../../../../../', \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
102 foreach ($dirs as $dir) {
103 $pathname = $dir->getPathname();
105 if ($dir->getExtension() == 'yml' && strpos($pathname, '/../../../../../vendor') === FALSE) {
106 if (strpos($dir->getRealPath(), 'invalid_file') !== FALSE) {
107 // There are some intentionally invalid files provided for testing
108 // library API behaviours, ignore them.
111 $files[] = [$dir->getRealPath()];
119 class YamlStub extends Yaml {
121 public static function getSerializer() {
122 return '\Drupal\Tests\Component\Serialization\YamlParserProxy';
127 class YamlParserProxy implements SerializationInterface {
130 * @var \Drupal\Component\Serialization\SerializationInterface
132 protected static $mock;
134 public static function setMock($mock) {
135 static::$mock = $mock;
138 public static function encode($data) {
139 return static::$mock->encode($data);
142 public static function decode($raw) {
143 return static::$mock->decode($raw);
146 public static function getFileExtension() {
147 return static::$mock->getFileExtension();