YamlSymfonyTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Drupal\Tests\Component\Serialization;
  3. use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
  4. use Drupal\Component\Serialization\YamlSymfony;
  5. /**
  6. * Tests the YamlSymfony serialization implementation.
  7. *
  8. * @group Drupal
  9. * @group Serialization
  10. * @coversDefaultClass \Drupal\Component\Serialization\YamlSymfony
  11. */
  12. class YamlSymfonyTest extends YamlTestBase {
  13. /**
  14. * Tests encoding and decoding basic data structures.
  15. *
  16. * @covers ::encode
  17. * @covers ::decode
  18. * @dataProvider providerEncodeDecodeTests
  19. */
  20. public function testEncodeDecode($data) {
  21. $this->assertEquals($data, YamlSymfony::decode(YamlSymfony::encode($data)));
  22. }
  23. /**
  24. * Tests decoding YAML node anchors.
  25. *
  26. * @covers ::decode
  27. * @dataProvider providerDecodeTests
  28. */
  29. public function testDecode($string, $data) {
  30. $this->assertEquals($data, YamlSymfony::decode($string));
  31. }
  32. /**
  33. * Tests our encode settings.
  34. *
  35. * @covers ::encode
  36. */
  37. public function testEncode() {
  38. $this->assertEquals('foo:
  39. bar: \'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis\'
  40. ', YamlSymfony::encode(['foo' => ['bar' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis']]));
  41. }
  42. /**
  43. * @covers ::getFileExtension
  44. */
  45. public function testGetFileExtension() {
  46. $this->assertEquals('yml', YamlSymfony::getFileExtension());
  47. }
  48. /**
  49. * Tests that invalid YAML throws an exception.
  50. *
  51. * @covers ::decode
  52. */
  53. public function testError() {
  54. if (method_exists($this, 'expectException')) {
  55. $this->expectException(InvalidDataTypeException::class);
  56. }
  57. else {
  58. $this->setExpectedException(InvalidDataTypeException::class);
  59. }
  60. YamlSymfony::decode('foo: [ads');
  61. }
  62. /**
  63. * Ensures that php object support is disabled.
  64. *
  65. * @covers ::encode
  66. */
  67. public function testObjectSupportDisabled() {
  68. if (method_exists($this, 'expectException')) {
  69. $this->expectException(InvalidDataTypeException::class);
  70. $this->expectExceptionMessage('Object support when dumping a YAML file has been disabled.');
  71. }
  72. else {
  73. $this->setExpectedException(InvalidDataTypeException::class, 'Object support when dumping a YAML file has been disabled.');
  74. }
  75. $object = new \stdClass();
  76. $object->foo = 'bar';
  77. YamlSymfony::encode([$object]);
  78. }
  79. }