/tests/Composer/Test/PolyfillTestCase.php

https://github.com/fabpot/composer · PHP · 762 lines · 351 code · 71 blank · 340 comment · 16 complexity · aa0884d8417885279f270d931057c20b MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test {
  12. use PHPUnit\Framework\TestCase;
  13. use PHPUnit\Framework\Constraint\IsEqual;
  14. use PHPUnit\Framework\Constraint\LogicalNot;
  15. use PHPUnit\Framework\Constraint\RegularExpression;
  16. use PHPUnit\Framework\Constraint\StringContains;
  17. use PHPUnit\Framework\Constraint\TraversableContains;
  18. if (method_exists('PHPUnit\Framework\TestCase', 'assertStringContainsString')) {
  19. abstract class PolyfillTestCase extends TestCase {}
  20. } else {
  21. abstract class PolyfillTestCase extends TestCase
  22. {
  23. // all the functions below are form https://github.com/symfony/phpunit-bridge/blob/bd341a45ef79b30918376e8b8e2279fac6894c3b/Legacy/PolyfillAssertTrait.php
  24. /**
  25. * @param string $message
  26. *
  27. * @return void
  28. */
  29. public static function assertIsInt($actual, $message = '')
  30. {
  31. static::assertInternalType('int', $actual, $message);
  32. }
  33. /**
  34. * @param string $message
  35. *
  36. * @return void
  37. */
  38. public static function assertIsNumeric($actual, $message = '')
  39. {
  40. static::assertInternalType('numeric', $actual, $message);
  41. }
  42. /**
  43. * @param string $message
  44. *
  45. * @return void
  46. */
  47. public static function assertIsObject($actual, $message = '')
  48. {
  49. static::assertInternalType('object', $actual, $message);
  50. }
  51. /**
  52. * @param string $message
  53. *
  54. * @return void
  55. */
  56. public static function assertIsResource($actual, $message = '')
  57. {
  58. static::assertInternalType('resource', $actual, $message);
  59. }
  60. /**
  61. * @param string $message
  62. *
  63. * @return void
  64. */
  65. public static function assertIsString($actual, $message = '')
  66. {
  67. static::assertInternalType('string', $actual, $message);
  68. }
  69. /**
  70. * @param string $message
  71. *
  72. * @return void
  73. */
  74. public static function assertIsScalar($actual, $message = '')
  75. {
  76. static::assertInternalType('scalar', $actual, $message);
  77. }
  78. /**
  79. * @param string $message
  80. *
  81. * @return void
  82. */
  83. public static function assertIsCallable($actual, $message = '')
  84. {
  85. static::assertInternalType('callable', $actual, $message);
  86. }
  87. /**
  88. * @param string $message
  89. *
  90. * @return void
  91. */
  92. public static function assertIsIterable($actual, $message = '')
  93. {
  94. static::assertInternalType('iterable', $actual, $message);
  95. }
  96. /**
  97. * @param string $needle
  98. * @param string $haystack
  99. * @param string $message
  100. *
  101. * @return void
  102. */
  103. public static function assertStringContainsString($needle, $haystack, $message = '')
  104. {
  105. $constraint = new StringContains($needle, false);
  106. static::assertThat($haystack, $constraint, $message);
  107. }
  108. /**
  109. * @param string $needle
  110. * @param string $haystack
  111. * @param string $message
  112. *
  113. * @return void
  114. */
  115. public static function assertStringContainsStringIgnoringCase($needle, $haystack, $message = '')
  116. {
  117. $constraint = new StringContains($needle, true);
  118. static::assertThat($haystack, $constraint, $message);
  119. }
  120. /**
  121. * @param string $needle
  122. * @param string $haystack
  123. * @param string $message
  124. *
  125. * @return void
  126. */
  127. public static function assertStringNotContainsString($needle, $haystack, $message = '')
  128. {
  129. $constraint = new LogicalNot(new StringContains($needle, false));
  130. static::assertThat($haystack, $constraint, $message);
  131. }
  132. /**
  133. * @param string $needle
  134. * @param string $haystack
  135. * @param string $message
  136. *
  137. * @return void
  138. */
  139. public static function assertStringNotContainsStringIgnoringCase($needle, $haystack, $message = '')
  140. {
  141. $constraint = new LogicalNot(new StringContains($needle, true));
  142. static::assertThat($haystack, $constraint, $message);
  143. }
  144. /**
  145. * @param string $message
  146. *
  147. * @return void
  148. */
  149. public static function assertFinite($actual, $message = '')
  150. {
  151. static::assertInternalType('float', $actual, $message);
  152. static::assertTrue(is_finite($actual), $message ? $message : "Failed asserting that $actual is finite.");
  153. }
  154. /**
  155. * @param string $message
  156. *
  157. * @return void
  158. */
  159. public static function assertInfinite($actual, $message = '')
  160. {
  161. static::assertInternalType('float', $actual, $message);
  162. static::assertTrue(is_infinite($actual), $message ? $message : "Failed asserting that $actual is infinite.");
  163. }
  164. /**
  165. * @param string $message
  166. *
  167. * @return void
  168. */
  169. public static function assertNan($actual, $message = '')
  170. {
  171. static::assertInternalType('float', $actual, $message);
  172. static::assertTrue(is_nan($actual), $message ? $message : "Failed asserting that $actual is nan.");
  173. }
  174. /**
  175. * @param string $filename
  176. * @param string $message
  177. *
  178. * @return void
  179. */
  180. public static function assertIsReadable($filename, $message = '')
  181. {
  182. static::assertInternalType('string', $filename, $message);
  183. static::assertTrue(is_readable($filename), $message ? $message : "Failed asserting that $filename is readable.");
  184. }
  185. /**
  186. * @param string $filename
  187. * @param string $message
  188. *
  189. * @return void
  190. */
  191. public static function assertNotIsReadable($filename, $message = '')
  192. {
  193. static::assertInternalType('string', $filename, $message);
  194. static::assertFalse(is_readable($filename), $message ? $message : "Failed asserting that $filename is not readable.");
  195. }
  196. /**
  197. * @param string $filename
  198. * @param string $message
  199. *
  200. * @return void
  201. */
  202. public static function assertIsNotReadable($filename, $message = '')
  203. {
  204. static::assertNotIsReadable($filename, $message);
  205. }
  206. /**
  207. * @param string $filename
  208. * @param string $message
  209. *
  210. * @return void
  211. */
  212. public static function assertIsWritable($filename, $message = '')
  213. {
  214. static::assertInternalType('string', $filename, $message);
  215. static::assertTrue(is_writable($filename), $message ? $message : "Failed asserting that $filename is writable.");
  216. }
  217. /**
  218. * @param string $filename
  219. * @param string $message
  220. *
  221. * @return void
  222. */
  223. public static function assertNotIsWritable($filename, $message = '')
  224. {
  225. static::assertInternalType('string', $filename, $message);
  226. static::assertFalse(is_writable($filename), $message ? $message : "Failed asserting that $filename is not writable.");
  227. }
  228. /**
  229. * @param string $filename
  230. * @param string $message
  231. *
  232. * @return void
  233. */
  234. public static function assertIsNotWritable($filename, $message = '')
  235. {
  236. static::assertNotIsWritable($filename, $message);
  237. }
  238. /**
  239. * @param string $directory
  240. * @param string $message
  241. *
  242. * @return void
  243. */
  244. public static function assertDirectoryExists($directory, $message = '')
  245. {
  246. static::assertInternalType('string', $directory, $message);
  247. static::assertTrue(is_dir($directory), $message ? $message : "Failed asserting that $directory exists.");
  248. }
  249. /**
  250. * @param string $directory
  251. * @param string $message
  252. *
  253. * @return void
  254. */
  255. public static function assertDirectoryNotExists($directory, $message = '')
  256. {
  257. static::assertInternalType('string', $directory, $message);
  258. static::assertFalse(is_dir($directory), $message ? $message : "Failed asserting that $directory does not exist.");
  259. }
  260. /**
  261. * @param string $directory
  262. * @param string $message
  263. *
  264. * @return void
  265. */
  266. public static function assertDirectoryDoesNotExist($directory, $message = '')
  267. {
  268. static::assertDirectoryNotExists($directory, $message);
  269. }
  270. /**
  271. * @param string $directory
  272. * @param string $message
  273. *
  274. * @return void
  275. */
  276. public static function assertDirectoryIsReadable($directory, $message = '')
  277. {
  278. static::assertDirectoryExists($directory, $message);
  279. static::assertIsReadable($directory, $message);
  280. }
  281. /**
  282. * @param string $directory
  283. * @param string $message
  284. *
  285. * @return void
  286. */
  287. public static function assertDirectoryNotIsReadable($directory, $message = '')
  288. {
  289. static::assertDirectoryExists($directory, $message);
  290. static::assertNotIsReadable($directory, $message);
  291. }
  292. /**
  293. * @param string $directory
  294. * @param string $message
  295. *
  296. * @return void
  297. */
  298. public static function assertDirectoryIsNotReadable($directory, $message = '')
  299. {
  300. static::assertDirectoryNotIsReadable($directory, $message);
  301. }
  302. /**
  303. * @param string $directory
  304. * @param string $message
  305. *
  306. * @return void
  307. */
  308. public static function assertDirectoryIsWritable($directory, $message = '')
  309. {
  310. static::assertDirectoryExists($directory, $message);
  311. static::assertIsWritable($directory, $message);
  312. }
  313. /**
  314. * @param string $directory
  315. * @param string $message
  316. *
  317. * @return void
  318. */
  319. public static function assertDirectoryNotIsWritable($directory, $message = '')
  320. {
  321. static::assertDirectoryExists($directory, $message);
  322. static::assertNotIsWritable($directory, $message);
  323. }
  324. /**
  325. * @param string $directory
  326. * @param string $message
  327. *
  328. * @return void
  329. */
  330. public static function assertDirectoryIsNotWritable($directory, $message = '')
  331. {
  332. static::assertDirectoryNotIsWritable($directory, $message);
  333. }
  334. /**
  335. * @param string $filename
  336. * @param string $message
  337. *
  338. * @return void
  339. */
  340. public static function assertFileExists($filename, $message = '')
  341. {
  342. static::assertInternalType('string', $filename, $message);
  343. static::assertTrue(file_exists($filename), $message ? $message : "Failed asserting that $filename exists.");
  344. }
  345. /**
  346. * @param string $filename
  347. * @param string $message
  348. *
  349. * @return void
  350. */
  351. public static function assertFileNotExists($filename, $message = '')
  352. {
  353. static::assertInternalType('string', $filename, $message);
  354. static::assertFalse(file_exists($filename), $message ? $message : "Failed asserting that $filename does not exist.");
  355. }
  356. /**
  357. * @param string $filename
  358. * @param string $message
  359. *
  360. * @return void
  361. */
  362. public static function assertFileDoesNotExist($filename, $message = '')
  363. {
  364. static::assertFileNotExists($filename, $message);
  365. }
  366. /**
  367. * @param string $filename
  368. * @param string $message
  369. *
  370. * @return void
  371. */
  372. public static function assertFileIsReadable($filename, $message = '')
  373. {
  374. static::assertFileExists($filename, $message);
  375. static::assertIsReadable($filename, $message);
  376. }
  377. /**
  378. * @param string $filename
  379. * @param string $message
  380. *
  381. * @return void
  382. */
  383. public static function assertFileNotIsReadable($filename, $message = '')
  384. {
  385. static::assertFileExists($filename, $message);
  386. static::assertNotIsReadable($filename, $message);
  387. }
  388. /**
  389. * @param string $filename
  390. * @param string $message
  391. *
  392. * @return void
  393. */
  394. public static function assertFileIsNotReadable($filename, $message = '')
  395. {
  396. static::assertFileNotIsReadable($filename, $message);
  397. }
  398. /**
  399. * @param string $filename
  400. * @param string $message
  401. *
  402. * @return void
  403. */
  404. public static function assertFileIsWritable($filename, $message = '')
  405. {
  406. static::assertFileExists($filename, $message);
  407. static::assertIsWritable($filename, $message);
  408. }
  409. /**
  410. * @param string $filename
  411. * @param string $message
  412. *
  413. * @return void
  414. */
  415. public static function assertFileNotIsWritable($filename, $message = '')
  416. {
  417. static::assertFileExists($filename, $message);
  418. static::assertNotIsWritable($filename, $message);
  419. }
  420. /**
  421. * @param string $filename
  422. * @param string $message
  423. *
  424. * @return void
  425. */
  426. public static function assertFileIsNotWritable($filename, $message = '')
  427. {
  428. static::assertFileNotIsWritable($filename, $message);
  429. }
  430. /**
  431. * @param string $pattern
  432. * @param string $string
  433. * @param string $message
  434. *
  435. * @return void
  436. */
  437. public static function assertMatchesRegularExpression($pattern, $string, $message = '')
  438. {
  439. static::assertRegExp($pattern, $string, $message);
  440. }
  441. /**
  442. * @param string $pattern
  443. * @param string $string
  444. * @param string $message
  445. *
  446. * @return void
  447. */
  448. public static function assertDoesNotMatchRegularExpression($pattern, $string, $message = '')
  449. {
  450. static::assertNotRegExp($pattern, $string, $message);
  451. }
  452. }
  453. }
  454. }
  455. namespace {
  456. foreach (array(
  457. 'PHPUnit\Framework\Constraint\IsEqual',
  458. 'PHPUnit\Framework\Constraint\StringContains',
  459. 'PHPUnit\Framework\Constraint\TraversableContains',
  460. ) as $class) {
  461. if (!class_exists($class) && class_exists(str_replace('\\', '_', $class))) {
  462. class_alias(str_replace('\\', '_', $class), $class);
  463. }
  464. }
  465. foreach (array(
  466. 'PHPUnit\Framework\SelfDescribing',
  467. ) as $interface) {
  468. if (!interface_exists($interface) && interface_exists(str_replace('\\', '_', $interface))) {
  469. class_alias(str_replace('\\', '_', $interface), $interface);
  470. }
  471. }
  472. if (!class_exists('PHPUnit\Framework\Constraint\Constraint')) {
  473. class_alias('PHPUnit_Framework_Constraint', 'PHPUnit\Framework\Constraint\Constraint');
  474. }
  475. }
  476. // all the code below taken from various PHPUnit versions to make things work on PHPUnit 4.8 / PHP 5.3
  477. /*
  478. * This file is part of PHPUnit.
  479. *
  480. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  481. *
  482. * For the full copyright and license information, please view the LICENSE
  483. * file that was distributed with this source code.
  484. */
  485. namespace PHPUnit\Framework\Constraint {
  486. use Countable;
  487. use PHPUnit\Framework\ExpectationFailedException;
  488. if (!class_exists('PHPUnit\Framework\Constraint\RegularExpression')) {
  489. /**
  490. * Constraint that asserts that the string it is evaluated for matches
  491. * a regular expression.
  492. *
  493. * Checks a given value using the Perl Compatible Regular Expression extension
  494. * in PHP. The pattern is matched by executing preg_match().
  495. *
  496. * The pattern string passed in the constructor.
  497. */
  498. class RegularExpression extends Constraint
  499. {
  500. /**
  501. * @var string
  502. */
  503. protected $pattern;
  504. /**
  505. * @param string $pattern
  506. */
  507. public function __construct($pattern)
  508. {
  509. parent::__construct();
  510. $this->pattern = $pattern;
  511. }
  512. /**
  513. * Evaluates the constraint for parameter $other. Returns true if the
  514. * constraint is met, false otherwise.
  515. *
  516. * @param mixed $other Value or object to evaluate.
  517. *
  518. * @return bool
  519. */
  520. protected function matches($other)
  521. {
  522. return \preg_match($this->pattern, $other) > 0;
  523. }
  524. /**
  525. * Returns a string representation of the constraint.
  526. *
  527. * @return string
  528. */
  529. public function toString()
  530. {
  531. return \sprintf(
  532. 'matches PCRE pattern "%s"',
  533. $this->pattern
  534. );
  535. }
  536. }
  537. }
  538. if (!class_exists('PHPUnit\Framework\Constraint\LogicalNot')) {
  539. /**
  540. * Logical NOT.
  541. */
  542. class LogicalNot extends Constraint
  543. {
  544. /**
  545. * @var Constraint
  546. */
  547. protected $constraint;
  548. /**
  549. * @param Constraint $constraint
  550. */
  551. public function __construct($constraint)
  552. {
  553. parent::__construct();
  554. if (!($constraint instanceof Constraint)) {
  555. $constraint = new IsEqual($constraint);
  556. }
  557. $this->constraint = $constraint;
  558. }
  559. /**
  560. * @param string $string
  561. *
  562. * @return string
  563. */
  564. public static function negate($string)
  565. {
  566. $positives = array(
  567. 'contains ',
  568. 'exists',
  569. 'has ',
  570. 'is ',
  571. 'are ',
  572. 'matches ',
  573. 'starts with ',
  574. 'ends with ',
  575. 'reference ',
  576. 'not not '
  577. );
  578. $negatives = array(
  579. 'does not contain ',
  580. 'does not exist',
  581. 'does not have ',
  582. 'is not ',
  583. 'are not ',
  584. 'does not match ',
  585. 'starts not with ',
  586. 'ends not with ',
  587. 'don\'t reference ',
  588. 'not '
  589. );
  590. \preg_match('/(\'[\w\W]*\')([\w\W]*)("[\w\W]*")/i', $string, $matches);
  591. if (\count($matches) > 0) {
  592. $nonInput = $matches[2];
  593. $negatedString = \str_replace(
  594. $nonInput,
  595. \str_replace(
  596. $positives,
  597. $negatives,
  598. $nonInput
  599. ),
  600. $string
  601. );
  602. } else {
  603. $negatedString = \str_replace(
  604. $positives,
  605. $negatives,
  606. $string
  607. );
  608. }
  609. return $negatedString;
  610. }
  611. /**
  612. * Evaluates the constraint for parameter $other
  613. *
  614. * If $returnResult is set to false (the default), an exception is thrown
  615. * in case of a failure. null is returned otherwise.
  616. *
  617. * If $returnResult is true, the result of the evaluation is returned as
  618. * a boolean value instead: true in case of success, false in case of a
  619. * failure.
  620. *
  621. * @param mixed $other Value or object to evaluate.
  622. * @param string $description Additional information about the test
  623. * @param bool $returnResult Whether to return a result or throw an exception
  624. *
  625. * @return mixed
  626. *
  627. * @throws ExpectationFailedException
  628. */
  629. public function evaluate($other, $description = '', $returnResult = false)
  630. {
  631. $success = !$this->constraint->evaluate($other, $description, true);
  632. if ($returnResult) {
  633. return $success;
  634. }
  635. if (!$success) {
  636. $this->fail($other, $description);
  637. }
  638. }
  639. /**
  640. * Returns the description of the failure
  641. *
  642. * The beginning of failure messages is "Failed asserting that" in most
  643. * cases. This method should return the second part of that sentence.
  644. *
  645. * @param mixed $other Evaluated value or object.
  646. *
  647. * @return string
  648. */
  649. protected function failureDescription($other)
  650. {
  651. switch (\get_class($this->constraint)) {
  652. case 'PHPUnit\Framework\Constraint\LogicalAnd':
  653. case 'PHPUnit\Framework\Constraint\LogicalNot':
  654. case 'PHPUnit\Framework\Constraint\LogicalOr':
  655. return 'not( ' . $this->constraint->failureDescription($other) . ' )';
  656. default:
  657. return self::negate(
  658. $this->constraint->failureDescription($other)
  659. );
  660. }
  661. }
  662. /**
  663. * Returns a string representation of the constraint.
  664. *
  665. * @return string
  666. */
  667. public function toString()
  668. {
  669. switch (\get_class($this->constraint)) {
  670. case 'PHPUnit\Framework\Constraint\LogicalAnd':
  671. case 'PHPUnit\Framework\Constraint\LogicalNot':
  672. case 'PHPUnit\Framework\Constraint\LogicalOr':
  673. return 'not( ' . $this->constraint->toString() . ' )';
  674. default:
  675. return self::negate(
  676. $this->constraint->toString()
  677. );
  678. }
  679. }
  680. /**
  681. * Counts the number of constraint elements.
  682. *
  683. * @return int
  684. */
  685. public function count()
  686. {
  687. return \count($this->constraint);
  688. }
  689. }
  690. }
  691. }