PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 1ms

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

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