TestCase.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. abstract class TestCase extends \PHPUnit_Framework_TestCase
  17. {
  18. private static $parser;
  19. protected static function getVersionParser()
  20. {
  21. if (!self::$parser) {
  22. self::$parser = new VersionParser();
  23. }
  24. return self::$parser;
  25. }
  26. protected function getVersionConstraint($operator, $version)
  27. {
  28. $constraint = new Constraint(
  29. $operator,
  30. self::getVersionParser()->normalize($version)
  31. );
  32. $constraint->setPrettyString($operator.' '.$version);
  33. return $constraint;
  34. }
  35. protected function getPackage($name, $version, $class = 'Composer\Package\Package')
  36. {
  37. $normVersion = self::getVersionParser()->normalize($version);
  38. return new $class($name, $normVersion, $version);
  39. }
  40. protected function getAliasPackage($package, $version)
  41. {
  42. $normVersion = self::getVersionParser()->normalize($version);
  43. return new AliasPackage($package, $normVersion, $version);
  44. }
  45. protected static function getUniqueTmpDirectory()
  46. {
  47. $attempts = 5;
  48. $root = sys_get_temp_dir();
  49. do {
  50. $unique = $root . DIRECTORY_SEPARATOR . uniqid('composer-test-');
  51. if (!file_exists($unique) && false !== @mkdir($unique, 0777)) {
  52. return realpath($unique);
  53. }
  54. } while (--$attempts);
  55. throw new \RuntimeException('Failed to create a unique temporary directory.');
  56. }
  57. protected static function ensureDirectoryExistsAndClear($directory)
  58. {
  59. $fs = new Filesystem();
  60. if (is_dir($directory)) {
  61. $fs->removeDirectory($directory);
  62. }
  63. mkdir($directory, 0777, true);
  64. }
  65. }