PageRenderTime 67ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPUnit/Framework/Assert.php

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