TestCase.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Composer\Semver\VersionParser;
  13. use Composer\Package\AliasPackage;
  14. use Composer\Semver\Constraint\Constraint;
  15. use Composer\Util\Filesystem;
  16. use Composer\Util\Silencer;
  17. use PHPUnit\Framework\TestCase as BaseTestCase;
  18. use Symfony\Component\Process\ExecutableFinder;
  19. abstract class TestCase extends BaseTestCase
  20. {
  21. private static $parser;
  22. private static $executableCache = array();
  23. public static function getUniqueTmpDirectory()
  24. {
  25. $attempts = 5;
  26. $root = sys_get_temp_dir();
  27. do {
  28. $unique = $root . DIRECTORY_SEPARATOR . uniqid('composer-test-' . rand(1000, 9000));
  29. if (!file_exists($unique) && Silencer::call('mkdir', $unique, 0777)) {
  30. return realpath($unique);
  31. }
  32. } while (--$attempts);
  33. throw new \RuntimeException('Failed to create a unique temporary directory.');
  34. }
  35. protected static function getVersionParser()
  36. {
  37. if (!self::$parser) {
  38. self::$parser = new VersionParser();
  39. }
  40. return self::$parser;
  41. }
  42. protected function getVersionConstraint($operator, $version)
  43. {
  44. $constraint = new Constraint(
  45. $operator,
  46. self::getVersionParser()->normalize($version)
  47. );
  48. $constraint->setPrettyString($operator.' '.$version);
  49. return $constraint;
  50. }
  51. protected function getPackage($name, $version, $class = 'Composer\Package\Package')
  52. {
  53. $normVersion = self::getVersionParser()->normalize($version);
  54. return new $class($name, $normVersion, $version);
  55. }
  56. protected function getAliasPackage($package, $version)
  57. {
  58. $normVersion = self::getVersionParser()->normalize($version);
  59. return new AliasPackage($package, $normVersion, $version);
  60. }
  61. protected static function ensureDirectoryExistsAndClear($directory)
  62. {
  63. $fs = new Filesystem();
  64. if (is_dir($directory)) {
  65. $fs->removeDirectory($directory);
  66. }
  67. mkdir($directory, 0777, true);
  68. }
  69. /**
  70. * Check whether or not the given name is an available executable.
  71. *
  72. * @param string $executableName The name of the binary to test.
  73. *
  74. * @throws \PHPUnit_Framework_SkippedTestError
  75. */
  76. protected function skipIfNotExecutable($executableName)
  77. {
  78. if (!isset(self::$executableCache[$executableName])) {
  79. $finder = new ExecutableFinder();
  80. self::$executableCache[$executableName] = (bool) $finder->find($executableName);
  81. }
  82. if (false === self::$executableCache[$executableName]) {
  83. $this->markTestSkipped($executableName . ' is not found or not executable.');
  84. }
  85. }
  86. /**
  87. * @param string $exception
  88. * @param string|null $message
  89. * @param int|null $code
  90. */
  91. public function setExpectedException($exception, $message = null, $code = null)
  92. {
  93. if (!class_exists('PHPUnit\Framework\Error\Notice')) {
  94. $exception = str_replace('PHPUnit\\Framework\\Error\\', 'PHPUnit_Framework_Error_', $exception);
  95. }
  96. if (method_exists($this, 'expectException')) {
  97. $this->expectException($exception);
  98. if (null !== $message) {
  99. $this->expectExceptionMessage($message);
  100. }
  101. } else {
  102. parent::setExpectedException($exception, $message, $code);
  103. }
  104. }
  105. }