Passed
Push — stable ( 3da3e1...b1f606 )
by Nuno
03:57 queued 02:04
created

src/Adapters/Phpunit/Listener.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 ReflectionObject;
15
use PHPUnit\Framework\Test;
16
use PHPUnit\Framework\Warning;
17
use Whoops\Exception\Inspector;
18
use NunoMaduro\Collision\Writer;
19
use PHPUnit\Framework\TestSuite;
20
use Symfony\Component\Console\Application;
21
use PHPUnit\Framework\AssertionFailedError;
22
use Symfony\Component\Console\Input\ArgvInput;
23
use Symfony\Component\Console\Output\ConsoleOutput;
24
use NunoMaduro\Collision\Contracts\Writer as WriterContract;
25
use NunoMaduro\Collision\Contracts\Adapters\Phpunit\Listener as ListenerContract;
26
27
if (class_exists(\PHPUnit\Runner\Version::class) && substr(\PHPUnit\Runner\Version::id(), 0, 2) === '7.') {
28
29
    /**
30
     * This is an Collision Phpunit Adapter implementation.
31
     *
32
     * @author Nuno Maduro <[email protected]>
33
     */
34
    class Listener implements ListenerContract
35
    {
36
        /**
37
         * Holds an instance of the writer.
38
         *
39
         * @var \NunoMaduro\Collision\Contracts\Writer
40
         */
41
        protected $writer;
42
43
        /**
44
         * Holds the exception found, if any.
45
         *
46
         * @var \Throwable|null
47
         */
48
        protected $exceptionFound;
49
50
        /**
51
         * Creates a new instance of the class.
52
         *
53
         * @param \NunoMaduro\Collision\Contracts\Writer|null $writer
54
         */
55 3
        public function __construct(WriterContract $writer = null)
56
        {
57 3
            $this->writer = $writer ?: $this->buildWriter();
58 3
        }
59
60
        /**
61
         * {@inheritdoc}
62
         */
63 1
        public function render(\Throwable $t)
64
        {
65 1
            $inspector = new Inspector($t);
66
67 1
            $this->writer->write($inspector);
68 1
        }
69
70
        /**
71
         * {@inheritdoc}
72
         */
73 1
        public function addError(Test $test, \Throwable $t, float $time): void
74
        {
75 1
            if ($this->exceptionFound === null) {
76 1
                $this->exceptionFound = $t;
77
            }
78 1
        }
79
80
        /**
81
         * {@inheritdoc}
82
         */
83 1
        public function addWarning(Test $test, Warning $t, float $time): void
84
        {
85 1
        }
86
87
        /**
88
         * {@inheritdoc}
89
         */
90 1
        public function addFailure(Test $test, AssertionFailedError $t, float $time): void
91
        {
92 1
            $this->writer->ignoreFilesIn(['/vendor/'])
93 1
            ->showTrace(false);
94
95 1
            if ($this->exceptionFound === null) {
96 1
                $this->exceptionFound = $t;
0 ignored issues
show
Documentation Bug introduced by
It seems like $t of type object<PHPUnit\Framework\AssertionFailedError> is incompatible with the declared type object<Throwable>|null of property $exceptionFound.

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...
97
            }
98 1
        }
99
100
        /**
101
         * {@inheritdoc}
102
         */
103 1
        public function addIncompleteTest(Test $test, \Throwable $t, float $time): void
104
        {
105 1
        }
106
107
        /**
108
         * {@inheritdoc}
109
         */
110 1
        public function addRiskyTest(Test $test, \Throwable $t, float $time): void
111
        {
112 1
        }
113
114
        /**
115
         * {@inheritdoc}
116
         */
117 1
        public function addSkippedTest(Test $test, \Throwable $t, float $time): void
118
        {
119 1
        }
120
121
        /**
122
         * {@inheritdoc}
123
         */
124 1
        public function startTestSuite(TestSuite $suite): void
125
        {
126 1
        }
127
128
        /**
129
         * {@inheritdoc}
130
         */
131 1
        public function endTestSuite(TestSuite $suite): void
132
        {
133 1
        }
134
135
        /**
136
         * {@inheritdoc}
137
         */
138 1
        public function startTest(Test $test): void
139
        {
140 1
        }
141
142
        /**
143
         * {@inheritdoc}
144
         */
145 1
        public function endTest(Test $test, float $time): void
146
        {
147 1
        }
148
149
        /**
150
         * {@inheritdoc}
151
         */
152 7
        public function __destruct()
153
        {
154 7
            if ($this->exceptionFound !== null) {
155 2
                $this->render($this->exceptionFound);
156
            }
157 7
        }
158
159
        /**
160
         * Builds an Writer.
161
         *
162
         * @return \NunoMaduro\Collision\Contracts\Writer
163
         */
164 1
        protected function buildWriter(): WriterContract
165
        {
166 1
            $writer = new Writer;
167
168 1
            $application = new Application();
169 1
            $reflector = new ReflectionObject($application);
170 1
            $method = $reflector->getMethod('configureIO');
171 1
            $method->setAccessible(true);
172 1
            $method->invoke($application, new ArgvInput, $output = new ConsoleOutput);
173
174 1
            return $writer->setOutput($output);
175
        }
176
    }
177
}
178