PageRenderTime 62ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Symfony/Tests/Component/Yaml/InlineTest.php

https://github.com/thewiredman/symfony
PHP | 155 lines | 129 code | 16 blank | 10 comment | 4 complexity | 77d42e33d9d8de41e63a782908578112 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\Yaml;
  10. use Symfony\Component\Yaml\Yaml;
  11. use Symfony\Component\Yaml\Inline;
  12. class InlineTest extends \PHPUnit_Framework_TestCase
  13. {
  14. static public function setUpBeforeClass()
  15. {
  16. Yaml::setSpecVersion('1.1');
  17. }
  18. public function testLoad()
  19. {
  20. foreach ($this->getTestsForLoad() as $yaml => $value) {
  21. $this->assertEquals($value, Inline::load($yaml), sprintf('::load() converts an inline YAML to a PHP structure (%s)', $yaml));
  22. }
  23. }
  24. public function testDump()
  25. {
  26. $testsForDump = $this->getTestsForDump();
  27. foreach ($testsForDump as $yaml => $value) {
  28. $this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
  29. }
  30. foreach ($this->getTestsForLoad() as $yaml => $value) {
  31. if ($value == 1230) {
  32. continue;
  33. }
  34. $this->assertEquals($value, Inline::load(Inline::dump($value)), 'check consistency');
  35. }
  36. foreach ($testsForDump as $yaml => $value) {
  37. if ($value == 1230) {
  38. continue;
  39. }
  40. $this->assertEquals($value, Inline::load(Inline::dump($value)), 'check consistency');
  41. }
  42. }
  43. protected function getTestsForLoad()
  44. {
  45. return array(
  46. '' => '',
  47. 'null' => null,
  48. 'false' => false,
  49. 'true' => true,
  50. '12' => 12,
  51. '"quoted string"' => 'quoted string',
  52. "'quoted string'" => 'quoted string',
  53. '12.30e+02' => 12.30e+02,
  54. '0x4D2' => 0x4D2,
  55. '02333' => 02333,
  56. '.Inf' => -log(0),
  57. '-.Inf' => log(0),
  58. '123456789123456789' => '123456789123456789',
  59. '"foo\r\nbar"' => "foo\r\nbar",
  60. "'foo#bar'" => 'foo#bar',
  61. "'foo # bar'" => 'foo # bar',
  62. "'#cfcfcf'" => '#cfcfcf',
  63. '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
  64. '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  65. '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  66. '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
  67. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  68. // sequences
  69. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  70. '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
  71. '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12),
  72. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  73. // mappings
  74. '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  75. '{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  76. '{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  77. '{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  78. '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'),
  79. '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'),
  80. // nested sequences and mappings
  81. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  82. '[foo, {bar: foo}]' => array('foo', array('bar' => 'foo')),
  83. '{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')),
  84. '{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')),
  85. '[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')),
  86. '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),
  87. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  88. '[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))),
  89. '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
  90. );
  91. }
  92. protected function getTestsForDump()
  93. {
  94. return array(
  95. 'null' => null,
  96. 'false' => false,
  97. 'true' => true,
  98. '12' => 12,
  99. "'quoted string'" => 'quoted string',
  100. '12.30e+02' => 12.30e+02,
  101. '1234' => 0x4D2,
  102. '1243' => 02333,
  103. '.Inf' => -log(0),
  104. '-.Inf' => log(0),
  105. '"foo\r\nbar"' => "foo\r\nbar",
  106. "'foo#bar'" => 'foo#bar',
  107. "'foo # bar'" => 'foo # bar',
  108. "'#cfcfcf'" => '#cfcfcf',
  109. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  110. // sequences
  111. '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
  112. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  113. // mappings
  114. '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  115. '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  116. // nested sequences and mappings
  117. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  118. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  119. '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')),
  120. '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),
  121. '[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))),
  122. );
  123. }
  124. }