TestCase.php 4.6 KB

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