PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/phpunit/phpunit/src/Framework/Assert.php

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