ThinkTesting.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\composer;
  12. use Composer\Package\PackageInterface;
  13. use Composer\Repository\InstalledRepositoryInterface;
  14. use InvalidArgumentException;
  15. class ThinkTesting extends LibraryInstaller
  16. {
  17. /**
  18. * {@inheritDoc}
  19. */
  20. public function getInstallPath(PackageInterface $package)
  21. {
  22. if ('topthink/think-testing' !== $package->getPrettyName()) {
  23. throw new InvalidArgumentException('Unable to install this library!');
  24. }
  25. return parent::getInstallPath($package);
  26. }
  27. public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
  28. {
  29. parent::install($repo, $package)->then(function () use ($package) {
  30. $this->copyTestDir($package);
  31. });
  32. }
  33. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  34. {
  35. return parent::update($repo, $initial, $target)->then(function () use ($target) {
  36. $this->copyTestDir($target);
  37. });
  38. }
  39. private function copyTestDir(PackageInterface $package)
  40. {
  41. $appDir = dirname($this->vendorDir);
  42. $source = $this->getInstallPath($package) . DIRECTORY_SEPARATOR . 'example';
  43. if (!is_file($appDir . DIRECTORY_SEPARATOR . 'phpunit.xml')) {
  44. $this->filesystem->copyThenRemove($source, $appDir);
  45. } else {
  46. $this->filesystem->removeDirectoryPhp($source);
  47. }
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function supports($packageType)
  53. {
  54. return 'think-testing' === $packageType;
  55. }
  56. }