Test Failed
Pull Request — master (#132)
by Alessandro
03:15
created

BaseIntegrationTestCase::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests;
6
7
use Paraunit\Configuration\ParallelConfiguration;
8
use Paraunit\Configuration\TempFilenameFactory;
9
use Paraunit\File\Cleaner;
10
use Paraunit\File\TempDirectory;
11
use Paraunit\Lifecycle\ProcessEvent;
12
use Paraunit\Parser\JSON\LogParser;
13
use Prophecy\Argument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Tests\Stub\PHPUnitJSONLogOutput\JSONLogStub;
18
use Tests\Stub\StubbedParaunitProcess;
19
use Tests\Stub\UnformattedOutputStub;
20
21
/**
22
 * Class BaseIntegrationTestCase
23
 * @package Paraunit\Tests
24
 */
25
abstract class BaseIntegrationTestCase extends BaseTestCase
26
{
27
    /** @var ContainerBuilder|null */
28
    private $container;
29
30
    /** @var ParallelConfiguration */
31
    protected $configuration;
32
33
    /** @var string */
34
    protected $textFilter;
35
36
    /** @var string[] */
37
    private $options;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function __construct($name = null, array $data = [], $dataName = '')
43
    {
44
        parent::__construct($name, $data, $dataName);
45
46
        $this->configuration = new ParallelConfiguration(true);
47
        $this->options = [];
48
        $this->setOption('configuration', $this->getStubPath() . DIRECTORY_SEPARATOR . 'phpunit_for_stubs.xml');
49
    }
50
51
    protected function setUp()
52
    {
53
        parent::setUp();
54
55
        $this->cleanUpTempDirForThisExecution();
56
    }
57
58
    protected function tearDown()
59
    {
60
        $this->cleanUpTempDirForThisExecution();
61
62
        parent::tearDown();
63
    }
64
65
    /**
66
     * @param StubbedParaunitProcess $process
67
     * @param string $stubLog
68
     */
69
    protected function createLogForProcessFromStubbedLog(StubbedParaunitProcess $process, string $stubLog): void
70
    {
71
        $stubLogFilename = __DIR__ . '/Stub/PHPUnitJSONLogOutput/' . $stubLog . '.json';
72
        $this->assertFileExists($stubLogFilename, 'Stub log file missing! ' . $stubLogFilename);
73
74
        /** @var TempFilenameFactory $filenameService */
75
        $filenameService = $this->getService(TempFilenameFactory::class);
76
        $filename = $filenameService->getFilenameForLog($process->getUniqueId());
77
78
        copy($stubLogFilename, $filename);
79
    }
80
81
    protected function cleanUpTempDirForThisExecution(): void
82
    {
83
        if ($this->container) {
84
            /** @var TempDirectory $tempDirectory */
85
            $tempDirectory = $this->getService(TempDirectory::class);
86
            Cleaner::cleanUpDir($tempDirectory->getTempDirForThisExecution());
87
        }
88
    }
89
90
    protected function assertOutputOrder(UnformattedOutputStub $output, array $strings): void
91
    {
92
        $previousPosition = 0;
93
        $previousString = '<beginning of output>';
94
        foreach ($strings as $string) {
95
            $position = strpos($output->getOutput(), $string, $previousPosition);
96
            $this->assertNotFalse($position, $output->getOutput() . PHP_EOL . 'String not found: ' . $string);
97
            $this->assertGreaterThan(
98
                $previousPosition,
99
                $position,
100
                'Failed asserting that "' . $string . '" comes after "' . $previousString . '"' . $output->getOutput()
101
            );
102
            $previousString = $string;
103
            $previousPosition = $position;
104
        }
105
    }
106
107
    protected function processAllTheStubLogs(): void
108
    {
109
        /** @var LogParser $logParser */
110
        $logParser = $this->getService(LogParser::class);
111
112
        $logsToBeProcessed = [
113
            JSONLogStub::TWO_ERRORS_TWO_FAILURES,
114
            JSONLogStub::ALL_GREEN,
115
            JSONLogStub::ONE_ERROR,
116
            JSONLogStub::ONE_INCOMPLETE,
117
            JSONLogStub::ONE_RISKY,
118
            JSONLogStub::ONE_SKIP,
119
            JSONLogStub::ONE_WARNING,
120
            JSONLogStub::FATAL_ERROR,
121
            JSONLogStub::SEGFAULT,
122
            JSONLogStub::UNKNOWN,
123
        ];
124
125
        $process = new StubbedParaunitProcess();
126
        $processEvent = new ProcessEvent($process);
127
128
        foreach ($logsToBeProcessed as $logName) {
129
            $process->setFilename($logName . '.php');
130
            $this->createLogForProcessFromStubbedLog($process, $logName);
131
            $logParser->onProcessTerminated($processEvent);
132
        }
133
    }
134
135
    /**
136
     * @param string $serviceName
137
     * @return object
138
     * @throws \Exception
139
     */
140
    public function getService(string $serviceName)
141
    {
142
        return $this->container->get(sprintf(ParallelConfiguration::PUBLIC_ALIAS_FORMAT, $serviceName));
143
    }
144
145
    /**
146
     * @param string $parameterName
147
     * @return mixed
148
     * @throws \Exception
149
     */
150
    public function getParameter(string $parameterName)
151
    {
152
        return $this->container->getParameter($parameterName);
153
    }
154
155
    protected function loadContainer(): void
156
    {
157
        $input = $this->prophesize(InputInterface::class);
158
        $input->getArgument('stringFilter')
159
            ->willReturn($this->textFilter);
160
        $input->getOption('parallel')
161
            ->willReturn(10);
162
        $input->getOption('logo')
163
            ->willReturn(false);
164
        $input->getOption(Argument::cetera())
165
            ->willReturn(null);
166
        $input->hasParameterOption(Argument::cetera())
167
            ->willReturn(false);
168
169
        foreach ($this->options as $name => $value) {
170
            $input->getOption($name)
171
                ->shouldBeCalled()
172
                ->willReturn($value);
173
        }
174
175
        $this->container = $this->configuration->buildContainer($input->reveal(), new UnformattedOutputStub());
176
    }
177
178
    protected function getConsoleOutput(): UnformattedOutputStub
179
    {
180
        /** @var UnformattedOutputStub $output */
181
        $output = $this->getService(OutputInterface::class);
182
183
        return $output;
184
    }
185
186
    protected function setTextFilter(string $textFilter): void
187
    {
188
        $this->textFilter = $textFilter;
189
    }
190
191
    protected function setOption(string $optionName, string $optionValue): void
192
    {
193
        $this->options[$optionName] = $optionValue;
194
    }
195
}
196