PHPUnit_Assert

A set of assert methods.

Defined (1)

The class is defined in the following location(s).

/src/xmlrpc-3.0/test/PHPUnit/Assert.php  
  1. class PHPUnit_Assert { 
  2. /** 
  3. * @var boolean 
  4. * @access private 
  5. */ 
  6. var $_looselyTyped = FALSE; 
  7.  
  8. /** 
  9. * Asserts that a haystack contains a needle. 
  10. * @param mixed 
  11. * @param mixed 
  12. * @param string 
  13. * @access public 
  14. * @since Method available since Release 1.1.0 
  15. */ 
  16. function assertContains($needle, $haystack, $message = '') { 
  17. if (is_string($needle) && is_string($haystack)) { 
  18. $this->assertTrue(strpos($haystack, $needle) !== FALSE, $message); 
  19.  
  20. else if (is_array($haystack) && !is_object($needle)) { 
  21. $this->assertTrue(in_array($needle, $haystack), $message); 
  22.  
  23. else { 
  24. $this->fail('Unsupported parameter passed to assertContains().'); 
  25.  
  26. /** 
  27. * Asserts that a haystack does not contain a needle. 
  28. * @param mixed 
  29. * @param mixed 
  30. * @param string 
  31. * @access public 
  32. * @since Method available since Release 1.1.0 
  33. */ 
  34. function assertNotContains($needle, $haystack, $message = '') { 
  35. if (is_string($needle) && is_string($haystack)) { 
  36. $this->assertFalse(strpos($haystack, $needle) !== FALSE, $message); 
  37.  
  38. else if (is_array($haystack) && !is_object($needle)) { 
  39. $this->assertFalse(in_array($needle, $haystack), $message); 
  40.  
  41. else { 
  42. $this->fail('Unsupported parameter passed to assertNotContains().'); 
  43.  
  44. /** 
  45. * Asserts that two variables are equal. 
  46. * @param mixed 
  47. * @param mixed 
  48. * @param string 
  49. * @param mixed 
  50. * @access public 
  51. */ 
  52. function assertEquals($expected, $actual, $message = '', $delta = 0) { 
  53. if ((is_array($actual) && is_array($expected)) || 
  54. (is_object($actual) && is_object($expected))) { 
  55. if (is_array($actual) && is_array($expected)) { 
  56. ksort($actual); 
  57. ksort($expected); 
  58.  
  59. if ($this->_looselyTyped) { 
  60. $actual = $this->_convertToString($actual); 
  61. $expected = $this->_convertToString($expected); 
  62.  
  63. $actual = serialize($actual); 
  64. $expected = serialize($expected); 
  65.  
  66. $message = sprintf( 
  67. '%sexpected %s, actual %s',  
  68.  
  69. !empty($message) ? $message . ' ' : '',  
  70. $expected,  
  71. $actual 
  72. ); 
  73.  
  74. if ($actual !== $expected) { 
  75. return $this->fail($message); 
  76.  
  77. elseif (is_numeric($actual) && is_numeric($expected)) { 
  78. $message = sprintf( 
  79. '%sexpected %s%s, actual %s',  
  80.  
  81. !empty($message) ? $message . ' ' : '',  
  82. $expected,  
  83. ($delta != 0) ? ('+/- ' . $delta) : '',  
  84. $actual 
  85. ); 
  86.  
  87. if (!($actual >= ($expected - $delta) && $actual <= ($expected + $delta))) { 
  88. return $this->fail($message); 
  89.  
  90. else { 
  91. $message = sprintf( 
  92. '%sexpected %s, actual %s',  
  93.  
  94. !empty($message) ? $message . ' ' : '',  
  95. $expected,  
  96. $actual 
  97. ); 
  98.  
  99. if ($actual !== $expected) { 
  100. return $this->fail($message); 
  101.  
  102. /** 
  103. * Asserts that two variables reference the same object. 
  104. * This requires the Zend Engine 2 to work. 
  105. * @param object 
  106. * @param object 
  107. * @param string 
  108. * @access public 
  109. * @deprecated 
  110. */ 
  111. function assertSame($expected, $actual, $message = '') { 
  112. if (!version_compare(phpversion(), '5.0.0', '>=')) { 
  113. $this->fail('assertSame() only works with PHP >= 5.0.0.'); 
  114.  
  115. if ((is_object($expected) || is_null($expected)) && 
  116. (is_object($actual) || is_null($actual))) { 
  117. $message = sprintf( 
  118. '%sexpected two variables to reference the same object',  
  119.  
  120. !empty($message) ? $message . ' ' : '' 
  121. ); 
  122.  
  123. if ($expected !== $actual) { 
  124. return $this->fail($message); 
  125. } else { 
  126. $this->fail('Unsupported parameter passed to assertSame().'); 
  127.  
  128. /** 
  129. * Asserts that two variables do not reference the same object. 
  130. * This requires the Zend Engine 2 to work. 
  131. * @param object 
  132. * @param object 
  133. * @param string 
  134. * @access public 
  135. * @deprecated 
  136. */ 
  137. function assertNotSame($expected, $actual, $message = '') { 
  138. if (!version_compare(phpversion(), '5.0.0', '>=')) { 
  139. $this->fail('assertNotSame() only works with PHP >= 5.0.0.'); 
  140.  
  141. if ((is_object($expected) || is_null($expected)) && 
  142. (is_object($actual) || is_null($actual))) { 
  143. $message = sprintf( 
  144. '%sexpected two variables to reference different objects',  
  145.  
  146. !empty($message) ? $message . ' ' : '' 
  147. ); 
  148.  
  149. if ($expected === $actual) { 
  150. return $this->fail($message); 
  151. } else { 
  152. $this->fail('Unsupported parameter passed to assertNotSame().'); 
  153.  
  154. /** 
  155. * Asserts that a variable is not NULL. 
  156. * @param mixed 
  157. * @param string 
  158. * @access public 
  159. */ 
  160. function assertNotNull($actual, $message = '') { 
  161. $message = sprintf( 
  162. '%sexpected NOT NULL, actual NULL',  
  163.  
  164. !empty($message) ? $message . ' ' : '' 
  165. ); 
  166.  
  167. if (is_null($actual)) { 
  168. return $this->fail($message); 
  169.  
  170. /** 
  171. * Asserts that a variable is NULL. 
  172. * @param mixed 
  173. * @param string 
  174. * @access public 
  175. */ 
  176. function assertNull($actual, $message = '') { 
  177. $message = sprintf( 
  178. '%sexpected NULL, actual NOT NULL',  
  179.  
  180. !empty($message) ? $message . ' ' : '' 
  181. ); 
  182.  
  183. if (!is_null($actual)) { 
  184. return $this->fail($message); 
  185.  
  186. /** 
  187. * Asserts that a condition is true. 
  188. * @param boolean 
  189. * @param string 
  190. * @access public 
  191. */ 
  192. function assertTrue($condition, $message = '') { 
  193. $message = sprintf( 
  194. '%sexpected TRUE, actual FALSE',  
  195.  
  196. !empty($message) ? $message . ' ' : '' 
  197. ); 
  198.  
  199. if (!$condition) { 
  200. return $this->fail($message); 
  201.  
  202. /** 
  203. * Asserts that a condition is false. 
  204. * @param boolean 
  205. * @param string 
  206. * @access public 
  207. */ 
  208. function assertFalse($condition, $message = '') { 
  209. $message = sprintf( 
  210. '%sexpected FALSE, actual TRUE',  
  211.  
  212. !empty($message) ? $message . ' ' : '' 
  213. ); 
  214.  
  215. if ($condition) { 
  216. return $this->fail($message); 
  217.  
  218. /** 
  219. * Asserts that a string matches a given regular expression. 
  220. * @param string 
  221. * @param string 
  222. * @param string 
  223. * @access public 
  224. */ 
  225. function assertRegExp($pattern, $string, $message = '') { 
  226. $message = sprintf( 
  227. '%s"%s" does not match pattern "%s"',  
  228.  
  229. !empty($message) ? $message . ' ' : '',  
  230. $string,  
  231. $pattern 
  232. ); 
  233.  
  234. if (!preg_match($pattern, $string)) { 
  235. return $this->fail($message); 
  236.  
  237. /** 
  238. * Asserts that a string does not match a given regular expression. 
  239. * @param string 
  240. * @param string 
  241. * @param string 
  242. * @access public 
  243. * @since Method available since Release 1.1.0 
  244. */ 
  245. function assertNotRegExp($pattern, $string, $message = '') { 
  246. $message = sprintf( 
  247. '%s"%s" matches pattern "%s"',  
  248.  
  249. !empty($message) ? $message . ' ' : '',  
  250. $string,  
  251. $pattern 
  252. ); 
  253.  
  254. if (preg_match($pattern, $string)) { 
  255. return $this->fail($message); 
  256.  
  257. /** 
  258. * Asserts that a variable is of a given type. 
  259. * @param string $expected 
  260. * @param mixed $actual 
  261. * @param optional string $message 
  262. * @access public 
  263. */ 
  264. function assertType($expected, $actual, $message = '') { 
  265. return $this->assertEquals( 
  266. $expected,  
  267. gettype($actual),  
  268. $message 
  269. ); 
  270.  
  271. /** 
  272. * Converts a value to a string. 
  273. * @param mixed $value 
  274. * @access private 
  275. */ 
  276. function _convertToString($value) { 
  277. foreach ($value as $k => $v) { 
  278. if (is_array($v)) { 
  279. $value[$k] = $this->_convertToString($value[$k]); 
  280. } else { 
  281. settype($value[$k], 'string'); 
  282.  
  283. return $value; 
  284.  
  285. /** 
  286. * @param boolean $looselyTyped 
  287. * @access public 
  288. */ 
  289. function setLooselyTyped($looselyTyped) { 
  290. if (is_bool($looselyTyped)) { 
  291. $this->_looselyTyped = $looselyTyped; 
  292.  
  293. /** 
  294. * Fails a test with the given message. 
  295. * @param string 
  296. * @access protected 
  297. * @abstract 
  298. */ 
  299. function fail($message = '') { /** abstract */ }