/src/xmlrpc-3.0/test/PHPUnit/Assert.php

  1. <?php 
  2. /** vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 
  3.  
  4. /** 
  5. * PHP Version 4 
  6. * 
  7. * LICENSE: This source file is subject to version 3.0 of the PHP license 
  8. * that is available through the world-wide-web at the following URI: 
  9. * http://www.php.net/license/3_0.txt. If you did not receive a copy of 
  10. * the PHP License and are unable to obtain it through the web, please 
  11. * send a note to license@php.net so we can mail you a copy immediately. 
  12. * 
  13. * @category Testing 
  14. * @package PHPUnit 
  15. * @author Sebastian Bergmann <sb@sebastian-bergmann.de> 
  16. * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> 
  17. * @license http://www.php.net/license/3_0.txt PHP License 3.0 
  18. * @version CVS: $Id: Assert.php 2 2009-03-16 20:22:51Z ggiunta $ 
  19. * @link http://pear.php.net/package/PHPUnit 
  20. * @since File available since Release 1.0.0 
  21. */ 
  22.  
  23. /** 
  24. * A set of assert methods. 
  25. * 
  26. * @category Testing 
  27. * @package PHPUnit 
  28. * @author Sebastian Bergmann <sb@sebastian-bergmann.de> 
  29. * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> 
  30. * @license http://www.php.net/license/3_0.txt PHP License 3.0 
  31. * @version Release: @package_version@ 
  32. * @link http://pear.php.net/package/PHPUnit 
  33. * @since Class available since Release 1.0.0 
  34. */ 
  35. class PHPUnit_Assert { 
  36. /** 
  37. * @var boolean 
  38. * @access private 
  39. */ 
  40. var $_looselyTyped = FALSE; 
  41.  
  42. /** 
  43. * Asserts that a haystack contains a needle. 
  44. * 
  45. * @param mixed 
  46. * @param mixed 
  47. * @param string 
  48. * @access public 
  49. * @since Method available since Release 1.1.0 
  50. */ 
  51. function assertContains($needle, $haystack, $message = '') { 
  52. if (is_string($needle) && is_string($haystack)) { 
  53. $this->assertTrue(strpos($haystack, $needle) !== FALSE, $message); 
  54.  
  55. else if (is_array($haystack) && !is_object($needle)) { 
  56. $this->assertTrue(in_array($needle, $haystack), $message); 
  57.  
  58. else { 
  59. $this->fail('Unsupported parameter passed to assertContains().'); 
  60.  
  61. /** 
  62. * Asserts that a haystack does not contain a needle. 
  63. * 
  64. * @param mixed 
  65. * @param mixed 
  66. * @param string 
  67. * @access public 
  68. * @since Method available since Release 1.1.0 
  69. */ 
  70. function assertNotContains($needle, $haystack, $message = '') { 
  71. if (is_string($needle) && is_string($haystack)) { 
  72. $this->assertFalse(strpos($haystack, $needle) !== FALSE, $message); 
  73.  
  74. else if (is_array($haystack) && !is_object($needle)) { 
  75. $this->assertFalse(in_array($needle, $haystack), $message); 
  76.  
  77. else { 
  78. $this->fail('Unsupported parameter passed to assertNotContains().'); 
  79.  
  80. /** 
  81. * Asserts that two variables are equal. 
  82. * 
  83. * @param mixed 
  84. * @param mixed 
  85. * @param string 
  86. * @param mixed 
  87. * @access public 
  88. */ 
  89. function assertEquals($expected, $actual, $message = '', $delta = 0) { 
  90. if ((is_array($actual) && is_array($expected)) || 
  91. (is_object($actual) && is_object($expected))) { 
  92. if (is_array($actual) && is_array($expected)) { 
  93. ksort($actual); 
  94. ksort($expected); 
  95.  
  96. if ($this->_looselyTyped) { 
  97. $actual = $this->_convertToString($actual); 
  98. $expected = $this->_convertToString($expected); 
  99.  
  100. $actual = serialize($actual); 
  101. $expected = serialize($expected); 
  102.  
  103. $message = sprintf( 
  104. '%sexpected %s, actual %s',  
  105.  
  106. !empty($message) ? $message . ' ' : '',  
  107. $expected,  
  108. $actual 
  109. ); 
  110.  
  111. if ($actual !== $expected) { 
  112. return $this->fail($message); 
  113.  
  114. elseif (is_numeric($actual) && is_numeric($expected)) { 
  115. $message = sprintf( 
  116. '%sexpected %s%s, actual %s',  
  117.  
  118. !empty($message) ? $message . ' ' : '',  
  119. $expected,  
  120. ($delta != 0) ? ('+/- ' . $delta) : '',  
  121. $actual 
  122. ); 
  123.  
  124. if (!($actual >= ($expected - $delta) && $actual <= ($expected + $delta))) { 
  125. return $this->fail($message); 
  126.  
  127. else { 
  128. $message = sprintf( 
  129. '%sexpected %s, actual %s',  
  130.  
  131. !empty($message) ? $message . ' ' : '',  
  132. $expected,  
  133. $actual 
  134. ); 
  135.  
  136. if ($actual !== $expected) { 
  137. return $this->fail($message); 
  138.  
  139. /** 
  140. * Asserts that two variables reference the same object. 
  141. * This requires the Zend Engine 2 to work. 
  142. * 
  143. * @param object 
  144. * @param object 
  145. * @param string 
  146. * @access public 
  147. * @deprecated 
  148. */ 
  149. function assertSame($expected, $actual, $message = '') { 
  150. if (!version_compare(phpversion(), '5.0.0', '>=')) { 
  151. $this->fail('assertSame() only works with PHP >= 5.0.0.'); 
  152.  
  153. if ((is_object($expected) || is_null($expected)) && 
  154. (is_object($actual) || is_null($actual))) { 
  155. $message = sprintf( 
  156. '%sexpected two variables to reference the same object',  
  157.  
  158. !empty($message) ? $message . ' ' : '' 
  159. ); 
  160.  
  161. if ($expected !== $actual) { 
  162. return $this->fail($message); 
  163. } else { 
  164. $this->fail('Unsupported parameter passed to assertSame().'); 
  165.  
  166. /** 
  167. * Asserts that two variables do not reference the same object. 
  168. * This requires the Zend Engine 2 to work. 
  169. * 
  170. * @param object 
  171. * @param object 
  172. * @param string 
  173. * @access public 
  174. * @deprecated 
  175. */ 
  176. function assertNotSame($expected, $actual, $message = '') { 
  177. if (!version_compare(phpversion(), '5.0.0', '>=')) { 
  178. $this->fail('assertNotSame() only works with PHP >= 5.0.0.'); 
  179.  
  180. if ((is_object($expected) || is_null($expected)) && 
  181. (is_object($actual) || is_null($actual))) { 
  182. $message = sprintf( 
  183. '%sexpected two variables to reference different objects',  
  184.  
  185. !empty($message) ? $message . ' ' : '' 
  186. ); 
  187.  
  188. if ($expected === $actual) { 
  189. return $this->fail($message); 
  190. } else { 
  191. $this->fail('Unsupported parameter passed to assertNotSame().'); 
  192.  
  193. /** 
  194. * Asserts that a variable is not NULL. 
  195. * 
  196. * @param mixed 
  197. * @param string 
  198. * @access public 
  199. */ 
  200. function assertNotNull($actual, $message = '') { 
  201. $message = sprintf( 
  202. '%sexpected NOT NULL, actual NULL',  
  203.  
  204. !empty($message) ? $message . ' ' : '' 
  205. ); 
  206.  
  207. if (is_null($actual)) { 
  208. return $this->fail($message); 
  209.  
  210. /** 
  211. * Asserts that a variable is NULL. 
  212. * 
  213. * @param mixed 
  214. * @param string 
  215. * @access public 
  216. */ 
  217. function assertNull($actual, $message = '') { 
  218. $message = sprintf( 
  219. '%sexpected NULL, actual NOT NULL',  
  220.  
  221. !empty($message) ? $message . ' ' : '' 
  222. ); 
  223.  
  224. if (!is_null($actual)) { 
  225. return $this->fail($message); 
  226.  
  227. /** 
  228. * Asserts that a condition is true. 
  229. * 
  230. * @param boolean 
  231. * @param string 
  232. * @access public 
  233. */ 
  234. function assertTrue($condition, $message = '') { 
  235. $message = sprintf( 
  236. '%sexpected TRUE, actual FALSE',  
  237.  
  238. !empty($message) ? $message . ' ' : '' 
  239. ); 
  240.  
  241. if (!$condition) { 
  242. return $this->fail($message); 
  243.  
  244. /** 
  245. * Asserts that a condition is false. 
  246. * 
  247. * @param boolean 
  248. * @param string 
  249. * @access public 
  250. */ 
  251. function assertFalse($condition, $message = '') { 
  252. $message = sprintf( 
  253. '%sexpected FALSE, actual TRUE',  
  254.  
  255. !empty($message) ? $message . ' ' : '' 
  256. ); 
  257.  
  258. if ($condition) { 
  259. return $this->fail($message); 
  260.  
  261. /** 
  262. * Asserts that a string matches a given regular expression. 
  263. * 
  264. * @param string 
  265. * @param string 
  266. * @param string 
  267. * @access public 
  268. */ 
  269. function assertRegExp($pattern, $string, $message = '') { 
  270. $message = sprintf( 
  271. '%s"%s" does not match pattern "%s"',  
  272.  
  273. !empty($message) ? $message . ' ' : '',  
  274. $string,  
  275. $pattern 
  276. ); 
  277.  
  278. if (!preg_match($pattern, $string)) { 
  279. return $this->fail($message); 
  280.  
  281. /** 
  282. * Asserts that a string does not match a given regular expression. 
  283. * 
  284. * @param string 
  285. * @param string 
  286. * @param string 
  287. * @access public 
  288. * @since Method available since Release 1.1.0 
  289. */ 
  290. function assertNotRegExp($pattern, $string, $message = '') { 
  291. $message = sprintf( 
  292. '%s"%s" matches pattern "%s"',  
  293.  
  294. !empty($message) ? $message . ' ' : '',  
  295. $string,  
  296. $pattern 
  297. ); 
  298.  
  299. if (preg_match($pattern, $string)) { 
  300. return $this->fail($message); 
  301.  
  302. /** 
  303. * Asserts that a variable is of a given type. 
  304. * 
  305. * @param string $expected 
  306. * @param mixed $actual 
  307. * @param optional string $message 
  308. * @access public 
  309. */ 
  310. function assertType($expected, $actual, $message = '') { 
  311. return $this->assertEquals( 
  312. $expected,  
  313. gettype($actual),  
  314. $message 
  315. ); 
  316.  
  317. /** 
  318. * Converts a value to a string. 
  319. * 
  320. * @param mixed $value 
  321. * @access private 
  322. */ 
  323. function _convertToString($value) { 
  324. foreach ($value as $k => $v) { 
  325. if (is_array($v)) { 
  326. $value[$k] = $this->_convertToString($value[$k]); 
  327. } else { 
  328. settype($value[$k], 'string'); 
  329.  
  330. return $value; 
  331.  
  332. /** 
  333. * @param boolean $looselyTyped 
  334. * @access public 
  335. */ 
  336. function setLooselyTyped($looselyTyped) { 
  337. if (is_bool($looselyTyped)) { 
  338. $this->_looselyTyped = $looselyTyped; 
  339.  
  340. /** 
  341. * Fails a test with the given message. 
  342. * 
  343. * @param string 
  344. * @access protected 
  345. * @abstract 
  346. */ 
  347. function fail($message = '') { /** abstract */
  348.  
  349. /** 
  350. * Local variables: 
  351. * tab-width: 4 
  352. * c-basic-offset: 4 
  353. * c-hanging-comment-ender-p: nil 
  354. * End: 
  355. */ 
  356. ?> 
.