PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/org.phpsrc.eclipse.pti.library.pear_1.2.2.R20120127000000/php/library/PEAR/PHPUnit/Framework/Assert.php

https://bitbucket.org/2GAndre/myeclipse4php
PHP | 2619 lines | 1154 code | 253 blank | 1212 comment | 113 complexity | 216c4870a552bdae55f70c771fd18d79 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2001-2012, Sebastian Bergmann <sebastian@phpunit.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. * @package PHPUnit
  38. * @subpackage Framework
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 2.0.0
  44. */
  45. /**
  46. * A set of assert methods.
  47. *
  48. * @package PHPUnit
  49. * @subpackage Framework
  50. * @author Sebastian Bergmann <sebastian@phpunit.de>
  51. * @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
  52. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  53. * @version Release: 3.6.10
  54. * @link http://www.phpunit.de/
  55. * @since Class available since Release 2.0.0
  56. */
  57. abstract class PHPUnit_Framework_Assert
  58. {
  59. /**
  60. * @var integer
  61. */
  62. private static $count = 0;
  63. /**
  64. * Asserts that an array has a specified key.
  65. *
  66. * @param mixed $key
  67. * @param array $array
  68. * @param string $message
  69. * @since Method available since Release 3.0.0
  70. */
  71. public static function assertArrayHasKey($key, array $array, $message = '')
  72. {
  73. if (!(is_integer($key) || is_string($key))) {
  74. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  75. 1, 'integer or string'
  76. );
  77. }
  78. $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey($key);
  79. self::assertThat($array, $constraint, $message);
  80. }
  81. /**
  82. * Asserts that an array does not have a specified key.
  83. *
  84. * @param mixed $key
  85. * @param array $array
  86. * @param string $message
  87. * @since Method available since Release 3.0.0
  88. */
  89. public static function assertArrayNotHasKey($key, array $array, $message = '')
  90. {
  91. if (!(is_integer($key) || is_string($key))) {
  92. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  93. 1, 'integer or string'
  94. );
  95. }
  96. $constraint = new PHPUnit_Framework_Constraint_Not(
  97. new PHPUnit_Framework_Constraint_ArrayHasKey($key)
  98. );
  99. self::assertThat($array, $constraint, $message);
  100. }
  101. /**
  102. * Asserts that a haystack contains a needle.
  103. *
  104. * @param mixed $needle
  105. * @param mixed $haystack
  106. * @param string $message
  107. * @param boolean $ignoreCase
  108. * @param boolean $checkForObjectIdentity
  109. * @since Method available since Release 2.1.0
  110. */
  111. public static function assertContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
  112. {
  113. if (is_array($haystack) ||
  114. is_object($haystack) && $haystack instanceof Traversable) {
  115. $constraint = new PHPUnit_Framework_Constraint_TraversableContains(
  116. $needle, $checkForObjectIdentity
  117. );
  118. }
  119. else if (is_string($haystack)) {
  120. $constraint = new PHPUnit_Framework_Constraint_StringContains(
  121. $needle, $ignoreCase
  122. );
  123. }
  124. else {
  125. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  126. 2, 'array, iterator or string'
  127. );
  128. }
  129. self::assertThat($haystack, $constraint, $message);
  130. }
  131. /**
  132. * Asserts that a haystack that is stored in a static attribute of a class
  133. * or an attribute of an object contains a needle.
  134. *
  135. * @param mixed $needle
  136. * @param string $haystackAttributeName
  137. * @param mixed $haystackClassOrObject
  138. * @param string $message
  139. * @param boolean $ignoreCase
  140. * @param boolean $checkForObjectIdentity
  141. * @since Method available since Release 3.0.0
  142. */
  143. public static function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
  144. {
  145. self::assertContains(
  146. $needle,
  147. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  148. $message,
  149. $ignoreCase,
  150. $checkForObjectIdentity
  151. );
  152. }
  153. /**
  154. * Asserts that a haystack does not contain a needle.
  155. *
  156. * @param mixed $needle
  157. * @param mixed $haystack
  158. * @param string $message
  159. * @param boolean $ignoreCase
  160. * @param boolean $checkForObjectIdentity
  161. * @since Method available since Release 2.1.0
  162. */
  163. public static function assertNotContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
  164. {
  165. if (is_array($haystack) ||
  166. is_object($haystack) && $haystack instanceof Traversable) {
  167. $constraint = new PHPUnit_Framework_Constraint_Not(
  168. new PHPUnit_Framework_Constraint_TraversableContains(
  169. $needle, $checkForObjectIdentity
  170. )
  171. );
  172. }
  173. else if (is_string($haystack)) {
  174. $constraint = new PHPUnit_Framework_Constraint_Not(
  175. new PHPUnit_Framework_Constraint_StringContains(
  176. $needle, $ignoreCase
  177. )
  178. );
  179. }
  180. else {
  181. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  182. 2, 'array, iterator or string'
  183. );
  184. }
  185. self::assertThat($haystack, $constraint, $message);
  186. }
  187. /**
  188. * Asserts that a haystack that is stored in a static attribute of a class
  189. * or an attribute of an object does not contain a needle.
  190. *
  191. * @param mixed $needle
  192. * @param string $haystackAttributeName
  193. * @param mixed $haystackClassOrObject
  194. * @param string $message
  195. * @param boolean $ignoreCase
  196. * @param boolean $checkForObjectIdentity
  197. * @since Method available since Release 3.0.0
  198. */
  199. public static function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE)
  200. {
  201. self::assertNotContains(
  202. $needle,
  203. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  204. $message,
  205. $ignoreCase,
  206. $checkForObjectIdentity
  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 the number of elements of an array, Countable or Iterator.
  310. *
  311. * @param integer $expectedCount
  312. * @param mixed $haystack
  313. * @param string $message
  314. */
  315. public static function assertCount($expectedCount, $haystack, $message = '')
  316. {
  317. if (!is_int($expectedCount)) {
  318. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer');
  319. }
  320. if (!$haystack instanceof Countable &&
  321. !$haystack instanceof Iterator &&
  322. !is_array($haystack)) {
  323. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable');
  324. }
  325. self::assertThat(
  326. $haystack,
  327. new PHPUnit_Framework_Constraint_Count($expectedCount),
  328. $message
  329. );
  330. }
  331. /**
  332. * Asserts the number of elements of an array, Countable or Iterator
  333. * that is stored in an attribute.
  334. *
  335. * @param integer $expectedCount
  336. * @param string $haystackAttributeName
  337. * @param mixed $haystackClassOrObject
  338. * @param string $message
  339. * @since Method available since Release 3.6.0
  340. */
  341. public static function assertAttributeCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '')
  342. {
  343. self::assertCount(
  344. $expectedCount,
  345. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  346. $message
  347. );
  348. }
  349. /**
  350. * Asserts the number of elements of an array, Countable or Iterator.
  351. *
  352. * @param integer $expectedCount
  353. * @param mixed $haystack
  354. * @param string $message
  355. */
  356. public static function assertNotCount($expectedCount, $haystack, $message = '')
  357. {
  358. if (!is_int($expectedCount)) {
  359. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer');
  360. }
  361. if (!$haystack instanceof Countable &&
  362. !$haystack instanceof Iterator &&
  363. !is_array($haystack)) {
  364. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable');
  365. }
  366. $constraint = new PHPUnit_Framework_Constraint_Not(
  367. new PHPUnit_Framework_Constraint_Count($expectedCount)
  368. );
  369. self::assertThat($haystack, $constraint, $message);
  370. }
  371. /**
  372. * Asserts the number of elements of an array, Countable or Iterator
  373. * that is stored in an attribute.
  374. *
  375. * @param integer $expectedCount
  376. * @param string $haystackAttributeName
  377. * @param mixed $haystackClassOrObject
  378. * @param string $message
  379. * @since Method available since Release 3.6.0
  380. */
  381. public static function assertAttributeNotCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '')
  382. {
  383. self::assertNotCount(
  384. $expectedCount,
  385. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  386. $message
  387. );
  388. }
  389. /**
  390. * Asserts that two variables are equal.
  391. *
  392. * @param mixed $expected
  393. * @param mixed $actual
  394. * @param string $message
  395. * @param float $delta
  396. * @param integer $maxDepth
  397. * @param boolean $canonicalize
  398. * @param boolean $ignoreCase
  399. */
  400. public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
  401. {
  402. $constraint = new PHPUnit_Framework_Constraint_IsEqual(
  403. $expected, $delta, $maxDepth, $canonicalize, $ignoreCase
  404. );
  405. self::assertThat($actual, $constraint, $message);
  406. }
  407. /**
  408. * Asserts that a variable is equal to an attribute of an object.
  409. *
  410. * @param mixed $expected
  411. * @param string $actualAttributeName
  412. * @param string $actualClassOrObject
  413. * @param string $message
  414. * @param float $delta
  415. * @param integer $maxDepth
  416. * @param boolean $canonicalize
  417. * @param boolean $ignoreCase
  418. */
  419. public static function assertAttributeEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
  420. {
  421. self::assertEquals(
  422. $expected,
  423. self::readAttribute($actualClassOrObject, $actualAttributeName),
  424. $message,
  425. $delta,
  426. $maxDepth,
  427. $canonicalize,
  428. $ignoreCase
  429. );
  430. }
  431. /**
  432. * Asserts that two variables are not equal.
  433. *
  434. * @param mixed $expected
  435. * @param mixed $actual
  436. * @param string $message
  437. * @param float $delta
  438. * @param integer $maxDepth
  439. * @param boolean $canonicalize
  440. * @param boolean $ignoreCase
  441. * @since Method available since Release 2.3.0
  442. */
  443. public static function assertNotEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
  444. {
  445. $constraint = new PHPUnit_Framework_Constraint_Not(
  446. new PHPUnit_Framework_Constraint_IsEqual(
  447. $expected, $delta, $maxDepth, $canonicalize, $ignoreCase
  448. )
  449. );
  450. self::assertThat($actual, $constraint, $message);
  451. }
  452. /**
  453. * Asserts that a variable is not equal to an attribute of an object.
  454. *
  455. * @param mixed $expected
  456. * @param string $actualAttributeName
  457. * @param string $actualClassOrObject
  458. * @param string $message
  459. * @param float $delta
  460. * @param integer $maxDepth
  461. * @param boolean $canonicalize
  462. * @param boolean $ignoreCase
  463. */
  464. public static function assertAttributeNotEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
  465. {
  466. self::assertNotEquals(
  467. $expected,
  468. self::readAttribute($actualClassOrObject, $actualAttributeName),
  469. $message,
  470. $delta,
  471. $maxDepth,
  472. $canonicalize,
  473. $ignoreCase
  474. );
  475. }
  476. /**
  477. * Asserts that a variable is empty.
  478. *
  479. * @param mixed $actual
  480. * @param string $message
  481. * @throws PHPUnit_Framework_AssertionFailedError
  482. */
  483. public static function assertEmpty($actual, $message = '')
  484. {
  485. self::assertThat($actual, self::isEmpty(), $message);
  486. }
  487. /**
  488. * Asserts that a static attribute of a class or an attribute of an object
  489. * is empty.
  490. *
  491. * @param string $haystackAttributeName
  492. * @param mixed $haystackClassOrObject
  493. * @param string $message
  494. * @since Method available since Release 3.5.0
  495. */
  496. public static function assertAttributeEmpty($haystackAttributeName, $haystackClassOrObject, $message = '')
  497. {
  498. self::assertEmpty(
  499. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  500. $message
  501. );
  502. }
  503. /**
  504. * Asserts that a variable is not empty.
  505. *
  506. * @param mixed $actual
  507. * @param string $message
  508. * @throws PHPUnit_Framework_AssertionFailedError
  509. */
  510. public static function assertNotEmpty($actual, $message = '')
  511. {
  512. self::assertThat($actual, self::logicalNot(self::isEmpty()), $message);
  513. }
  514. /**
  515. * Asserts that a static attribute of a class or an attribute of an object
  516. * is not empty.
  517. *
  518. * @param string $haystackAttributeName
  519. * @param mixed $haystackClassOrObject
  520. * @param string $message
  521. * @since Method available since Release 3.5.0
  522. */
  523. public static function assertAttributeNotEmpty($haystackAttributeName, $haystackClassOrObject, $message = '')
  524. {
  525. self::assertNotEmpty(
  526. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  527. $message
  528. );
  529. }
  530. /**
  531. * Asserts that a value is greater than another value.
  532. *
  533. * @param mixed $expected
  534. * @param mixed $actual
  535. * @param string $message
  536. * @since Method available since Release 3.1.0
  537. */
  538. public static function assertGreaterThan($expected, $actual, $message = '')
  539. {
  540. self::assertThat($actual, self::greaterThan($expected), $message);
  541. }
  542. /**
  543. * Asserts that an attribute is greater than another value.
  544. *
  545. * @param mixed $expected
  546. * @param string $actualAttributeName
  547. * @param string $actualClassOrObject
  548. * @param string $message
  549. * @since Method available since Release 3.1.0
  550. */
  551. public static function assertAttributeGreaterThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  552. {
  553. self::assertGreaterThan(
  554. $expected,
  555. self::readAttribute($actualClassOrObject, $actualAttributeName),
  556. $message
  557. );
  558. }
  559. /**
  560. * Asserts that a value is greater than or equal to another value.
  561. *
  562. * @param mixed $expected
  563. * @param mixed $actual
  564. * @param string $message
  565. * @since Method available since Release 3.1.0
  566. */
  567. public static function assertGreaterThanOrEqual($expected, $actual, $message = '')
  568. {
  569. self::assertThat(
  570. $actual, self::greaterThanOrEqual($expected), $message
  571. );
  572. }
  573. /**
  574. * Asserts that an attribute is greater than or equal to another value.
  575. *
  576. * @param mixed $expected
  577. * @param string $actualAttributeName
  578. * @param string $actualClassOrObject
  579. * @param string $message
  580. * @since Method available since Release 3.1.0
  581. */
  582. public static function assertAttributeGreaterThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  583. {
  584. self::assertGreaterThanOrEqual(
  585. $expected,
  586. self::readAttribute($actualClassOrObject, $actualAttributeName),
  587. $message
  588. );
  589. }
  590. /**
  591. * Asserts that a value is smaller than another value.
  592. *
  593. * @param mixed $expected
  594. * @param mixed $actual
  595. * @param string $message
  596. * @since Method available since Release 3.1.0
  597. */
  598. public static function assertLessThan($expected, $actual, $message = '')
  599. {
  600. self::assertThat($actual, self::lessThan($expected), $message);
  601. }
  602. /**
  603. * Asserts that an attribute is smaller than another value.
  604. *
  605. * @param mixed $expected
  606. * @param string $actualAttributeName
  607. * @param string $actualClassOrObject
  608. * @param string $message
  609. * @since Method available since Release 3.1.0
  610. */
  611. public static function assertAttributeLessThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  612. {
  613. self::assertLessThan(
  614. $expected,
  615. self::readAttribute($actualClassOrObject, $actualAttributeName),
  616. $message
  617. );
  618. }
  619. /**
  620. * Asserts that a value is smaller than or equal to another value.
  621. *
  622. * @param mixed $expected
  623. * @param mixed $actual
  624. * @param string $message
  625. * @since Method available since Release 3.1.0
  626. */
  627. public static function assertLessThanOrEqual($expected, $actual, $message = '')
  628. {
  629. self::assertThat($actual, self::lessThanOrEqual($expected), $message);
  630. }
  631. /**
  632. * Asserts that an attribute is smaller than or equal to another value.
  633. *
  634. * @param mixed $expected
  635. * @param string $actualAttributeName
  636. * @param string $actualClassOrObject
  637. * @param string $message
  638. * @since Method available since Release 3.1.0
  639. */
  640. public static function assertAttributeLessThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  641. {
  642. self::assertLessThanOrEqual(
  643. $expected,
  644. self::readAttribute($actualClassOrObject, $actualAttributeName),
  645. $message
  646. );
  647. }
  648. /**
  649. * Asserts that the contents of one file is equal to the contents of another
  650. * file.
  651. *
  652. * @param string $expected
  653. * @param string $actual
  654. * @param string $message
  655. * @param boolean $canonicalize
  656. * @param boolean $ignoreCase
  657. * @since Method available since Release 3.2.14
  658. */
  659. public static function assertFileEquals($expected, $actual, $message = '', $canonicalize = FALSE, $ignoreCase = FALSE)
  660. {
  661. self::assertFileExists($expected, $message);
  662. self::assertFileExists($actual, $message);
  663. self::assertEquals(
  664. file_get_contents($expected),
  665. file_get_contents($actual),
  666. $message,
  667. 0,
  668. 10,
  669. $canonicalize,
  670. $ignoreCase
  671. );
  672. }
  673. /**
  674. * Asserts that the contents of one file is not equal to the contents of
  675. * another file.
  676. *
  677. * @param string $expected
  678. * @param string $actual
  679. * @param string $message
  680. * @param boolean $canonicalize
  681. * @param boolean $ignoreCase
  682. * @since Method available since Release 3.2.14
  683. */
  684. public static function assertFileNotEquals($expected, $actual, $message = '', $canonicalize = FALSE, $ignoreCase = FALSE)
  685. {
  686. self::assertFileExists($expected, $message);
  687. self::assertFileExists($actual, $message);
  688. self::assertNotEquals(
  689. file_get_contents($expected),
  690. file_get_contents($actual),
  691. $message,
  692. 0,
  693. 10,
  694. $canonicalize,
  695. $ignoreCase
  696. );
  697. }
  698. /**
  699. * Asserts that the contents of a string is equal
  700. * to the contents of a file.
  701. *
  702. * @param string $expectedFile
  703. * @param string $actualString
  704. * @param string $message
  705. * @param boolean $canonicalize
  706. * @param boolean $ignoreCase
  707. * @since Method available since Release 3.3.0
  708. */
  709. public static function assertStringEqualsFile($expectedFile, $actualString, $message = '', $canonicalize = FALSE, $ignoreCase = FALSE)
  710. {
  711. self::assertFileExists($expectedFile, $message);
  712. self::assertEquals(
  713. file_get_contents($expectedFile),
  714. $actualString,
  715. $message,
  716. 0,
  717. 10,
  718. $canonicalize,
  719. $ignoreCase
  720. );
  721. }
  722. /**
  723. * Asserts that the contents of a string is not equal
  724. * to the contents of a file.
  725. *
  726. * @param string $expectedFile
  727. * @param string $actualString
  728. * @param string $message
  729. * @param boolean $canonicalize
  730. * @param boolean $ignoreCase
  731. * @since Method available since Release 3.3.0
  732. */
  733. public static function assertStringNotEqualsFile($expectedFile, $actualString, $message = '', $canonicalize = FALSE, $ignoreCase = FALSE)
  734. {
  735. self::assertFileExists($expectedFile, $message);
  736. self::assertNotEquals(
  737. file_get_contents($expectedFile),
  738. $actualString,
  739. $message,
  740. 0,
  741. 10,
  742. $canonicalize,
  743. $ignoreCase
  744. );
  745. }
  746. /**
  747. * Asserts that a file exists.
  748. *
  749. * @param string $filename
  750. * @param string $message
  751. * @since Method available since Release 3.0.0
  752. */
  753. public static function assertFileExists($filename, $message = '')
  754. {
  755. if (!is_string($filename)) {
  756. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  757. }
  758. $constraint = new PHPUnit_Framework_Constraint_FileExists;
  759. self::assertThat($filename, $constraint, $message);
  760. }
  761. /**
  762. * Asserts that a file does not exist.
  763. *
  764. * @param string $filename
  765. * @param string $message
  766. * @since Method available since Release 3.0.0
  767. */
  768. public static function assertFileNotExists($filename, $message = '')
  769. {
  770. if (!is_string($filename)) {
  771. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  772. }
  773. $constraint = new PHPUnit_Framework_Constraint_Not(
  774. new PHPUnit_Framework_Constraint_FileExists
  775. );
  776. self::assertThat($filename, $constraint, $message);
  777. }
  778. /**
  779. * Asserts that a condition is true.
  780. *
  781. * @param boolean $condition
  782. * @param string $message
  783. * @throws PHPUnit_Framework_AssertionFailedError
  784. */
  785. public static function assertTrue($condition, $message = '')
  786. {
  787. self::assertThat($condition, self::isTrue(), $message);
  788. }
  789. /**
  790. * Asserts that a condition is false.
  791. *
  792. * @param boolean $condition
  793. * @param string $message
  794. * @throws PHPUnit_Framework_AssertionFailedError
  795. */
  796. public static function assertFalse($condition, $message = '')
  797. {
  798. self::assertThat($condition, self::isFalse(), $message);
  799. }
  800. /**
  801. * Asserts that a variable is not NULL.
  802. *
  803. * @param mixed $actual
  804. * @param string $message
  805. */
  806. public static function assertNotNull($actual, $message = '')
  807. {
  808. self::assertThat($actual, self::logicalNot(self::isNull()), $message);
  809. }
  810. /**
  811. * Asserts that a variable is NULL.
  812. *
  813. * @param mixed $actual
  814. * @param string $message
  815. */
  816. public static function assertNull($actual, $message = '')
  817. {
  818. self::assertThat($actual, self::isNull(), $message);
  819. }
  820. /**
  821. * Asserts that a class has a specified attribute.
  822. *
  823. * @param string $attributeName
  824. * @param string $className
  825. * @param string $message
  826. * @since Method available since Release 3.1.0
  827. */
  828. public static function assertClassHasAttribute($attributeName, $className, $message = '')
  829. {
  830. if (!is_string($attributeName)) {
  831. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  832. }
  833. if (!is_string($className) || !class_exists($className, FALSE)) {
  834. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
  835. }
  836. $constraint = new PHPUnit_Framework_Constraint_ClassHasAttribute(
  837. $attributeName
  838. );
  839. self::assertThat($className, $constraint, $message);
  840. }
  841. /**
  842. * Asserts that a class does not have a specified attribute.
  843. *
  844. * @param string $attributeName
  845. * @param string $className
  846. * @param string $message
  847. * @since Method available since Release 3.1.0
  848. */
  849. public static function assertClassNotHasAttribute($attributeName, $className, $message = '')
  850. {
  851. if (!is_string($attributeName)) {
  852. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  853. }
  854. if (!is_string($className) || !class_exists($className, FALSE)) {
  855. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
  856. }
  857. $constraint = new PHPUnit_Framework_Constraint_Not(
  858. new PHPUnit_Framework_Constraint_ClassHasAttribute($attributeName)
  859. );
  860. self::assertThat($className, $constraint, $message);
  861. }
  862. /**
  863. * Asserts that a class has a specified static attribute.
  864. *
  865. * @param string $attributeName
  866. * @param string $className
  867. * @param string $message
  868. * @since Method available since Release 3.1.0
  869. */
  870. public static function assertClassHasStaticAttribute($attributeName, $className, $message = '')
  871. {
  872. if (!is_string($attributeName)) {
  873. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  874. }
  875. if (!is_string($className) || !class_exists($className, FALSE)) {
  876. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
  877. }
  878. $constraint = new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
  879. $attributeName
  880. );
  881. self::assertThat($className, $constraint, $message);
  882. }
  883. /**
  884. * Asserts that a class does not have a specified static attribute.
  885. *
  886. * @param string $attributeName
  887. * @param string $className
  888. * @param string $message
  889. * @since Method available since Release 3.1.0
  890. */
  891. public static function assertClassNotHasStaticAttribute($attributeName, $className, $message = '')
  892. {
  893. if (!is_string($attributeName)) {
  894. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  895. }
  896. if (!is_string($className) || !class_exists($className, FALSE)) {
  897. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
  898. }
  899. $constraint = new PHPUnit_Framework_Constraint_Not(
  900. new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
  901. $attributeName
  902. )
  903. );
  904. self::assertThat($className, $constraint, $message);
  905. }
  906. /**
  907. * Asserts that an object has a specified attribute.
  908. *
  909. * @param string $attributeName
  910. * @param object $object
  911. * @param string $message
  912. * @since Method available since Release 3.0.0
  913. */
  914. public static function assertObjectHasAttribute($attributeName, $object, $message = '')
  915. {
  916. if (!is_string($attributeName)) {
  917. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  918. }
  919. if (!is_object($object)) {
  920. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
  921. }
  922. $constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute(
  923. $attributeName
  924. );
  925. self::assertThat($object, $constraint, $message);
  926. }
  927. /**
  928. * Asserts that an object does not have a specified attribute.
  929. *
  930. * @param string $attributeName
  931. * @param object $object
  932. * @param string $message
  933. * @since Method available since Release 3.0.0
  934. */
  935. public static function assertObjectNotHasAttribute($attributeName, $object, $message = '')
  936. {
  937. if (!is_string($attributeName)) {
  938. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  939. }
  940. if (!is_object($object)) {
  941. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
  942. }
  943. $constraint = new PHPUnit_Framework_Constraint_Not(
  944. new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName)
  945. );
  946. self::assertThat($object, $constraint, $message);
  947. }
  948. /**
  949. * Asserts that two variables have the same type and value.
  950. * Used on objects, it asserts that two variables reference
  951. * the same object.
  952. *
  953. * @param mixed $expected
  954. * @param mixed $actual
  955. * @param string $message
  956. */
  957. public static function assertSame($expected, $actual, $message = '')
  958. {
  959. if (is_bool($expected) && is_bool($actual)) {
  960. self::assertEquals($expected, $actual, $message);
  961. } else {
  962. $constraint = new PHPUnit_Framework_Constraint_IsIdentical(
  963. $expected
  964. );
  965. self::assertThat($actual, $constraint, $message);
  966. }
  967. }
  968. /**
  969. * Asserts that a variable and an attribute of an object have the same type
  970. * and value.
  971. *
  972. * @param mixed $expected
  973. * @param string $actualAttributeName
  974. * @param object $actualClassOrObject
  975. * @param string $message
  976. */
  977. public static function assertAttributeSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  978. {
  979. self::assertSame(
  980. $expected,
  981. self::readAttribute($actualClassOrObject, $actualAttributeName),
  982. $message
  983. );
  984. }
  985. /**
  986. * Asserts that two variables do not have the same type and value.
  987. * Used on objects, it asserts that two variables do not reference
  988. * the same object.
  989. *
  990. * @param mixed $expected
  991. * @param mixed $actual
  992. * @param string $message
  993. */
  994. public static function assertNotSame($expected, $actual, $message = '')
  995. {
  996. if (is_bool($expected) && is_bool($actual)) {
  997. self::assertNotEquals($expected, $actual, $message);
  998. } else {
  999. $constraint = new PHPUnit_Framework_Constraint_Not(
  1000. new PHPUnit_Framework_Constraint_IsIdentical($expected)
  1001. );
  1002. self::assertThat($actual, $constraint, $message);
  1003. }
  1004. }
  1005. /**
  1006. * Asserts that a variable and an attribute of an object do not have the
  1007. * same type and value.
  1008. *
  1009. * @param mixed $expected
  1010. * @param string $actualAttributeName
  1011. * @param object $actualClassOrObject
  1012. * @param string $message
  1013. */
  1014. public static function assertAttributeNotSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  1015. {
  1016. self::assertNotSame(
  1017. $expected,
  1018. self::readAttribute($actualClassOrObject, $actualAttributeName),
  1019. $message
  1020. );
  1021. }
  1022. /**
  1023. * Asserts that a variable is of a given type.
  1024. *
  1025. * @param string $expected
  1026. * @param mixed $actual
  1027. * @param string $message
  1028. * @since Method available since Release 3.5.0
  1029. */
  1030. public static function assertInstanceOf($expected, $actual, $message = '')
  1031. {
  1032. if (is_string($expected)) {
  1033. if (class_exists($expected) || interface_exists($expected)) {
  1034. $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
  1035. $expected
  1036. );
  1037. }
  1038. else {
  1039. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  1040. 1, 'class or interface name'
  1041. );
  1042. }
  1043. } else {
  1044. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1045. }
  1046. self::assertThat($actual, $constraint, $message);
  1047. }
  1048. /**
  1049. * Asserts that an attribute is of a given type.
  1050. *
  1051. * @param string $expected
  1052. * @param string $attributeName
  1053. * @param mixed $classOrObject
  1054. * @param string $message
  1055. * @since Method available since Release 3.5.0
  1056. */
  1057. public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '')
  1058. {
  1059. self::assertInstanceOf(
  1060. $expected,
  1061. self::readAttribute($classOrObject, $attributeName),
  1062. $message
  1063. );
  1064. }
  1065. /**
  1066. * Asserts that a variable is not of a given type.
  1067. *
  1068. * @param string $expected
  1069. * @param mixed $actual
  1070. * @param string $message
  1071. * @since Method available since Release 3.5.0
  1072. */
  1073. public static function assertNotInstanceOf($expected, $actual, $message = '')
  1074. {
  1075. if (is_string($expected)) {
  1076. if (class_exists($expected) || interface_exists($expected)) {
  1077. $constraint = new PHPUnit_Framework_Constraint_Not(
  1078. new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
  1079. );
  1080. }
  1081. else {
  1082. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  1083. 1, 'class or interface name'
  1084. );
  1085. }
  1086. } else {
  1087. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1088. }
  1089. self::assertThat($actual, $constraint, $message);
  1090. }
  1091. /**
  1092. * Asserts that an attribute is of a given type.
  1093. *
  1094. * @param string $expected
  1095. * @param string $attributeName
  1096. * @param mixed $classOrObject
  1097. * @param string $message
  1098. * @since Method available since Release 3.5.0
  1099. */
  1100. public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '')
  1101. {
  1102. self::assertNotInstanceOf(
  1103. $expected,
  1104. self::readAttribute($classOrObject, $attributeName),
  1105. $message
  1106. );
  1107. }
  1108. /**
  1109. * Asserts that a variable is of a given type.
  1110. *
  1111. * @param string $expected
  1112. * @param mixed $actual
  1113. * @param string $message
  1114. * @since Method available since Release 3.5.0
  1115. */
  1116. public static function assertInternalType($expected, $actual, $message = '')
  1117. {
  1118. if (is_string($expected)) {
  1119. $constraint = new PHPUnit_Framework_Constraint_IsType(
  1120. $expected
  1121. );
  1122. } else {
  1123. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1124. }
  1125. self::assertThat($actual, $constraint, $message);
  1126. }
  1127. /**
  1128. * Asserts that an attribute is of a given type.
  1129. *
  1130. * @param string $expected
  1131. * @param string $attributeName
  1132. * @param mixed $classOrObject
  1133. * @param string $message
  1134. * @since Method available since Release 3.5.0
  1135. */
  1136. public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '')
  1137. {
  1138. self::assertInternalType(
  1139. $expected,
  1140. self::readAttribute($classOrObject, $attributeName),
  1141. $message
  1142. );
  1143. }
  1144. /**
  1145. * Asserts that a variable is not of a given type.
  1146. *
  1147. * @param string $expected
  1148. * @param mixed $actual
  1149. * @param string $message
  1150. * @since Method available since Release 3.5.0
  1151. */
  1152. public static function assertNotInternalType($expected, $actual, $message = '')
  1153. {
  1154. if (is_string($expected)) {
  1155. $constraint = new PHPUnit_Framework_Constraint_Not(
  1156. new PHPUnit_Framework_Constraint_IsType($expected)
  1157. );
  1158. } else {
  1159. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1160. }
  1161. self::assertThat($actual, $constraint, $message);
  1162. }
  1163. /**
  1164. * Asserts that an attribute is of a given type.
  1165. *
  1166. * @param string $expected
  1167. * @param string $attributeName
  1168. * @param mixed $classOrObject
  1169. * @param string $message
  1170. * @since Method available since Release 3.5.0
  1171. */
  1172. public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '')
  1173. {
  1174. self::assertNotInternalType(
  1175. $expected,
  1176. self::readAttribute($classOrObject, $attributeName),
  1177. $message
  1178. );
  1179. }
  1180. /**
  1181. * Asserts that a string matches a given regular expression.
  1182. *
  1183. * @param string $pattern
  1184. * @param string $string
  1185. * @param string $message
  1186. */
  1187. public static function assertRegExp($pattern, $string, $message = '')
  1188. {
  1189. if (!is_string($pattern)) {
  1190. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1191. }
  1192. if (!is_string($string)) {
  1193. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1194. }
  1195. $constraint = new PHPUnit_Framework_Constraint_PCREMatch($pattern);
  1196. self::assertThat($string, $constraint, $message);
  1197. }
  1198. /**
  1199. * Asserts that a string does not match a given regular expression.
  1200. *
  1201. * @param string $pattern
  1202. * @param string $string
  1203. * @param string $message
  1204. * @since Method available since Release 2.1.0
  1205. */
  1206. public static function assertNotRegExp($pattern, $string, $message = '')
  1207. {
  1208. if (!is_string($pattern)) {
  1209. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1210. }
  1211. if (!is_string($string)) {
  1212. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1213. }
  1214. $constraint = new PHPUnit_Framework_Constraint_Not(
  1215. new PHPUnit_Framework_Constraint_PCREMatch($pattern)
  1216. );
  1217. self::assertThat($string, $constraint, $message);
  1218. }
  1219. /**
  1220. * Assert that the size of two arrays (or `Countable` or `Iterator` objects)
  1221. * is the same.
  1222. *
  1223. * @param integer $expected
  1224. * @param mixed $actual
  1225. * @param string $message
  1226. */
  1227. public function assertSameSize($expected, $actual, $message = '')
  1228. {
  1229. if (!$expected instanceof Countable &&
  1230. !$expected instanceof Iterator &&
  1231. !is_array($expected)) {
  1232. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable');
  1233. }
  1234. if (!$actual instanceof Countable &&
  1235. !$actual instanceof Iterator &&
  1236. !is_array($actual)) {
  1237. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable');
  1238. }
  1239. self::assertThat(
  1240. $actual,
  1241. new PHPUnit_Framework_Constraint_SameSize($expected),
  1242. $message
  1243. );
  1244. }
  1245. /**
  1246. * Assert that the size of two arrays (or `Countable` or `Iterator` objects)
  1247. * is not the same.
  1248. *
  1249. * @param integer $expected
  1250. * @param mixed $actual
  1251. * @param string $message
  1252. */
  1253. public function assertNotSameSize($expected, $actual, $message = '')
  1254. {
  1255. if (!$expected instanceof Countable &&
  1256. !$expected instanceof Iterator &&
  1257. !is_array($expected)) {
  1258. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable');
  1259. }
  1260. if (!$actual instanceof Countable &&
  1261. !$actual instanceof Iterator &&
  1262. !is_array($actual)) {
  1263. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable');
  1264. }
  1265. $constraint = new PHPUnit_Framework_Constraint_Not(
  1266. new PHPUnit_Framework_Constraint_SameSize($expected)
  1267. );
  1268. self::assertThat($actual, $constraint, $message);
  1269. }
  1270. /**
  1271. * Asserts that a string matches a given format string.
  1272. *
  1273. * @param string $format
  1274. * @param string $string
  1275. * @param string $message
  1276. * @since Method available since Release 3.5.0
  1277. */
  1278. public static function assertStringMatchesFormat($format, $string, $message = '')
  1279. {
  1280. if (!is_string($format)) {
  1281. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1282. }
  1283. if (!is_string($string)) {
  1284. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1285. }
  1286. $constraint = new PHPUnit_Framework_Constraint_StringMatches($format);
  1287. self::assertThat($string, $constraint, $message);
  1288. }
  1289. /**
  1290. * Asserts that a string does not match a given format string.
  1291. *
  1292. * @param string $format
  1293. * @param string $string
  1294. * @param string $message
  1295. * @since Method available since Release 3.5.0
  1296. */
  1297. public static function assertStringNotMatchesFormat($format, $string, $message = '')
  1298. {
  1299. if (!is_string($format)) {
  1300. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1301. }
  1302. if (!is_string($string)) {
  1303. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1304. }
  1305. $constraint = new PHPUnit_Framework_Constraint_Not(
  1306. new PHPUnit_Framework_Constraint_StringMatches($format)
  1307. );
  1308. self::assertThat($string, $constraint, $message);
  1309. }
  1310. /**
  1311. * Asserts that a string matches a given format file.
  1312. *
  1313. * @param string $formatFile
  1314. * @param string $string
  1315. * @param string $message
  1316. * @since Method available since Release 3.5.0
  1317. */
  1318. public static function assertStringMatchesFormatFile($formatFile, $string, $message = '')
  1319. {
  1320. self::assertFileExists($formatFile, $message);
  1321. if (!is_string($string)) {
  1322. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1323. }
  1324. $constraint = new PHPUnit_Framework_Constraint_StringMatches(
  1325. file_get_contents($formatFile)
  1326. );
  1327. self::assertThat($string, $constraint, $message);
  1328. }
  1329. /**
  1330. * Asserts that a string does not match a given format string.
  1331. *
  1332. * @param string $formatFile
  1333. * @param string $string
  1334. * @param string $message
  1335. * @since Method available since Release 3.5.0
  1336. */
  1337. public static function assertStringNotMatchesFormatFile($formatFile, $string, $message = '')
  1338. {
  1339. self::assertFileExists($formatFile, $message);
  1340. if (!is_string($string)) {
  1341. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1342. }
  1343. $constraint = new PHPUnit_Framework_Constraint_Not(
  1344. new PHPUnit_Framework_Constraint_StringMatches(
  1345. file_get_contents($formatFile)
  1346. )
  1347. );
  1348. self::assertThat($string, $constraint, $message);
  1349. }
  1350. /**
  1351. * Asserts that a string starts with a given prefix.
  1352. *
  1353. * @param string $prefix
  1354. * @param string $string
  1355. * @param string $message
  1356. * @since Method available since Release 3.4.0
  1357. */
  1358. public static function assertStringStartsWith($prefix, $string, $message = '')
  1359. {
  1360. if (!is_string($prefix)) {
  1361. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1362. }
  1363. if (!is_string($string)) {
  1364. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1365. }
  1366. $constraint = new PHPUnit_Framework_Constraint_StringStartsWith(
  1367. $prefix
  1368. );
  1369. self::assertThat($string, $constraint, $message);
  1370. }
  1371. /**
  1372. * Asserts that a string starts not with a given prefix.
  1373. *
  1374. * @param string $prefix
  1375. * @param string $string
  1376. * @param string $message
  1377. * @since Method available since Release 3.4.0
  1378. */
  1379. public static function assertStringStartsNotWith($prefix, $string, $message = '')
  1380. {
  1381. if (!is_string($prefix)) {
  1382. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1383. }
  1384. if (!is_string($string)) {
  1385. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1386. }
  1387. $constraint = new PHPUnit_Framework_Constraint_Not(
  1388. new PHPUnit_Framework_Constraint_StringStartsWith($prefix)
  1389. );
  1390. self::assertThat($string, $constraint, $message);
  1391. }
  1392. /**
  1393. * Asserts that a string ends with a given prefix.
  1394. *
  1395. * @param string $suffix
  1396. * @param string $string
  1397. * @param string $message
  1398. * @since Method available since Release 3.4.0
  1399. */
  1400. public static function assertStringEndsWith($suffix, $string, $message = '')
  1401. {
  1402. if (!is_string($suffix)) {
  1403. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1404. }
  1405. if (!is_string($string)) {
  1406. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1407. }
  1408. $constraint = new PHPUnit_Framework_Constraint_StringEndsWith($suffix);
  1409. self::assertThat($string, $constraint, $message);
  1410. }
  1411. /**
  1412. * Asserts that a string ends not with a given prefix.
  1413. *
  1414. * @param string $suffix
  1415. * @param string $string
  1416. * @param string $message
  1417. * @since Method available since Release 3.4.0
  1418. */
  1419. public static function assertStringEndsNotWith($suffix, $string, $message = '')
  1420. {
  1421. if (!is_string($suffix)) {
  1422. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1423. }
  1424. if (!is_string($string)) {
  1425. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1426. }
  1427. $constraint = new PHPUnit_Framework_Constraint_Not(
  1428. new PHPUnit_Framework_Constraint_StringEndsWith($suffix)
  1429. );
  1430. self::assertThat($string, $constraint, $message);
  1431. }
  1432. /**
  1433. * Asserts that two XML files are equal.
  1434. *
  1435. * @param string $expectedFile
  1436. * @param string $actualFile
  1437. * @param string $message
  1438. * @since Method available since Release 3.1.0
  1439. */
  1440. public static function assertXmlFileEqualsXmlFile($expectedFile, $actualFile, $message = '')
  1441. {
  1442. self::assertFileExists($expectedFile);
  1443. self::assertFileExists($actualFile);
  1444. $expected = new DOMDocument;
  1445. $expected->preserveWhiteSpace = FALSE;
  1446. $expected->load($expectedFile);
  1447. $actual = new DOMDocument;
  1448. $actual->preserveWhiteSpace = FALSE;
  1449. $actual->load($actualFile);
  1450. self::assertEquals($expected, $actual, $message);
  1451. }
  1452. /**
  1453. * Asserts that two XML files are not equal.
  1454. *
  1455. * @param string $expectedFile
  1456. * @param string $actualFile
  1457. * @param string $message
  1458. * @since Method available since Release 3.1.0
  1459. */
  1460. public static function assertXmlFileNotEqualsXmlFile($expectedFile, $actualFile, $message = '')
  1461. {
  1462. self::assertFileExists($expectedFile);
  1463. self::assertFileExists($actualFile);
  1464. $expected = new DOMDocument;
  1465. $expected->preserveWhiteSpace = FALSE;
  1466. $expected->load($expectedFile);
  1467. $actual = new DOMDocument;
  1468. $actual->preserveWhiteSpace = FALSE;
  1469. $actual->load($actualFile);
  1470. self::assertNotEquals($expected, $actual, $message);
  1471. }
  1472. /**
  1473. * Asserts that two XML documents are equal.
  1474. *
  1475. * @param string $expectedFile
  1476. * @param string $actualXml
  1477. * @param string $message
  1478. * @since Method available since Release 3.3.0
  1479. */
  1480. public static function assertXmlStringEqualsXmlFile($expectedFile, $actualXml, $message = '')
  1481. {
  1482. self::assertFileExists($expectedFile);
  1483. $expected = new DOMDocument;
  1484. $expected->preserveWhiteSpace = FALSE;
  1485. $expected->load($expectedFile);
  1486. $actual = new DOMDocument;
  1487. $actual->preserveWhiteSpace = FALSE;
  1488. $actual->loadXML($actualXml);
  1489. self::assertEquals($expected, $actual, $message);
  1490. }
  1491. /**
  1492. * Asserts that two XML documents are not equal.
  1493. *
  1494. * @param string $expectedFile
  1495. * @param string $actualXml
  1496. * @param string $message
  1497. * @since Method available since Release 3.3.0
  1498. */
  1499. public static function assertXmlStringNotEqualsXmlFile($expectedFile, $actualXml, $message = '')
  1500. {
  1501. self::assertFileExists($expectedFile);
  1502. $expected = new DOMDocument;
  1503. $expected->preserveWhiteSpace = FALSE;
  1504. $expected->load($expectedFile);
  1505. $actual = new DOMDocument;
  1506. $actual->preserveWhiteSpace = FALSE;
  1507. $actual->loadXML($actualXml);
  1508. self::assertNotEquals($expected, $actual, $message);
  1509. }
  1510. /**
  1511. * Asserts that two XML documents are equal.
  1512. *
  1513. * @param string $expectedXml
  1514. * @param string $actualXml
  1515. * @param string $message
  1516. * @since Method available since Release 3.1.0
  1517. */
  1518. public static function assertXmlStringEqualsXmlString($expectedXml, $actualXml, $message = '')
  1519. {
  1520. $expected = new DOMDocument;
  1521. $expected->preserveWhiteSpace = FALSE;
  1522. $expected->loadXML($expectedXml);
  1523. $actual = new DOMDocument;
  1524. $actual->preserveWhiteSpace = FALSE;
  1525. $actual->loadXML($actualXml);
  1526. self::assertEquals($expected, $actual, $message);
  1527. }
  1528. /**
  1529. * Asserts that two XML documents are not equal.
  1530. *
  1531. * @param string $expectedXml
  1532. * @param string $actualXml
  1533. * @param string $message
  1534. * @since Method available since Release 3.1.0
  1535. */
  1536. public static function assertXmlStringNotEqualsXmlString($expectedXml, $actualXml, $message = '')
  1537. {
  1538. $expected = new DOMDocument;
  1539. $expected->preserveWhiteSpace = FALSE;
  1540. $expected->loadXML($expectedXml);
  1541. $actual = new DOMDocument;
  1542. $actual->preserveWhiteSpace = FALSE;
  1543. $actual->loadXML($actualXml);
  1544. self::assertNotEquals($expected, $actual, $message);
  1545. }
  1546. /**
  1547. * Asserts that a hierarchy of DOMElements matches.
  1548. *
  1549. * @param DOMElement $expectedElement
  1550. * @param DOMElement $actualElement
  1551. * @param boolean $checkAttributes
  1552. * @param string $message
  1553. * @author Mattis Stordalen Flister <mattis@xait.no>
  1554. * @since Method available since Release 3.3.0
  1555. */
  1556. public static function assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement, $checkAttributes = FALSE, $message = '')
  1557. {
  1558. self::assertEquals(
  1559. $expectedElement->tagName,
  1560. $actualElement->tagName,
  1561. $message
  1562. );
  1563. if ($checkAttributes) {
  1564. self::assertEquals(
  1565. $expectedElement->attributes->length,
  1566. $actualElement->attributes->length,
  1567. sprintf(
  1568. '%s%sNumber of attributes on node "%s" does not match',
  1569. $message,
  1570. !empty($message) ? "\n" : '',
  1571. $expectedElement->tagName
  1572. )
  1573. );
  1574. for ($i = 0 ; $i < $expectedElement->attributes->length; $i++) {
  1575. $expectedAttribute = $expectedElement->attributes->item($i);
  1576. $actualAttribute = $actualElement->attributes->getNamedItem(
  1577. $expectedAttribute->name
  1578. );
  1579. if (!$actualAttribute) {
  1580. self::fail(
  1581. sprintf(
  1582. '%s%sCould not find attribute "%s" on node "%s"',
  1583. $message,
  1584. !empty($message) ? "\n" : '',
  1585. $expectedAttribute->name,
  1586. $expectedElement->tagName
  1587. )
  1588. );
  1589. }
  1590. }
  1591. }
  1592. PHPUnit_Util_XML::removeCharacterDataNodes($expectedElement);
  1593. PHPUnit_Util_XML::removeCharacterDataNodes($actualElement);
  1594. self::assertEquals(
  1595. $expectedElement->childNodes->length,
  1596. $actualElement->childNodes->length,
  1597. sprintf(
  1598. '%s%sNumber of child nodes of "%s" differs',
  1599. $message,
  1600. !empty($message) ? "\n" : '',
  1601. $expectedElement->tagName
  1602. )
  1603. );
  1604. for ($i = 0; $i < $expectedElement->childNodes->length; $i++) {
  1605. self::assertEqualXMLStructure(
  1606. $expectedElement->childNodes->item($i),
  1607. $actualElement->childNodes->item($i),
  1608. $checkAttributes,
  1609. $message
  1610. );
  1611. }
  1612. }
  1613. /**
  1614. * Assert the presence, absence, or count of elements in a document matching
  1615. * the CSS $selector, regardless of the contents of those elements.
  1616. *
  1617. * The first argument, $selector, is the CSS selector used to match
  1618. * the elements in the $actual document.
  1619. *
  1620. * The second argument, $count, can be either boolean or numeric.
  1621. * When boolean, it asserts for presence of elements matching the selector
  1622. * (TRUE) or absence of elements (FALSE).
  1623. * When numeric, it asserts the count of elements.
  1624. *
  1625. * assertSelectCount("#binder", true, $xml); // any?
  1626. * assertSelectCount(".binder", 3, $xml); // exactly 3?
  1627. *
  1628. * @param array $selector
  1629. * @param integer $count
  1630. * @param mixed $actual
  1631. * @param string $message
  1632. * @param boolean $isHtml
  1633. * @since Method available since Release 3.3.0
  1634. * @author Mike Naberezny <mike@maintainable.com>
  1635. * @author Derek DeVries <derek@maintainable.com>
  1636. */
  1637. public static function assertSelectCount($selector, $count, $actual, $message = '', $isHtml = TRUE)
  1638. {
  1639. self::assertSelectEquals(
  1640. $selector, TRUE, $count, $actual, $message, $isHtml
  1641. );
  1642. }
  1643. /**
  1644. * assertSelectRegExp("#binder .name", "/Mike|Derek/", true, $xml); // any?
  1645. * assertSelectRegExp("#binder .name", "/Mike|Derek/", 3, $xml); // 3?
  1646. *
  1647. * @param array $selector
  1648. * @param string $pattern
  1649. * @param integer $count
  1650. * @param mixed $actual
  1651. * @param string $message
  1652. * @param boolean $isHtml
  1653. * @since Method available since Release 3.3.0
  1654. * @author Mike Naberezny <mike@maintainable.com>
  1655. * @author Derek DeVries <derek@maintainable.com>
  1656. */
  1657. public static function assertSelectRegExp($selector, $pattern, $count, $actual, $message = '', $isHtml = TRUE)
  1658. {
  1659. self::assertSelectEquals(
  1660. $selector, "regexp:$pattern", $count, $actual, $message, $isHtml
  1661. );
  1662. }
  1663. /**
  1664. * assertSelectEquals("#binder .name", "Chuck", true, $xml); // any?
  1665. * assertSelectEquals("#binder .name", "Chuck", false, $xml); // none?
  1666. *
  1667. * @param array $selector
  1668. * @param string $content
  1669. * @param integer $count
  1670. * @param mixed $actual
  1671. * @param string $message
  1672. * @param boolean $isHtml
  1673. * @since Method available since Release 3.3.0
  1674. * @author Mike Naberezny <mike@maintainable.com>
  1675. * @author Derek DeVries <derek@maintainable.com>
  1676. */
  1677. public static function assertSelectEquals($selector, $content, $count, $actual, $message = '', $isHtml = TRUE)
  1678. {
  1679. $tags = PHPUnit_Util_XML::cssSelect(
  1680. $selector, $content, $actual, $isHtml
  1681. );
  1682. // assert specific number of elements
  1683. if (is_numeric($count)) {
  1684. $counted = $tags ? count($tags) : 0;
  1685. self::assertEquals($count, $counted, $message);
  1686. }
  1687. // assert any elements exist if true, assert no elements exist if false
  1688. else if (is_bool($count)) {
  1689. $any = count($tags) > 0 && $tags[0] instanceof DOMNode;
  1690. if ($count) {
  1691. self::assertTrue($any, $message);
  1692. } else {
  1693. self::assertFalse($any, $message);
  1694. }
  1695. }
  1696. // check for range number of elements
  1697. else if (is_array($count) &&
  1698. (isset($count['>']) || isset($count['<']) ||
  1699. isset($count['>=']) || isset($count['<=']))) {
  1700. $counted = $tags ? count($tags) : 0;
  1701. if (isset($count['>'])) {
  1702. self::assertTrue($counted > $count['>'], $message);
  1703. }
  1704. if (isset($count['>='])) {
  1705. self::assertTrue($counted >= $count['>='], $message);
  1706. }
  1707. if (isset($count['<'])) {
  1708. self::assertTrue($counted < $count['<'], $message);
  1709. }
  1710. if (isset($count['<='])) {
  1711. self::assertTrue($counted <= $count['<='], $message);
  1712. }
  1713. } else {
  1714. throw new InvalidArgumentException();
  1715. }
  1716. }
  1717. /**
  1718. * Evaluate an HTML or XML string and assert its structure and/or contents.
  1719. *
  1720. * The first argument ($matcher) is an associative array that specifies the
  1721. * match criteria for the assertion:
  1722. *
  1723. * - `id` : the node with the given id attribute must match the
  1724. * corresponsing value.
  1725. * - `tag` : the node type must match the corresponding value.
  1726. * - `attributes` : a hash. The node's attributres must match the
  1727. * corresponsing values in the hash.
  1728. * - `content` : The text content must match the given value.
  1729. * - `parent` : a hash. The node's parent must match the
  1730. * corresponsing hash.
  1731. * - `child` : a hash. At least one of the node's immediate children
  1732. * must meet the criteria described by the hash.
  1733. * - `ancestor` : a hash. At least one of the node's ancestors must
  1734. * meet the criteria described by the hash.
  1735. * - `descendant` : a hash. At least one of the node's descendants must
  1736. * meet the criteria described by the hash.
  1737. * - `children` : a hash, for counting children of a node.
  1738. * Accepts the keys:
  1739. * - `count` : a number which must equal the number of children
  1740. * that match
  1741. * - `less_than` : the number of matching children must be greater
  1742. * than this number
  1743. * - `greater_than` : the number of matching children must be less than
  1744. * this number
  1745. * - `only` : another hash consisting of the keys to use to match
  1746. * on the children, and only matching children will be
  1747. * counted
  1748. *
  1749. * <code>
  1750. * // Matcher that asserts that there is an element with an id="my_id".
  1751. * $matcher = array('id' => 'my_id');
  1752. *
  1753. * // Matcher that asserts that there is a "span" tag.
  1754. * $matcher = array('tag' => 'span');
  1755. *
  1756. * // Matcher that asserts that there is a "span" tag with the content
  1757. * // "Hello World".
  1758. * $matcher = array('tag' => 'span', 'content' => 'Hello World');
  1759. *
  1760. * // Matcher that asserts that there is a "span" tag with content matching
  1761. * // the regular expression pattern.
  1762. * $matcher = array('tag' => 'span', 'content' => 'regexp:/Try P(HP|ython)/');
  1763. *
  1764. * // Matcher that asserts that there is a "span" with an "list" class
  1765. * // attribute.
  1766. * $matcher = array(
  1767. * 'tag' => 'span',
  1768. * 'attributes' => array('class' => 'list')
  1769. * );
  1770. *
  1771. * // Matcher that asserts that there is a "span" inside of a "div".
  1772. * $matcher = array(
  1773. * 'tag' => 'span',
  1774. * 'parent' => array('tag' => 'div')
  1775. * );
  1776. *
  1777. * // Matcher that asserts that there is a "span" somewhere inside a
  1778. * // "table".
  1779. * $matcher = array(
  1780. * 'tag' => 'span',
  1781. * 'ancestor' => array('tag' => 'table')
  1782. * );
  1783. *
  1784. * // Matcher that asserts that there is a "span" with at least one "em"
  1785. * // child.
  1786. * $matcher = array(
  1787. * 'tag' => 'span',
  1788. * 'child' => array('tag' => 'em')
  1789. * );
  1790. *
  1791. * // Matcher that asserts that there is a "span" containing a (possibly
  1792. * // nested) "strong" tag.
  1793. * $matcher = array(
  1794. * 'tag' => 'span',
  1795. * 'descendant' => array('tag' => 'strong')
  1796. * );
  1797. *
  1798. * // Matcher that asserts that there is a "span" containing 5-10 "em" tags
  1799. * // as immediate children.
  1800. * $matcher = array(
  1801. * 'tag' => 'span',
  1802. * 'children' => array(
  1803. * 'less_than' => 11,
  1804. * 'greater_than' => 4,
  1805. * 'only' => array('tag' => 'em')
  1806. * )
  1807. * );
  1808. *
  1809. * // Matcher that asserts that there is a "div", with an "ul" ancestor and
  1810. * // a "li" parent (with class="enum"), and containing a "span" descendant
  1811. * // that contains an element with id="my_test" and the text "Hello World".
  1812. * $matcher = array(
  1813. * 'tag' => 'div',
  1814. * 'ancestor' => array('tag' => 'ul'),
  1815. * 'parent' => array(
  1816. * 'tag' => 'li',
  1817. * 'attributes' => array('class' => 'enum')
  1818. * ),
  1819. * 'descendant' => array(
  1820. * 'tag' => 'span',
  1821. * 'child' => array(
  1822. * 'id' => 'my_test',
  1823. * 'content' => 'Hello World'
  1824. * )
  1825. * )
  1826. * );
  1827. *
  1828. * // Use assertTag() to apply a $matcher to a piece of $html.
  1829. * $this->assertTag($matcher, $html);
  1830. *
  1831. * // Use assertTag() to apply a $matcher to a piece of $xml.
  1832. * $this->assertTag($matcher, $xml, '', FALSE);
  1833. * </code>
  1834. *
  1835. * The second argument ($actual) is a string containing either HTML or
  1836. * XML text to be tested.
  1837. *
  1838. * The third argument ($message) is an optional message that will be
  1839. * used if the assertion fails.
  1840. *
  1841. * The fourth argument ($html) is an optional flag specifying whether
  1842. * to load the $actual string into a DOMDocument using the HTML or
  1843. * XML load strategy. It is TRUE by default, which assumes the HTML
  1844. * load strategy. In many cases, this will be acceptable for XML as well.
  1845. *
  1846. * @param array $matcher
  1847. * @param string $actual
  1848. * @param string $message
  1849. * @param boolean $isHtml
  1850. * @since Method available since Release 3.3.0
  1851. * @author Mike Naberezny <mike@maintainable.com>
  1852. * @author Derek DeVries <derek@maintainable.com>
  1853. */
  1854. public static function assertTag($matcher, $actual, $message = '', $isHtml = TRUE)
  1855. {
  1856. $dom = PHPUnit_Util_XML::load($actual, $isHtml);
  1857. $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
  1858. $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
  1859. self::assertTrue($matched, $message);
  1860. }
  1861. /**
  1862. * This assertion is the exact opposite of assertTag().
  1863. *
  1864. * Rather than asserting that $matcher results in a match, it asserts that
  1865. * $matcher does not match.
  1866. *
  1867. * @param array $matcher
  1868. * @param string $actual
  1869. * @param string $message
  1870. * @param boolean $isHtml
  1871. * @since Method available since Release 3.3.0
  1872. * @author Mike Naberezny <mike@maintainable.com>
  1873. * @author Derek DeVries <derek@maintainable.com>
  1874. */
  1875. public static function assertNotTag($matcher, $actual, $message = '', $isHtml = TRUE)
  1876. {
  1877. $dom = PHPUnit_Util_XML::load($actual, $isHtml);
  1878. $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
  1879. $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
  1880. self::assertFalse($matched, $message);
  1881. }
  1882. /**
  1883. * Evaluates a PHPUnit_Framework_Constraint matcher object.
  1884. *
  1885. * @param mixed $value
  1886. * @param PHPUnit_Framework_Constraint $constraint
  1887. * @param string $message
  1888. * @since Method available since Release 3.0.0
  1889. */
  1890. public static function assertThat($value, PHPUnit_Framework_Constraint $constraint, $message = '')
  1891. {
  1892. self::$count += count($constraint);
  1893. $constraint->evaluate($value, $message);
  1894. }
  1895. /**
  1896. * Returns a PHPUnit_Framework_Constraint_And matcher object.
  1897. *
  1898. * @return PHPUnit_Framework_Constraint_And
  1899. * @since Method available since Release 3.0.0
  1900. */
  1901. public static function logicalAnd()
  1902. {
  1903. $constraints = func_get_args();
  1904. $constraint = new PHPUnit_Framework_Constraint_And;
  1905. $constraint->setConstraints($constraints);
  1906. return $constraint;
  1907. }
  1908. /**
  1909. * Returns a PHPUnit_Framework_Constraint_Or matcher object.
  1910. *
  1911. * @return PHPUnit_Framework_Constraint_Or
  1912. * @since Method available since Release 3.0.0
  1913. */
  1914. public static function logicalOr()
  1915. {
  1916. $constraints = func_get_args();
  1917. $constraint = new PHPUnit_Framework_Constraint_Or;
  1918. $constraint->setConstraints($constraints);
  1919. return $constraint;
  1920. }
  1921. /**
  1922. * Returns a PHPUnit_Framework_Constraint_Not matcher object.
  1923. *
  1924. * @param PHPUnit_Framework_Constraint $constraint
  1925. * @return PHPUnit_Framework_Constraint_Not
  1926. * @since Method available since Release 3.0.0
  1927. */
  1928. public static function logicalNot(PHPUnit_Framework_Constraint $constraint)
  1929. {
  1930. return new PHPUnit_Framework_Constraint_Not($constraint);
  1931. }
  1932. /**
  1933. * Returns a PHPUnit_Framework_Constraint_Xor matcher object.
  1934. *
  1935. * @return PHPUnit_Framework_Constraint_Xor
  1936. * @since Method available since Release 3.0.0
  1937. */
  1938. public static function logicalXor()
  1939. {
  1940. $constraints = func_get_args();
  1941. $constraint = new PHPUnit_Framework_Constraint_Xor;
  1942. $constraint->setConstraints($constraints);
  1943. return $constraint;
  1944. }
  1945. /**
  1946. * Returns a PHPUnit_Framework_Constraint_IsAnything matcher object.
  1947. *
  1948. * @return PHPUnit_Framework_Constraint_IsAnything
  1949. * @since Method available since Release 3.0.0
  1950. */
  1951. public static function anything()
  1952. {
  1953. return new PHPUnit_Framework_Constraint_IsAnything;
  1954. }
  1955. /**
  1956. * Returns a PHPUnit_Framework_Constraint_IsTrue matcher object.
  1957. *
  1958. * @return PHPUnit_Framework_Constraint_IsTrue
  1959. * @since Method available since Release 3.3.0
  1960. */
  1961. public static function isTrue()
  1962. {
  1963. return new PHPUnit_Framework_Constraint_IsTrue;
  1964. }
  1965. /**
  1966. * Returns a PHPUnit_Framework_Constraint_IsFalse matcher object.
  1967. *
  1968. * @return PHPUnit_Framework_Constraint_IsFalse
  1969. * @since Method available since Release 3.3.0
  1970. */
  1971. public static function isFalse()
  1972. {
  1973. return new PHPUnit_Framework_Constraint_IsFalse;
  1974. }
  1975. /**
  1976. * Returns a PHPUnit_Framework_Constraint_IsNull matcher object.
  1977. *
  1978. * @return PHPUnit_Framework_Constraint_IsNull
  1979. * @since Method available since Release 3.3.0
  1980. */
  1981. public static function isNull()
  1982. {
  1983. return new PHPUnit_Framework_Constraint_IsNull;
  1984. }
  1985. /**
  1986. * Returns a PHPUnit_Framework_Constraint_Attribute matcher object.
  1987. *
  1988. * @param PHPUnit_Framework_Constraint $constraint
  1989. * @param string $attributeName
  1990. * @return PHPUnit_Framework_Constraint_Attribute
  1991. * @since Method available since Release 3.1.0
  1992. */
  1993. public static function attribute(PHPUnit_Framework_Constraint $constraint, $attributeName)
  1994. {
  1995. return new PHPUnit_Framework_Constraint_Attribute(
  1996. $constraint, $attributeName
  1997. );
  1998. }
  1999. /**
  2000. * Returns a PHPUnit_Framework_Constraint_TraversableContains matcher
  2001. * object.
  2002. *
  2003. * @param mixed $value
  2004. * @param boolean $checkForObjectIdentity
  2005. * @return PHPUnit_Framework_Constraint_TraversableContains
  2006. * @since Method available since Release 3.0.0
  2007. */
  2008. public static function contains($value, $checkForObjectIdentity = TRUE)
  2009. {
  2010. return new PHPUnit_Framework_Constraint_TraversableContains($value, $checkForObjectIdentity);
  2011. }
  2012. /**
  2013. * Returns a PHPUnit_Framework_Constraint_TraversableContainsOnly matcher
  2014. * object.
  2015. *
  2016. * @param string $type
  2017. * @return PHPUnit_Framework_Constraint_TraversableContainsOnly
  2018. * @since Method available since Release 3.1.4
  2019. */
  2020. public static function containsOnly($type)
  2021. {
  2022. return new PHPUnit_Framework_Constraint_TraversableContainsOnly($type);
  2023. }
  2024. /**
  2025. * Returns a PHPUnit_Framework_Constraint_ArrayHasKey matcher object.
  2026. *
  2027. * @param mixed $key
  2028. * @return PHPUnit_Framework_Constraint_ArrayHasKey
  2029. * @since Method available since Release 3.0.0
  2030. */
  2031. public static function arrayHasKey($key)
  2032. {
  2033. return new PHPUnit_Framework_Constraint_ArrayHasKey($key);
  2034. }
  2035. /**
  2036. * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object.
  2037. *
  2038. * @param mixed $value
  2039. * @param float $delta
  2040. * @param integer $maxDepth
  2041. * @param boolean $canonicalize
  2042. * @param boolean $ignoreCase
  2043. * @return PHPUnit_Framework_Constraint_IsEqual
  2044. * @since Method available since Release 3.0.0
  2045. */
  2046. public static function equalTo($value, $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
  2047. {
  2048. return new PHPUnit_Framework_Constraint_IsEqual(
  2049. $value, $delta, $maxDepth, $canonicalize, $ignoreCase
  2050. );
  2051. }
  2052. /**
  2053. * Returns a PHPUnit_Framework_Constraint_IsEqual matcher object
  2054. * that is wrapped in a PHPUnit_Framework_Constraint_Attribute matcher
  2055. * object.
  2056. *
  2057. * @param string $attributeName
  2058. * @param mixed $value
  2059. * @param float $delta
  2060. * @param integer $maxDepth
  2061. * @param boolean $canonicalize
  2062. * @param boolean $ignoreCase
  2063. * @return PHPUnit_Framework_Constraint_Attribute
  2064. * @since Method available since Release 3.1.0
  2065. */
  2066. public static function attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
  2067. {
  2068. return self::attribute(
  2069. self::equalTo(
  2070. $value, $delta, $maxDepth, $canonicalize, $ignoreCase
  2071. ),
  2072. $attributeName
  2073. );
  2074. }
  2075. /**
  2076. * Returns a PHPUnit_Framework_Constraint_IsEmpty matcher object.
  2077. *
  2078. * @return PHPUnit_Framework_Constraint_IsEmpty
  2079. * @since Method available since Release 3.5.0
  2080. */
  2081. public static function isEmpty()
  2082. {
  2083. return new PHPUnit_Framework_Constraint_IsEmpty;
  2084. }
  2085. /**
  2086. * Returns a PHPUnit_Framework_Constraint_FileExists matcher object.
  2087. *
  2088. * @return PHPUnit_Framework_Constraint_FileExists
  2089. * @since Method available since Release 3.0.0
  2090. */
  2091. public static function fileExists()
  2092. {
  2093. return new PHPUnit_Framework_Constraint_FileExists;
  2094. }
  2095. /**
  2096. * Returns a PHPUnit_Framework_Constraint_GreaterThan matcher object.
  2097. *
  2098. * @param mixed $value
  2099. * @return PHPUnit_Framework_Constraint_GreaterThan
  2100. * @since Method available since Release 3.0.0
  2101. */
  2102. public static function greaterThan($value)
  2103. {
  2104. return new PHPUnit_Framework_Constraint_GreaterThan($value);
  2105. }
  2106. /**
  2107. * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps
  2108. * a PHPUnit_Framework_Constraint_IsEqual and a
  2109. * PHPUnit_Framework_Constraint_GreaterThan matcher object.
  2110. *
  2111. * @param mixed $value
  2112. * @return PHPUnit_Framework_Constraint_Or
  2113. * @since Method available since Release 3.1.0
  2114. */
  2115. public static function greaterThanOrEqual($value)
  2116. {
  2117. return self::logicalOr(
  2118. new PHPUnit_Framework_Constraint_IsEqual($value),
  2119. new PHPUnit_Framework_Constraint_GreaterThan($value)
  2120. );
  2121. }
  2122. /**
  2123. * Returns a PHPUnit_Framework_Constraint_ClassHasAttribute matcher object.
  2124. *
  2125. * @param string $attributeName
  2126. * @return PHPUnit_Framework_Constraint_ClassHasAttribute
  2127. * @since Method available since Release 3.1.0
  2128. */
  2129. public static function classHasAttribute($attributeName)
  2130. {
  2131. return new PHPUnit_Framework_Constraint_ClassHasAttribute(
  2132. $attributeName
  2133. );
  2134. }
  2135. /**
  2136. * Returns a PHPUnit_Framework_Constraint_ClassHasStaticAttribute matcher
  2137. * object.
  2138. *
  2139. * @param string $attributeName
  2140. * @return PHPUnit_Framework_Constraint_ClassHasStaticAttribute
  2141. * @since Method available since Release 3.1.0
  2142. */
  2143. public static function classHasStaticAttribute($attributeName)
  2144. {
  2145. return new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
  2146. $attributeName
  2147. );
  2148. }
  2149. /**
  2150. * Returns a PHPUnit_Framework_Constraint_ObjectHasAttribute matcher object.
  2151. *
  2152. * @param string $attributeName
  2153. * @return PHPUnit_Framework_Constraint_ObjectHasAttribute
  2154. * @since Method available since Release 3.0.0
  2155. */
  2156. public static function objectHasAttribute($attributeName)
  2157. {
  2158. return new PHPUnit_Framework_Constraint_ObjectHasAttribute(
  2159. $attributeName
  2160. );
  2161. }
  2162. /**
  2163. * Returns a PHPUnit_Framework_Constraint_IsIdentical matcher object.
  2164. *
  2165. * @param mixed $value
  2166. * @return PHPUnit_Framework_Constraint_IsIdentical
  2167. * @since Method available since Release 3.0.0
  2168. */
  2169. public static function identicalTo($value)
  2170. {
  2171. return new PHPUnit_Framework_Constraint_IsIdentical($value);
  2172. }
  2173. /**
  2174. * Returns a PHPUnit_Framework_Constraint_IsInstanceOf matcher object.
  2175. *
  2176. * @param string $className
  2177. * @return PHPUnit_Framework_Constraint_IsInstanceOf
  2178. * @since Method available since Release 3.0.0
  2179. */
  2180. public static function isInstanceOf($className)
  2181. {
  2182. return new PHPUnit_Framework_Constraint_IsInstanceOf($className);
  2183. }
  2184. /**
  2185. * Returns a PHPUnit_Framework_Constraint_IsType matcher object.
  2186. *
  2187. * @param string $type
  2188. * @return PHPUnit_Framework_Constraint_IsType
  2189. * @since Method available since Release 3.0.0
  2190. */
  2191. public static function isType($type)
  2192. {
  2193. return new PHPUnit_Framework_Constraint_IsType($type);
  2194. }
  2195. /**
  2196. * Returns a PHPUnit_Framework_Constraint_LessThan matcher object.
  2197. *
  2198. * @param mixed $value
  2199. * @return PHPUnit_Framework_Constraint_LessThan
  2200. * @since Method available since Release 3.0.0
  2201. */
  2202. public static function lessThan($value)
  2203. {
  2204. return new PHPUnit_Framework_Constraint_LessThan($value);
  2205. }
  2206. /**
  2207. * Returns a PHPUnit_Framework_Constraint_Or matcher object that wraps
  2208. * a PHPUnit_Framework_Constraint_IsEqual and a
  2209. * PHPUnit_Framework_Constraint_LessThan matcher object.
  2210. *
  2211. * @param mixed $value
  2212. * @return PHPUnit_Framework_Constraint_Or
  2213. * @since Method available since Release 3.1.0
  2214. */
  2215. public static function lessThanOrEqual($value)
  2216. {
  2217. return self::logicalOr(
  2218. new PHPUnit_Framework_Constraint_IsEqual($value),
  2219. new PHPUnit_Framework_Constraint_LessThan($value)
  2220. );
  2221. }
  2222. /**
  2223. * Returns a PHPUnit_Framework_Constraint_PCREMatch matcher object.
  2224. *
  2225. * @param string $pattern
  2226. * @return PHPUnit_Framework_Constraint_PCREMatch
  2227. * @since Method available since Release 3.0.0
  2228. */
  2229. public static function matchesRegularExpression($pattern)
  2230. {
  2231. return new PHPUnit_Framework_Constraint_PCREMatch($pattern);
  2232. }
  2233. /**
  2234. * Returns a PHPUnit_Framework_Constraint_StringMatches matcher object.
  2235. *
  2236. * @param string $string
  2237. * @return PHPUnit_Framework_Constraint_StringMatches
  2238. * @since Method available since Release 3.5.0
  2239. */
  2240. public static function matches($string)
  2241. {
  2242. return new PHPUnit_Framework_Constraint_StringMatches($string);
  2243. }
  2244. /**
  2245. * Returns a PHPUnit_Framework_Constraint_StringStartsWith matcher object.
  2246. *
  2247. * @param mixed $prefix
  2248. * @return PHPUnit_Framework_Constraint_StringStartsWith
  2249. * @since Method available since Release 3.4.0
  2250. */
  2251. public static function stringStartsWith($prefix)
  2252. {
  2253. return new PHPUnit_Framework_Constraint_StringStartsWith($prefix);
  2254. }
  2255. /**
  2256. * Returns a PHPUnit_Framework_Constraint_StringContains matcher object.
  2257. *
  2258. * @param string $string
  2259. * @param boolean $case
  2260. * @return PHPUnit_Framework_Constraint_StringContains
  2261. * @since Method available since Release 3.0.0
  2262. */
  2263. public static function stringContains($string, $case = TRUE)
  2264. {
  2265. return new PHPUnit_Framework_Constraint_StringContains($string, $case);
  2266. }
  2267. /**
  2268. * Returns a PHPUnit_Framework_Constraint_StringEndsWith matcher object.
  2269. *
  2270. * @param mixed $suffix
  2271. * @return PHPUnit_Framework_Constraint_StringEndsWith
  2272. * @since Method available since Release 3.4.0
  2273. */
  2274. public static function stringEndsWith($suffix)
  2275. {
  2276. return new PHPUnit_Framework_Constraint_StringEndsWith($suffix);
  2277. }
  2278. /**
  2279. * Fails a test with the given message.
  2280. *
  2281. * @param string $message
  2282. * @throws PHPUnit_Framework_AssertionFailedError
  2283. */
  2284. public static function fail($message = '')
  2285. {
  2286. throw new PHPUnit_Framework_AssertionFailedError($message);
  2287. }
  2288. /**
  2289. * Returns the value of an attribute of a class or an object.
  2290. * This also works for attributes that are declared protected or private.
  2291. *
  2292. * @param mixed $classOrObject
  2293. * @param string $attributeName
  2294. * @return mixed
  2295. * @throws InvalidArgumentException
  2296. */
  2297. public static function readAttribute($classOrObject, $attributeName)
  2298. {
  2299. if (!is_string($attributeName)) {
  2300. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  2301. }
  2302. if (is_string($classOrObject)) {
  2303. if (!class_exists($classOrObject)) {
  2304. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  2305. 1, 'class name'
  2306. );
  2307. }
  2308. return PHPUnit_Util_Class::getStaticAttribute(
  2309. $classOrObject,
  2310. $attributeName
  2311. );
  2312. }
  2313. else if (is_object($classOrObject)) {
  2314. return PHPUnit_Util_Class::getObjectAttribute(
  2315. $classOrObject,
  2316. $attributeName
  2317. );
  2318. }
  2319. else {
  2320. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  2321. 1, 'class name or object'
  2322. );
  2323. }
  2324. }
  2325. /**
  2326. * Mark the test as incomplete.
  2327. *
  2328. * @param string $message
  2329. * @throws PHPUnit_Framework_IncompleteTestError
  2330. * @since Method available since Release 3.0.0
  2331. */
  2332. public static function markTestIncomplete($message = '')
  2333. {
  2334. throw new PHPUnit_Framework_IncompleteTestError($message);
  2335. }
  2336. /**
  2337. * Mark the test as skipped.
  2338. *
  2339. * @param string $message
  2340. * @throws PHPUnit_Framework_SkippedTestError
  2341. * @since Method available since Release 3.0.0
  2342. */
  2343. public static function markTestSkipped($message = '')
  2344. {
  2345. throw new PHPUnit_Framework_SkippedTestError($message);
  2346. }
  2347. /**
  2348. * Return the current assertion count.
  2349. *
  2350. * @return integer
  2351. * @since Method available since Release 3.3.3
  2352. */
  2353. public static function getCount()
  2354. {
  2355. return self::$count;
  2356. }
  2357. /**
  2358. * Reset the assertion counter.
  2359. *
  2360. * @since Method available since Release 3.3.3
  2361. */
  2362. public static function resetCount()
  2363. {
  2364. self::$count = 0;
  2365. }
  2366. }