/cpulogger-server/public_html/xmlrpc/test/PHPUnit/Assert.php

https://gitlab.com/komputer007123/cpulogger · PHP · 400 lines · 189 code · 52 blank · 159 comment · 46 complexity · 1ad7d3fbea2502c10283fcab8973f9cc MD5 · raw file

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