TestCase.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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;
  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 Symfony\Component\Process\ExecutableFinder;
  18. abstract class TestCase extends \PHPUnit_Framework_TestCase
  19. {
  20. private static $parser;
  21. private static $executableCache = array();
  22. public static function getUniqueTmpDirectory()
  23. {
  24. $attempts = 5;
  25. $root = sys_get_temp_dir();
  26. do {
  27. $unique = $root . DIRECTORY_SEPARATOR . uniqid('composer-test-' . rand(1000, 9000));
  28. if (!file_exists($unique) && Silencer::call('mkdir', $unique, 0777)) {
  29. return realpath($unique);
  30. }
  31. } while (--$attempts);
  32. throw new \RuntimeException('Failed to create a unique temporary directory.');
  33. }
  34. protected static function getVersionParser()
  35. {
  36. if (!self::$parser) {
  37. self::$parser = new VersionParser();
  38. }
  39. return self::$parser;
  40. }
  41. protected function getVersionConstraint($operator, $version)
  42. {
  43. $constraint = new Constraint(
  44. $operator,
  45. self::getVersionParser()->normalize($version)
  46. );
  47. $constraint->setPrettyString($operator.' '.$version);
  48. return $constraint;
  49. }
  50. protected function getPackage($name, $version, $class = 'Composer\Package\Package')
  51. {
  52. $normVersion = self::getVersionParser()->normalize($version);
  53. return new $class($name, $normVersion, $version);
  54. }
  55. protected function getAliasPackage($package, $version)
  56. {
  57. $normVersion = self::getVersionParser()->normalize($version);
  58. return new AliasPackage($package, $normVersion, $version);
  59. }
  60. protected static function ensureDirectoryExistsAndClear($directory)
  61. {
  62. $fs = new Filesystem();
  63. if (is_dir($directory)) {
  64. $fs->removeDirectory($directory);
  65. }
  66. mkdir($directory, 0777, true);
  67. }
  68. /**
  69. * Check whether or not the given name is an available executable.
  70. *
  71. * @param string $executableName The name of the binary to test.
  72. *
  73. * @throws \PHPUnit_Framework_SkippedTestError
  74. */
  75. protected function skipIfNotExecutable($executableName)
  76. {
  77. if (!isset(self::$executableCache[$executableName])) {
  78. $finder = new ExecutableFinder();
  79. self::$executableCache[$executableName] = (bool) $finder->find($executableName);
  80. }
  81. if (false === self::$executableCache[$executableName]) {
  82. $this->markTestSkipped($executableName . ' is not found or not executable.');
  83. }
  84. }
  85. }