PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/classes/Symfony/Component/Yaml/Tests/InlineTest.php

https://gitlab.com/jalon/doadoronline
PHP | 231 lines | 179 code | 26 blank | 26 comment | 2 complexity | 8218e514512562e64f82e2c64a433a6a MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml\Tests;
  11. use Symfony\Component\Yaml\Inline;
  12. class InlineTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testParse()
  15. {
  16. foreach ($this->getTestsForParse() as $yaml => $value) {
  17. $this->assertSame($value, Inline::parse($yaml), sprintf('::parse() converts an inline YAML to a PHP structure (%s)', $yaml));
  18. }
  19. }
  20. public function testDump()
  21. {
  22. $testsForDump = $this->getTestsForDump();
  23. foreach ($testsForDump as $yaml => $value) {
  24. $this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
  25. }
  26. foreach ($this->getTestsForParse() as $value) {
  27. $this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
  28. }
  29. foreach ($testsForDump as $value) {
  30. $this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency');
  31. }
  32. }
  33. public function testDumpNumericValueWithLocale()
  34. {
  35. $locale = setlocale(LC_NUMERIC, 0);
  36. if (false === $locale) {
  37. $this->markTestSkipped('Your platform does not support locales.');
  38. }
  39. $required_locales = array('fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252');
  40. if (false === setlocale(LC_ALL, $required_locales)) {
  41. $this->markTestSkipped('Could not set any of required locales: '.implode(", ", $required_locales));
  42. }
  43. $this->assertEquals('1.2', Inline::dump(1.2));
  44. $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
  45. setlocale(LC_ALL, $locale);
  46. }
  47. public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
  48. {
  49. $value = '686e444';
  50. $this->assertSame($value, Inline::parse(Inline::dump($value)));
  51. }
  52. /**
  53. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  54. */
  55. public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException()
  56. {
  57. $value = "'don't do somthin' like that'";
  58. Inline::parse($value);
  59. }
  60. /**
  61. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  62. */
  63. public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
  64. {
  65. $value = '"don"t do somthin" like that"';
  66. Inline::parse($value);
  67. }
  68. /**
  69. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  70. */
  71. public function testParseInvalidMappingKeyShouldThrowException()
  72. {
  73. $value = '{ "foo " bar": "bar" }';
  74. Inline::parse($value);
  75. }
  76. /**
  77. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  78. */
  79. public function testParseInvalidMappingShouldThrowException()
  80. {
  81. Inline::parse('[foo] bar');
  82. }
  83. /**
  84. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  85. */
  86. public function testParseInvalidSequenceShouldThrowException()
  87. {
  88. Inline::parse('{ foo: bar } bar');
  89. }
  90. public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
  91. {
  92. $value = "'don''t do somthin'' like that'";
  93. $expect = "don't do somthin' like that";
  94. $this->assertSame($expect, Inline::parseScalar($value));
  95. }
  96. protected function getTestsForParse()
  97. {
  98. return array(
  99. '' => '',
  100. 'null' => null,
  101. 'false' => false,
  102. 'true' => true,
  103. '12' => 12,
  104. '-12' => -12,
  105. '"quoted string"' => 'quoted string',
  106. "'quoted string'" => 'quoted string',
  107. '12.30e+02' => 12.30e+02,
  108. '0x4D2' => 0x4D2,
  109. '02333' => 02333,
  110. '.Inf' => -log(0),
  111. '-.Inf' => log(0),
  112. "'686e444'" => '686e444',
  113. '686e444' => 646e444,
  114. '123456789123456789123456789123456789' => '123456789123456789123456789123456789',
  115. '"foo\r\nbar"' => "foo\r\nbar",
  116. "'foo#bar'" => 'foo#bar',
  117. "'foo # bar'" => 'foo # bar',
  118. "'#cfcfcf'" => '#cfcfcf',
  119. '::form_base.html.twig' => '::form_base.html.twig',
  120. '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007),
  121. '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  122. '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007),
  123. '1960-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 1960),
  124. '1730-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 1730),
  125. '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',
  126. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  127. // sequences
  128. // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
  129. '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12),
  130. '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12),
  131. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  132. // mappings
  133. '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  134. '{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  135. '{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  136. '{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  137. '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'),
  138. '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'),
  139. // nested sequences and mappings
  140. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  141. '[foo, {bar: foo}]' => array('foo', array('bar' => 'foo')),
  142. '{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')),
  143. '{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')),
  144. '[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')),
  145. '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))),
  146. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  147. '[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'))),
  148. '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))),
  149. '[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']' => array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%',), true, '@service_container',),
  150. );
  151. }
  152. protected function getTestsForDump()
  153. {
  154. return array(
  155. 'null' => null,
  156. 'false' => false,
  157. 'true' => true,
  158. '12' => 12,
  159. "'quoted string'" => 'quoted string',
  160. '12.30e+02' => 12.30e+02,
  161. '1234' => 0x4D2,
  162. '1243' => 02333,
  163. '.Inf' => -log(0),
  164. '-.Inf' => log(0),
  165. "'686e444'" => '686e444',
  166. '"foo\r\nbar"' => "foo\r\nbar",
  167. "'foo#bar'" => 'foo#bar',
  168. "'foo # bar'" => 'foo # bar',
  169. "'#cfcfcf'" => '#cfcfcf',
  170. "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'',
  171. "'-dash'" => '-dash',
  172. "'-'" => '-',
  173. // sequences
  174. '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
  175. '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
  176. // mappings
  177. '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12),
  178. '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'),
  179. // nested sequences and mappings
  180. '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')),
  181. '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')),
  182. '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')),
  183. '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')),
  184. '[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'))),
  185. '[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']' => array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%',), true, '@service_container',),
  186. );
  187. }
  188. }