PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/PEAR/PHPUnit/Assert.php

https://bitbucket.org/kucing2k/ediassoc
PHP | 426 lines | 189 code | 52 blank | 185 comment | 46 complexity | e02536ea659036e8b860b5e6730e21db MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, BSD-2-Clause, GPL-2.0
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * PHP Version 4
  5. *
  6. * Copyright (c) 2002-2005, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * Neither the name of Sebastian Bergmann nor the names of his
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * @category Testing
  39. * @package PHPUnit
  40. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. * @version CVS: $Id: Assert.php,v 1.29 2005/11/10 09:47:14 sebastian Exp $
  44. * @link http://pear.php.net/package/PHPUnit
  45. * @since File available since Release 1.0.0
  46. */
  47. /**
  48. * A set of assert methods.
  49. *
  50. * @category Testing
  51. * @package PHPUnit
  52. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  53. * @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  54. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  55. * @version Release: 1.3.2
  56. * @link http://pear.php.net/package/PHPUnit
  57. * @since Class available since Release 1.0.0
  58. */
  59. class PHPUnit_Assert {
  60. /**
  61. * @var boolean
  62. * @access private
  63. */
  64. var $_looselyTyped = FALSE;
  65. /**
  66. * Asserts that a haystack contains a needle.
  67. *
  68. * @param mixed
  69. * @param mixed
  70. * @param string
  71. * @access public
  72. * @since Method available since Release 1.1.0
  73. */
  74. function assertContains($needle, $haystack, $message = '') {
  75. if (is_string($needle) && is_string($haystack)) {
  76. $this->assertTrue(strpos($haystack, $needle) !== FALSE, $message);
  77. }
  78. else if (is_array($haystack) && !is_object($needle)) {
  79. $this->assertTrue(in_array($needle, $haystack), $message);
  80. }
  81. else {
  82. $this->fail('Unsupported parameter passed to assertContains().');
  83. }
  84. }
  85. /**
  86. * Asserts that a haystack does not contain a needle.
  87. *
  88. * @param mixed
  89. * @param mixed
  90. * @param string
  91. * @access public
  92. * @since Method available since Release 1.1.0
  93. */
  94. function assertNotContains($needle, $haystack, $message = '') {
  95. if (is_string($needle) && is_string($haystack)) {
  96. $this->assertFalse(strpos($haystack, $needle) !== FALSE, $message);
  97. }
  98. else if (is_array($haystack) && !is_object($needle)) {
  99. $this->assertFalse(in_array($needle, $haystack), $message);
  100. }
  101. else {
  102. $this->fail('Unsupported parameter passed to assertNotContains().');
  103. }
  104. }
  105. /**
  106. * Asserts that two variables are equal.
  107. *
  108. * @param mixed
  109. * @param mixed
  110. * @param string
  111. * @param mixed
  112. * @access public
  113. */
  114. function assertEquals($expected, $actual, $message = '', $delta = 0) {
  115. if ((is_array($actual) && is_array($expected)) ||
  116. (is_object($actual) && is_object($expected))) {
  117. if (is_array($actual) && is_array($expected)) {
  118. ksort($actual);
  119. ksort($expected);
  120. }
  121. if ($this->_looselyTyped) {
  122. $actual = $this->_convertToString($actual);
  123. $expected = $this->_convertToString($expected);
  124. }
  125. $actual = serialize($actual);
  126. $expected = serialize($expected);
  127. $message = sprintf(
  128. '%sexpected %s, actual %s',
  129. !empty($message) ? $message . ' ' : '',
  130. $expected,
  131. $actual
  132. );
  133. if ($actual !== $expected) {
  134. return $this->fail($message);
  135. }
  136. }
  137. elseif (is_numeric($actual) && is_numeric($expected)) {
  138. $message = sprintf(
  139. '%sexpected %s%s, actual %s',
  140. !empty($message) ? $message . ' ' : '',
  141. $expected,
  142. ($delta != 0) ? ('+/- ' . $delta) : '',
  143. $actual
  144. );
  145. if (!($actual >= ($expected - $delta) && $actual <= ($expected + $delta))) {
  146. return $this->fail($message);
  147. }
  148. }
  149. else {
  150. $message = sprintf(
  151. '%sexpected %s, actual %s',
  152. !empty($message) ? $message . ' ' : '',
  153. $expected,
  154. $actual
  155. );
  156. if ($actual !== $expected) {
  157. return $this->fail($message);
  158. }
  159. }
  160. }
  161. /**
  162. * Asserts that two variables reference the same object.
  163. * This requires the Zend Engine 2 to work.
  164. *
  165. * @param object
  166. * @param object
  167. * @param string
  168. * @access public
  169. * @deprecated
  170. */
  171. function assertSame($expected, $actual, $message = '') {
  172. if (!version_compare(phpversion(), '5.0.0', '>=')) {
  173. $this->fail('assertSame() only works with PHP >= 5.0.0.');
  174. }
  175. if ((is_object($expected) || is_null($expected)) &&
  176. (is_object($actual) || is_null($actual))) {
  177. $message = sprintf(
  178. '%sexpected two variables to reference the same object',
  179. !empty($message) ? $message . ' ' : ''
  180. );
  181. if ($expected !== $actual) {
  182. return $this->fail($message);
  183. }
  184. } else {
  185. $this->fail('Unsupported parameter passed to assertSame().');
  186. }
  187. }
  188. /**
  189. * Asserts that two variables do not reference the same object.
  190. * This requires the Zend Engine 2 to work.
  191. *
  192. * @param object
  193. * @param object
  194. * @param string
  195. * @access public
  196. * @deprecated
  197. */
  198. function assertNotSame($expected, $actual, $message = '') {
  199. if (!version_compare(phpversion(), '5.0.0', '>=')) {
  200. $this->fail('assertNotSame() only works with PHP >= 5.0.0.');
  201. }
  202. if ((is_object($expected) || is_null($expected)) &&
  203. (is_object($actual) || is_null($actual))) {
  204. $message = sprintf(
  205. '%sexpected two variables to reference different objects',
  206. !empty($message) ? $message . ' ' : ''
  207. );
  208. if ($expected === $actual) {
  209. return $this->fail($message);
  210. }
  211. } else {
  212. $this->fail('Unsupported parameter passed to assertNotSame().');
  213. }
  214. }
  215. /**
  216. * Asserts that a variable is not NULL.
  217. *
  218. * @param mixed
  219. * @param string
  220. * @access public
  221. */
  222. function assertNotNull($actual, $message = '') {
  223. $message = sprintf(
  224. '%sexpected NOT NULL, actual NULL',
  225. !empty($message) ? $message . ' ' : ''
  226. );
  227. if (is_null($actual)) {
  228. return $this->fail($message);
  229. }
  230. }
  231. /**
  232. * Asserts that a variable is NULL.
  233. *
  234. * @param mixed
  235. * @param string
  236. * @access public
  237. */
  238. function assertNull($actual, $message = '') {
  239. $message = sprintf(
  240. '%sexpected NULL, actual NOT NULL',
  241. !empty($message) ? $message . ' ' : ''
  242. );
  243. if (!is_null($actual)) {
  244. return $this->fail($message);
  245. }
  246. }
  247. /**
  248. * Asserts that a condition is true.
  249. *
  250. * @param boolean
  251. * @param string
  252. * @access public
  253. */
  254. function assertTrue($condition, $message = '') {
  255. $message = sprintf(
  256. '%sexpected TRUE, actual FALSE',
  257. !empty($message) ? $message . ' ' : ''
  258. );
  259. if (!$condition) {
  260. return $this->fail($message);
  261. }
  262. }
  263. /**
  264. * Asserts that a condition is false.
  265. *
  266. * @param boolean
  267. * @param string
  268. * @access public
  269. */
  270. function assertFalse($condition, $message = '') {
  271. $message = sprintf(
  272. '%sexpected FALSE, actual TRUE',
  273. !empty($message) ? $message . ' ' : ''
  274. );
  275. if ($condition) {
  276. return $this->fail($message);
  277. }
  278. }
  279. /**
  280. * Asserts that a string matches a given regular expression.
  281. *
  282. * @param string
  283. * @param string
  284. * @param string
  285. * @access public
  286. */
  287. function assertRegExp($pattern, $string, $message = '') {
  288. $message = sprintf(
  289. '%s"%s" does not match pattern "%s"',
  290. !empty($message) ? $message . ' ' : '',
  291. $string,
  292. $pattern
  293. );
  294. if (!preg_match($pattern, $string)) {
  295. return $this->fail($message);
  296. }
  297. }
  298. /**
  299. * Asserts that a string does not match a given regular expression.
  300. *
  301. * @param string
  302. * @param string
  303. * @param string
  304. * @access public
  305. * @since Method available since Release 1.1.0
  306. */
  307. function assertNotRegExp($pattern, $string, $message = '') {
  308. $message = sprintf(
  309. '%s"%s" matches pattern "%s"',
  310. !empty($message) ? $message . ' ' : '',
  311. $string,
  312. $pattern
  313. );
  314. if (preg_match($pattern, $string)) {
  315. return $this->fail($message);
  316. }
  317. }
  318. /**
  319. * Asserts that a variable is of a given type.
  320. *
  321. * @param string $expected
  322. * @param mixed $actual
  323. * @param optional string $message
  324. * @access public
  325. */
  326. function assertType($expected, $actual, $message = '') {
  327. return $this->assertEquals(
  328. $expected,
  329. gettype($actual),
  330. $message
  331. );
  332. }
  333. /**
  334. * Converts a value to a string.
  335. *
  336. * @param mixed $value
  337. * @access private
  338. */
  339. function _convertToString($value) {
  340. foreach ($value as $k => $v) {
  341. if (is_array($v)) {
  342. $value[$k] = $this->_convertToString($value[$k]);
  343. } else {
  344. settype($value[$k], 'string');
  345. }
  346. }
  347. return $value;
  348. }
  349. /**
  350. * @param boolean $looselyTyped
  351. * @access public
  352. */
  353. function setLooselyTyped($looselyTyped) {
  354. if (is_bool($looselyTyped)) {
  355. $this->_looselyTyped = $looselyTyped;
  356. }
  357. }
  358. /**
  359. * Fails a test with the given message.
  360. *
  361. * @param string
  362. * @access protected
  363. * @abstract
  364. */
  365. function fail($message = '') { /* abstract */ }
  366. }
  367. /*
  368. * Local variables:
  369. * tab-width: 4
  370. * c-basic-offset: 4
  371. * c-hanging-comment-ender-p: nil
  372. * End:
  373. */
  374. ?>