Passed
Push — master ( 8bad73...c5d158 )
by David
57s queued 11s
created

AbstractDiscoveryTest::getComposer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $installationManager returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Composer\Installer\InstallationManager.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $repository returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Composer\Repository\RepositoryInterface.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $repositoryManager returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Composer\Repository\RepositoryManager.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $io returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Composer\IO\IOInterface.
Loading history...
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