PageRenderTime 70ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/common/phpunit/PHPUnit/Framework/Assert.php

https://bitbucket.org/dukeofgaming/old_ether
PHP | 2232 lines | 971 code | 214 blank | 1047 comment | 100 complexity | 35119a0ded20ec0d967ca37704529b4f MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category Testing
  38. * @package PHPUnit
  39. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @version SVN: $Id: Assert.php 5162 2009-08-29 08:49:43Z sb $
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 2.0.0
  45. */
  46. require_once 'PHPUnit/Framework.php';
  47. require_once 'PHPUnit/Util/Class.php';
  48. require_once 'PHPUnit/Util/InvalidArgumentHelper.php';
  49. require_once 'PHPUnit/Util/Type.php';
  50. require_once 'PHPUnit/Util/XML.php';
  51. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  52. /**
  53. * A set of assert methods.
  54. *
  55. * @category Testing
  56. * @package PHPUnit
  57. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  58. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  59. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  60. * @version Release: 3.4.2
  61. * @link http://www.phpunit.de/
  62. * @since Class available since Release 2.0.0
  63. * @abstract
  64. */
  65. abstract class PHPUnit_Framework_Assert
  66. {
  67. /**
  68. * @var integer
  69. */
  70. private static $count = 0;
  71. /**
  72. * Asserts that an array has a specified key.
  73. *
  74. * @param mixed $key
  75. * @param array $array
  76. * @param string $message
  77. * @since Method available since Release 3.0.0
  78. */
  79. public static function assertArrayHasKey($key, array $array, $message = '')
  80. {
  81. if (!(is_integer($key) || is_string($key))) {
  82. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  83. 1, 'integer or string'
  84. );
  85. }
  86. $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey($key);
  87. self::assertThat($array, $constraint, $message);
  88. }
  89. /**
  90. * Asserts that an array does not have a specified key.
  91. *
  92. * @param mixed $key
  93. * @param array $array
  94. * @param string $message
  95. * @since Method available since Release 3.0.0
  96. */
  97. public static function assertArrayNotHasKey($key, array $array, $message = '')
  98. {
  99. if (!(is_integer($key) || is_string($key))) {
  100. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  101. 1, 'integer or string'
  102. );
  103. }
  104. $constraint = new PHPUnit_Framework_Constraint_Not(
  105. new PHPUnit_Framework_Constraint_ArrayHasKey($key)
  106. );
  107. self::assertThat($array, $constraint, $message);
  108. }
  109. /**
  110. * Asserts that a haystack contains a needle.
  111. *
  112. * @param mixed $needle
  113. * @param mixed $haystack
  114. * @param string $message
  115. * @param boolean $ignoreCase
  116. * @since Method available since Release 2.1.0
  117. */
  118. public static function assertContains($needle, $haystack, $message = '', $ignoreCase = FALSE)
  119. {
  120. if (is_array($haystack) ||
  121. is_object($haystack) && $haystack instanceof Traversable) {
  122. $constraint = new PHPUnit_Framework_Constraint_TraversableContains(
  123. $needle
  124. );
  125. }
  126. else if (is_string($haystack)) {
  127. $constraint = new PHPUnit_Framework_Constraint_StringContains(
  128. $needle, $ignoreCase
  129. );
  130. }
  131. else {
  132. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  133. 2, 'array, iterator or string'
  134. );
  135. }
  136. self::assertThat($haystack, $constraint, $message);
  137. }
  138. /**
  139. * Asserts that a haystack that is stored in a static attribute of a class
  140. * or an attribute of an object contains a needle.
  141. *
  142. * @param mixed $needle
  143. * @param string $haystackAttributeName
  144. * @param mixed $haystackClassOrObject
  145. * @param string $message
  146. * @param boolean $ignoreCase
  147. * @since Method available since Release 3.0.0
  148. */
  149. public static function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE)
  150. {
  151. self::assertContains(
  152. $needle,
  153. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  154. $message,
  155. $ignoreCase
  156. );
  157. }
  158. /**
  159. * Asserts that a haystack does not contain a needle.
  160. *
  161. * @param mixed $needle
  162. * @param mixed $haystack
  163. * @param string $message
  164. * @param boolean $ignoreCase
  165. * @since Method available since Release 2.1.0
  166. */
  167. public static function assertNotContains($needle, $haystack, $message = '', $ignoreCase = FALSE)
  168. {
  169. if (is_array($haystack) ||
  170. is_object($haystack) && $haystack instanceof Traversable) {
  171. $constraint = new PHPUnit_Framework_Constraint_Not(
  172. new PHPUnit_Framework_Constraint_TraversableContains($needle)
  173. );
  174. }
  175. else if (is_string($haystack)) {
  176. $constraint = new PHPUnit_Framework_Constraint_Not(
  177. new PHPUnit_Framework_Constraint_StringContains(
  178. $needle, $ignoreCase
  179. )
  180. );
  181. }
  182. else {
  183. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  184. 2, 'array, iterator or string'
  185. );
  186. }
  187. self::assertThat($haystack, $constraint, $message);
  188. }
  189. /**
  190. * Asserts that a haystack that is stored in a static attribute of a class
  191. * or an attribute of an object does not contain a needle.
  192. *
  193. * @param mixed $needle
  194. * @param string $haystackAttributeName
  195. * @param mixed $haystackClassOrObject
  196. * @param string $message
  197. * @param boolean $ignoreCase
  198. * @since Method available since Release 3.0.0
  199. */
  200. public static function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE)
  201. {
  202. self::assertNotContains(
  203. $needle,
  204. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  205. $message,
  206. $ignoreCase
  207. );
  208. }
  209. /**
  210. * Asserts that a haystack contains only values of a given type.
  211. *
  212. * @param string $type
  213. * @param mixed $haystack
  214. * @param boolean $isNativeType
  215. * @param string $message
  216. * @since Method available since Release 3.1.4
  217. */
  218. public static function assertContainsOnly($type, $haystack, $isNativeType = NULL, $message = '')
  219. {
  220. if (!(is_array($haystack) ||
  221. is_object($haystack) && $haystack instanceof Traversable)) {
  222. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  223. 2, 'array or iterator'
  224. );
  225. }
  226. if ($isNativeType == NULL) {
  227. $isNativeType = PHPUnit_Util_Type::isType($type);
  228. }
  229. self::assertThat(
  230. $haystack,
  231. new PHPUnit_Framework_Constraint_TraversableContainsOnly(
  232. $type, $isNativeType
  233. ),
  234. $message
  235. );
  236. }
  237. /**
  238. * Asserts that a haystack that is stored in a static attribute of a class
  239. * or an attribute of an object contains only values of a given type.
  240. *
  241. * @param string $type
  242. * @param string $haystackAttributeName
  243. * @param mixed $haystackClassOrObject
  244. * @param boolean $isNativeType
  245. * @param string $message
  246. * @since Method available since Release 3.1.4
  247. */
  248. public static function assertAttributeContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = NULL, $message = '')
  249. {
  250. self::assertContainsOnly(
  251. $type,
  252. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  253. $isNativeType,
  254. $message
  255. );
  256. }
  257. /**
  258. * Asserts that a haystack does not contain only values of a given type.
  259. *
  260. * @param string $type
  261. * @param mixed $haystack
  262. * @param boolean $isNativeType
  263. * @param string $message
  264. * @since Method available since Release 3.1.4
  265. */
  266. public static function assertNotContainsOnly($type, $haystack, $isNativeType = NULL, $message = '')
  267. {
  268. if (!(is_array($haystack) ||
  269. is_object($haystack) && $haystack instanceof Traversable)) {
  270. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  271. 2, 'array or iterator'
  272. );
  273. }
  274. if ($isNativeType == NULL) {
  275. $isNativeType = PHPUnit_Util_Type::isType($type);
  276. }
  277. self::assertThat(
  278. $haystack,
  279. new PHPUnit_Framework_Constraint_Not(
  280. new PHPUnit_Framework_Constraint_TraversableContainsOnly(
  281. $type, $isNativeType
  282. )
  283. ),
  284. $message
  285. );
  286. }
  287. /**
  288. * Asserts that a haystack that is stored in a static attribute of a class
  289. * or an attribute of an object does not contain only values of a given
  290. * type.
  291. *
  292. * @param string $type
  293. * @param string $haystackAttributeName
  294. * @param mixed $haystackClassOrObject
  295. * @param boolean $isNativeType
  296. * @param string $message
  297. * @since Method available since Release 3.1.4
  298. */
  299. public static function assertAttributeNotContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = NULL, $message = '')
  300. {
  301. self::assertNotContainsOnly(
  302. $type,
  303. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  304. $isNativeType,
  305. $message
  306. );
  307. }
  308. /**
  309. * Asserts that two variables are equal.
  310. *
  311. * @param mixed $expected
  312. * @param mixed $actual
  313. * @param string $message
  314. * @param float $delta
  315. * @param integer $maxDepth
  316. * @param boolean $canonicalizeEol
  317. * @param boolean $ignoreCase
  318. */
  319. public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE, $ignoreCase = FALSE)
  320. {
  321. $constraint = new PHPUnit_Framework_Constraint_IsEqual(
  322. $expected, $delta, $maxDepth, $canonicalizeEol, $ignoreCase
  323. );
  324. self::assertThat($actual, $constraint, $message);
  325. }
  326. /**
  327. * Asserts that a variable is equal to an attribute of an object.
  328. *
  329. * @param mixed $expected
  330. * @param string $actualAttributeName
  331. * @param string $actualClassOrObject
  332. * @param string $message
  333. * @param float $delta
  334. * @param integer $maxDepth
  335. * @param boolean $canonicalizeEol
  336. * @param boolean $ignoreCase
  337. */
  338. public static function assertAttributeEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE, $ignoreCase = FALSE)
  339. {
  340. self::assertEquals(
  341. $expected,
  342. self::readAttribute($actualClassOrObject, $actualAttributeName),
  343. $message,
  344. $delta,
  345. $maxDepth,
  346. $canonicalizeEol,
  347. $ignoreCase
  348. );
  349. }
  350. /**
  351. * Asserts that two variables are not equal.
  352. *
  353. * @param mixed $expected
  354. * @param mixed $actual
  355. * @param string $message
  356. * @param float $delta
  357. * @param integer $maxDepth
  358. * @param boolean $canonicalizeEol
  359. * @param boolean $ignoreCase
  360. * @since Method available since Release 2.3.0
  361. */
  362. public static function assertNotEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE, $ignoreCase = FALSE)
  363. {
  364. $constraint = new PHPUnit_Framework_Constraint_Not(
  365. new PHPUnit_Framework_Constraint_IsEqual(
  366. $expected, $delta, $maxDepth, $canonicalizeEol, $ignoreCase
  367. )
  368. );
  369. self::assertThat($actual, $constraint, $message);
  370. }
  371. /**
  372. * Asserts that a variable is not equal to an attribute of an object.
  373. *
  374. * @param mixed $expected
  375. * @param string $actualAttributeName
  376. * @param string $actualClassOrObject
  377. * @param string $message
  378. * @param float $delta
  379. * @param integer $maxDepth
  380. * @param boolean $canonicalizeEol
  381. * @param boolean $ignoreCase
  382. */
  383. public static function assertAttributeNotEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE, $ignoreCase = FALSE)
  384. {
  385. self::assertNotEquals(
  386. $expected,
  387. self::readAttribute($actualClassOrObject, $actualAttributeName),
  388. $message,
  389. $delta,
  390. $maxDepth,
  391. $canonicalizeEol,
  392. $ignoreCase
  393. );
  394. }
  395. /**
  396. * Asserts that a value is greater than another value.
  397. *
  398. * @param mixed $expected
  399. * @param mixed $actual
  400. * @param string $message
  401. * @since Method available since Release 3.1.0
  402. */
  403. public static function assertGreaterThan($expected, $actual, $message = '')
  404. {
  405. self::assertThat($actual, self::greaterThan($expected), $message);
  406. }
  407. /**
  408. * Asserts that an attribute is greater than another value.
  409. *
  410. * @param mixed $expected
  411. * @param string $actualAttributeName
  412. * @param string $actualClassOrObject
  413. * @param string $message
  414. * @since Method available since Release 3.1.0
  415. */
  416. public static function assertAttributeGreaterThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  417. {
  418. self::assertGreaterThan(
  419. $expected,
  420. self::readAttribute($actualClassOrObject, $actualAttributeName),
  421. $message
  422. );
  423. }
  424. /**
  425. * Asserts that a value is greater than or equal to another value.
  426. *
  427. * @param mixed $expected
  428. * @param mixed $actual
  429. * @param string $message
  430. * @since Method available since Release 3.1.0
  431. */
  432. public static function assertGreaterThanOrEqual($expected, $actual, $message = '')
  433. {
  434. self::assertThat(
  435. $actual, self::greaterThanOrEqual($expected), $message
  436. );
  437. }
  438. /**
  439. * Asserts that an attribute is greater than or equal to another value.
  440. *
  441. * @param mixed $expected
  442. * @param string $actualAttributeName
  443. * @param string $actualClassOrObject
  444. * @param string $message
  445. * @since Method available since Release 3.1.0
  446. */
  447. public static function assertAttributeGreaterThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  448. {
  449. self::assertGreaterThanOrEqual(
  450. $expected,
  451. self::readAttribute($actualClassOrObject, $actualAttributeName),
  452. $message
  453. );
  454. }
  455. /**
  456. * Asserts that a value is smaller than another value.
  457. *
  458. * @param mixed $expected
  459. * @param mixed $actual
  460. * @param string $message
  461. * @since Method available since Release 3.1.0
  462. */
  463. public static function assertLessThan($expected, $actual, $message = '')
  464. {
  465. self::assertThat($actual, self::lessThan($expected), $message);
  466. }
  467. /**
  468. * Asserts that an attribute is smaller than another value.
  469. *
  470. * @param mixed $expected
  471. * @param string $actualAttributeName
  472. * @param string $actualClassOrObject
  473. * @param string $message
  474. * @since Method available since Release 3.1.0
  475. */
  476. public static function assertAttributeLessThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  477. {
  478. self::assertLessThan(
  479. $expected,
  480. self::readAttribute($actualClassOrObject, $actualAttributeName),
  481. $message
  482. );
  483. }
  484. /**
  485. * Asserts that a value is smaller than or equal to another value.
  486. *
  487. * @param mixed $expected
  488. * @param mixed $actual
  489. * @param string $message
  490. * @since Method available since Release 3.1.0
  491. */
  492. public static function assertLessThanOrEqual($expected, $actual, $message = '')
  493. {
  494. self::assertThat($actual, self::lessThanOrEqual($expected), $message);
  495. }
  496. /**
  497. * Asserts that an attribute is smaller than or equal to another value.
  498. *
  499. * @param mixed $expected
  500. * @param string $actualAttributeName
  501. * @param string $actualClassOrObject
  502. * @param string $message
  503. * @since Method available since Release 3.1.0
  504. */
  505. public static function assertAttributeLessThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  506. {
  507. self::assertLessThanOrEqual(
  508. $expected,
  509. self::readAttribute($actualClassOrObject, $actualAttributeName),
  510. $message
  511. );
  512. }
  513. /**
  514. * Asserts that the contents of one file is equal to the contents of another
  515. * file.
  516. *
  517. * @param string $expected
  518. * @param string $actual
  519. * @param string $message
  520. * @param boolean $canonicalizeEol
  521. * @param boolean $ignoreCase
  522. * @since Method available since Release 3.2.14
  523. */
  524. public static function assertFileEquals($expected, $actual, $message = '', $canonicalizeEol = FALSE, $ignoreCase = FALSE)
  525. {
  526. self::assertFileExists($expected, $message);
  527. self::assertFileExists($actual, $message);
  528. self::assertEquals(
  529. file_get_contents($expected),
  530. file_get_contents($actual),
  531. $message,
  532. 0,
  533. 10,
  534. $canonicalizeEol,
  535. $ignoreCase
  536. );
  537. }
  538. /**
  539. * Asserts that the contents of one file is not equal to the contents of
  540. * another file.
  541. *
  542. * @param string $expected
  543. * @param string $actual
  544. * @param string $message
  545. * @param boolean $canonicalizeEol
  546. * @param boolean $ignoreCase
  547. * @since Method available since Release 3.2.14
  548. */
  549. public static function assertFileNotEquals($expected, $actual, $message = '', $canonicalizeEol = FALSE, $ignoreCase = FALSE)
  550. {
  551. self::assertFileExists($expected, $message);
  552. self::assertFileExists($actual, $message);
  553. self::assertNotEquals(
  554. file_get_contents($expected),
  555. file_get_contents($actual),
  556. $message,
  557. 0,
  558. 10,
  559. $canonicalizeEol,
  560. $ignoreCase
  561. );
  562. }
  563. /**
  564. * Asserts that the contents of a string is equal
  565. * to the contents of a file.
  566. *
  567. * @param string $expectedFile
  568. * @param string $actualString
  569. * @param string $message
  570. * @param boolean $canonicalizeEol
  571. * @param boolean $ignoreCase
  572. * @since Method available since Release 3.3.0
  573. */
  574. public static function assertStringEqualsFile($expectedFile, $actualString, $message = '', $canonicalizeEol = FALSE, $ignoreCase = FALSE)
  575. {
  576. self::assertFileExists($expectedFile, $message);
  577. self::assertEquals(
  578. file_get_contents($expectedFile),
  579. $actualString,
  580. $message,
  581. 0,
  582. 10,
  583. $canonicalizeEol,
  584. $ignoreCase
  585. );
  586. }
  587. /**
  588. * Asserts that the contents of a string is not equal
  589. * to the contents of a file.
  590. *
  591. * @param string $expectedFile
  592. * @param string $actualString
  593. * @param string $message
  594. * @param boolean $canonicalizeEol
  595. * @param boolean $ignoreCase
  596. * @since Method available since Release 3.3.0
  597. */
  598. public static function assertStringNotEqualsFile($expectedFile, $actualString, $message = '', $canonicalizeEol = FALSE, $ignoreCase = FALSE)
  599. {
  600. self::assertFileExists($expectedFile, $message);
  601. self::assertNotEquals(
  602. file_get_contents($expectedFile),
  603. $actualString,
  604. $message,
  605. 0,
  606. 10,
  607. $canonicalizeEol,
  608. $ignoreCase
  609. );
  610. }
  611. /**
  612. * Asserts that a file exists.
  613. *
  614. * @param string $filename
  615. * @param string $message
  616. * @since Method available since Release 3.0.0
  617. */
  618. public static function assertFileExists($filename, $message = '')
  619. {
  620. if (!is_string($filename)) {
  621. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  622. }
  623. $constraint = new PHPUnit_Framework_Constraint_FileExists;
  624. self::assertThat($filename, $constraint, $message);
  625. }
  626. /**
  627. * Asserts that a file does not exist.
  628. *
  629. * @param string $filename
  630. * @param string $message
  631. * @since Method available since Release 3.0.0
  632. */
  633. public static function assertFileNotExists($filename, $message = '')
  634. {
  635. if (!is_string($filename)) {
  636. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  637. }
  638. $constraint = new PHPUnit_Framework_Constraint_Not(
  639. new PHPUnit_Framework_Constraint_FileExists
  640. );
  641. self::assertThat($filename, $constraint, $message);
  642. }
  643. /**
  644. * Asserts that a condition is true.
  645. *
  646. * @param boolean $condition
  647. * @param string $message
  648. * @throws PHPUnit_Framework_AssertionFailedError
  649. */
  650. public static function assertTrue($condition, $message = '')
  651. {
  652. self::assertThat($condition, self::isTrue(), $message);
  653. }
  654. /**
  655. * Asserts that a condition is false.
  656. *
  657. * @param boolean $condition
  658. * @param string $message
  659. * @throws PHPUnit_Framework_AssertionFailedError
  660. */
  661. public static function assertFalse($condition, $message = '')
  662. {
  663. self::assertThat($condition, self::isFalse(), $message);
  664. }
  665. /**
  666. * Asserts that a variable is not NULL.
  667. *
  668. * @param mixed $actual
  669. * @param string $message
  670. */
  671. public static function assertNotNull($actual, $message = '')
  672. {
  673. self::assertThat($actual, self::logicalNot(self::isNull()), $message);
  674. }
  675. /**
  676. * Asserts that a variable is NULL.
  677. *
  678. * @param mixed $actual
  679. * @param string $message
  680. */
  681. public static function assertNull($actual, $message = '')
  682. {
  683. self::assertThat($actual, self::isNull(), $message);
  684. }
  685. /**
  686. * Asserts that a class has a specified attribute.
  687. *
  688. * @param string $attributeName
  689. * @param string $className
  690. * @param string $message
  691. * @since Method available since Release 3.1.0
  692. */
  693. public static function assertClassHasAttribute($attributeName, $className, $message = '')
  694. {
  695. if (!is_string($attributeName)) {
  696. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  697. }
  698. if (!is_string($className) || !class_exists($className, FALSE)) {
  699. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
  700. }
  701. $constraint = new PHPUnit_Framework_Constraint_ClassHasAttribute(
  702. $attributeName
  703. );
  704. self::assertThat($className, $constraint, $message);
  705. }
  706. /**
  707. * Asserts that a class does not have a specified attribute.
  708. *
  709. * @param string $attributeName
  710. * @param string $className
  711. * @param string $message
  712. * @since Method available since Release 3.1.0
  713. */
  714. public static function assertClassNotHasAttribute($attributeName, $className, $message = '')
  715. {
  716. if (!is_string($attributeName)) {
  717. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  718. }
  719. if (!is_string($className) || !class_exists($className, FALSE)) {
  720. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
  721. }
  722. $constraint = new PHPUnit_Framework_Constraint_Not(
  723. new PHPUnit_Framework_Constraint_ClassHasAttribute($attributeName)
  724. );
  725. self::assertThat($className, $constraint, $message);
  726. }
  727. /**
  728. * Asserts that a class has a specified static attribute.
  729. *
  730. * @param string $attributeName
  731. * @param string $className
  732. * @param string $message
  733. * @since Method available since Release 3.1.0
  734. */
  735. public static function assertClassHasStaticAttribute($attributeName, $className, $message = '')
  736. {
  737. if (!is_string($attributeName)) {
  738. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  739. }
  740. if (!is_string($className) || !class_exists($className, FALSE)) {
  741. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
  742. }
  743. $constraint = new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
  744. $attributeName
  745. );
  746. self::assertThat($className, $constraint, $message);
  747. }
  748. /**
  749. * Asserts that a class does not have a specified static attribute.
  750. *
  751. * @param string $attributeName
  752. * @param string $className
  753. * @param string $message
  754. * @since Method available since Release 3.1.0
  755. */
  756. public static function assertClassNotHasStaticAttribute($attributeName, $className, $message = '')
  757. {
  758. if (!is_string($attributeName)) {
  759. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  760. }
  761. if (!is_string($className) || !class_exists($className, FALSE)) {
  762. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
  763. }
  764. $constraint = new PHPUnit_Framework_Constraint_Not(
  765. new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
  766. $attributeName
  767. )
  768. );
  769. self::assertThat($className, $constraint, $message);
  770. }
  771. /**
  772. * Asserts that an object has a specified attribute.
  773. *
  774. * @param string $attributeName
  775. * @param object $object
  776. * @param string $message
  777. * @since Method available since Release 3.0.0
  778. */
  779. public static function assertObjectHasAttribute($attributeName, $object, $message = '')
  780. {
  781. if (!is_string($attributeName)) {
  782. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  783. }
  784. if (!is_object($object)) {
  785. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
  786. }
  787. $constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute(
  788. $attributeName
  789. );
  790. self::assertThat($object, $constraint, $message);
  791. }
  792. /**
  793. * Asserts that an object does not have a specified attribute.
  794. *
  795. * @param string $attributeName
  796. * @param object $object
  797. * @param string $message
  798. * @since Method available since Release 3.0.0
  799. */
  800. public static function assertObjectNotHasAttribute($attributeName, $object, $message = '')
  801. {
  802. if (!is_string($attributeName)) {
  803. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  804. }
  805. if (!is_object($object)) {
  806. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
  807. }
  808. $constraint = new PHPUnit_Framework_Constraint_Not(
  809. new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName)
  810. );
  811. self::assertThat($object, $constraint, $message);
  812. }
  813. /**
  814. * Asserts that two variables have the same type and value.
  815. * Used on objects, it asserts that two variables reference
  816. * the same object.
  817. *
  818. * @param mixed $expected
  819. * @param mixed $actual
  820. * @param string $message
  821. */
  822. public static function assertSame($expected, $actual, $message = '')
  823. {
  824. if (is_bool($expected) && is_bool($actual)) {
  825. self::assertEquals($expected, $actual, $message);
  826. } else {
  827. $constraint = new PHPUnit_Framework_Constraint_IsIdentical(
  828. $expected
  829. );
  830. self::assertThat($actual, $constraint, $message);
  831. }
  832. }
  833. /**
  834. * Asserts that a variable and an attribute of an object have the same type
  835. * and value.
  836. *
  837. * @param mixed $expected
  838. * @param string $actualAttributeName
  839. * @param object $actualClassOrObject
  840. * @param string $message
  841. */
  842. public static function assertAttributeSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  843. {
  844. self::assertSame(
  845. $expected,
  846. self::readAttribute($actualClassOrObject, $actualAttributeName),
  847. $message
  848. );
  849. }
  850. /**
  851. * Asserts that two variables do not have the same type and value.
  852. * Used on objects, it asserts that two variables do not reference
  853. * the same object.
  854. *
  855. * @param mixed $expected
  856. * @param mixed $actual
  857. * @param string $message
  858. */
  859. public static function assertNotSame($expected, $actual, $message = '')
  860. {
  861. if (is_bool($expected) && is_bool($actual)) {
  862. self::assertNotEquals($expected, $actual, $message);
  863. } else {
  864. $constraint = new PHPUnit_Framework_Constraint_Not(
  865. new PHPUnit_Framework_Constraint_IsIdentical($expected)
  866. );
  867. self::assertThat($actual, $constraint, $message);
  868. }
  869. }
  870. /**
  871. * Asserts that a variable and an attribute of an object do not have the
  872. * same type and value.
  873. *
  874. * @param mixed $expected
  875. * @param string $actualAttributeName
  876. * @param object $actualClassOrObject
  877. * @param string $message
  878. */
  879. public static function assertAttributeNotSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  880. {
  881. self::assertNotSame(
  882. $expected,
  883. self::readAttribute($actualClassOrObject, $actualAttributeName),
  884. $message
  885. );
  886. }
  887. /**
  888. * Asserts that a variable is of a given type.
  889. *
  890. * @param string $expected
  891. * @param mixed $actual
  892. * @param string $message
  893. */
  894. public static function assertType($expected, $actual, $message = '')
  895. {
  896. if (is_string($expected)) {
  897. if (PHPUnit_Util_Type::isType($expected)) {
  898. $constraint = new PHPUnit_Framework_Constraint_IsType(
  899. $expected
  900. );
  901. }
  902. else if (class_exists($expected) || interface_exists($expected)) {
  903. $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
  904. $expected
  905. );
  906. }
  907. else {
  908. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  909. 1, 'class or interface name'
  910. );
  911. }
  912. } else {
  913. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  914. }
  915. self::assertThat($actual, $constraint, $message);
  916. }
  917. /**
  918. * Asserts that an attribute is of a given type.
  919. *
  920. * @param string $expected
  921. * @param string $attributeName
  922. * @param mixed $classOrObject
  923. * @param string $message
  924. * @since Method available since Release 3.4.0
  925. */
  926. public static function assertAttributeType($expected, $attributeName, $classOrObject, $message = '')
  927. {
  928. self::assertType(
  929. $expected,
  930. self::readAttribute($classOrObject, $attributeName),
  931. $message
  932. );
  933. }
  934. /**
  935. * Asserts that a variable is not of a given type.
  936. *
  937. * @param string $expected
  938. * @param mixed $actual
  939. * @param string $message
  940. * @since Method available since Release 2.2.0
  941. */
  942. public static function assertNotType($expected, $actual, $message = '')
  943. {
  944. if (is_string($expected)) {
  945. if (PHPUnit_Util_Type::isType($expected)) {
  946. $constraint = new PHPUnit_Framework_Constraint_Not(
  947. new PHPUnit_Framework_Constraint_IsType($expected)
  948. );
  949. }
  950. else if (class_exists($expected) || interface_exists($expected)) {
  951. $constraint = new PHPUnit_Framework_Constraint_Not(
  952. new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
  953. );
  954. }
  955. else {
  956. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  957. 1, 'class or interface name'
  958. );
  959. }
  960. } else {
  961. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  962. }
  963. self::assertThat($actual, $constraint, $message);
  964. }
  965. /**
  966. * Asserts that an attribute is of a given type.
  967. *
  968. * @param string $expected
  969. * @param string $attributeName
  970. * @param mixed $classOrObject
  971. * @param string $message
  972. * @since Method available since Release 3.4.0
  973. */
  974. public static function assertAttributeNotType($expected, $attributeName, $classOrObject, $message = '')
  975. {
  976. self::assertNotType(
  977. $expected,
  978. self::readAttribute($classOrObject, $attributeName),
  979. $message
  980. );
  981. }
  982. /**
  983. * Asserts that a string matches a given regular expression.
  984. *
  985. * @param string $pattern
  986. * @param string $string
  987. * @param string $message
  988. */
  989. public static function assertRegExp($pattern, $string, $message = '')
  990. {
  991. if (!is_string($pattern)) {
  992. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  993. }
  994. if (!is_string($string)) {
  995. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  996. }
  997. $constraint = new PHPUnit_Framework_Constraint_PCREMatch($pattern);
  998. self::assertThat($string, $constraint, $message);
  999. }
  1000. /**
  1001. * Asserts that a string does not match a given regular expression.
  1002. *
  1003. * @param string $pattern
  1004. * @param string $string
  1005. * @param string $message
  1006. * @since Method available since Release 2.1.0
  1007. */
  1008. public static function assertNotRegExp($pattern, $string, $message = '')
  1009. {
  1010. if (!is_string($pattern)) {
  1011. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1012. }
  1013. if (!is_string($string)) {
  1014. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1015. }
  1016. $constraint = new PHPUnit_Framework_Constraint_Not(
  1017. new PHPUnit_Framework_Constraint_PCREMatch($pattern)
  1018. );
  1019. self::assertThat($string, $constraint, $message);
  1020. }
  1021. /**
  1022. * Asserts that a string starts with a given prefix.
  1023. *
  1024. * @param string $prefix
  1025. * @param string $string
  1026. * @param string $message
  1027. * @since Method available since Release 3.4.0
  1028. */
  1029. public static function assertStringStartsWith($prefix, $string, $message = '')
  1030. {
  1031. if (!is_string($prefix)) {
  1032. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1033. }
  1034. if (!is_string($string)) {
  1035. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1036. }
  1037. $constraint = new PHPUnit_Framework_Constraint_StringStartsWith(
  1038. $prefix
  1039. );
  1040. self::assertThat($string, $constraint, $message);
  1041. }
  1042. /**
  1043. * Asserts that a string starts not with a given prefix.
  1044. *
  1045. * @param string $prefix
  1046. * @param string $string
  1047. * @param string $message
  1048. * @since Method available since Release 3.4.0
  1049. */
  1050. public static function assertStringStartsNotWith($prefix, $string, $message = '')
  1051. {
  1052. if (!is_string($prefix)) {
  1053. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1054. }
  1055. if (!is_string($string)) {
  1056. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1057. }
  1058. $constraint = new PHPUnit_Framework_Constraint_Not(
  1059. new PHPUnit_Framework_Constraint_StringStartsWith($prefix)
  1060. );
  1061. self::assertThat($string, $constraint, $message);
  1062. }
  1063. /**
  1064. * Asserts that a string ends with a given prefix.
  1065. *
  1066. * @param string $suffix
  1067. * @param string $string
  1068. * @param string $message
  1069. * @since Method available since Release 3.4.0
  1070. */
  1071. public static function assertStringEndsWith($suffix, $string, $message = '')
  1072. {
  1073. if (!is_string($suffix)) {
  1074. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1075. }
  1076. if (!is_string($string)) {
  1077. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1078. }
  1079. $constraint = new PHPUnit_Framework_Constraint_StringEndsWith($suffix);
  1080. self::assertThat($string, $constraint, $message);
  1081. }
  1082. /**
  1083. * Asserts that a string ends not with a given prefix.
  1084. *
  1085. * @param string $suffix
  1086. * @param string $string
  1087. * @param string $message
  1088. * @since Method available since Release 3.4.0
  1089. */
  1090. public static function assertStringEndsNotWith($suffix, $string, $message = '')
  1091. {
  1092. if (!is_string($suffix)) {
  1093. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1094. }
  1095. if (!is_string($string)) {
  1096. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1097. }
  1098. $constraint = new PHPUnit_Framework_Constraint_Not(
  1099. new PHPUnit_Framework_Constraint_StringEndsWith($suffix)
  1100. );
  1101. self::assertThat($string, $constraint, $message);
  1102. }
  1103. /**
  1104. * Asserts that two XML files are equal.
  1105. *
  1106. * @param string $expectedFile
  1107. * @param string $actualFile
  1108. * @param string $message
  1109. * @since Method available since Release 3.1.0
  1110. */
  1111. public static function assertXmlFileEqualsXmlFile($expectedFile, $actualFile, $message = '')
  1112. {
  1113. self::assertFileExists($expectedFile);
  1114. self::assertFileExists($actualFile);
  1115. $expected = new DOMDocument;
  1116. $expected->preserveWhiteSpace = FALSE;
  1117. $expected->load($expectedFile);
  1118. $actual = new DOMDocument;
  1119. $actual->preserveWhiteSpace = FALSE;
  1120. $actual->load($actualFile);
  1121. self::assertEquals($expected, $actual, $message);
  1122. }
  1123. /**
  1124. * Asserts that two XML files are not equal.
  1125. *
  1126. * @param string $expectedFile
  1127. * @param string $actualFile
  1128. * @param string $message
  1129. * @since Method available since Release 3.1.0
  1130. */
  1131. public static function assertXmlFileNotEqualsXmlFile($expectedFile, $actualFile, $message = '')
  1132. {
  1133. self::assertFileExists($expectedFile);
  1134. self::assertFileExists($actualFile);
  1135. $expected = new DOMDocument;
  1136. $expected->preserveWhiteSpace = FALSE;
  1137. $expected->load($expectedFile);
  1138. $actual = new DOMDocument;
  1139. $actual->preserveWhiteSpace = FALSE;
  1140. $actual->load($actualFile);
  1141. self::assertNotEquals($expected, $actual, $message);
  1142. }
  1143. /**
  1144. * Asserts that two XML documents are equal.
  1145. *
  1146. * @param string $expectedFile
  1147. * @param string $actualXml
  1148. * @param string $message
  1149. * @since Method available since Release 3.3.0
  1150. */
  1151. public static function assertXmlStringEqualsXmlFile($expectedFile, $actualXml, $message = '')
  1152. {
  1153. self::assertFileExists($expectedFile);
  1154. $expected = new DOMDocument;
  1155. $expected->preserveWhiteSpace = FALSE;
  1156. $expected->load($expectedFile);
  1157. $actual = new DOMDocument;
  1158. $actual->preserveWhiteSpace = FALSE;
  1159. $actual->loadXML($actualXml);
  1160. self::assertEquals($expected, $actual, $message);
  1161. }
  1162. /**
  1163. * Asserts that two XML documents are not equal.
  1164. *
  1165. * @param string $expectedFile
  1166. * @param string $actualXml
  1167. * @param string $message
  1168. * @since Method available since Release 3.3.0
  1169. */
  1170. public static function assertXmlStringNotEqualsXmlFile($expectedFile, $actualXml, $message = '')
  1171. {
  1172. self::assertFileExists($expectedFile);
  1173. $expected = new DOMDocument;
  1174. $expected->preserveWhiteSpace = FALSE;
  1175. $expected->load($expectedFile);
  1176. $actual = new DOMDocument;
  1177. $actual->preserveWhiteSpace = FALSE;
  1178. $actual->loadXML($actualXml);
  1179. self::assertNotEquals($expected, $actual, $message);
  1180. }
  1181. /**
  1182. * Asserts that two XML documents are equal.
  1183. *
  1184. * @param string $expectedXml
  1185. * @param string $actualXml
  1186. * @param string $message
  1187. * @since Method available since Release 3.1.0
  1188. */
  1189. public static function assertXmlStringEqualsXmlString($expectedXml, $actualXml, $message = '')
  1190. {
  1191. $expected = new DOMDocument;
  1192. $expected->preserveWhiteSpace = FALSE;
  1193. $expected->loadXML($expectedXml);
  1194. $actual = new DOMDocument;
  1195. $actual->preserveWhiteSpace = FALSE;
  1196. $actual->loadXML($actualXml);
  1197. self::assertEquals($expected, $actual, $message);
  1198. }
  1199. /**
  1200. * Asserts that two XML documents are not equal.
  1201. *
  1202. * @param string $expectedXml
  1203. * @param string $actualXml
  1204. * @param string $message
  1205. * @since Method available since Release 3.1.0
  1206. */
  1207. public static function assertXmlStringNotEqualsXmlString($expectedXml, $actualXml, $message = '')
  1208. {
  1209. $expected = new DOMDocument;
  1210. $expected->preserveWhiteSpace = FALSE;
  1211. $expected->loadXML($expectedXml);
  1212. $actual = new DOMDocument;
  1213. $actual->preserveWhiteSpace = FALSE;
  1214. $actual->loadXML($actualXml);
  1215. self::assertNotEquals($expected, $actual, $message);
  1216. }
  1217. /**
  1218. * Asserts that a hierarchy of DOMNodes matches.
  1219. *
  1220. * @param DOMNode $expectedNode
  1221. * @param DOMNode $actualNode
  1222. * @param boolean $checkAttributes
  1223. * @param string $message
  1224. * @author Mattis Stordalen Flister <mattis@xait.no>
  1225. * @since Method available since Release 3.3.0
  1226. */
  1227. public static function assertEqualXMLStructure(DOMNode $expectedNode, DOMNode $actualNode, $checkAttributes = FALSE, $message = '')
  1228. {
  1229. self::assertEquals(
  1230. $expectedNode->tagName,
  1231. $actualNode->tagName,
  1232. $message
  1233. );
  1234. if ($checkAttributes) {
  1235. self::assertEquals(
  1236. $expectedNode->attributes->length,
  1237. $actualNode->attributes->length,
  1238. sprintf(
  1239. '%s%sNumber of attributes on node "%s" does not match',
  1240. $message,
  1241. !empty($message) ? "\n" : '',
  1242. $expectedNode->tagName
  1243. )
  1244. );
  1245. for ($i = 0 ; $i < $expectedNode->attributes->length; $i++) {
  1246. $expectedAttribute = $expectedNode->attributes->item($i);
  1247. $actualAttribute = $actualNode->attributes->getNamedItem(
  1248. $expectedAttribute->name
  1249. );
  1250. if (!$actualAttribute) {
  1251. self::fail(
  1252. sprintf(
  1253. '%s%sCould not find attribute "%s" on node "%s"',
  1254. $message,
  1255. !empty($message) ? "\n" : '',
  1256. $expectedAttribute->name,
  1257. $expectedNode->tagName
  1258. )
  1259. );
  1260. }
  1261. }
  1262. }
  1263. PHPUnit_Util_XML::removeCharacterDataNodes($expectedNode);
  1264. PHPUnit_Util_XML::removeCharacterDataNodes($actualNode);
  1265. self::assertEquals(
  1266. $expectedNode->childNodes->length,
  1267. $actualNode->childNodes->length,
  1268. sprintf(
  1269. '%s%sNumber of child nodes of "%s" differs',
  1270. $message,
  1271. !empty($message) ? "\n" : '',
  1272. $expectedNode->tagName
  1273. )
  1274. );
  1275. for ($i = 0; $i < $expectedNode->childNodes->length; $i++) {
  1276. self::assertEqualXMLStructure(
  1277. $expectedNode->childNodes->item($i),
  1278. $actualNode->childNodes->item($i),
  1279. $checkAttributes,
  1280. $message
  1281. );
  1282. }
  1283. }
  1284. /**
  1285. * Assert the presence, absence, or count of elements in a document matching
  1286. * the CSS $selector, regardless of the contents of those elements.
  1287. *
  1288. * The first argument, $selector, is the CSS selector used to match
  1289. * the elements in the $actual document.
  1290. *
  1291. * The second argument, $count, can be either boolean or numeric.
  1292. * When boolean, it asserts for presence of elements matching the selector
  1293. * (TRUE) or absence of elements (FALSE).
  1294. * When numeric, it asserts the count of elements.
  1295. *
  1296. * assertSelectCount("#binder", true, $xml); // any?
  1297. * assertSelectCount(".binder", 3, $xml); // exactly 3?
  1298. *
  1299. * @param array $selector
  1300. * @param integer $count
  1301. * @param mixed $actual
  1302. * @param string $message
  1303. * @param boolean $isHtml
  1304. * @since Method available since Release 3.3.0
  1305. * @author Mike Naberezny <mike@maintainable.com>
  1306. * @author Derek DeVries <derek@maintainable.com>
  1307. */
  1308. public static function assertSelectCount($selector, $count, $actual, $message = '', $isHtml = TRUE)
  1309. {
  1310. self::assertSelectEquals(
  1311. $selector, TRUE, $count, $actual, $message, $isHtml
  1312. );
  1313. }
  1314. /**
  1315. * assertSelectRegExp("#binder .name", "/Mike|Derek/", true, $xml); // any?
  1316. * assertSelectRegExp("#binder .name", "/Mike|Derek/", 3, $xml); // 3?
  1317. *
  1318. * @param array $selector
  1319. * @param string $pattern
  1320. * @param integer $count
  1321. * @param mixed $actual
  1322. * @param string $message
  1323. * @param boolean $isHtml
  1324. * @since Method available since Release 3.3.0
  1325. * @author Mike Naberezny <mike@maintainable.com>
  1326. * @author Derek DeVries <derek@maintainable.com>
  1327. */
  1328. public static function assertSelectRegExp($selector, $pattern, $count, $actual, $message = '', $isHtml = TRUE)
  1329. {
  1330. self::assertSelectEquals(
  1331. $selector, "regexp:$pattern", $count, $actual, $message, $isHtml
  1332. );
  1333. }
  1334. /**
  1335. * assertSelectEquals("#binder .name", "Chuck", true, $xml); // any?
  1336. * assertSelectEquals("#binder .name", "Chuck", false, $xml); // none?
  1337. *
  1338. * @param array $selector
  1339. * @param string $content
  1340. * @param integer $count
  1341. * @param mixed $actual
  1342. * @param string $message
  1343. * @param boolean $isHtml
  1344. * @since Method available since Release 3.3.0
  1345. * @author Mike Naberezny <mike@maintainable.com>
  1346. * @author Derek DeVries <derek@maintainable.com>
  1347. */
  1348. public static function assertSelectEquals($selector, $content, $count, $actual, $message = '', $isHtml = TRUE)
  1349. {
  1350. $tags = PHPUnit_Util_XML::cssSelect(
  1351. $selector, $content, $actual, $isHtml
  1352. );
  1353. // assert specific number of elements
  1354. if (is_numeric($count)) {
  1355. $counted = $tags ? count($tags) : 0;
  1356. self::assertEquals($count, $counted);
  1357. }
  1358. // assert any elements exist if true, assert no elements exist if false
  1359. else if (is_bool($count)) {
  1360. $any = count($tags) > 0 && $tags[0] instanceof DOMNode;
  1361. if ($count) {
  1362. self::assertTrue($any, $message);
  1363. } else {
  1364. self::assertFalse($any, $message);
  1365. }
  1366. }
  1367. // check for range number of elements
  1368. else if (is_array($count) &&
  1369. (isset($count['>']) || isset($count['<']) ||
  1370. isset($count['>=']) || isset($count['<=']))) {
  1371. $counted = $tags ? count($tags) : 0;
  1372. if (isset($count['>'])) {
  1373. self::assertTrue($counted > $count['>'], $message);
  1374. }
  1375. if (isset($count['>='])) {
  1376. self::assertTrue($counted >= $count['>='], $message);
  1377. }
  1378. if (isset($count['<'])) {
  1379. self::assertTrue($counted < $count['<'], $message);
  1380. }
  1381. if (isset($count['<='])) {
  1382. self::assertTrue($counted <= $count['<='], $message);
  1383. }
  1384. } else {
  1385. throw new InvalidArgumentException();
  1386. }
  1387. }
  1388. /**
  1389. * Evaluate an HTML or XML string and assert its structure and/or contents.
  1390. *
  1391. * The first argument ($matcher) is an associative array that specifies the
  1392. * match criteria for the assertion:
  1393. *
  1394. * - `id` : the node with the given id attribute must match the
  1395. * corresponsing value.
  1396. * - `tag` : the node type must match the corresponding value.
  1397. * - `attributes` : a hash. The node's attributres must match the
  1398. * corresponsing values in the hash.
  1399. * - `content` : The text content must match the given value.
  1400. * - `parent` : a hash. The node's parent must match the
  1401. * corresponsing hash.
  1402. * - `child` : a hash. At least one of the node's immediate children
  1403. * must meet the criteria described by the hash.
  1404. * - `ancestor` : a hash. At least one of the node's ancestors must
  1405. * meet the criteria described by the hash.
  1406. * - `descendant` : a hash. At least one of the node's descendants must
  1407. * meet the criteria described by the hash.
  1408. * - `children` : a hash, for counting children of a node.
  1409. * Accepts the keys:
  1410. * - `count` : a number which must equal the number of children
  1411. * that match
  1412. * - `less_than` : the number of matching children must be greater
  1413. * than this number
  1414. * - `greater_than` : the number of matching children must be less than
  1415. * this number
  1416. * - `only` : another hash consisting of the keys to use to match
  1417. * on the children, and only matching children will be
  1418. * counted
  1419. *
  1420. * <code>
  1421. * // Matcher that asserts that there is an element with an id="my_id".
  1422. * $matcher = array('id' => 'my_id');
  1423. *
  1424. * // Matcher that asserts that there is a "span" tag.
  1425. * $matcher = array('tag' => 'span');
  1426. *
  1427. * // Matcher that asserts that there is a "span" tag with the content
  1428. * // "Hello World".
  1429. * $matcher = array('tag' => 'span', 'content' => 'Hello World');
  1430. *
  1431. * // Matcher that asserts that there is a "span" tag with content matching
  1432. * // the regular expression pattern.
  1433. * $matcher = array('tag' => 'span', 'content' => '/Try P(HP|ython)/');
  1434. *
  1435. * // Matcher that asserts that there is a "span" with an "list" class
  1436. * // attribute.
  1437. * $matcher = array(
  1438. * 'tag' => 'span',
  1439. * 'attributes' => array('class' => 'list')
  1440. * );
  1441. *
  1442. * // Matcher that asserts that there is a "span" inside of a "div".
  1443. * $matcher = array(
  1444. * 'tag' => 'span',
  1445. * 'parent' => array('tag' => 'div')
  1446. * );
  1447. *
  1448. * // Matcher that asserts that there is a "span" somewhere inside a
  1449. * // "table".
  1450. * $matcher = array(
  1451. * 'tag' => 'span',
  1452. * 'ancestor' => array('tag' => 'table')
  1453. * );
  1454. *
  1455. * // Matcher that asserts that there is a "span" with at least one "em"
  1456. * // child.
  1457. * $matcher = array(
  1458. * 'tag' => 'span',
  1459. * 'child' => array('tag' => 'em')
  1460. * );
  1461. *
  1462. * // Matcher that asserts that there is a "span" containing a (possibly
  1463. * // nested) "strong" tag.
  1464. * $matcher = array(
  1465. * 'tag' => 'span',
  1466. * 'descendant' => array('tag' => 'strong')
  1467. * );
  1468. *
  1469. * // Matcher that asserts that there is a "span" containing 5-10 "em" tags
  1470. * // as immediate children.
  1471. * $matcher = array(
  1472. * 'tag' => 'span',
  1473. * 'children' => array(
  1474. * 'less_than' => 11,
  1475. * 'greater_than' => 4,
  1476. * 'only' => array('tag' => 'em')
  1477. * )
  1478. * );
  1479. *
  1480. * // Matcher that asserts that there is a "div", with an "ul" ancestor and
  1481. * // a "li" parent (with class="enum"), and containing a "span" descendant
  1482. * // that contains an element with id="my_test" and the text "Hello World".
  1483. * $matcher = array(
  1484. * 'tag' => 'div',
  1485. * 'ancestor' => array('tag' => 'ul'),
  1486. * 'parent' => array(
  1487. * 'tag' => 'li',
  1488. * 'attributes' => array('class' => 'enum')
  1489. * ),
  1490. * 'descendant' => array(
  1491. * 'tag' => 'span',
  1492. * 'child' => array(
  1493. * 'id' => 'my_test',
  1494. * 'content' => 'Hello World'
  1495. * )
  1496. * )
  1497. * );
  1498. *
  1499. * // Use assertTag() to apply a $matcher to a piece of $html.
  1500. * $this->assertTag($matcher, $html);
  1501. *
  1502. * // Use assertTag() to apply a $matcher to a piece of $xml.
  1503. * $this->assertTag($matcher, $xml, '', FALSE);
  1504. * </code>
  1505. *
  1506. * The second argument ($actual) is a string containing either HTML or
  1507. * XML text to be tested.
  1508. *
  1509. * The third argument ($message) is an optional message that will be
  1510. * used if the assertion fails.
  1511. *
  1512. * The fourth argument ($html) is an optional flag specifying whether
  1513. * to load the $actual string into a DOMDocument using the HTML or
  1514. * XML load strategy. It is TRUE by default, which assumes the HTML
  1515. * load strategy. In many cases, this will be acceptable for XML as well.
  1516. *
  1517. * @param array $matcher
  1518. * @param string $actual
  1519. * @param string $message
  1520. * @param boolean $isHtml
  1521. * @since Method available since Release 3.3.0
  1522. * @author Mike Naberezny <mike@maintainable.com>
  1523. * @author Derek DeVries <derek@maintainable.com>
  1524. */
  1525. public static function assertTag($matcher, $actual, $message = '', $isHtml = TRUE)
  1526. {
  1527. $dom = PHPUnit_Util_XML::load($actual, $isHtml);
  1528. $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
  1529. $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
  1530. self::assertTrue($matched, $message);
  1531. }
  1532. /**
  1533. * This assertion is the exact opposite of assertTag().
  1534. *
  1535. * Rather than asserting that $matcher results in a match, it asserts that
  1536. * $matcher does not match.
  1537. *
  1538. * @param array $matcher
  1539. * @param string $actual
  1540. * @param string $message
  1541. * @param boolean $isHtml
  1542. * @since Method available since Release 3.3.0
  1543. * @author Mike Naberezny <mike@maintainable.com>
  1544. * @author Derek DeVries <derek@maintainable.com>
  1545. */
  1546. public static function assertNotTag($matcher, $actual, $message = '', $isHtml = TRUE)
  1547. {
  1548. $dom = PHPUnit_Util_XML::load($actual, $isHtml);
  1549. $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
  1550. $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
  1551. self::assertFalse($matched, $message);
  1552. }
  1553. /**
  1554. * Evaluates a PHPUnit_Framework_Constraint matcher object.
  1555. *
  1556. * @param mixed $value
  1557. * @param PHPUnit_Framework_Constraint $constraint
  1558. * @param string $message
  1559. * @since Method available since Release 3.0.0
  1560. */
  1561. public static function assertThat($value, PHPUnit_Framework_Constraint $constraint, $message = '')
  1562. {
  1563. self::$count += count($constraint);
  1564. if (!$constraint->evaluate($value)) {
  1565. $constraint->fail($value, $message);
  1566. }
  1567. }
  1568. /**
  1569. * Returns a PHPUnit_Framework_Constraint_And matcher object.
  1570. *
  1571. * @return PHPUnit_Framework_Constraint_And
  1572. * @since Method available since Release 3.0.0
  1573. */
  1574. public static function logicalAnd()
  1575. {
  1576. $constraints = func_get_args();
  1577. $constraint = new PHPUnit_Framework_Constraint_And;
  1578. $constraint->setConstraints($constraints);
  1579. return $constraint;
  1580. }
  1581. /**
  1582. * Returns a PHPUnit_Framework_Constraint_Or matcher object.
  1583. *
  1584. * @return PHPUnit_Framework_Constraint_Or
  1585. * @since Method available since Release 3.0.0
  1586. */
  1587. public static function logicalOr()
  1588. {
  1589. $constraints = func_get_args();
  1590. $constraint = new PHPUnit_Framework_Constraint_Or;
  1591. $constraint->setConstraints($constraints);
  1592. return $constraint;
  1593. }
  1594. /**
  1595. * Returns a PHPUnit_Framework_Constraint_Not matcher object.
  1596. *
  1597. * @param PHPUnit_Framework_Constraint $constraint
  1598. * @return PHPUnit_Framework_Constraint_Not
  1599. * @since Method available since Release 3.0.0
  1600. */
  1601. public static function logicalNot(PHPUnit_Framework_Constraint $constraint)
  1602. {
  1603. return new PHPUnit_Framework_Constraint_Not($constraint);
  1604. }
  1605. /**
  1606. * Returns a PHPUnit_Framework_Constraint_Xor matcher object.
  1607. *
  1608. * @return PHPUnit_Framework_Constraint_Xor
  1609. * @since Method available since Release 3.0.0
  1610. */
  1611. public static function logicalXor()
  1612. {
  1613. $constraints = func_get_args();
  1614. $constraint = new PHPUnit_Framework_Constraint_Xor;
  1615. $constraint->setConstraints($constraints);
  1616. return $constraint;
  1617. }
  1618. /**
  1619. * Returns a PHPUnit_Framework_Constraint_IsAnything matcher object.
  1620. *
  1621. * @return PHPUnit_Framework_Constraint_IsAnything
  1622. * @since Method available since Release 3.0.0
  1623. */
  1624. public static function anything()
  1625. {
  1626. return new PHPUnit_Framework_Constraint_IsAnything;
  1627. }
  1628. /**
  1629. * Returns a PHPUnit_Framework_Constraint_IsTrue matcher object.
  1630. *
  1631. * @return PHPUnit_Framework_Constraint_IsTrue
  1632. * @since Method available since Release 3.3.0
  1633. */
  1634. public static function isTrue()
  1635. {
  1636. return new PHPUnit_Framework_Constraint_IsTrue;
  1637. }
  1638. /**
  1639. * Returns a PHPUnit_Framework_Constraint_IsFalse matcher object.
  1640. *
  1641. * @return PHPUnit_Framework_Constraint_IsFalse
  1642. * @since Method available since Release 3.3.0
  1643. */
  1644. public static function isFalse()
  1645. {
  1646. return new PHPUnit_Framework_Constraint_IsFalse;
  1647. }
  1648. /**
  1649. * Returns a PHPUnit_Framework_Constraint_IsNull matcher object.
  1650. *
  1651. * @return PHPUnit_Framework_Constraint_IsNull
  1652. * @since Method available since Release 3.3.0
  1653. */
  1654. public static function isNull()
  1655. {
  1656. return new PHPUnit_Framework_Constraint_IsNull;
  1657. }
  1658. /**
  1659. * Returns a PHPUnit_Framework_Constraint_Attribute matcher object.
  1660. *
  1661. * @param PHPUnit_Framework_Constraint $constraint
  1662. * @param string $attributeName
  1663. * @return PHPUnit_Framework_Constraint_Attribute
  1664. * @since Method available since Release 3.1.0
  1665. */
  1666. public static function attribute(PHPUnit_Framework_Constraint $constraint, $attributeName)
  1667. {
  1668. return new PHPUnit_Framework_Constraint_Attribute(
  1669. $constraint, $attributeName
  1670. );
  1671. }
  1672. /**
  1673. * Returns a PHPUnit_Framework_Constraint_TraversableContains matcher
  1674. * object.
  1675. *
  1676. * @param mixed $value
  1677. * @return PHPUnit_Framework_Constraint_TraversableContains
  1678. * @since Method available since Release 3.0.0
  1679. */
  1680. public static function contains($value)
  1681. {
  1682. return new PHPUnit_Framework_Constraint_TraversableContains($value);
  1683. }
  1684. /**
  1685. * Returns a PHPUnit_Framework_Constraint_TraversableContainsOnly matcher
  1686. * object.
  1687. *
  1688. * @param string $type
  1689. * @return PHPUnit_Framework_Constraint_TraversableContainsOnly
  1690. * @since Method available since Release 3.1.4
  1691. */
  1692. public static function containsOnly($type)
  1693. {
  1694. return new PHPUnit_Framework_Constraint_TraversableContainsOnly($type);
  1695. }
  1696. /**
  1697. * Returns a PHPUnit_Framework_Constraint_ArrayHasKey matcher object.
  1698. *
  1699. * @param mixed $key
  1700. * @return PHPUnit_Framework_Constraint_ArrayHasKey
  1701. * @since Method available since Release 3.0.0
  1702. */
  1703. public static function arrayHasKey($key)
  1704. {
  1705. return new PHPUnit_Framework_Constraint_ArrayHasKey($key);
  1706. }
  1707. /**
  1708. * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object.
  1709. *
  1710. * @param mixed $value
  1711. * @param float $delta
  1712. * @param integer $maxDepth
  1713. * @param boolean $canonicalizeEol
  1714. * @param boolean $ignoreCase
  1715. * @return PHPUnit_Framework_Constraint_IsEqual
  1716. * @since Method available since Release 3.0.0
  1717. */
  1718. public static function equalTo($value, $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE, $ignoreCase = FALSE)
  1719. {
  1720. return new PHPUnit_Framework_Constraint_IsEqual(
  1721. $value, $delta, $maxDepth, $canonicalizeEol, $ignoreCase
  1722. );
  1723. }
  1724. /**
  1725. * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object
  1726. * that is wrapped in a PHPUnit_Framework_Constraint_Attribute matcher
  1727. * object.
  1728. *
  1729. * @param string $attributeName
  1730. * @param mixed $value
  1731. * @param float $delta
  1732. * @param integer $maxDepth
  1733. * @param boolean $canonicalizeEol
  1734. * @param boolean $ignoreCase
  1735. * @return PHPUnit_Framework_Constraint_Attribute
  1736. * @since Method available since Release 3.1.0
  1737. */
  1738. public static function attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10, $canonicalizeEol = FALSE, $ignoreCase = FALSE)
  1739. {
  1740. return self::attribute(
  1741. self::equalTo(
  1742. $value, $delta, $maxDepth, $canonicalizeEol, $ignoreCase
  1743. ),
  1744. $attributeName
  1745. );
  1746. }
  1747. /**
  1748. * Returns a PHPUnit_Framework_Constraint_FileExists matcher object.
  1749. *
  1750. * @return PHPUnit_Framework_Constraint_FileExists
  1751. * @since Method available since Release 3.0.0
  1752. */
  1753. public static function fileExists()
  1754. {
  1755. return new PHPUnit_Framework_Constraint_FileExists;
  1756. }
  1757. /**
  1758. * Returns a PHPUnit_Framework_Constraint_GreaterThan matcher object.
  1759. *
  1760. * @param mixed $value
  1761. * @return PHPUnit_Framework_Constraint_GreaterThan
  1762. * @since Method available since Release 3.0.0
  1763. */
  1764. public static function greaterThan($value)
  1765. {
  1766. return new PHPUnit_Framework_Constraint_GreaterThan($value);
  1767. }
  1768. /**
  1769. * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps
  1770. * a PHPUnit_Framework_Constraint_IsEqual and a
  1771. * PHPUnit_Framework_Constraint_GreaterThan matcher object.
  1772. *
  1773. * @param mixed $value
  1774. * @return PHPUnit_Framework_Constraint_Or
  1775. * @since Method available since Release 3.1.0
  1776. */
  1777. public static function greaterThanOrEqual($value)
  1778. {
  1779. return self::logicalOr(
  1780. new PHPUnit_Framework_Constraint_IsEqual($value),
  1781. new PHPUnit_Framework_Constraint_GreaterThan($value)
  1782. );
  1783. }
  1784. /**
  1785. * Returns a PHPUnit_Framework_Constraint_ClassHasAttribute matcher object.
  1786. *
  1787. * @param string $attributeName
  1788. * @return PHPUnit_Framework_Constraint_ClassHasAttribute
  1789. * @since Method available since Release 3.1.0
  1790. */
  1791. public static function classHasAttribute($attributeName)
  1792. {
  1793. return new PHPUnit_Framework_Constraint_ClassHasAttribute(
  1794. $attributeName
  1795. );
  1796. }
  1797. /**
  1798. * Returns a PHPUnit_Framework_Constraint_ClassHasStaticAttribute matcher
  1799. * object.
  1800. *
  1801. * @param string $attributeName
  1802. * @return PHPUnit_Framework_Constraint_ClassHasStaticAttribute
  1803. * @since Method available since Release 3.1.0
  1804. */
  1805. public static function classHasStaticAttribute($attributeName)
  1806. {
  1807. return new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
  1808. $attributeName
  1809. );
  1810. }
  1811. /**
  1812. * Returns a PHPUnit_Framework_Constraint_ObjectHasAttribute matcher object.
  1813. *
  1814. * @param string $attributeName
  1815. * @return PHPUnit_Framework_Constraint_ObjectHasAttribute
  1816. * @since Method available since Release 3.0.0
  1817. */
  1818. public static function objectHasAttribute($attributeName)
  1819. {
  1820. return new PHPUnit_Framework_Constraint_ObjectHasAttribute(
  1821. $attributeName
  1822. );
  1823. }
  1824. /**
  1825. * Returns a PHPUnit_Framework_Constraint_IsIdentical matcher object.
  1826. *
  1827. * @param mixed $value
  1828. * @return PHPUnit_Framework_Constraint_IsIdentical
  1829. * @since Method available since Release 3.0.0
  1830. */
  1831. public static function identicalTo($value)
  1832. {
  1833. return new PHPUnit_Framework_Constraint_IsIdentical($value);
  1834. }
  1835. /**
  1836. * Returns a PHPUnit_Framework_Constraint_IsInstanceOf matcher object.
  1837. *
  1838. * @param string $className
  1839. * @return PHPUnit_Framework_Constraint_IsInstanceOf
  1840. * @since Method available since Release 3.0.0
  1841. */
  1842. public static function isInstanceOf($className)
  1843. {
  1844. return new PHPUnit_Framework_Constraint_IsInstanceOf($className);
  1845. }
  1846. /**
  1847. * Returns a PHPUnit_Framework_Constraint_IsType matcher object.
  1848. *
  1849. * @param string $type
  1850. * @return PHPUnit_Framework_Constraint_IsType
  1851. * @since Method available since Release 3.0.0
  1852. */
  1853. public static function isType($type)
  1854. {
  1855. return new PHPUnit_Framework_Constraint_IsType($type);
  1856. }
  1857. /**
  1858. * Returns a PHPUnit_Framework_Constraint_LessThan matcher object.
  1859. *
  1860. * @param mixed $value
  1861. * @return PHPUnit_Framework_Constraint_LessThan
  1862. * @since Method available since Release 3.0.0
  1863. */
  1864. public static function lessThan($value)
  1865. {
  1866. return new PHPUnit_Framework_Constraint_LessThan($value);
  1867. }
  1868. /**
  1869. * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps
  1870. * a PHPUnit_Framework_Constraint_IsEqual and a
  1871. * PHPUnit_Framework_Constraint_LessThan matcher object.
  1872. *
  1873. * @param mixed $value
  1874. * @return PHPUnit_Framework_Constraint_Or
  1875. * @since Method available since Release 3.1.0
  1876. */
  1877. public static function lessThanOrEqual($value)
  1878. {
  1879. return self::logicalOr(
  1880. new PHPUnit_Framework_Constraint_IsEqual($value),
  1881. new PHPUnit_Framework_Constraint_LessThan($value)
  1882. );
  1883. }
  1884. /**
  1885. * Returns a PHPUnit_Framework_Constraint_PCREMatch matcher object.
  1886. *
  1887. * @param string $pattern
  1888. * @return PHPUnit_Framework_Constraint_PCREMatch
  1889. * @since Method available since Release 3.0.0
  1890. */
  1891. public static function matchesRegularExpression($pattern)
  1892. {
  1893. return new PHPUnit_Framework_Constraint_PCREMatch($pattern);
  1894. }
  1895. /**
  1896. * Returns a PHPUnit_Framework_Constraint_StringStartsWith matcher object.
  1897. *
  1898. * @param mixed $prefix
  1899. * @return PHPUnit_Framework_Constraint_StringStartsWith
  1900. * @since Method available since Release 3.4.0
  1901. */
  1902. public static function stringStartsWith($prefix)
  1903. {
  1904. return new PHPUnit_Framework_Constraint_StringStartsWith($prefix);
  1905. }
  1906. /**
  1907. * Returns a PHPUnit_Framework_Constraint_StringContains matcher object.
  1908. *
  1909. * @param string $string
  1910. * @param boolean $case
  1911. * @return PHPUnit_Framework_Constraint_StringContains
  1912. * @since Method available since Release 3.0.0
  1913. */
  1914. public static function stringContains($string, $case = TRUE)
  1915. {
  1916. return new PHPUnit_Framework_Constraint_StringContains($string, $case);
  1917. }
  1918. /**
  1919. * Returns a PHPUnit_Framework_Constraint_StringEndsWith matcher object.
  1920. *
  1921. * @param mixed $suffix
  1922. * @return PHPUnit_Framework_Constraint_StringEndsWith
  1923. * @since Method available since Release 3.4.0
  1924. */
  1925. public static function stringEndsWith($suffix)
  1926. {
  1927. return new PHPUnit_Framework_Constraint_StringEndsWith($suffix);
  1928. }
  1929. /**
  1930. * Fails a test with the given message.
  1931. *
  1932. * @param string $message
  1933. * @throws PHPUnit_Framework_AssertionFailedError
  1934. */
  1935. public static function fail($message = '')
  1936. {
  1937. throw new PHPUnit_Framework_AssertionFailedError($message);
  1938. }
  1939. /**
  1940. * Returns the value of an attribute of a class or an object.
  1941. * This also works for attributes that are declared protected or private.
  1942. *
  1943. * @param mixed $classOrObject
  1944. * @param string $attributeName
  1945. * @return mixed
  1946. * @throws InvalidArgumentException
  1947. */
  1948. public static function readAttribute($classOrObject, $attributeName)
  1949. {
  1950. if (!is_string($attributeName)) {
  1951. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1952. }
  1953. if (is_string($classOrObject)) {
  1954. if (!class_exists($classOrObject)) {
  1955. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  1956. 1, 'class name'
  1957. );
  1958. }
  1959. return PHPUnit_Util_Class::getStaticAttribute(
  1960. $classOrObject,
  1961. $attributeName
  1962. );
  1963. }
  1964. else if (is_object($classOrObject)) {
  1965. return PHPUnit_Util_Class::getObjectAttribute(
  1966. $classOrObject,
  1967. $attributeName
  1968. );
  1969. }
  1970. else {
  1971. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  1972. 1, 'class name or object'
  1973. );
  1974. }
  1975. }
  1976. /**
  1977. * Mark the test as incomplete.
  1978. *
  1979. * @param string $message
  1980. * @throws PHPUnit_Framework_IncompleteTestError
  1981. * @since Method available since Release 3.0.0
  1982. */
  1983. public static function markTestIncomplete($message = '')
  1984. {
  1985. throw new PHPUnit_Framework_IncompleteTestError($message);
  1986. }
  1987. /**
  1988. * Mark the test as skipped.
  1989. *
  1990. * @param string $message
  1991. * @throws PHPUnit_Framework_SkippedTestError
  1992. * @since Method available since Release 3.0.0
  1993. */
  1994. public static function markTestSkipped($message = '')
  1995. {
  1996. throw new PHPUnit_Framework_SkippedTestError($message);
  1997. }
  1998. /**
  1999. * Return the current assertion count.
  2000. *
  2001. * @return integer
  2002. * @since Method available since Release 3.3.3
  2003. */
  2004. public static function getCount()
  2005. {
  2006. return self::$count;
  2007. }
  2008. /**
  2009. * Reset the assertion counter.
  2010. *
  2011. * @since Method available since Release 3.3.3
  2012. */
  2013. public static function resetCount()
  2014. {
  2015. self::$count = 0;
  2016. }
  2017. }
  2018. ?>