Completed
Push — stable ( 8ba0c8...0d8cdb )
by Nuno
17:40 queued 15:16
created

PrinterContents::endTestSuite()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace NunoMaduro\Collision\Adapters\Phpunit;
4
5
use NunoMaduro\Collision\Exceptions\ShouldNotHappen;
6
use PHPUnit\Framework\AssertionFailedError;
7
use PHPUnit\Framework\Test;
8
use PHPUnit\Framework\TestCase;
9
use PHPUnit\Framework\TestSuite;
10
use PHPUnit\Framework\Warning;
11
use ReflectionObject;
12
use Symfony\Component\Console\Input\ArgvInput;
13
use Symfony\Component\Console\Output\ConsoleOutput;
14
use Throwable;
15
16
trait PrinterContents
17
{
18
    /**
19
     * Holds an instance of the style.
20
     *
21
     * Style is a class we use to interact with output.
22
     *
23
     * @var Style
24
     */
25
    private $style;
26
27
    /**
28
     * Holds the duration time of the test suite.
29
     *
30
     * @var Timer
31
     */
32
    private $timer;
33
34
    /**
35
     * Holds the state of the test
36
     * suite. The number of tests, etc.
37
     *
38
     * @var State
39
     */
40
    private $state;
41
42
    /**
43
     * If the test suite has ended before.
44
     *
45
     * @var bool
46
     */
47
    private $ended = false;
48
49
    /**
50
     * Creates a new instance of the listener.
51
     *
52
     * @param  ConsoleOutput  $output
53
     *
54
     * @throws \ReflectionException
55
     */
56 3
    public function __construct(ConsoleOutput $output = null)
57
    {
58 3
        if (intval(substr(\PHPUnit\Runner\Version::id(), 0, 1)) === 8) {
59 3
            parent::__construct();
60
        }
61
62 3
        $this->timer = Timer::start();
63
64 3
        $output = $output ?? new ConsoleOutput();
65 3
        ConfigureIO::of(new ArgvInput(), $output);
66
67 3
        $this->style = new Style($output);
68
        $dummyTest = new class extends TestCase {
69
        };
70
71 3
        $this->state = State::from($dummyTest);
0 ignored issues
show
Documentation Bug introduced by
It seems like \NunoMaduro\Collision\Ad...State::from($dummyTest) of type object<self> is incompatible with the declared type object<NunoMaduro\Collis...Adapters\Phpunit\State> of property $state.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72 3
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 View Code Duplication
    public function addError(Test $testCase, Throwable $throwable, float $time): void
0 ignored issues
show
Unused Code introduced by
The parameter $time is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $testCase = $this->testCaseFromTest($testCase);
80
81
        $this->state->add(TestResult::fromTestCase($testCase, TestResult::FAIL));
82
83
        $this->style->writeError($this->state, $throwable);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function addWarning(Test $testCase, Warning $warning, float $time): void
0 ignored issues
show
Unused Code introduced by
The parameter $time is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        $testCase = $this->testCaseFromTest($testCase);
92
93
        $this->state->add(TestResult::fromTestCase($testCase, TestResult::WARN, $warning->getMessage()));
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function addFailure(Test $testCase, AssertionFailedError $error, float $time): void
0 ignored issues
show
Unused Code introduced by
The parameter $time is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        $testCase = $this->testCaseFromTest($testCase);
102
103
        $this->state->add(TestResult::fromTestCase($testCase, TestResult::FAIL));
104
105
        $reflector = new ReflectionObject($error);
106
107
        if ($reflector->hasProperty('message')) {
108
            $message = trim((string) preg_replace("/\r|\n/", ' ', $error->getMessage()));
109
            $property = $reflector->getProperty('message');
110
            $property->setAccessible(true);
111
            $property->setValue($error, $message);
112
        }
113
114
        $this->style->writeError($this->state, $error);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function addIncompleteTest(Test $testCase, Throwable $t, float $time): void
0 ignored issues
show
Unused Code introduced by
The parameter $t is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $time is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        $testCase = $this->testCaseFromTest($testCase);
123
124
        $this->state->add(TestResult::fromTestCase($testCase, TestResult::INCOMPLETE));
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function addRiskyTest(Test $testCase, Throwable $t, float $time): void
0 ignored issues
show
Unused Code introduced by
The parameter $time is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        $testCase = $this->testCaseFromTest($testCase);
133
134
        $this->state->add(TestResult::fromTestCase($testCase, TestResult::RISKY, $t->getMessage()));
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function addSkippedTest(Test $testCase, Throwable $t, float $time): void
0 ignored issues
show
Unused Code introduced by
The parameter $time is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142
        $testCase = $this->testCaseFromTest($testCase);
143
144
        $this->state->add(TestResult::fromTestCase($testCase, TestResult::SKIPPED, $t->getMessage()));
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function startTestSuite(TestSuite $suite): void
151
    {
152
        if ($this->state->suiteTotalTests === null) {
153
            $this->state->suiteTotalTests = $suite->count();
154
        }
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function endTestSuite(TestSuite $suite): void
0 ignored issues
show
Unused Code introduced by
The parameter $suite is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
161
    {
162
        if (! $this->ended && $this->state->suiteTotalTests === $this->state->testSuiteTestsCount()) {
163
            $this->ended = true;
164
165
            $this->style->writeCurrentRecap($this->state);
166
167
            $this->style->updateFooter($this->state);
168
            $this->style->writeRecap($this->timer);
169
        }
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 1
    public function startTest(Test $testCase): void
176
    {
177 1
        $testCase = $this->testCaseFromTest($testCase);
178
179
        // Let's check first if the testCase is over.
180
        if ($this->state->testCaseHasChanged($testCase)) {
181
            $this->style->writeCurrentRecap($this->state);
182
183
            $this->state->moveTo($testCase);
184
        }
185
186
        $this->style->updateFooter($this->state, $testCase);
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192 View Code Duplication
    public function endTest(Test $testCase, float $time): void
0 ignored issues
show
Unused Code introduced by
The parameter $time is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
    {
194
        $testCase = $this->testCaseFromTest($testCase);
195
196
        if (! $this->state->existsInTestCase($testCase)) {
197
            $this->state->add(TestResult::fromTestCase($testCase, TestResult::PASS));
198
        }
199
    }
200
201
    /**
202
     * Intentionally left blank as we output things on events of the listener.
203
     *
204
     * @param  string  $content
205
     *
206
     * @return  void
207
     */
208
    public function write(string $content): void
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
209
    {
210
        // ..
211
    }
212
213
    /**
214
     * Returns a test case from the given test.
215
     *
216
     * Note: This printer is do not work with normal Test classes - only
217
     * with Test Case classes. Please report an issue if you think
218
     * this should work any other way.
219
     *
220
     * @param  Test  $test
221
     *
222
     * @return TestCase
223
     */
224 1
    private function testCaseFromTest(Test $test): TestCase
225
    {
226 1
        if (! $test instanceof TestCase) {
227 1
            throw new ShouldNotHappen();
228
        }
229
230
        return $test;
231
    }
232
}
233