PageRenderTime 89ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 1ms

/vendor/phpunit/phpunit/PHPUnit/Framework/Assert.php

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