PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/Util/TypeTest.php

https://github.com/WalterShe/phpunit
PHP | 269 lines | 182 code | 16 blank | 71 comment | 0 complexity | 369d8ff9184cd3f5f3eaae76de1f0da1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2001-2012, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHPUnit
  38. * @author Bernhard Schussek <bschussek@2bepublished.at>
  39. * @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
  40. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  41. * @link http://www.phpunit.de/
  42. * @since File available since Release 3.6.0
  43. */
  44. require_once 'PHPUnit/Framework/TestCase.php';
  45. require_once 'PHPUnit/Util/Type.php';
  46. /**
  47. *
  48. *
  49. * @package PHPUnit
  50. * @author Bernhard Schussek <bschussek@2bepublished.at>
  51. * @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
  52. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  53. * @link http://www.phpunit.de/
  54. * @since Class available since Release 3.6.0
  55. */
  56. class Util_TypeTest extends PHPUnit_Framework_TestCase
  57. {
  58. /**
  59. * Removes spaces in front newlines
  60. *
  61. * @param string $string
  62. * @return string
  63. */
  64. public static function trimnl($string)
  65. {
  66. return preg_replace('/[ ]*\n/', "\n", $string);
  67. }
  68. public function exportProvider()
  69. {
  70. $obj2 = new stdClass;
  71. $obj2->foo = 'bar';
  72. $obj = new stdClass;
  73. //@codingStandardsIgnoreStart
  74. $obj->null = NULL;
  75. //@codingStandardsIgnoreEnd
  76. $obj->boolean = TRUE;
  77. $obj->integer = 1;
  78. $obj->double = 1.2;
  79. $obj->string = '1';
  80. $obj->text = "this\nis\na\nvery\nvery\nvery\nvery\nvery\nvery\rlong\n\rtext";
  81. $obj->object = $obj2;
  82. $obj->objectagain = $obj2;
  83. $obj->array = array('foo' => 'bar');
  84. $obj->self = $obj;
  85. $array = array(
  86. 0 => 0,
  87. 'null' => NULL,
  88. 'boolean' => TRUE,
  89. 'integer' => 1,
  90. 'double' => 1.2,
  91. 'string' => '1',
  92. 'text' => "this\nis\na\nvery\nvery\nvery\nvery\nvery\nvery\rlong\n\rtext",
  93. 'object' => $obj2,
  94. 'objectagain' => $obj2,
  95. 'array' => array('foo' => 'bar'),
  96. );
  97. $array['self'] = &$array;
  98. return array(
  99. array(NULL, 'null'),
  100. array(TRUE, 'true'),
  101. array(1, '1'),
  102. array(1.0, '1.0'),
  103. array(1.2, '1.2'),
  104. array('1', "'1'"),
  105. // \n\r and \r is converted to \n
  106. array("this\nis\na\nvery\nvery\nvery\nvery\nvery\nvery\rlong\n\rtext",
  107. <<<EOF
  108. 'this
  109. is
  110. a
  111. very
  112. very
  113. very
  114. very
  115. very
  116. very
  117. long
  118. text'
  119. EOF
  120. ),
  121. array(new stdClass, 'stdClass Object ()'),
  122. array($obj,
  123. <<<EOF
  124. stdClass Object (
  125. 'null' => null
  126. 'boolean' => true
  127. 'integer' => 1
  128. 'double' => 1.2
  129. 'string' => '1'
  130. 'text' => 'this
  131. is
  132. a
  133. very
  134. very
  135. very
  136. very
  137. very
  138. very
  139. long
  140. text'
  141. 'object' => stdClass Object (
  142. 'foo' => 'bar'
  143. )
  144. 'objectagain' => stdClass Object (*RECURSION*)
  145. 'array' => Array (
  146. 'foo' => 'bar'
  147. )
  148. 'self' => stdClass Object (*RECURSION*)
  149. )
  150. EOF
  151. ),
  152. array(array(), 'Array ()'),
  153. array($array,
  154. <<<EOF
  155. Array (
  156. 0 => 0
  157. 'null' => null
  158. 'boolean' => true
  159. 'integer' => 1
  160. 'double' => 1.2
  161. 'string' => '1'
  162. 'text' => 'this
  163. is
  164. a
  165. very
  166. very
  167. very
  168. very
  169. very
  170. very
  171. long
  172. text'
  173. 'object' => stdClass Object (
  174. 'foo' => 'bar'
  175. )
  176. 'objectagain' => stdClass Object (*RECURSION*)
  177. 'array' => Array (
  178. 'foo' => 'bar'
  179. )
  180. 'self' => Array (*RECURSION*)
  181. )
  182. EOF
  183. ),
  184. array(
  185. chr(0) . chr(1) . chr(2) . chr(3) . chr(4) . chr(5),
  186. 'Binary String: 0x000102030405'
  187. ),
  188. array(
  189. implode('', array_map('chr', range(0x0e, 0x1f))),
  190. 'Binary String: 0x0e0f101112131415161718191a1b1c1d1e1f'
  191. ),
  192. array(
  193. chr(0x00) . chr(0x09),
  194. 'Binary String: 0x0009'
  195. ),
  196. array(
  197. '',
  198. "''"
  199. ),
  200. );
  201. }
  202. /**
  203. * @dataProvider exportProvider
  204. */
  205. public function testExport($value, $expected)
  206. {
  207. $this->assertSame($expected, self::trimnl(PHPUnit_Util_Type::export($value)));
  208. }
  209. public function shortenedExportProvider()
  210. {
  211. $obj = new stdClass;
  212. $obj->foo = 'bar';
  213. $array = array(
  214. 'foo' => 'bar',
  215. );
  216. return array(
  217. array(NULL, 'null'),
  218. array(TRUE, 'true'),
  219. array(1, '1'),
  220. array(1.0, '1.0'),
  221. array(1.2, '1.2'),
  222. array('1', "'1'"),
  223. // \n\r and \r is converted to \n
  224. array("this\nis\na\nvery\nvery\nvery\nvery\nvery\nvery\rlong\n\rtext", "'this\\nis\\na\\nvery\\nvery\\nvery\\nvery...g\\ntext'"),
  225. array(new stdClass, 'stdClass Object ()'),
  226. array($obj, 'stdClass Object (...)'),
  227. array(array(), 'Array ()'),
  228. array($array, 'Array (...)'),
  229. );
  230. }
  231. /**
  232. * @dataProvider shortenedExportProvider
  233. */
  234. public function testShortenedExport($value, $expected)
  235. {
  236. $this->assertSame($expected, self::trimnl(PHPUnit_Util_Type::shortenedExport($value)));
  237. }
  238. public function provideNonBinaryMultibyteStrings()
  239. {
  240. return array(
  241. array(implode('', array_map('chr', range(0x09, 0x0d))), 5),
  242. array(implode('', array_map('chr', range(0x20, 0x7f))), 96),
  243. array(implode('', array_map('chr', range(0x80, 0xff))), 128),
  244. );
  245. }
  246. /**
  247. * @dataProvider provideNonBinaryMultibyteStrings
  248. */
  249. public function testNonBinaryStringExport($value, $expectedLength)
  250. {
  251. $this->assertRegExp("~'.{{$expectedLength}}'\$~s", PHPUnit_Util_Type::export($value));
  252. }
  253. }