|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Discovery\Tests; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Composer\Command\BaseCommand; |
|
8
|
|
|
use Composer\Composer; |
|
9
|
|
|
use Composer\Installer\InstallationManager; |
|
10
|
|
|
use Composer\IO\IOInterface; |
|
11
|
|
|
use Composer\Package\Package; |
|
12
|
|
|
use Composer\Package\RootPackage; |
|
13
|
|
|
use Composer\Repository\RepositoryInterface; |
|
14
|
|
|
use Composer\Repository\RepositoryManager; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
18
|
|
|
|
|
19
|
|
|
abstract class AbstractDiscoveryTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
protected function getInstallationManagerMock(string $installPath = null) : InstallationManager |
|
22
|
|
|
{ |
|
23
|
|
|
$installationManager = $this->createMock(InstallationManager::class); |
|
24
|
|
|
|
|
25
|
|
|
// Configure the stub. |
|
26
|
|
|
$installationManager->method('getInstallPath') |
|
27
|
|
|
->willReturn(realpath($installPath ?? 'tests/fixtures/package_a')); |
|
28
|
|
|
|
|
29
|
|
|
return $installationManager; |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function getRepositoryMock() : RepositoryInterface |
|
33
|
|
|
{ |
|
34
|
|
|
$packageA = new Package('package/a', '1.0.0', '1.0.0'); |
|
35
|
|
|
|
|
36
|
|
|
$repository = $this->createMock(RepositoryInterface::class); |
|
37
|
|
|
$repository->method('getPackages')->willReturn([ $packageA ]); |
|
38
|
|
|
|
|
39
|
|
|
return $repository; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function getRepositoryManagerMock() : RepositoryManager |
|
43
|
|
|
{ |
|
44
|
|
|
$repositoryManager = $this->createMock(RepositoryManager::class); |
|
45
|
|
|
$repositoryManager->method('getLocalRepository')->willReturn($this->getRepositoryMock()); |
|
46
|
|
|
|
|
47
|
|
|
return $repositoryManager; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function getComposer() : Composer |
|
51
|
|
|
{ |
|
52
|
|
|
$composer = new Composer(); |
|
53
|
|
|
$composer->setInstallationManager($this->getInstallationManagerMock()); |
|
54
|
|
|
$composer->setRepositoryManager($this->getRepositoryManagerMock()); |
|
55
|
|
|
$composer->setPackage(new RootPackage('__root__', 'stable', '1.0')); |
|
56
|
|
|
|
|
57
|
|
|
return $composer; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function getIO() : IOInterface |
|
61
|
|
|
{ |
|
62
|
|
|
$io = $this->createMock(IOInterface::class); |
|
63
|
|
|
$io->method('writeError'); |
|
64
|
|
|
|
|
65
|
|
|
return $io; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Calls the command passed in parameter. Returns the output. |
|
70
|
|
|
* |
|
71
|
|
|
* @param BaseCommand $command |
|
72
|
|
|
* @param InputInterface $input |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function callCommand(BaseCommand $command, InputInterface $input) : string |
|
76
|
|
|
{ |
|
77
|
|
|
$command->setComposer($this->getComposer()); |
|
78
|
|
|
$command->setIO($this->getIO()); |
|
79
|
|
|
|
|
80
|
|
|
$output = new BufferedOutput(); |
|
81
|
|
|
|
|
82
|
|
|
$r = new \ReflectionMethod($command, 'execute'); |
|
83
|
|
|
$r->setAccessible(true); |
|
84
|
|
|
$r->invoke($command, $input, $output); |
|
85
|
|
|
|
|
86
|
|
|
return $output->fetch(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|