Passed
Push — stable ( 85ed36...e7655b )
by Nuno
12:43 queued 11s
created

Listener::addIncompleteTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
/**
4
 * This file is part of Collision.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace NunoMaduro\Collision\Adapters\Phpunit;
13
14
use NunoMaduro\Collision\Exceptions\ShouldNotHappen;
15
use PHPUnit\Framework\AssertionFailedError;
16
use PHPUnit\Framework\Test;
17
use PHPUnit\Framework\TestCase;
18
use PHPUnit\Framework\TestListener;
19
use PHPUnit\Framework\TestSuite;
20
use PHPUnit\Framework\Warning;
21
use ReflectionObject;
22
use Symfony\Component\Console\Input\ArgvInput;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Output\ConsoleOutput;
25
use Throwable;
26
use PHPUnit\Util\Printer;
27
28
/**
29
 * This `if` condition exists because phpunit
30
 * is not a direct dependency of Collision.
31
 */
32
if (class_exists(\PHPUnit\Runner\Version::class) && intval(substr(\PHPUnit\Runner\Version::id(), 0, 1)) >= 8) {
33
34
    /**
35
     * This is an Collision Phpunit Adapter implementation.
36
     *
37
     * @internal
38
     */
39
    final class Listener extends Printer implements TestListener
0 ignored issues
show
Deprecated Code introduced by
The interface PHPUnit\Framework\TestListener has been deprecated with message: Use the `TestHook` interfaces instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
40
    {
41
        /**
42
         * Holds an instance of the console input.
43
         *
44
         * @var InputInterface
45
         */
46
        private $input;
47
48
        /**
49
         * Holds an instance of the console input.
50
         *
51
         * @var ConsoleOutput
52
         */
53
        private $output;
54
55
        /**
56
         * The current section, if any.
57
         *
58
         * @var Section
59
         */
60
        private $section;
61
62
        /**
63
         * Creates a new instance of the listener.
64
         *
65
         * @param  InputInterface  $input
66
         * @param  ConsoleOutput  $output
67
         *
68
         * @throws \ReflectionException
69
         */
70 3
        public function __construct(InputInterface $input = null, ConsoleOutput $output = null)
71
        {
72 3
            parent::__construct();
73
74 3
            $this->input = $input ?? new ArgvInput();
75 3
            $this->output = $output ?? new ConsoleOutput();
76 3
            ConfigureIO::of($this->input, $this->output);
77 3
            $this->section = Section::create($this->output, new TestSuite());
78 3
        }
79
80
        /**
81
         * @inheritdoc
82
         */
83
        public function addError(Test $test, \Throwable $throwable, float $time): void
84
        {
85
            $this->section->fail();
86
87
            OnError::display($this->output, $throwable);
88
        }
89
90
        /**
91
         * @inheritdoc
92
         */
93
        public function addWarning(Test $test, Warning $warning, float $time): void
94
        {
95
            $this->section->warn($warning);
96
        }
97
98
        /**
99
         * @inheritdoc
100
         */
101
        public function addFailure(Test $test, AssertionFailedError $error, float $time): void
102
        {
103
            $this->section->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
            OnError::display($this->output, $error);
115
        }
116
117
        /**
118
         * {@inheritdoc}
119
         */
120
        public function addIncompleteTest(Test $test, \Throwable $t, float $time): void
121
        {
122
            $this->section->incomplete($t);
123
        }
124
125
        /**
126
         * {@inheritdoc}
127
         */
128
        public function addRiskyTest(Test $test, \Throwable $t, float $time): void
129
        {
130
            $this->section->risky();
131
        }
132
133
        /**
134
         * {@inheritdoc}
135
         */
136
        public function addSkippedTest(Test $test, Throwable $t, float $time): void
137
        {
138
            $this->section->skipped($t);
139
        }
140
141
        /**
142
         * {@inheritdoc}
143
         */
144
        public function startTestSuite(TestSuite $suite): void
145
        {
146
            $this->section = Section::create($this->output, $suite);
147
        }
148
149
        /**
150
         * {@inheritdoc}
151
         */
152
        public function endTestSuite(TestSuite $suite): void
153
        {
154
            $this->section->end();
155
        }
156
157
        /**
158
         * {@inheritdoc}
159
         */
160 1
        public function startTest(Test $test): void
161
        {
162 1
            if (! $test instanceof TestCase) {
163 1
                throw new ShouldNotHappen();
164
            }
165
166
            $this->section->runs($test);
167
        }
168
169
        /**
170
         * {@inheritdoc}
171
         */
172
        public function endTest(Test $test, float $time): void
173
        {
174
            $this->section->pass();
175
        }
176
177
178
        /**
179
         * Intencionally left blank as we
180
         * output things on events of the
181
         * listener.
182
         *
183
         * @param  string  $content
184
         *
185
         * @return  void
186
         */
187
        public function write(string $content): void
188
        {
189
            //
190
        }
191
    }
192
}
193