PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/php/PHPUnit/Assert.php

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