/vendor/phpunit/phpunit/tests/Framework/ConstraintTest.php

https://gitlab.com/ealexis.t/trends · PHP · 1759 lines · 1176 code · 280 blank · 303 comment · 2 complexity · 29ca37acdbeb43de56933033a03201e0 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. * @since Class available since Release 3.0.0
  12. */
  13. class Framework_ConstraintTest extends PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @covers PHPUnit_Framework_Constraint_ArrayHasKey
  17. * @covers PHPUnit_Framework_Assert::arrayHasKey
  18. * @covers PHPUnit_Framework_Constraint::count
  19. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  20. */
  21. public function testConstraintArrayHasKey()
  22. {
  23. $constraint = PHPUnit_Framework_Assert::arrayHasKey(0);
  24. $this->assertFalse($constraint->evaluate(array(), '', true));
  25. $this->assertEquals('has the key 0', $constraint->toString());
  26. $this->assertEquals(1, count($constraint));
  27. try {
  28. $constraint->evaluate(array());
  29. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  30. $this->assertEquals(<<<EOF
  31. Failed asserting that an array has the key 0.
  32. EOF
  33. ,
  34. PHPUnit_Framework_TestFailure::exceptionToString($e)
  35. );
  36. return;
  37. }
  38. $this->fail();
  39. }
  40. /**
  41. * @covers PHPUnit_Framework_Constraint_ArrayHasKey
  42. * @covers PHPUnit_Framework_Assert::arrayHasKey
  43. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  44. */
  45. public function testConstraintArrayHasKey2()
  46. {
  47. $constraint = PHPUnit_Framework_Assert::arrayHasKey(0);
  48. try {
  49. $constraint->evaluate(array(), 'custom message');
  50. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  51. $this->assertEquals(
  52. <<<EOF
  53. custom message\nFailed asserting that an array has the key 0.
  54. EOF
  55. ,
  56. PHPUnit_Framework_TestFailure::exceptionToString($e)
  57. );
  58. return;
  59. }
  60. $this->fail();
  61. }
  62. /**
  63. * @covers PHPUnit_Framework_Constraint_ArrayHasKey
  64. * @covers PHPUnit_Framework_Constraint_Not
  65. * @covers PHPUnit_Framework_Assert::arrayHasKey
  66. * @covers PHPUnit_Framework_Assert::logicalNot
  67. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  68. */
  69. public function testConstraintArrayNotHasKey()
  70. {
  71. $constraint = PHPUnit_Framework_Assert::logicalNot(
  72. PHPUnit_Framework_Assert::arrayHasKey(0)
  73. );
  74. $this->assertFalse($constraint->evaluate(array(0 => 1), '', true));
  75. $this->assertEquals('does not have the key 0', $constraint->toString());
  76. $this->assertEquals(1, count($constraint));
  77. try {
  78. $constraint->evaluate(array(0 => 1));
  79. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  80. $this->assertEquals(
  81. <<<EOF
  82. Failed asserting that an array does not have the key 0.
  83. EOF
  84. ,
  85. PHPUnit_Framework_TestFailure::exceptionToString($e)
  86. );
  87. return;
  88. }
  89. $this->fail();
  90. }
  91. /**
  92. * @covers PHPUnit_Framework_Constraint_ArrayHasKey
  93. * @covers PHPUnit_Framework_Constraint_Not
  94. * @covers PHPUnit_Framework_Assert::arrayHasKey
  95. * @covers PHPUnit_Framework_Assert::logicalNot
  96. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  97. */
  98. public function testConstraintArrayNotHasKey2()
  99. {
  100. $constraint = PHPUnit_Framework_Assert::logicalNot(
  101. PHPUnit_Framework_Assert::arrayHasKey(0)
  102. );
  103. try {
  104. $constraint->evaluate(array(0), 'custom message');
  105. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  106. $this->assertEquals(
  107. <<<EOF
  108. custom message
  109. Failed asserting that an array does not have the key 0.
  110. EOF
  111. ,
  112. PHPUnit_Framework_TestFailure::exceptionToString($e)
  113. );
  114. return;
  115. }
  116. $this->fail();
  117. }
  118. /**
  119. * @covers PHPUnit_Framework_Constraint_FileExists
  120. * @covers PHPUnit_Framework_Assert::fileExists
  121. * @covers PHPUnit_Framework_Constraint::count
  122. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  123. */
  124. public function testConstraintFileExists()
  125. {
  126. $constraint = PHPUnit_Framework_Assert::fileExists();
  127. $this->assertFalse($constraint->evaluate('foo', '', true));
  128. $this->assertEquals('file exists', $constraint->toString());
  129. $this->assertEquals(1, count($constraint));
  130. try {
  131. $constraint->evaluate('foo');
  132. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  133. $this->assertEquals(
  134. <<<EOF
  135. Failed asserting that file "foo" exists.
  136. EOF
  137. ,
  138. PHPUnit_Framework_TestFailure::exceptionToString($e)
  139. );
  140. return;
  141. }
  142. $this->fail();
  143. }
  144. /**
  145. * @covers PHPUnit_Framework_Constraint_FileExists
  146. * @covers PHPUnit_Framework_Assert::fileExists
  147. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  148. */
  149. public function testConstraintFileExists2()
  150. {
  151. $constraint = PHPUnit_Framework_Assert::fileExists();
  152. try {
  153. $constraint->evaluate('foo', 'custom message');
  154. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  155. $this->assertEquals(<<<EOF
  156. custom message
  157. Failed asserting that file "foo" exists.
  158. EOF
  159. ,
  160. PHPUnit_Framework_TestFailure::exceptionToString($e)
  161. );
  162. return;
  163. }
  164. $this->fail();
  165. }
  166. /**
  167. * @covers PHPUnit_Framework_Constraint_FileExists
  168. * @covers PHPUnit_Framework_Constraint_Not
  169. * @covers PHPUnit_Framework_Assert::logicalNot
  170. * @covers PHPUnit_Framework_Assert::fileExists
  171. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  172. */
  173. public function testConstraintFileNotExists()
  174. {
  175. $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ClassWithNonPublicAttributes.php';
  176. $constraint = PHPUnit_Framework_Assert::logicalNot(
  177. PHPUnit_Framework_Assert::fileExists()
  178. );
  179. $this->assertFalse($constraint->evaluate($file, '', true));
  180. $this->assertEquals('file does not exist', $constraint->toString());
  181. $this->assertEquals(1, count($constraint));
  182. try {
  183. $constraint->evaluate($file);
  184. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  185. $this->assertEquals(
  186. <<<EOF
  187. Failed asserting that file "$file" does not exist.
  188. EOF
  189. ,
  190. PHPUnit_Framework_TestFailure::exceptionToString($e)
  191. );
  192. return;
  193. }
  194. $this->fail();
  195. }
  196. /**
  197. * @covers PHPUnit_Framework_Constraint_FileExists
  198. * @covers PHPUnit_Framework_Constraint_Not
  199. * @covers PHPUnit_Framework_Assert::logicalNot
  200. * @covers PHPUnit_Framework_Assert::fileExists
  201. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  202. */
  203. public function testConstraintFileNotExists2()
  204. {
  205. $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ClassWithNonPublicAttributes.php';
  206. $constraint = PHPUnit_Framework_Assert::logicalNot(
  207. PHPUnit_Framework_Assert::fileExists()
  208. );
  209. try {
  210. $constraint->evaluate($file, 'custom message');
  211. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  212. $this->assertEquals(<<<EOF
  213. custom message
  214. Failed asserting that file "$file" does not exist.
  215. EOF
  216. ,
  217. PHPUnit_Framework_TestFailure::exceptionToString($e)
  218. );
  219. return;
  220. }
  221. $this->fail();
  222. }
  223. /**
  224. * @covers PHPUnit_Framework_Constraint_GreaterThan
  225. * @covers PHPUnit_Framework_Assert::greaterThan
  226. * @covers PHPUnit_Framework_Constraint::count
  227. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  228. */
  229. public function testConstraintGreaterThan()
  230. {
  231. $constraint = PHPUnit_Framework_Assert::greaterThan(1);
  232. $this->assertFalse($constraint->evaluate(0, '', true));
  233. $this->assertTrue($constraint->evaluate(2, '', true));
  234. $this->assertEquals('is greater than 1', $constraint->toString());
  235. $this->assertEquals(1, count($constraint));
  236. try {
  237. $constraint->evaluate(0);
  238. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  239. $this->assertEquals(
  240. <<<EOF
  241. Failed asserting that 0 is greater than 1.
  242. EOF
  243. ,
  244. PHPUnit_Framework_TestFailure::exceptionToString($e)
  245. );
  246. return;
  247. }
  248. $this->fail();
  249. }
  250. /**
  251. * @covers PHPUnit_Framework_Constraint_GreaterThan
  252. * @covers PHPUnit_Framework_Assert::greaterThan
  253. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  254. */
  255. public function testConstraintGreaterThan2()
  256. {
  257. $constraint = PHPUnit_Framework_Assert::greaterThan(1);
  258. try {
  259. $constraint->evaluate(0, 'custom message');
  260. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  261. $this->assertEquals(
  262. <<<EOF
  263. custom message
  264. Failed asserting that 0 is greater than 1.
  265. EOF
  266. ,
  267. PHPUnit_Framework_TestFailure::exceptionToString($e)
  268. );
  269. return;
  270. }
  271. $this->fail();
  272. }
  273. /**
  274. * @covers PHPUnit_Framework_Constraint_GreaterThan
  275. * @covers PHPUnit_Framework_Constraint_Not
  276. * @covers PHPUnit_Framework_Assert::greaterThan
  277. * @covers PHPUnit_Framework_Assert::logicalNot
  278. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  279. */
  280. public function testConstraintNotGreaterThan()
  281. {
  282. $constraint = PHPUnit_Framework_Assert::logicalNot(
  283. PHPUnit_Framework_Assert::greaterThan(1)
  284. );
  285. $this->assertTrue($constraint->evaluate(1, '', true));
  286. $this->assertEquals('is not greater than 1', $constraint->toString());
  287. $this->assertEquals(1, count($constraint));
  288. try {
  289. $constraint->evaluate(2);
  290. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  291. $this->assertEquals(
  292. <<<EOF
  293. Failed asserting that 2 is not greater than 1.
  294. EOF
  295. ,
  296. PHPUnit_Framework_TestFailure::exceptionToString($e)
  297. );
  298. return;
  299. }
  300. $this->fail();
  301. }
  302. /**
  303. * @covers PHPUnit_Framework_Constraint_GreaterThan
  304. * @covers PHPUnit_Framework_Constraint_Not
  305. * @covers PHPUnit_Framework_Assert::greaterThan
  306. * @covers PHPUnit_Framework_Assert::logicalNot
  307. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  308. */
  309. public function testConstraintNotGreaterThan2()
  310. {
  311. $constraint = PHPUnit_Framework_Assert::logicalNot(
  312. PHPUnit_Framework_Assert::greaterThan(1)
  313. );
  314. try {
  315. $constraint->evaluate(2, 'custom message');
  316. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  317. $this->assertEquals(
  318. <<<EOF
  319. custom message
  320. Failed asserting that 2 is not greater than 1.
  321. EOF
  322. ,
  323. PHPUnit_Framework_TestFailure::exceptionToString($e)
  324. );
  325. return;
  326. }
  327. $this->fail();
  328. }
  329. /**
  330. * @covers PHPUnit_Framework_Constraint_IsEqual
  331. * @covers PHPUnit_Framework_Constraint_GreaterThan
  332. * @covers PHPUnit_Framework_Constraint_Or
  333. * @covers PHPUnit_Framework_Assert::greaterThanOrEqual
  334. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  335. */
  336. public function testConstraintGreaterThanOrEqual()
  337. {
  338. $constraint = PHPUnit_Framework_Assert::greaterThanOrEqual(1);
  339. $this->assertTrue($constraint->evaluate(1, '', true));
  340. $this->assertFalse($constraint->evaluate(0, '', true));
  341. $this->assertEquals('is equal to 1 or is greater than 1', $constraint->toString());
  342. $this->assertEquals(2, count($constraint));
  343. try {
  344. $constraint->evaluate(0);
  345. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  346. $this->assertEquals(
  347. <<<EOF
  348. Failed asserting that 0 is equal to 1 or is greater than 1.
  349. EOF
  350. ,
  351. PHPUnit_Framework_TestFailure::exceptionToString($e)
  352. );
  353. return;
  354. }
  355. $this->fail();
  356. }
  357. /**
  358. * @covers PHPUnit_Framework_Constraint_IsEqual
  359. * @covers PHPUnit_Framework_Constraint_GreaterThan
  360. * @covers PHPUnit_Framework_Constraint_Or
  361. * @covers PHPUnit_Framework_Assert::greaterThanOrEqual
  362. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  363. */
  364. public function testConstraintGreaterThanOrEqual2()
  365. {
  366. $constraint = PHPUnit_Framework_Assert::greaterThanOrEqual(1);
  367. try {
  368. $constraint->evaluate(0, 'custom message');
  369. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  370. $this->assertEquals(
  371. <<<EOF
  372. custom message
  373. Failed asserting that 0 is equal to 1 or is greater than 1.
  374. EOF
  375. ,
  376. PHPUnit_Framework_TestFailure::exceptionToString($e)
  377. );
  378. return;
  379. }
  380. $this->fail();
  381. }
  382. /**
  383. * @covers PHPUnit_Framework_Constraint_IsEqual
  384. * @covers PHPUnit_Framework_Constraint_GreaterThan
  385. * @covers PHPUnit_Framework_Constraint_Or
  386. * @covers PHPUnit_Framework_Constraint_Not
  387. * @covers PHPUnit_Framework_Assert::greaterThanOrEqual
  388. * @covers PHPUnit_Framework_Assert::logicalNot
  389. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  390. */
  391. public function testConstraintNotGreaterThanOrEqual()
  392. {
  393. $constraint = PHPUnit_Framework_Assert::logicalNot(
  394. PHPUnit_Framework_Assert::greaterThanOrEqual(1)
  395. );
  396. $this->assertFalse($constraint->evaluate(1, '', true));
  397. $this->assertEquals('not( is equal to 1 or is greater than 1 )', $constraint->toString());
  398. $this->assertEquals(2, count($constraint));
  399. try {
  400. $constraint->evaluate(1);
  401. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  402. $this->assertEquals(
  403. <<<EOF
  404. Failed asserting that not( 1 is equal to 1 or is greater than 1 ).
  405. EOF
  406. ,
  407. PHPUnit_Framework_TestFailure::exceptionToString($e)
  408. );
  409. return;
  410. }
  411. $this->fail();
  412. }
  413. /**
  414. * @covers PHPUnit_Framework_Constraint_IsEqual
  415. * @covers PHPUnit_Framework_Constraint_GreaterThan
  416. * @covers PHPUnit_Framework_Constraint_Or
  417. * @covers PHPUnit_Framework_Constraint_Not
  418. * @covers PHPUnit_Framework_Assert::greaterThanOrEqual
  419. * @covers PHPUnit_Framework_Assert::logicalNot
  420. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  421. */
  422. public function testConstraintNotGreaterThanOrEqual2()
  423. {
  424. $constraint = PHPUnit_Framework_Assert::logicalNot(
  425. PHPUnit_Framework_Assert::greaterThanOrEqual(1)
  426. );
  427. try {
  428. $constraint->evaluate(1, 'custom message');
  429. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  430. $this->assertEquals(
  431. <<<EOF
  432. custom message
  433. Failed asserting that not( 1 is equal to 1 or is greater than 1 ).
  434. EOF
  435. ,
  436. PHPUnit_Framework_TestFailure::exceptionToString($e)
  437. );
  438. return;
  439. }
  440. $this->fail();
  441. }
  442. /**
  443. * @covers PHPUnit_Framework_Constraint_IsAnything
  444. * @covers PHPUnit_Framework_Assert::anything
  445. * @covers PHPUnit_Framework_Constraint::count
  446. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  447. */
  448. public function testConstraintIsAnything()
  449. {
  450. $constraint = PHPUnit_Framework_Assert::anything();
  451. $this->assertTrue($constraint->evaluate(null, '', true));
  452. $this->assertNull($constraint->evaluate(null));
  453. $this->assertEquals('is anything', $constraint->toString());
  454. $this->assertEquals(0, count($constraint));
  455. }
  456. /**
  457. * @covers PHPUnit_Framework_Constraint_IsAnything
  458. * @covers PHPUnit_Framework_Constraint_Not
  459. * @covers PHPUnit_Framework_Assert::anything
  460. * @covers PHPUnit_Framework_Assert::logicalNot
  461. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  462. */
  463. public function testConstraintNotIsAnything()
  464. {
  465. $constraint = PHPUnit_Framework_Assert::logicalNot(
  466. PHPUnit_Framework_Assert::anything()
  467. );
  468. $this->assertFalse($constraint->evaluate(null, '', true));
  469. $this->assertEquals('is not anything', $constraint->toString());
  470. $this->assertEquals(0, count($constraint));
  471. try {
  472. $constraint->evaluate(null);
  473. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  474. $this->assertEquals(
  475. <<<EOF
  476. Failed asserting that null is not anything.
  477. EOF
  478. ,
  479. PHPUnit_Framework_TestFailure::exceptionToString($e)
  480. );
  481. return;
  482. }
  483. $this->fail();
  484. }
  485. /**
  486. * @covers PHPUnit_Framework_Constraint_IsEqual
  487. * @covers PHPUnit_Framework_Assert::equalTo
  488. * @covers PHPUnit_Framework_Constraint::count
  489. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  490. */
  491. public function testConstraintIsEqual()
  492. {
  493. $constraint = PHPUnit_Framework_Assert::equalTo(1);
  494. $this->assertTrue($constraint->evaluate(1, '', true));
  495. $this->assertFalse($constraint->evaluate(0, '', true));
  496. $this->assertEquals('is equal to 1', $constraint->toString());
  497. $this->assertEquals(1, count($constraint));
  498. try {
  499. $constraint->evaluate(0);
  500. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  501. $this->assertEquals(
  502. <<<EOF
  503. Failed asserting that 0 matches expected 1.
  504. EOF
  505. ,
  506. PHPUnit_Framework_TestFailure::exceptionToString($e)
  507. );
  508. return;
  509. }
  510. $this->fail();
  511. }
  512. public function isEqualProvider()
  513. {
  514. $a = new stdClass;
  515. $a->foo = 'bar';
  516. $b = new stdClass;
  517. $ahash = spl_object_hash($a);
  518. $bhash = spl_object_hash($b);
  519. $c = new stdClass;
  520. $c->foo = 'bar';
  521. $c->int = 1;
  522. $c->array = array(0, array(1), array(2), 3);
  523. $c->related = new stdClass;
  524. $c->related->foo = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk";
  525. $c->self = $c;
  526. $c->c = $c;
  527. $d = new stdClass;
  528. $d->foo = 'bar';
  529. $d->int = 2;
  530. $d->array = array(0, array(4), array(2), 3);
  531. $d->related = new stdClass;
  532. $d->related->foo = "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk";
  533. $d->self = $d;
  534. $d->c = $c;
  535. $storage1 = new SplObjectStorage;
  536. $storage1->attach($a);
  537. $storage1->attach($b);
  538. $storage2 = new SplObjectStorage;
  539. $storage2->attach($b);
  540. $storage1hash = spl_object_hash($storage1);
  541. $storage2hash = spl_object_hash($storage2);
  542. $dom1 = new DOMDocument;
  543. $dom1->preserveWhiteSpace = false;
  544. $dom1->loadXML('<root></root>');
  545. $dom2 = new DOMDocument;
  546. $dom2->preserveWhiteSpace = false;
  547. $dom2->loadXML('<root><foo/></root>');
  548. $data = array(
  549. array(1, 0, <<<EOF
  550. Failed asserting that 0 matches expected 1.
  551. EOF
  552. ),
  553. array(1.1, 0, <<<EOF
  554. Failed asserting that 0 matches expected 1.1.
  555. EOF
  556. ),
  557. array('a', 'b', <<<EOF
  558. Failed asserting that two strings are equal.
  559. --- Expected
  560. +++ Actual
  561. @@ @@
  562. -'a'
  563. +'b'
  564. EOF
  565. ),
  566. array("a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk", "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk", <<<EOF
  567. Failed asserting that two strings are equal.
  568. --- Expected
  569. +++ Actual
  570. @@ @@
  571. 'a
  572. -b
  573. +p
  574. @@ @@
  575. i
  576. -j
  577. +w
  578. k'
  579. EOF
  580. ),
  581. array(1, array(0), <<<EOF
  582. Array (...) does not match expected type "integer".
  583. EOF
  584. ),
  585. array(array(0), 1, <<<EOF
  586. 1 does not match expected type "array".
  587. EOF
  588. ),
  589. array(array(0), array(1), <<<EOF
  590. Failed asserting that two arrays are equal.
  591. --- Expected
  592. +++ Actual
  593. @@ @@
  594. Array (
  595. - 0 => 0
  596. + 0 => 1
  597. )
  598. EOF
  599. ),
  600. array(array(true), array('true'), <<<EOF
  601. Failed asserting that two arrays are equal.
  602. --- Expected
  603. +++ Actual
  604. @@ @@
  605. Array (
  606. - 0 => true
  607. + 0 => 'true'
  608. )
  609. EOF
  610. ),
  611. array(array(0, array(1), array(2), 3), array(0, array(4), array(2), 3), <<<EOF
  612. Failed asserting that two arrays are equal.
  613. --- Expected
  614. +++ Actual
  615. @@ @@
  616. Array (
  617. 0 => 0
  618. 1 => Array (
  619. - 0 => 1
  620. + 0 => 4
  621. )
  622. 2 => Array (...)
  623. 3 => 3
  624. )
  625. EOF
  626. ),
  627. array($a, array(0), <<<EOF
  628. Array (...) does not match expected type "object".
  629. EOF
  630. ),
  631. array(array(0), $a, <<<EOF
  632. stdClass Object (...) does not match expected type "array".
  633. EOF
  634. ),
  635. array($a, $b, <<<EOF
  636. Failed asserting that two objects are equal.
  637. --- Expected
  638. +++ Actual
  639. @@ @@
  640. stdClass Object (
  641. - 'foo' => 'bar'
  642. )
  643. EOF
  644. ),
  645. array($c, $d, <<<EOF
  646. Failed asserting that two objects are equal.
  647. --- Expected
  648. +++ Actual
  649. @@ @@
  650. stdClass Object (
  651. 'foo' => 'bar'
  652. - 'int' => 1
  653. + 'int' => 2
  654. 'array' => Array (
  655. 0 => 0
  656. 1 => Array (
  657. - 0 => 1
  658. + 0 => 4
  659. @@ @@
  660. 'foo' => 'a
  661. - b
  662. + p
  663. @@ @@
  664. i
  665. - j
  666. + w
  667. k'
  668. )
  669. 'self' => stdClass Object (...)
  670. 'c' => stdClass Object (...)
  671. )
  672. EOF
  673. ),
  674. array($dom1, $dom2, <<<EOF
  675. Failed asserting that two DOM documents are equal.
  676. --- Expected
  677. +++ Actual
  678. @@ @@
  679. <?xml version="1.0"?>
  680. -<root/>
  681. +<root>
  682. + <foo/>
  683. +</root>
  684. EOF
  685. ),
  686. array(
  687. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  688. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/Chicago')),
  689. <<<EOF
  690. Failed asserting that two DateTime objects are equal.
  691. --- Expected
  692. +++ Actual
  693. @@ @@
  694. -2013-03-29T04:13:35-0400
  695. +2013-03-29T04:13:35-0500
  696. EOF
  697. ),
  698. );
  699. if (PHP_MAJOR_VERSION < 7) {
  700. $data[] = array($storage1, $storage2, <<<EOF
  701. Failed asserting that two objects are equal.
  702. --- Expected
  703. +++ Actual
  704. @@ @@
  705. -SplObjectStorage Object &$storage1hash (
  706. - '$ahash' => Array &0 (
  707. - 'obj' => stdClass Object &$ahash (
  708. - 'foo' => 'bar'
  709. - )
  710. +SplObjectStorage Object &$storage2hash (
  711. + '$bhash' => Array &0 (
  712. + 'obj' => stdClass Object &$bhash ()
  713. 'inf' => null
  714. )
  715. - '$bhash' => Array &0
  716. )
  717. EOF
  718. );
  719. } else {
  720. $data[] = array($storage1, $storage2, <<<EOF
  721. Failed asserting that two objects are equal.
  722. --- Expected
  723. +++ Actual
  724. @@ @@
  725. -SplObjectStorage Object &$storage1hash (
  726. - '$ahash' => Array &0 (
  727. - 'obj' => stdClass Object &$ahash (
  728. - 'foo' => 'bar'
  729. - )
  730. +SplObjectStorage Object &$storage2hash (
  731. + '$bhash' => Array &0 (
  732. + 'obj' => stdClass Object &$bhash ()
  733. 'inf' => null
  734. )
  735. - '$bhash' => Array &0
  736. )
  737. EOF
  738. );
  739. }
  740. return $data;
  741. }
  742. /**
  743. * @dataProvider isEqualProvider
  744. * @covers PHPUnit_Framework_Constraint_IsEqual
  745. * @covers PHPUnit_Framework_Assert::equalTo
  746. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  747. */
  748. public function testConstraintIsEqual2($expected, $actual, $message)
  749. {
  750. $constraint = PHPUnit_Framework_Assert::equalTo($expected);
  751. try {
  752. $constraint->evaluate($actual, 'custom message');
  753. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  754. $this->assertEquals(
  755. "custom message\n$message",
  756. $this->trimnl(PHPUnit_Framework_TestFailure::exceptionToString($e))
  757. );
  758. return;
  759. }
  760. $this->fail();
  761. }
  762. /**
  763. * @covers PHPUnit_Framework_Constraint_IsEqual
  764. * @covers PHPUnit_Framework_Constraint_Not
  765. * @covers PHPUnit_Framework_Assert::equalTo
  766. * @covers PHPUnit_Framework_Assert::logicalNot
  767. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  768. */
  769. public function testConstraintIsNotEqual()
  770. {
  771. $constraint = PHPUnit_Framework_Assert::logicalNot(
  772. PHPUnit_Framework_Assert::equalTo(1)
  773. );
  774. $this->assertTrue($constraint->evaluate(0, '', true));
  775. $this->assertFalse($constraint->evaluate(1, '', true));
  776. $this->assertEquals('is not equal to 1', $constraint->toString());
  777. $this->assertEquals(1, count($constraint));
  778. try {
  779. $constraint->evaluate(1);
  780. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  781. $this->assertEquals(
  782. <<<EOF
  783. Failed asserting that 1 is not equal to 1.
  784. EOF
  785. ,
  786. PHPUnit_Framework_TestFailure::exceptionToString($e)
  787. );
  788. return;
  789. }
  790. $this->fail();
  791. }
  792. /**
  793. * @covers PHPUnit_Framework_Constraint_IsEqual
  794. * @covers PHPUnit_Framework_Constraint_Not
  795. * @covers PHPUnit_Framework_Assert::equalTo
  796. * @covers PHPUnit_Framework_Assert::logicalNot
  797. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  798. */
  799. public function testConstraintIsNotEqual2()
  800. {
  801. $constraint = PHPUnit_Framework_Assert::logicalNot(
  802. PHPUnit_Framework_Assert::equalTo(1)
  803. );
  804. try {
  805. $constraint->evaluate(1, 'custom message');
  806. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  807. $this->assertEquals(
  808. <<<EOF
  809. custom message
  810. Failed asserting that 1 is not equal to 1.
  811. EOF
  812. ,
  813. PHPUnit_Framework_TestFailure::exceptionToString($e)
  814. );
  815. return;
  816. }
  817. $this->fail();
  818. }
  819. /**
  820. * @covers PHPUnit_Framework_Constraint_IsIdentical
  821. * @covers PHPUnit_Framework_Assert::identicalTo
  822. * @covers PHPUnit_Framework_Constraint::count
  823. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  824. */
  825. public function testConstraintIsIdentical()
  826. {
  827. $a = new stdClass;
  828. $b = new stdClass;
  829. $constraint = PHPUnit_Framework_Assert::identicalTo($a);
  830. $this->assertFalse($constraint->evaluate($b, '', true));
  831. $this->assertTrue($constraint->evaluate($a, '', true));
  832. $this->assertEquals('is identical to an object of class "stdClass"', $constraint->toString());
  833. $this->assertEquals(1, count($constraint));
  834. try {
  835. $constraint->evaluate($b);
  836. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  837. $this->assertEquals(<<<EOF
  838. Failed asserting that two variables reference the same object.
  839. EOF
  840. ,
  841. PHPUnit_Framework_TestFailure::exceptionToString($e)
  842. );
  843. return;
  844. }
  845. $this->fail();
  846. }
  847. /**
  848. * @covers PHPUnit_Framework_Constraint_IsIdentical
  849. * @covers PHPUnit_Framework_Assert::identicalTo
  850. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  851. */
  852. public function testConstraintIsIdentical2()
  853. {
  854. $a = new stdClass;
  855. $b = new stdClass;
  856. $constraint = PHPUnit_Framework_Assert::identicalTo($a);
  857. try {
  858. $constraint->evaluate($b, 'custom message');
  859. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  860. $this->assertEquals(<<<EOF
  861. custom message
  862. Failed asserting that two variables reference the same object.
  863. EOF
  864. ,
  865. PHPUnit_Framework_TestFailure::exceptionToString($e)
  866. );
  867. return;
  868. }
  869. $this->fail();
  870. }
  871. /**
  872. * @covers PHPUnit_Framework_Constraint_IsIdentical
  873. * @covers PHPUnit_Framework_Assert::identicalTo
  874. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  875. */
  876. public function testConstraintIsIdentical3()
  877. {
  878. $constraint = PHPUnit_Framework_Assert::identicalTo('a');
  879. try {
  880. $constraint->evaluate('b', 'custom message');
  881. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  882. $this->assertEquals(<<<EOF
  883. custom message
  884. Failed asserting that two strings are identical.
  885. --- Expected
  886. +++ Actual
  887. @@ @@
  888. -a
  889. +b
  890. EOF
  891. ,
  892. PHPUnit_Framework_TestFailure::exceptionToString($e)
  893. );
  894. return;
  895. }
  896. $this->fail();
  897. }
  898. /**
  899. * @covers PHPUnit_Framework_Constraint_IsIdentical
  900. * @covers PHPUnit_Framework_Constraint_Not
  901. * @covers PHPUnit_Framework_Assert::identicalTo
  902. * @covers PHPUnit_Framework_Assert::logicalNot
  903. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  904. */
  905. public function testConstraintIsNotIdentical()
  906. {
  907. $a = new stdClass;
  908. $b = new stdClass;
  909. $constraint = PHPUnit_Framework_Assert::logicalNot(
  910. PHPUnit_Framework_Assert::identicalTo($a)
  911. );
  912. $this->assertTrue($constraint->evaluate($b, '', true));
  913. $this->assertFalse($constraint->evaluate($a, '', true));
  914. $this->assertEquals('is not identical to an object of class "stdClass"', $constraint->toString());
  915. $this->assertEquals(1, count($constraint));
  916. try {
  917. $constraint->evaluate($a);
  918. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  919. $this->assertEquals(<<<EOF
  920. Failed asserting that two variables don't reference the same object.
  921. EOF
  922. ,
  923. $this->trimnl(PHPUnit_Framework_TestFailure::exceptionToString($e))
  924. );
  925. return;
  926. }
  927. $this->fail();
  928. }
  929. /**
  930. * @covers PHPUnit_Framework_Constraint_IsIdentical
  931. * @covers PHPUnit_Framework_Constraint_Not
  932. * @covers PHPUnit_Framework_Assert::identicalTo
  933. * @covers PHPUnit_Framework_Assert::logicalNot
  934. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  935. */
  936. public function testConstraintIsNotIdentical2()
  937. {
  938. $a = new stdClass;
  939. $constraint = PHPUnit_Framework_Assert::logicalNot(
  940. PHPUnit_Framework_Assert::identicalTo($a)
  941. );
  942. try {
  943. $constraint->evaluate($a, 'custom message');
  944. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  945. $this->assertEquals(<<<EOF
  946. custom message
  947. Failed asserting that two variables don't reference the same object.
  948. EOF
  949. ,
  950. PHPUnit_Framework_TestFailure::exceptionToString($e)
  951. );
  952. return;
  953. }
  954. $this->fail();
  955. }
  956. /**
  957. * @covers PHPUnit_Framework_Constraint_IsIdentical
  958. * @covers PHPUnit_Framework_Constraint_Not
  959. * @covers PHPUnit_Framework_Assert::identicalTo
  960. * @covers PHPUnit_Framework_Assert::logicalNot
  961. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  962. */
  963. public function testConstraintIsNotIdentical3()
  964. {
  965. $constraint = PHPUnit_Framework_Assert::logicalNot(
  966. PHPUnit_Framework_Assert::identicalTo('a')
  967. );
  968. try {
  969. $constraint->evaluate('a', 'custom message');
  970. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  971. $this->assertEquals(<<<EOF
  972. custom message
  973. Failed asserting that two strings are not identical.
  974. EOF
  975. ,
  976. $this->trimnl(PHPUnit_Framework_TestFailure::exceptionToString($e))
  977. );
  978. return;
  979. }
  980. $this->fail();
  981. }
  982. /**
  983. * @covers PHPUnit_Framework_Constraint_IsInstanceOf
  984. * @covers PHPUnit_Framework_Assert::isInstanceOf
  985. * @covers PHPUnit_Framework_Constraint::count
  986. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  987. */
  988. public function testConstraintIsInstanceOf()
  989. {
  990. $constraint = PHPUnit_Framework_Assert::isInstanceOf('Exception');
  991. $this->assertFalse($constraint->evaluate(new stdClass, '', true));
  992. $this->assertTrue($constraint->evaluate(new Exception, '', true));
  993. $this->assertEquals('is instance of class "Exception"', $constraint->toString());
  994. $this->assertEquals(1, count($constraint));
  995. $interfaceConstraint = PHPUnit_Framework_Assert::isInstanceOf('Countable');
  996. $this->assertFalse($interfaceConstraint->evaluate(new stdClass, '', true));
  997. $this->assertTrue($interfaceConstraint->evaluate(new ArrayObject, '', true));
  998. $this->assertEquals('is instance of interface "Countable"', $interfaceConstraint->toString());
  999. try {
  1000. $constraint->evaluate(new stdClass);
  1001. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1002. $this->assertEquals(
  1003. <<<EOF
  1004. Failed asserting that stdClass Object () is an instance of class "Exception".
  1005. EOF
  1006. ,
  1007. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1008. );
  1009. return;
  1010. }
  1011. $this->fail();
  1012. }
  1013. /**
  1014. * @covers PHPUnit_Framework_Constraint_IsInstanceOf
  1015. * @covers PHPUnit_Framework_Assert::isInstanceOf
  1016. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1017. */
  1018. public function testConstraintIsInstanceOf2()
  1019. {
  1020. $constraint = PHPUnit_Framework_Assert::isInstanceOf('Exception');
  1021. try {
  1022. $constraint->evaluate(new stdClass, 'custom message');
  1023. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1024. $this->assertEquals(<<<EOF
  1025. custom message
  1026. Failed asserting that stdClass Object () is an instance of class "Exception".
  1027. EOF
  1028. ,
  1029. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1030. );
  1031. return;
  1032. }
  1033. $this->fail();
  1034. }
  1035. /**
  1036. * @covers PHPUnit_Framework_Constraint_IsInstanceOf
  1037. * @covers PHPUnit_Framework_Constraint_Not
  1038. * @covers PHPUnit_Framework_Assert::isInstanceOf
  1039. * @covers PHPUnit_Framework_Assert::logicalNot
  1040. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1041. */
  1042. public function testConstraintIsNotInstanceOf()
  1043. {
  1044. $constraint = PHPUnit_Framework_Assert::logicalNot(
  1045. PHPUnit_Framework_Assert::isInstanceOf('stdClass')
  1046. );
  1047. $this->assertFalse($constraint->evaluate(new stdClass, '', true));
  1048. $this->assertTrue($constraint->evaluate(new Exception, '', true));
  1049. $this->assertEquals('is not instance of class "stdClass"', $constraint->toString());
  1050. $this->assertEquals(1, count($constraint));
  1051. try {
  1052. $constraint->evaluate(new stdClass);
  1053. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1054. $this->assertEquals(
  1055. <<<EOF
  1056. Failed asserting that stdClass Object () is not an instance of class "stdClass".
  1057. EOF
  1058. ,
  1059. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1060. );
  1061. return;
  1062. }
  1063. $this->fail();
  1064. }
  1065. /**
  1066. * @covers PHPUnit_Framework_Constraint_IsInstanceOf
  1067. * @covers PHPUnit_Framework_Constraint_Not
  1068. * @covers PHPUnit_Framework_Assert::isInstanceOf
  1069. * @covers PHPUnit_Framework_Assert::logicalNot
  1070. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1071. */
  1072. public function testConstraintIsNotInstanceOf2()
  1073. {
  1074. $constraint = PHPUnit_Framework_Assert::logicalNot(
  1075. PHPUnit_Framework_Assert::isInstanceOf('stdClass')
  1076. );
  1077. try {
  1078. $constraint->evaluate(new stdClass, 'custom message');
  1079. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1080. $this->assertEquals(<<<EOF
  1081. custom message
  1082. Failed asserting that stdClass Object () is not an instance of class "stdClass".
  1083. EOF
  1084. ,
  1085. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1086. );
  1087. return;
  1088. }
  1089. $this->fail();
  1090. }
  1091. /**
  1092. * @covers PHPUnit_Framework_Constraint_IsType
  1093. * @covers PHPUnit_Framework_Assert::isType
  1094. * @covers PHPUnit_Framework_Constraint::count
  1095. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1096. */
  1097. public function testConstraintIsType()
  1098. {
  1099. $constraint = PHPUnit_Framework_Assert::isType('string');
  1100. $this->assertFalse($constraint->evaluate(0, '', true));
  1101. $this->assertTrue($constraint->evaluate('', '', true));
  1102. $this->assertEquals('is of type "string"', $constraint->toString());
  1103. $this->assertEquals(1, count($constraint));
  1104. try {
  1105. $constraint->evaluate(new stdClass);
  1106. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1107. $this->assertStringMatchesFormat(<<<EOF
  1108. Failed asserting that stdClass Object &%x () is of type "string".
  1109. EOF
  1110. ,
  1111. $this->trimnl(PHPUnit_Framework_TestFailure::exceptionToString($e))
  1112. );
  1113. return;
  1114. }
  1115. $this->fail();
  1116. }
  1117. /**
  1118. * @covers PHPUnit_Framework_Constraint_IsType
  1119. * @covers PHPUnit_Framework_Assert::isType
  1120. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1121. */
  1122. public function testConstraintIsType2()
  1123. {
  1124. $constraint = PHPUnit_Framework_Assert::isType('string');
  1125. try {
  1126. $constraint->evaluate(new stdClass, 'custom message');
  1127. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1128. $this->assertStringMatchesFormat(<<<EOF
  1129. custom message
  1130. Failed asserting that stdClass Object &%x () is of type "string".
  1131. EOF
  1132. ,
  1133. $this->trimnl(PHPUnit_Framework_TestFailure::exceptionToString($e))
  1134. );
  1135. return;
  1136. }
  1137. $this->fail();
  1138. }
  1139. public function resources()
  1140. {
  1141. $fh = fopen(__FILE__, 'r');
  1142. fclose($fh);
  1143. return array(
  1144. 'open resource' => array(fopen(__FILE__, 'r')),
  1145. 'closed resource' => array($fh),
  1146. );
  1147. }
  1148. /**
  1149. * @dataProvider resources
  1150. * @covers PHPUnit_Framework_Constraint_IsType
  1151. * @covers PHPUnit_Framework_Assert::isType
  1152. */
  1153. public function testConstraintIsResourceTypeEvaluatesCorrectlyWithResources($resource)
  1154. {
  1155. $constraint = PHPUnit_Framework_Assert::isType('resource');
  1156. $this->assertTrue($constraint->evaluate($resource, '', true));
  1157. @fclose($resource);
  1158. }
  1159. /**
  1160. * @covers PHPUnit_Framework_Constraint_IsType
  1161. * @covers PHPUnit_Framework_Constraint_Not
  1162. * @covers PHPUnit_Framework_Assert::isType
  1163. * @covers PHPUnit_Framework_Assert::logicalNot
  1164. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1165. */
  1166. public function testConstraintIsNotType()
  1167. {
  1168. $constraint = PHPUnit_Framework_Assert::logicalNot(
  1169. PHPUnit_Framework_Assert::isType('string')
  1170. );
  1171. $this->assertTrue($constraint->evaluate(0, '', true));
  1172. $this->assertFalse($constraint->evaluate('', '', true));
  1173. $this->assertEquals('is not of type "string"', $constraint->toString());
  1174. $this->assertEquals(1, count($constraint));
  1175. try {
  1176. $constraint->evaluate('');
  1177. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1178. $this->assertEquals(
  1179. <<<EOF
  1180. Failed asserting that '' is not of type "string".
  1181. EOF
  1182. ,
  1183. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1184. );
  1185. return;
  1186. }
  1187. $this->fail();
  1188. }
  1189. /**
  1190. * @covers PHPUnit_Framework_Constraint_IsType
  1191. * @covers PHPUnit_Framework_Constraint_Not
  1192. * @covers PHPUnit_Framework_Assert::isType
  1193. * @covers PHPUnit_Framework_Assert::logicalNot
  1194. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1195. */
  1196. public function testConstraintIsNotType2()
  1197. {
  1198. $constraint = PHPUnit_Framework_Assert::logicalNot(
  1199. PHPUnit_Framework_Assert::isType('string')
  1200. );
  1201. try {
  1202. $constraint->evaluate('', 'custom message');
  1203. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1204. $this->assertEquals(<<<EOF
  1205. custom message
  1206. Failed asserting that '' is not of type "string".
  1207. EOF
  1208. ,
  1209. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1210. );
  1211. return;
  1212. }
  1213. $this->fail();
  1214. }
  1215. /**
  1216. * @covers PHPUnit_Framework_Constraint_IsNull
  1217. * @covers PHPUnit_Framework_Assert::isNull
  1218. * @covers PHPUnit_Framework_Constraint::count
  1219. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1220. */
  1221. public function testConstraintIsNull()
  1222. {
  1223. $constraint = PHPUnit_Framework_Assert::isNull();
  1224. $this->assertFalse($constraint->evaluate(0, '', true));
  1225. $this->assertTrue($constraint->evaluate(null, '', true));
  1226. $this->assertEquals('is null', $constraint->toString());
  1227. $this->assertEquals(1, count($constraint));
  1228. try {
  1229. $constraint->evaluate(0);
  1230. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1231. $this->assertEquals(<<<EOF
  1232. Failed asserting that 0 is null.
  1233. EOF
  1234. ,
  1235. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1236. );
  1237. return;
  1238. }
  1239. $this->fail();
  1240. }
  1241. /**
  1242. * @covers PHPUnit_Framework_Constraint_IsNull
  1243. * @covers PHPUnit_Framework_Assert::isNull
  1244. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1245. */
  1246. public function testConstraintIsNull2()
  1247. {
  1248. $constraint = PHPUnit_Framework_Assert::isNull();
  1249. try {
  1250. $constraint->evaluate(0, 'custom message');
  1251. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1252. $this->assertEquals(<<<EOF
  1253. custom message
  1254. Failed asserting that 0 is null.
  1255. EOF
  1256. ,
  1257. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1258. );
  1259. return;
  1260. }
  1261. $this->fail();
  1262. }
  1263. /**
  1264. * @covers PHPUnit_Framework_Constraint_IsNull
  1265. * @covers PHPUnit_Framework_Constraint_Not
  1266. * @covers PHPUnit_Framework_Assert::isNull
  1267. * @covers PHPUnit_Framework_Assert::logicalNot
  1268. * @covers PHPUnit_Framework_Constraint::count
  1269. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1270. */
  1271. public function testConstraintIsNotNull()
  1272. {
  1273. $constraint = PHPUnit_Framework_Assert::logicalNot(
  1274. PHPUnit_Framework_Assert::isNull()
  1275. );
  1276. $this->assertFalse($constraint->evaluate(null, '', true));
  1277. $this->assertTrue($constraint->evaluate(0, '', true));
  1278. $this->assertEquals('is not null', $constraint->toString());
  1279. $this->assertEquals(1, count($constraint));
  1280. try {
  1281. $constraint->evaluate(null);
  1282. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1283. $this->assertEquals(<<<EOF
  1284. Failed asserting that null is not null.
  1285. EOF
  1286. ,
  1287. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1288. );
  1289. return;
  1290. }
  1291. $this->fail();
  1292. }
  1293. /**
  1294. * @covers PHPUnit_Framework_Constraint_IsNull
  1295. * @covers PHPUnit_Framework_Constraint_Not
  1296. * @covers PHPUnit_Framework_Assert::isNull
  1297. * @covers PHPUnit_Framework_Assert::logicalNot
  1298. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1299. */
  1300. public function testConstraintIsNotNull2()
  1301. {
  1302. $constraint = PHPUnit_Framework_Assert::logicalNot(
  1303. PHPUnit_Framework_Assert::isNull()
  1304. );
  1305. try {
  1306. $constraint->evaluate(null, 'custom message');
  1307. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1308. $this->assertEquals(<<<EOF
  1309. custom message
  1310. Failed asserting that null is not null.
  1311. EOF
  1312. ,
  1313. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1314. );
  1315. return;
  1316. }
  1317. $this->fail();
  1318. }
  1319. /**
  1320. * @covers PHPUnit_Framework_Constraint_LessThan
  1321. * @covers PHPUnit_Framework_Assert::lessThan
  1322. * @covers PHPUnit_Framework_Constraint::count
  1323. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1324. */
  1325. public function testConstraintLessThan()
  1326. {
  1327. $constraint = PHPUnit_Framework_Assert::lessThan(1);
  1328. $this->assertTrue($constraint->evaluate(0, '', true));
  1329. $this->assertFalse($constraint->evaluate(1, '', true));
  1330. $this->assertEquals('is less than 1', $constraint->toString());
  1331. $this->assertEquals(1, count($constraint));
  1332. try {
  1333. $constraint->evaluate(1);
  1334. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1335. $this->assertEquals(
  1336. <<<EOF
  1337. Failed asserting that 1 is less than 1.
  1338. EOF
  1339. ,
  1340. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1341. );
  1342. return;
  1343. }
  1344. $this->fail();
  1345. }
  1346. /**
  1347. * @covers PHPUnit_Framework_Constraint_LessThan
  1348. * @covers PHPUnit_Framework_Assert::lessThan
  1349. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1350. */
  1351. public function testConstraintLessThan2()
  1352. {
  1353. $constraint = PHPUnit_Framework_Assert::lessThan(1);
  1354. try {
  1355. $constraint->evaluate(1, 'custom message');
  1356. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1357. $this->assertEquals(
  1358. <<<EOF
  1359. custom message
  1360. Failed asserting that 1 is less than 1.
  1361. EOF
  1362. ,
  1363. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1364. );
  1365. return;
  1366. }
  1367. $this->fail();
  1368. }
  1369. /**
  1370. * @covers PHPUnit_Framework_Constraint_LessThan
  1371. * @covers PHPUnit_Framework_Constraint_Not
  1372. * @covers PHPUnit_Framework_Assert::lessThan
  1373. * @covers PHPUnit_Framework_Assert::logicalNot
  1374. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1375. */
  1376. public function testConstraintNotLessThan()
  1377. {
  1378. $constraint = PHPUnit_Framework_Assert::logicalNot(
  1379. PHPUnit_Framework_Assert::lessThan(1)
  1380. );
  1381. $this->assertTrue($constraint->evaluate(1, '', true));
  1382. $this->assertFalse($constraint->evaluate(0, '', true));
  1383. $this->assertEquals('is not less than 1', $constraint->toString());
  1384. $this->assertEquals(1, count($constraint));
  1385. try {
  1386. $constraint->evaluate(0);
  1387. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1388. $this->assertEquals(
  1389. <<<EOF
  1390. Failed asserting that 0 is not less than 1.
  1391. EOF
  1392. ,
  1393. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1394. );
  1395. return;
  1396. }
  1397. $this->fail();
  1398. }
  1399. /**
  1400. * @covers PHPUnit_Framework_Constraint_LessThan
  1401. * @covers PHPUnit_Framework_Constraint_Not
  1402. * @covers PHPUnit_Framework_Assert::lessThan
  1403. * @covers PHPUnit_Framework_Assert::logicalNot
  1404. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1405. */
  1406. public function testConstraintNotLessThan2()
  1407. {
  1408. $constraint = PHPUnit_Framework_Assert::logicalNot(
  1409. PHPUnit_Framework_Assert::lessThan(1)
  1410. );
  1411. try {
  1412. $constraint->evaluate(0, 'custom message');
  1413. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1414. $this->assertEquals(
  1415. <<<EOF
  1416. custom message
  1417. Failed asserting that 0 is not less than 1.
  1418. EOF
  1419. ,
  1420. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1421. );
  1422. return;
  1423. }
  1424. $this->fail();
  1425. }
  1426. /**
  1427. * @covers PHPUnit_Framework_Constraint_IsEqual
  1428. * @covers PHPUnit_Framework_Constraint_LessThan
  1429. * @covers PHPUnit_Framework_Constraint_Or
  1430. * @covers PHPUnit_Framework_Assert::lessThanOrEqual
  1431. * @covers PHPUnit_Framework_TestFailure::exceptionToString
  1432. */
  1433. public function testConstraintLessThanOrEqual()
  1434. {
  1435. $constraint = PHPUnit_Framework_Assert::lessThanOrEqual(1);
  1436. $this->assertTrue($constraint->evaluate(1, '', true));
  1437. $this->assertFalse($constraint->evaluate(2, '', true));
  1438. $this->assertEquals('is equal to 1 or is less than 1', $constraint->toString());
  1439. $this->assertEquals(2, count($constraint));
  1440. try {
  1441. $constraint->evaluate(2);
  1442. } catch (PHPUnit_Framework_ExpectationFailedException $e) {
  1443. $this->assertEquals(
  1444. <<<EOF
  1445. Failed asserting that 2 is equal to 1 or is less than 1.
  1446. EOF
  1447. ,
  1448. PHPUnit_Framework_TestFailure::exceptionToString($e)
  1449. );
  1450. return;
  1451. }
  1452. $this->fail();
  1453. }
  1454. /**
  1455. * @covers PHPUnit_Framework_Constraint_Callback
  1456. */
  1457. public function testConstraintCallback()
  1458. {
  1459. $closureReflect = function ($parameter) {
  1460. return $parameter;
  1461. };
  1462. $closureWithoutParameter = function () {
  1463. return true;
  1464. };
  1465. $constraint = PHPUnit_Framework_Assert::callback($closureWithoutParameter);
  1466. $this->assertTrue($constraint->evaluate('', '', true));
  1467. $constraint = PHPUnit_Framework_Assert::callback($closureReflect);
  1468. $this->assertTrue($constraint->evaluate(true, '', true));
  1469. $this->assertFalse($constraint->evaluate(false, '', true));
  1470. $callback = array($this, 'callbackReturningTrue');
  1471. $constraint = PHPUnit_Framework_Assert::callback($callback);