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

https://gitlab.com/ealexis.t/trends · PHP · 1622 lines · 786 code · 159 blank · 677 comment · 83 complexity · ccab74614c3fe0dc0dfd87471bc673f6 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of PHPUnit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * A set of assert methods.
  12. *
  13. * @since Class available since Release 2.0.0
  14. */
  15. abstract class PHPUnit_Framework_Assert
  16. {
  17. /**
  18. * @var int
  19. */
  20. private static $count = 0;
  21. /**
  22. * Asserts that an array has a specified key.
  23. *
  24. * @param mixed $key
  25. * @param array|ArrayAccess $array
  26. * @param string $message
  27. *
  28. * @since Method available since Release 3.0.0
  29. */
  30. public static function assertArrayHasKey($key, $array, $message = '')
  31. {
  32. if (!(is_integer($key) || is_string($key))) {
  33. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  34. 1,
  35. 'integer or string'
  36. );
  37. }
  38. if (!(is_array($array) || $array instanceof ArrayAccess)) {
  39. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  40. 2,
  41. 'array or ArrayAccess'
  42. );
  43. }
  44. $constraint = new PHPUnit_Framework_Constraint_ArrayHasKey($key);
  45. self::assertThat($array, $constraint, $message);
  46. }
  47. /**
  48. * Asserts that an array has a specified subset.
  49. *
  50. * @param array|ArrayAccess $subset
  51. * @param array|ArrayAccess $array
  52. * @param bool $strict Check for object identity
  53. * @param string $message
  54. *
  55. * @since Method available since Release 4.4.0
  56. */
  57. public static function assertArraySubset($subset, $array, $strict = false, $message = '')
  58. {
  59. if (!is_array($subset)) {
  60. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  61. 1,
  62. 'array or ArrayAccess'
  63. );
  64. }
  65. if (!is_array($array)) {
  66. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  67. 2,
  68. 'array or ArrayAccess'
  69. );
  70. }
  71. $constraint = new PHPUnit_Framework_Constraint_ArraySubset($subset, $strict);
  72. self::assertThat($array, $constraint, $message);
  73. }
  74. /**
  75. * Asserts that an array does not have a specified key.
  76. *
  77. * @param mixed $key
  78. * @param array|ArrayAccess $array
  79. * @param string $message
  80. *
  81. * @since Method available since Release 3.0.0
  82. */
  83. public static function assertArrayNotHasKey($key, $array, $message = '')
  84. {
  85. if (!(is_integer($key) || is_string($key))) {
  86. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  87. 1,
  88. 'integer or string'
  89. );
  90. }
  91. if (!(is_array($array) || $array instanceof ArrayAccess)) {
  92. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  93. 2,
  94. 'array or ArrayAccess'
  95. );
  96. }
  97. $constraint = new PHPUnit_Framework_Constraint_Not(
  98. new PHPUnit_Framework_Constraint_ArrayHasKey($key)
  99. );
  100. self::assertThat($array, $constraint, $message);
  101. }
  102. /**
  103. * Asserts that a haystack contains a needle.
  104. *
  105. * @param mixed $needle
  106. * @param mixed $haystack
  107. * @param string $message
  108. * @param bool $ignoreCase
  109. * @param bool $checkForObjectIdentity
  110. * @param bool $checkForNonObjectIdentity
  111. *
  112. * @since Method available since Release 2.1.0
  113. */
  114. public static function assertContains($needle, $haystack, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
  115. {
  116. if (is_array($haystack) ||
  117. is_object($haystack) && $haystack instanceof Traversable) {
  118. $constraint = new PHPUnit_Framework_Constraint_TraversableContains(
  119. $needle,
  120. $checkForObjectIdentity,
  121. $checkForNonObjectIdentity
  122. );
  123. } elseif (is_string($haystack)) {
  124. if (!is_string($needle)) {
  125. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  126. 1,
  127. 'string'
  128. );
  129. }
  130. $constraint = new PHPUnit_Framework_Constraint_StringContains(
  131. $needle,
  132. $ignoreCase
  133. );
  134. } else {
  135. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  136. 2,
  137. 'array, traversable or string'
  138. );
  139. }
  140. self::assertThat($haystack, $constraint, $message);
  141. }
  142. /**
  143. * Asserts that a haystack that is stored in a static attribute of a class
  144. * or an attribute of an object contains a needle.
  145. *
  146. * @param mixed $needle
  147. * @param string $haystackAttributeName
  148. * @param mixed $haystackClassOrObject
  149. * @param string $message
  150. * @param bool $ignoreCase
  151. * @param bool $checkForObjectIdentity
  152. * @param bool $checkForNonObjectIdentity
  153. *
  154. * @since Method available since Release 3.0.0
  155. */
  156. public static function assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
  157. {
  158. self::assertContains(
  159. $needle,
  160. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  161. $message,
  162. $ignoreCase,
  163. $checkForObjectIdentity,
  164. $checkForNonObjectIdentity
  165. );
  166. }
  167. /**
  168. * Asserts that a haystack does not contain a needle.
  169. *
  170. * @param mixed $needle
  171. * @param mixed $haystack
  172. * @param string $message
  173. * @param bool $ignoreCase
  174. * @param bool $checkForObjectIdentity
  175. * @param bool $checkForNonObjectIdentity
  176. *
  177. * @since Method available since Release 2.1.0
  178. */
  179. public static function assertNotContains($needle, $haystack, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
  180. {
  181. if (is_array($haystack) ||
  182. is_object($haystack) && $haystack instanceof Traversable) {
  183. $constraint = new PHPUnit_Framework_Constraint_Not(
  184. new PHPUnit_Framework_Constraint_TraversableContains(
  185. $needle,
  186. $checkForObjectIdentity,
  187. $checkForNonObjectIdentity
  188. )
  189. );
  190. } elseif (is_string($haystack)) {
  191. if (!is_string($needle)) {
  192. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  193. 1,
  194. 'string'
  195. );
  196. }
  197. $constraint = new PHPUnit_Framework_Constraint_Not(
  198. new PHPUnit_Framework_Constraint_StringContains(
  199. $needle,
  200. $ignoreCase
  201. )
  202. );
  203. } else {
  204. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  205. 2,
  206. 'array, traversable or string'
  207. );
  208. }
  209. self::assertThat($haystack, $constraint, $message);
  210. }
  211. /**
  212. * Asserts that a haystack that is stored in a static attribute of a class
  213. * or an attribute of an object does not contain a needle.
  214. *
  215. * @param mixed $needle
  216. * @param string $haystackAttributeName
  217. * @param mixed $haystackClassOrObject
  218. * @param string $message
  219. * @param bool $ignoreCase
  220. * @param bool $checkForObjectIdentity
  221. * @param bool $checkForNonObjectIdentity
  222. *
  223. * @since Method available since Release 3.0.0
  224. */
  225. public static function assertAttributeNotContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
  226. {
  227. self::assertNotContains(
  228. $needle,
  229. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  230. $message,
  231. $ignoreCase,
  232. $checkForObjectIdentity,
  233. $checkForNonObjectIdentity
  234. );
  235. }
  236. /**
  237. * Asserts that a haystack contains only values of a given type.
  238. *
  239. * @param string $type
  240. * @param mixed $haystack
  241. * @param bool $isNativeType
  242. * @param string $message
  243. *
  244. * @since Method available since Release 3.1.4
  245. */
  246. public static function assertContainsOnly($type, $haystack, $isNativeType = null, $message = '')
  247. {
  248. if (!(is_array($haystack) ||
  249. is_object($haystack) && $haystack instanceof Traversable)) {
  250. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  251. 2,
  252. 'array or traversable'
  253. );
  254. }
  255. if ($isNativeType == null) {
  256. $isNativeType = PHPUnit_Util_Type::isType($type);
  257. }
  258. self::assertThat(
  259. $haystack,
  260. new PHPUnit_Framework_Constraint_TraversableContainsOnly(
  261. $type,
  262. $isNativeType
  263. ),
  264. $message
  265. );
  266. }
  267. /**
  268. * Asserts that a haystack contains only instances of a given classname
  269. *
  270. * @param string $classname
  271. * @param array|Traversable $haystack
  272. * @param string $message
  273. */
  274. public static function assertContainsOnlyInstancesOf($classname, $haystack, $message = '')
  275. {
  276. if (!(is_array($haystack) ||
  277. is_object($haystack) && $haystack instanceof Traversable)) {
  278. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  279. 2,
  280. 'array or traversable'
  281. );
  282. }
  283. self::assertThat(
  284. $haystack,
  285. new PHPUnit_Framework_Constraint_TraversableContainsOnly(
  286. $classname,
  287. false
  288. ),
  289. $message
  290. );
  291. }
  292. /**
  293. * Asserts that a haystack that is stored in a static attribute of a class
  294. * or an attribute of an object contains only values of a given type.
  295. *
  296. * @param string $type
  297. * @param string $haystackAttributeName
  298. * @param mixed $haystackClassOrObject
  299. * @param bool $isNativeType
  300. * @param string $message
  301. *
  302. * @since Method available since Release 3.1.4
  303. */
  304. public static function assertAttributeContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = null, $message = '')
  305. {
  306. self::assertContainsOnly(
  307. $type,
  308. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  309. $isNativeType,
  310. $message
  311. );
  312. }
  313. /**
  314. * Asserts that a haystack does not contain only values of a given type.
  315. *
  316. * @param string $type
  317. * @param mixed $haystack
  318. * @param bool $isNativeType
  319. * @param string $message
  320. *
  321. * @since Method available since Release 3.1.4
  322. */
  323. public static function assertNotContainsOnly($type, $haystack, $isNativeType = null, $message = '')
  324. {
  325. if (!(is_array($haystack) ||
  326. is_object($haystack) && $haystack instanceof Traversable)) {
  327. throw PHPUnit_Util_InvalidArgumentHelper::factory(
  328. 2,
  329. 'array or traversable'
  330. );
  331. }
  332. if ($isNativeType == null) {
  333. $isNativeType = PHPUnit_Util_Type::isType($type);
  334. }
  335. self::assertThat(
  336. $haystack,
  337. new PHPUnit_Framework_Constraint_Not(
  338. new PHPUnit_Framework_Constraint_TraversableContainsOnly(
  339. $type,
  340. $isNativeType
  341. )
  342. ),
  343. $message
  344. );
  345. }
  346. /**
  347. * Asserts that a haystack that is stored in a static attribute of a class
  348. * or an attribute of an object does not contain only values of a given
  349. * type.
  350. *
  351. * @param string $type
  352. * @param string $haystackAttributeName
  353. * @param mixed $haystackClassOrObject
  354. * @param bool $isNativeType
  355. * @param string $message
  356. *
  357. * @since Method available since Release 3.1.4
  358. */
  359. public static function assertAttributeNotContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = null, $message = '')
  360. {
  361. self::assertNotContainsOnly(
  362. $type,
  363. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  364. $isNativeType,
  365. $message
  366. );
  367. }
  368. /**
  369. * Asserts the number of elements of an array, Countable or Traversable.
  370. *
  371. * @param int $expectedCount
  372. * @param mixed $haystack
  373. * @param string $message
  374. */
  375. public static function assertCount($expectedCount, $haystack, $message = '')
  376. {
  377. if (!is_int($expectedCount)) {
  378. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer');
  379. }
  380. if (!$haystack instanceof Countable &&
  381. !$haystack instanceof Traversable &&
  382. !is_array($haystack)) {
  383. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
  384. }
  385. self::assertThat(
  386. $haystack,
  387. new PHPUnit_Framework_Constraint_Count($expectedCount),
  388. $message
  389. );
  390. }
  391. /**
  392. * Asserts the number of elements of an array, Countable or Traversable
  393. * that is stored in an attribute.
  394. *
  395. * @param int $expectedCount
  396. * @param string $haystackAttributeName
  397. * @param mixed $haystackClassOrObject
  398. * @param string $message
  399. *
  400. * @since Method available since Release 3.6.0
  401. */
  402. public static function assertAttributeCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '')
  403. {
  404. self::assertCount(
  405. $expectedCount,
  406. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  407. $message
  408. );
  409. }
  410. /**
  411. * Asserts the number of elements of an array, Countable or Traversable.
  412. *
  413. * @param int $expectedCount
  414. * @param mixed $haystack
  415. * @param string $message
  416. */
  417. public static function assertNotCount($expectedCount, $haystack, $message = '')
  418. {
  419. if (!is_int($expectedCount)) {
  420. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer');
  421. }
  422. if (!$haystack instanceof Countable &&
  423. !$haystack instanceof Traversable &&
  424. !is_array($haystack)) {
  425. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
  426. }
  427. $constraint = new PHPUnit_Framework_Constraint_Not(
  428. new PHPUnit_Framework_Constraint_Count($expectedCount)
  429. );
  430. self::assertThat($haystack, $constraint, $message);
  431. }
  432. /**
  433. * Asserts the number of elements of an array, Countable or Traversable
  434. * that is stored in an attribute.
  435. *
  436. * @param int $expectedCount
  437. * @param string $haystackAttributeName
  438. * @param mixed $haystackClassOrObject
  439. * @param string $message
  440. *
  441. * @since Method available since Release 3.6.0
  442. */
  443. public static function assertAttributeNotCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '')
  444. {
  445. self::assertNotCount(
  446. $expectedCount,
  447. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  448. $message
  449. );
  450. }
  451. /**
  452. * Asserts that two variables are equal.
  453. *
  454. * @param mixed $expected
  455. * @param mixed $actual
  456. * @param string $message
  457. * @param float $delta
  458. * @param int $maxDepth
  459. * @param bool $canonicalize
  460. * @param bool $ignoreCase
  461. */
  462. public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
  463. {
  464. $constraint = new PHPUnit_Framework_Constraint_IsEqual(
  465. $expected,
  466. $delta,
  467. $maxDepth,
  468. $canonicalize,
  469. $ignoreCase
  470. );
  471. self::assertThat($actual, $constraint, $message);
  472. }
  473. /**
  474. * Asserts that a variable is equal to an attribute of an object.
  475. *
  476. * @param mixed $expected
  477. * @param string $actualAttributeName
  478. * @param string $actualClassOrObject
  479. * @param string $message
  480. * @param float $delta
  481. * @param int $maxDepth
  482. * @param bool $canonicalize
  483. * @param bool $ignoreCase
  484. */
  485. public static function assertAttributeEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
  486. {
  487. self::assertEquals(
  488. $expected,
  489. self::readAttribute($actualClassOrObject, $actualAttributeName),
  490. $message,
  491. $delta,
  492. $maxDepth,
  493. $canonicalize,
  494. $ignoreCase
  495. );
  496. }
  497. /**
  498. * Asserts that two variables are not equal.
  499. *
  500. * @param mixed $expected
  501. * @param mixed $actual
  502. * @param string $message
  503. * @param float $delta
  504. * @param int $maxDepth
  505. * @param bool $canonicalize
  506. * @param bool $ignoreCase
  507. *
  508. * @since Method available since Release 2.3.0
  509. */
  510. public static function assertNotEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
  511. {
  512. $constraint = new PHPUnit_Framework_Constraint_Not(
  513. new PHPUnit_Framework_Constraint_IsEqual(
  514. $expected,
  515. $delta,
  516. $maxDepth,
  517. $canonicalize,
  518. $ignoreCase
  519. )
  520. );
  521. self::assertThat($actual, $constraint, $message);
  522. }
  523. /**
  524. * Asserts that a variable is not equal to an attribute of an object.
  525. *
  526. * @param mixed $expected
  527. * @param string $actualAttributeName
  528. * @param string $actualClassOrObject
  529. * @param string $message
  530. * @param float $delta
  531. * @param int $maxDepth
  532. * @param bool $canonicalize
  533. * @param bool $ignoreCase
  534. */
  535. public static function assertAttributeNotEquals($expected, $actualAttributeName, $actualClassOrObject, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
  536. {
  537. self::assertNotEquals(
  538. $expected,
  539. self::readAttribute($actualClassOrObject, $actualAttributeName),
  540. $message,
  541. $delta,
  542. $maxDepth,
  543. $canonicalize,
  544. $ignoreCase
  545. );
  546. }
  547. /**
  548. * Asserts that a variable is empty.
  549. *
  550. * @param mixed $actual
  551. * @param string $message
  552. *
  553. * @throws PHPUnit_Framework_AssertionFailedError
  554. */
  555. public static function assertEmpty($actual, $message = '')
  556. {
  557. self::assertThat($actual, self::isEmpty(), $message);
  558. }
  559. /**
  560. * Asserts that a static attribute of a class or an attribute of an object
  561. * is empty.
  562. *
  563. * @param string $haystackAttributeName
  564. * @param mixed $haystackClassOrObject
  565. * @param string $message
  566. *
  567. * @since Method available since Release 3.5.0
  568. */
  569. public static function assertAttributeEmpty($haystackAttributeName, $haystackClassOrObject, $message = '')
  570. {
  571. self::assertEmpty(
  572. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  573. $message
  574. );
  575. }
  576. /**
  577. * Asserts that a variable is not empty.
  578. *
  579. * @param mixed $actual
  580. * @param string $message
  581. *
  582. * @throws PHPUnit_Framework_AssertionFailedError
  583. */
  584. public static function assertNotEmpty($actual, $message = '')
  585. {
  586. self::assertThat($actual, self::logicalNot(self::isEmpty()), $message);
  587. }
  588. /**
  589. * Asserts that a static attribute of a class or an attribute of an object
  590. * is not empty.
  591. *
  592. * @param string $haystackAttributeName
  593. * @param mixed $haystackClassOrObject
  594. * @param string $message
  595. *
  596. * @since Method available since Release 3.5.0
  597. */
  598. public static function assertAttributeNotEmpty($haystackAttributeName, $haystackClassOrObject, $message = '')
  599. {
  600. self::assertNotEmpty(
  601. self::readAttribute($haystackClassOrObject, $haystackAttributeName),
  602. $message
  603. );
  604. }
  605. /**
  606. * Asserts that a value is greater than another value.
  607. *
  608. * @param mixed $expected
  609. * @param mixed $actual
  610. * @param string $message
  611. *
  612. * @since Method available since Release 3.1.0
  613. */
  614. public static function assertGreaterThan($expected, $actual, $message = '')
  615. {
  616. self::assertThat($actual, self::greaterThan($expected), $message);
  617. }
  618. /**
  619. * Asserts that an attribute is greater than another value.
  620. *
  621. * @param mixed $expected
  622. * @param string $actualAttributeName
  623. * @param string $actualClassOrObject
  624. * @param string $message
  625. *
  626. * @since Method available since Release 3.1.0
  627. */
  628. public static function assertAttributeGreaterThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  629. {
  630. self::assertGreaterThan(
  631. $expected,
  632. self::readAttribute($actualClassOrObject, $actualAttributeName),
  633. $message
  634. );
  635. }
  636. /**
  637. * Asserts that a value is greater than or equal to another value.
  638. *
  639. * @param mixed $expected
  640. * @param mixed $actual
  641. * @param string $message
  642. *
  643. * @since Method available since Release 3.1.0
  644. */
  645. public static function assertGreaterThanOrEqual($expected, $actual, $message = '')
  646. {
  647. self::assertThat(
  648. $actual,
  649. self::greaterThanOrEqual($expected),
  650. $message
  651. );
  652. }
  653. /**
  654. * Asserts that an attribute is greater than or equal to another value.
  655. *
  656. * @param mixed $expected
  657. * @param string $actualAttributeName
  658. * @param string $actualClassOrObject
  659. * @param string $message
  660. *
  661. * @since Method available since Release 3.1.0
  662. */
  663. public static function assertAttributeGreaterThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  664. {
  665. self::assertGreaterThanOrEqual(
  666. $expected,
  667. self::readAttribute($actualClassOrObject, $actualAttributeName),
  668. $message
  669. );
  670. }
  671. /**
  672. * Asserts that a value is smaller than another value.
  673. *
  674. * @param mixed $expected
  675. * @param mixed $actual
  676. * @param string $message
  677. *
  678. * @since Method available since Release 3.1.0
  679. */
  680. public static function assertLessThan($expected, $actual, $message = '')
  681. {
  682. self::assertThat($actual, self::lessThan($expected), $message);
  683. }
  684. /**
  685. * Asserts that an attribute is smaller than another value.
  686. *
  687. * @param mixed $expected
  688. * @param string $actualAttributeName
  689. * @param string $actualClassOrObject
  690. * @param string $message
  691. *
  692. * @since Method available since Release 3.1.0
  693. */
  694. public static function assertAttributeLessThan($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  695. {
  696. self::assertLessThan(
  697. $expected,
  698. self::readAttribute($actualClassOrObject, $actualAttributeName),
  699. $message
  700. );
  701. }
  702. /**
  703. * Asserts that a value is smaller than or equal to another value.
  704. *
  705. * @param mixed $expected
  706. * @param mixed $actual
  707. * @param string $message
  708. *
  709. * @since Method available since Release 3.1.0
  710. */
  711. public static function assertLessThanOrEqual($expected, $actual, $message = '')
  712. {
  713. self::assertThat($actual, self::lessThanOrEqual($expected), $message);
  714. }
  715. /**
  716. * Asserts that an attribute is smaller than or equal to another value.
  717. *
  718. * @param mixed $expected
  719. * @param string $actualAttributeName
  720. * @param string $actualClassOrObject
  721. * @param string $message
  722. *
  723. * @since Method available since Release 3.1.0
  724. */
  725. public static function assertAttributeLessThanOrEqual($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  726. {
  727. self::assertLessThanOrEqual(
  728. $expected,
  729. self::readAttribute($actualClassOrObject, $actualAttributeName),
  730. $message
  731. );
  732. }
  733. /**
  734. * Asserts that the contents of one file is equal to the contents of another
  735. * file.
  736. *
  737. * @param string $expected
  738. * @param string $actual
  739. * @param string $message
  740. * @param bool $canonicalize
  741. * @param bool $ignoreCase
  742. *
  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 bool $canonicalize
  767. * @param bool $ignoreCase
  768. *
  769. * @since Method available since Release 3.2.14
  770. */
  771. public static function assertFileNotEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false)
  772. {
  773. self::assertFileExists($expected, $message);
  774. self::assertFileExists($actual, $message);
  775. self::assertNotEquals(
  776. file_get_contents($expected),
  777. file_get_contents($actual),
  778. $message,
  779. 0,
  780. 10,
  781. $canonicalize,
  782. $ignoreCase
  783. );
  784. }
  785. /**
  786. * Asserts that the contents of a string is equal
  787. * to the contents of a file.
  788. *
  789. * @param string $expectedFile
  790. * @param string $actualString
  791. * @param string $message
  792. * @param bool $canonicalize
  793. * @param bool $ignoreCase
  794. *
  795. * @since Method available since Release 3.3.0
  796. */
  797. public static function assertStringEqualsFile($expectedFile, $actualString, $message = '', $canonicalize = false, $ignoreCase = false)
  798. {
  799. self::assertFileExists($expectedFile, $message);
  800. self::assertEquals(
  801. file_get_contents($expectedFile),
  802. $actualString,
  803. $message,
  804. 0,
  805. 10,
  806. $canonicalize,
  807. $ignoreCase
  808. );
  809. }
  810. /**
  811. * Asserts that the contents of a string is not equal
  812. * to the contents of a file.
  813. *
  814. * @param string $expectedFile
  815. * @param string $actualString
  816. * @param string $message
  817. * @param bool $canonicalize
  818. * @param bool $ignoreCase
  819. *
  820. * @since Method available since Release 3.3.0
  821. */
  822. public static function assertStringNotEqualsFile($expectedFile, $actualString, $message = '', $canonicalize = false, $ignoreCase = false)
  823. {
  824. self::assertFileExists($expectedFile, $message);
  825. self::assertNotEquals(
  826. file_get_contents($expectedFile),
  827. $actualString,
  828. $message,
  829. 0,
  830. 10,
  831. $canonicalize,
  832. $ignoreCase
  833. );
  834. }
  835. /**
  836. * Asserts that a file exists.
  837. *
  838. * @param string $filename
  839. * @param string $message
  840. *
  841. * @since Method available since Release 3.0.0
  842. */
  843. public static function assertFileExists($filename, $message = '')
  844. {
  845. if (!is_string($filename)) {
  846. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  847. }
  848. $constraint = new PHPUnit_Framework_Constraint_FileExists;
  849. self::assertThat($filename, $constraint, $message);
  850. }
  851. /**
  852. * Asserts that a file does not exist.
  853. *
  854. * @param string $filename
  855. * @param string $message
  856. *
  857. * @since Method available since Release 3.0.0
  858. */
  859. public static function assertFileNotExists($filename, $message = '')
  860. {
  861. if (!is_string($filename)) {
  862. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  863. }
  864. $constraint = new PHPUnit_Framework_Constraint_Not(
  865. new PHPUnit_Framework_Constraint_FileExists
  866. );
  867. self::assertThat($filename, $constraint, $message);
  868. }
  869. /**
  870. * Asserts that a condition is true.
  871. *
  872. * @param bool $condition
  873. * @param string $message
  874. *
  875. * @throws PHPUnit_Framework_AssertionFailedError
  876. */
  877. public static function assertTrue($condition, $message = '')
  878. {
  879. self::assertThat($condition, self::isTrue(), $message);
  880. }
  881. /**
  882. * Asserts that a condition is not true.
  883. *
  884. * @param bool $condition
  885. * @param string $message
  886. *
  887. * @throws PHPUnit_Framework_AssertionFailedError
  888. */
  889. public static function assertNotTrue($condition, $message = '')
  890. {
  891. self::assertThat($condition, self::logicalNot(self::isTrue()), $message);
  892. }
  893. /**
  894. * Asserts that a condition is false.
  895. *
  896. * @param bool $condition
  897. * @param string $message
  898. *
  899. * @throws PHPUnit_Framework_AssertionFailedError
  900. */
  901. public static function assertFalse($condition, $message = '')
  902. {
  903. self::assertThat($condition, self::isFalse(), $message);
  904. }
  905. /**
  906. * Asserts that a condition is not false.
  907. *
  908. * @param bool $condition
  909. * @param string $message
  910. *
  911. * @throws PHPUnit_Framework_AssertionFailedError
  912. */
  913. public static function assertNotFalse($condition, $message = '')
  914. {
  915. self::assertThat($condition, self::logicalNot(self::isFalse()), $message);
  916. }
  917. /**
  918. * Asserts that a variable is not null.
  919. *
  920. * @param mixed $actual
  921. * @param string $message
  922. */
  923. public static function assertNotNull($actual, $message = '')
  924. {
  925. self::assertThat($actual, self::logicalNot(self::isNull()), $message);
  926. }
  927. /**
  928. * Asserts that a variable is null.
  929. *
  930. * @param mixed $actual
  931. * @param string $message
  932. */
  933. public static function assertNull($actual, $message = '')
  934. {
  935. self::assertThat($actual, self::isNull(), $message);
  936. }
  937. /**
  938. * Asserts that a class has a specified attribute.
  939. *
  940. * @param string $attributeName
  941. * @param string $className
  942. * @param string $message
  943. *
  944. * @since Method available since Release 3.1.0
  945. */
  946. public static function assertClassHasAttribute($attributeName, $className, $message = '')
  947. {
  948. if (!is_string($attributeName)) {
  949. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  950. }
  951. if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
  952. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
  953. }
  954. if (!is_string($className) || !class_exists($className)) {
  955. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className);
  956. }
  957. $constraint = new PHPUnit_Framework_Constraint_ClassHasAttribute(
  958. $attributeName
  959. );
  960. self::assertThat($className, $constraint, $message);
  961. }
  962. /**
  963. * Asserts that a class does not have a specified attribute.
  964. *
  965. * @param string $attributeName
  966. * @param string $className
  967. * @param string $message
  968. *
  969. * @since Method available since Release 3.1.0
  970. */
  971. public static function assertClassNotHasAttribute($attributeName, $className, $message = '')
  972. {
  973. if (!is_string($attributeName)) {
  974. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  975. }
  976. if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
  977. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
  978. }
  979. if (!is_string($className) || !class_exists($className)) {
  980. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className);
  981. }
  982. $constraint = new PHPUnit_Framework_Constraint_Not(
  983. new PHPUnit_Framework_Constraint_ClassHasAttribute($attributeName)
  984. );
  985. self::assertThat($className, $constraint, $message);
  986. }
  987. /**
  988. * Asserts that a class has a specified static attribute.
  989. *
  990. * @param string $attributeName
  991. * @param string $className
  992. * @param string $message
  993. *
  994. * @since Method available since Release 3.1.0
  995. */
  996. public static function assertClassHasStaticAttribute($attributeName, $className, $message = '')
  997. {
  998. if (!is_string($attributeName)) {
  999. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1000. }
  1001. if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
  1002. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
  1003. }
  1004. if (!is_string($className) || !class_exists($className)) {
  1005. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className);
  1006. }
  1007. $constraint = new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
  1008. $attributeName
  1009. );
  1010. self::assertThat($className, $constraint, $message);
  1011. }
  1012. /**
  1013. * Asserts that a class does not have a specified static attribute.
  1014. *
  1015. * @param string $attributeName
  1016. * @param string $className
  1017. * @param string $message
  1018. *
  1019. * @since Method available since Release 3.1.0
  1020. */
  1021. public static function assertClassNotHasStaticAttribute($attributeName, $className, $message = '')
  1022. {
  1023. if (!is_string($attributeName)) {
  1024. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1025. }
  1026. if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
  1027. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
  1028. }
  1029. if (!is_string($className) || !class_exists($className)) {
  1030. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name', $className);
  1031. }
  1032. $constraint = new PHPUnit_Framework_Constraint_Not(
  1033. new PHPUnit_Framework_Constraint_ClassHasStaticAttribute(
  1034. $attributeName
  1035. )
  1036. );
  1037. self::assertThat($className, $constraint, $message);
  1038. }
  1039. /**
  1040. * Asserts that an object has a specified attribute.
  1041. *
  1042. * @param string $attributeName
  1043. * @param object $object
  1044. * @param string $message
  1045. *
  1046. * @since Method available since Release 3.0.0
  1047. */
  1048. public static function assertObjectHasAttribute($attributeName, $object, $message = '')
  1049. {
  1050. if (!is_string($attributeName)) {
  1051. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1052. }
  1053. if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
  1054. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
  1055. }
  1056. if (!is_object($object)) {
  1057. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
  1058. }
  1059. $constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute(
  1060. $attributeName
  1061. );
  1062. self::assertThat($object, $constraint, $message);
  1063. }
  1064. /**
  1065. * Asserts that an object does not have a specified attribute.
  1066. *
  1067. * @param string $attributeName
  1068. * @param object $object
  1069. * @param string $message
  1070. *
  1071. * @since Method available since Release 3.0.0
  1072. */
  1073. public static function assertObjectNotHasAttribute($attributeName, $object, $message = '')
  1074. {
  1075. if (!is_string($attributeName)) {
  1076. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1077. }
  1078. if (!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
  1079. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
  1080. }
  1081. if (!is_object($object)) {
  1082. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
  1083. }
  1084. $constraint = new PHPUnit_Framework_Constraint_Not(
  1085. new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName)
  1086. );
  1087. self::assertThat($object, $constraint, $message);
  1088. }
  1089. /**
  1090. * Asserts that two variables have the same type and value.
  1091. * Used on objects, it asserts that two variables reference
  1092. * the same object.
  1093. *
  1094. * @param mixed $expected
  1095. * @param mixed $actual
  1096. * @param string $message
  1097. */
  1098. public static function assertSame($expected, $actual, $message = '')
  1099. {
  1100. if (is_bool($expected) && is_bool($actual)) {
  1101. self::assertEquals($expected, $actual, $message);
  1102. } else {
  1103. $constraint = new PHPUnit_Framework_Constraint_IsIdentical(
  1104. $expected
  1105. );
  1106. self::assertThat($actual, $constraint, $message);
  1107. }
  1108. }
  1109. /**
  1110. * Asserts that a variable and an attribute of an object have the same type
  1111. * and value.
  1112. *
  1113. * @param mixed $expected
  1114. * @param string $actualAttributeName
  1115. * @param object $actualClassOrObject
  1116. * @param string $message
  1117. */
  1118. public static function assertAttributeSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  1119. {
  1120. self::assertSame(
  1121. $expected,
  1122. self::readAttribute($actualClassOrObject, $actualAttributeName),
  1123. $message
  1124. );
  1125. }
  1126. /**
  1127. * Asserts that two variables do not have the same type and value.
  1128. * Used on objects, it asserts that two variables do not reference
  1129. * the same object.
  1130. *
  1131. * @param mixed $expected
  1132. * @param mixed $actual
  1133. * @param string $message
  1134. */
  1135. public static function assertNotSame($expected, $actual, $message = '')
  1136. {
  1137. if (is_bool($expected) && is_bool($actual)) {
  1138. self::assertNotEquals($expected, $actual, $message);
  1139. } else {
  1140. $constraint = new PHPUnit_Framework_Constraint_Not(
  1141. new PHPUnit_Framework_Constraint_IsIdentical($expected)
  1142. );
  1143. self::assertThat($actual, $constraint, $message);
  1144. }
  1145. }
  1146. /**
  1147. * Asserts that a variable and an attribute of an object do not have the
  1148. * same type and value.
  1149. *
  1150. * @param mixed $expected
  1151. * @param string $actualAttributeName
  1152. * @param object $actualClassOrObject
  1153. * @param string $message
  1154. */
  1155. public static function assertAttributeNotSame($expected, $actualAttributeName, $actualClassOrObject, $message = '')
  1156. {
  1157. self::assertNotSame(
  1158. $expected,
  1159. self::readAttribute($actualClassOrObject, $actualAttributeName),
  1160. $message
  1161. );
  1162. }
  1163. /**
  1164. * Asserts that a variable is of a given type.
  1165. *
  1166. * @param string $expected
  1167. * @param mixed $actual
  1168. * @param string $message
  1169. *
  1170. * @since Method available since Release 3.5.0
  1171. */
  1172. public static function assertInstanceOf($expected, $actual, $message = '')
  1173. {
  1174. if (!(is_string($expected) && (class_exists($expected) || interface_exists($expected)))) {
  1175. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class or interface name');
  1176. }
  1177. $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
  1178. $expected
  1179. );
  1180. self::assertThat($actual, $constraint, $message);
  1181. }
  1182. /**
  1183. * Asserts that an attribute is of a given type.
  1184. *
  1185. * @param string $expected
  1186. * @param string $attributeName
  1187. * @param mixed $classOrObject
  1188. * @param string $message
  1189. *
  1190. * @since Method available since Release 3.5.0
  1191. */
  1192. public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '')
  1193. {
  1194. self::assertInstanceOf(
  1195. $expected,
  1196. self::readAttribute($classOrObject, $attributeName),
  1197. $message
  1198. );
  1199. }
  1200. /**
  1201. * Asserts that a variable is not of a given type.
  1202. *
  1203. * @param string $expected
  1204. * @param mixed $actual
  1205. * @param string $message
  1206. *
  1207. * @since Method available since Release 3.5.0
  1208. */
  1209. public static function assertNotInstanceOf($expected, $actual, $message = '')
  1210. {
  1211. if (!(is_string($expected) && (class_exists($expected) || interface_exists($expected)))) {
  1212. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class or interface name');
  1213. }
  1214. $constraint = new PHPUnit_Framework_Constraint_Not(
  1215. new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
  1216. );
  1217. self::assertThat($actual, $constraint, $message);
  1218. }
  1219. /**
  1220. * Asserts that an attribute is of a given type.
  1221. *
  1222. * @param string $expected
  1223. * @param string $attributeName
  1224. * @param mixed $classOrObject
  1225. * @param string $message
  1226. *
  1227. * @since Method available since Release 3.5.0
  1228. */
  1229. public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '')
  1230. {
  1231. self::assertNotInstanceOf(
  1232. $expected,
  1233. self::readAttribute($classOrObject, $attributeName),
  1234. $message
  1235. );
  1236. }
  1237. /**
  1238. * Asserts that a variable is of a given type.
  1239. *
  1240. * @param string $expected
  1241. * @param mixed $actual
  1242. * @param string $message
  1243. *
  1244. * @since Method available since Release 3.5.0
  1245. */
  1246. public static function assertInternalType($expected, $actual, $message = '')
  1247. {
  1248. if (!is_string($expected)) {
  1249. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1250. }
  1251. $constraint = new PHPUnit_Framework_Constraint_IsType(
  1252. $expected
  1253. );
  1254. self::assertThat($actual, $constraint, $message);
  1255. }
  1256. /**
  1257. * Asserts that an attribute is of a given type.
  1258. *
  1259. * @param string $expected
  1260. * @param string $attributeName
  1261. * @param mixed $classOrObject
  1262. * @param string $message
  1263. *
  1264. * @since Method available since Release 3.5.0
  1265. */
  1266. public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '')
  1267. {
  1268. self::assertInternalType(
  1269. $expected,
  1270. self::readAttribute($classOrObject, $attributeName),
  1271. $message
  1272. );
  1273. }
  1274. /**
  1275. * Asserts that a variable is not of a given type.
  1276. *
  1277. * @param string $expected
  1278. * @param mixed $actual
  1279. * @param string $message
  1280. *
  1281. * @since Method available since Release 3.5.0
  1282. */
  1283. public static function assertNotInternalType($expected, $actual, $message = '')
  1284. {
  1285. if (!is_string($expected)) {
  1286. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1287. }
  1288. $constraint = new PHPUnit_Framework_Constraint_Not(
  1289. new PHPUnit_Framework_Constraint_IsType($expected)
  1290. );
  1291. self::assertThat($actual, $constraint, $message);
  1292. }
  1293. /**
  1294. * Asserts that an attribute is of a given type.
  1295. *
  1296. * @param string $expected
  1297. * @param string $attributeName
  1298. * @param mixed $classOrObject
  1299. * @param string $message
  1300. *
  1301. * @since Method available since Release 3.5.0
  1302. */
  1303. public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '')
  1304. {
  1305. self::assertNotInternalType(
  1306. $expected,
  1307. self::readAttribute($classOrObject, $attributeName),
  1308. $message
  1309. );
  1310. }
  1311. /**
  1312. * Asserts that a string matches a given regular expression.
  1313. *
  1314. * @param string $pattern
  1315. * @param string $string
  1316. * @param string $message
  1317. */
  1318. public static function assertRegExp($pattern, $string, $message = '')
  1319. {
  1320. if (!is_string($pattern)) {
  1321. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1322. }
  1323. if (!is_string($string)) {
  1324. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1325. }
  1326. $constraint = new PHPUnit_Framework_Constraint_PCREMatch($pattern);
  1327. self::assertThat($string, $constraint, $message);
  1328. }
  1329. /**
  1330. * Asserts that a string does not match a given regular expression.
  1331. *
  1332. * @param string $pattern
  1333. * @param string $string
  1334. * @param string $message
  1335. *
  1336. * @since Method available since Release 2.1.0
  1337. */
  1338. public static function assertNotRegExp($pattern, $string, $message = '')
  1339. {
  1340. if (!is_string($pattern)) {
  1341. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1342. }
  1343. if (!is_string($string)) {
  1344. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1345. }
  1346. $constraint = new PHPUnit_Framework_Constraint_Not(
  1347. new PHPUnit_Framework_Constraint_PCREMatch($pattern)
  1348. );
  1349. self::assertThat($string, $constraint, $message);
  1350. }
  1351. /**
  1352. * Assert that the size of two arrays (or `Countable` or `Traversable` objects)
  1353. * is the same.
  1354. *
  1355. * @param array|Countable|Traversable $expected
  1356. * @param array|Countable|Traversable $actual
  1357. * @param string $message
  1358. */
  1359. public static function assertSameSize($expected, $actual, $message = '')
  1360. {
  1361. if (!$expected instanceof Countable &&
  1362. !$expected instanceof Traversable &&
  1363. !is_array($expected)) {
  1364. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable or traversable');
  1365. }
  1366. if (!$actual instanceof Countable &&
  1367. !$actual instanceof Traversable &&
  1368. !is_array($actual)) {
  1369. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
  1370. }
  1371. self::assertThat(
  1372. $actual,
  1373. new PHPUnit_Framework_Constraint_SameSize($expected),
  1374. $message
  1375. );
  1376. }
  1377. /**
  1378. * Assert that the size of two arrays (or `Countable` or `Traversable` objects)
  1379. * is not the same.
  1380. *
  1381. * @param array|Countable|Traversable $expected
  1382. * @param array|Countable|Traversable $actual
  1383. * @param string $message
  1384. */
  1385. public static function assertNotSameSize($expected, $actual, $message = '')
  1386. {
  1387. if (!$expected instanceof Countable &&
  1388. !$expected instanceof Traversable &&
  1389. !is_array($expected)) {
  1390. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable or traversable');
  1391. }
  1392. if (!$actual instanceof Countable &&
  1393. !$actual instanceof Traversable &&
  1394. !is_array($actual)) {
  1395. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
  1396. }
  1397. $constraint = new PHPUnit_Framework_Constraint_Not(
  1398. new PHPUnit_Framework_Constraint_SameSize($expected)
  1399. );
  1400. self::assertThat($actual, $constraint, $message);
  1401. }
  1402. /**
  1403. * Asserts that a string matches a given format string.
  1404. *
  1405. * @param string $format
  1406. * @param string $string
  1407. * @param string $message
  1408. *
  1409. * @since Method available since Release 3.5.0
  1410. */
  1411. public static function assertStringMatchesFormat($format, $string, $message = '')
  1412. {
  1413. if (!is_string($format)) {
  1414. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1415. }
  1416. if (!is_string($string)) {
  1417. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1418. }
  1419. $constraint = new PHPUnit_Framework_Constraint_StringMatches($format);
  1420. self::assertThat($string, $constraint, $message);
  1421. }
  1422. /**
  1423. * Asserts that a string does not match a given format string.
  1424. *
  1425. * @param string $format
  1426. * @param string $string
  1427. * @param string $message
  1428. *
  1429. * @since Method available since Release 3.5.0
  1430. */
  1431. public static function assertStringNotMatchesFormat($format, $string, $message = '')
  1432. {
  1433. if (!is_string($format)) {
  1434. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  1435. }
  1436. if (!is_string($string)) {
  1437. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1438. }
  1439. $constraint = new PHPUnit_Framework_Constraint_Not(
  1440. new PHPUnit_Framework_Constraint_StringMatches($format)
  1441. );
  1442. self::assertThat($string, $constraint, $message);
  1443. }
  1444. /**
  1445. * Asserts that a string matches a given format file.
  1446. *
  1447. * @param string $formatFile
  1448. * @param string $string
  1449. * @param string $message
  1450. *
  1451. * @since Method available since Release 3.5.0
  1452. */
  1453. public static function assertStringMatchesFormatFile($formatFile, $string, $message = '')
  1454. {
  1455. self::assertFileExists($formatFile, $message);
  1456. if (!is_string($string)) {
  1457. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  1458. }
  1459. $constraint = new PHPUnit_Framework_Constraint_StringMatches(
  1460. file_get_contents($formatFile)
  1461. );
  1462. self::assertThat($string, $constraint, $message);
  1463. }