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

https://gitlab.com/daniruizcamacho/pfcascensores · PHP · 1804 lines · 1155 code · 305 blank · 344 comment · 0 complexity · db120c7a58a9646357cd29e3e9423c95 MD5 · raw file

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