YamlPeclTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Drupal\Tests\Component\Serialization;
  3. use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
  4. use Drupal\Component\Serialization\YamlPecl;
  5. /**
  6. * Tests the YamlPecl serialization implementation.
  7. *
  8. * @group Drupal
  9. * @group Serialization
  10. * @coversDefaultClass \Drupal\Component\Serialization\YamlPecl
  11. * @requires extension yaml
  12. */
  13. class YamlPeclTest extends YamlTestBase {
  14. /**
  15. * Tests encoding and decoding basic data structures.
  16. *
  17. * @covers ::encode
  18. * @covers ::decode
  19. * @dataProvider providerEncodeDecodeTests
  20. */
  21. public function testEncodeDecode($data) {
  22. $this->assertEquals($data, YamlPecl::decode(YamlPecl::encode($data)));
  23. }
  24. /**
  25. * Ensures that php object support is disabled.
  26. */
  27. public function testObjectSupportDisabled() {
  28. $object = new \stdClass();
  29. $object->foo = 'bar';
  30. $this->assertEquals(['O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}'], YamlPecl::decode(YamlPecl::encode([$object])));
  31. $this->assertEquals(0, ini_get('yaml.decode_php'));
  32. }
  33. /**
  34. * Tests decoding YAML node anchors.
  35. *
  36. * @covers ::decode
  37. * @dataProvider providerDecodeTests
  38. */
  39. public function testDecode($string, $data) {
  40. $this->assertEquals($data, YamlPecl::decode($string));
  41. }
  42. /**
  43. * Tests our encode settings.
  44. *
  45. * @covers ::encode
  46. */
  47. public function testEncode() {
  48. $this->assertEquals('---
  49. foo:
  50. bar: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis
  51. ...
  52. ', YamlPecl::encode(['foo' => ['bar' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis']]));
  53. }
  54. /**
  55. * Tests YAML boolean callback.
  56. *
  57. * @param string $string
  58. * String value for the YAML boolean.
  59. * @param string|bool $expected
  60. * The expected return value.
  61. *
  62. * @covers ::applyBooleanCallbacks
  63. * @dataProvider providerBoolTest
  64. */
  65. public function testApplyBooleanCallbacks($string, $expected) {
  66. $this->assertEquals($expected, YamlPecl::applyBooleanCallbacks($string, 'bool', NULL));
  67. }
  68. /**
  69. * @covers ::getFileExtension
  70. */
  71. public function testGetFileExtension() {
  72. $this->assertEquals('yml', YamlPecl::getFileExtension());
  73. }
  74. /**
  75. * Tests that invalid YAML throws an exception.
  76. *
  77. * @covers ::errorHandler
  78. */
  79. public function testError() {
  80. if (method_exists($this, 'expectException')) {
  81. $this->expectException(InvalidDataTypeException::class);
  82. }
  83. else {
  84. $this->setExpectedException(InvalidDataTypeException::class);
  85. }
  86. YamlPecl::decode('foo: [ads');
  87. }
  88. }