1 | <?php |
||||||
2 | |||||||
3 | /* |
||||||
4 | * This file is part of the doyo/code-coverage project. |
||||||
5 | * |
||||||
6 | * (c) Anthonius Munthi <https://itstoni.com> |
||||||
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 | declare(strict_types=1); |
||||||
13 | |||||||
14 | namespace Doyo\Bridge\CodeCoverage\Session; |
||||||
15 | |||||||
16 | use Doyo\Bridge\CodeCoverage\ContainerFactory; |
||||||
17 | use Doyo\Bridge\CodeCoverage\Exception\SessionException; |
||||||
18 | use Doyo\Bridge\CodeCoverage\Processor; |
||||||
19 | use Doyo\Bridge\CodeCoverage\ProcessorInterface; |
||||||
20 | use Doyo\Bridge\CodeCoverage\TestCase; |
||||||
21 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
22 | use SebastianBergmann\CodeCoverage\CodeCoverage; |
||||||
23 | use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
||||||
24 | |||||||
25 | abstract class AbstractSession implements SessionInterface, \Serializable |
||||||
26 | { |
||||||
27 | const CACHE_KEY = 'session'; |
||||||
28 | |||||||
29 | /** |
||||||
30 | * @var string |
||||||
31 | */ |
||||||
32 | protected $name; |
||||||
33 | |||||||
34 | /** |
||||||
35 | * @var TestCase|null |
||||||
36 | */ |
||||||
37 | protected $testCase; |
||||||
38 | |||||||
39 | /** |
||||||
40 | * @var FilesystemAdapter|null |
||||||
41 | */ |
||||||
42 | protected $adapter; |
||||||
43 | |||||||
44 | /** |
||||||
45 | * @var ProcessorInterface |
||||||
46 | */ |
||||||
47 | protected $processor; |
||||||
48 | |||||||
49 | /** |
||||||
50 | * @var array |
||||||
51 | */ |
||||||
52 | protected $exceptions = []; |
||||||
53 | |||||||
54 | /** |
||||||
55 | * @var array |
||||||
56 | */ |
||||||
57 | protected $config = []; |
||||||
58 | |||||||
59 | /** |
||||||
60 | * @var ContainerInterface|null |
||||||
61 | */ |
||||||
62 | protected $container; |
||||||
63 | |||||||
64 | /** |
||||||
65 | * @var ProcessorInterface|null |
||||||
66 | */ |
||||||
67 | protected $currentProcessor; |
||||||
68 | |||||||
69 | protected $cachedProperties = [ |
||||||
70 | 'name', |
||||||
71 | 'processor', |
||||||
72 | 'exceptions', |
||||||
73 | 'config', |
||||||
74 | 'testCase', |
||||||
75 | ]; |
||||||
76 | |||||||
77 | private $started = false; |
||||||
78 | |||||||
79 | /** |
||||||
80 | * AbstractSession constructor. |
||||||
81 | * |
||||||
82 | * @param string $name |
||||||
83 | * @param array $config |
||||||
84 | */ |
||||||
85 | 17 | public function __construct(string $name) |
|||||
86 | { |
||||||
87 | 17 | $dir = sys_get_temp_dir().'/doyo/code-coverage/sessions'; |
|||||
88 | 17 | $this->adapter = new FilesystemAdapter($name, 0, $dir); |
|||||
89 | 17 | $this->refresh(); |
|||||
90 | |||||||
91 | 17 | $this->name = $name; |
|||||
92 | |||||||
93 | 17 | register_shutdown_function([$this, 'shutdown']); |
|||||
94 | } |
||||||
95 | |||||||
96 | 9 | public function init(array $config) |
|||||
97 | { |
||||||
98 | 9 | $this->config = $config; |
|||||
99 | 9 | $this->createContainer($config); |
|||||
100 | 9 | $this->processor = $this->container->get('factory')->createProcessor(true); |
|||||
101 | 9 | $this->save(); |
|||||
102 | } |
||||||
103 | |||||||
104 | 3 | public function serialize() |
|||||
105 | { |
||||||
106 | 3 | $data = $this->toCache(); |
|||||
107 | |||||||
108 | 3 | return serialize($data); |
|||||
109 | } |
||||||
110 | |||||||
111 | 2 | public function unserialize($serialized) |
|||||
112 | { |
||||||
113 | 2 | $cache = unserialize($serialized); |
|||||
114 | 2 | $this->fromCache($cache); |
|||||
115 | } |
||||||
116 | |||||||
117 | /** |
||||||
118 | * {@inheritdoc} |
||||||
119 | */ |
||||||
120 | 3 | public function getName(): string |
|||||
121 | { |
||||||
122 | 3 | return $this->name; |
|||||
123 | } |
||||||
124 | |||||||
125 | /** |
||||||
126 | * {@inheritdoc} |
||||||
127 | */ |
||||||
128 | 8 | public function getProcessor() |
|||||
129 | { |
||||||
130 | 8 | return $this->processor; |
|||||
131 | } |
||||||
132 | |||||||
133 | 17 | public function refresh() |
|||||
134 | { |
||||||
135 | 17 | $adapter = $this->adapter; |
|||||
136 | |||||||
137 | 17 | $cached = $adapter->getItem(static::CACHE_KEY)->get(); |
|||||
0 ignored issues
–
show
|
|||||||
138 | |||||||
139 | 17 | if(!is_null($cached)){ |
|||||
140 | 13 | $this->fromCache($cached); |
|||||
141 | } |
||||||
142 | 17 | if(!empty($this->config)){ |
|||||
143 | 10 | $this->createContainer($this->config); |
|||||
144 | } |
||||||
145 | } |
||||||
146 | |||||||
147 | 15 | private function createContainer($config) |
|||||
148 | { |
||||||
149 | 15 | $container = (new ContainerFactory($config))->getContainer(); |
|||||
150 | 15 | $this->container = $container; |
|||||
151 | } |
||||||
152 | |||||||
153 | 12 | private function toCache() |
|||||
154 | { |
||||||
155 | 12 | $data = []; |
|||||
156 | |||||||
157 | 12 | foreach ($this->cachedProperties as $property) { |
|||||
158 | 12 | $data[$property] = $this->{$property}; |
|||||
159 | } |
||||||
160 | |||||||
161 | 12 | return $data; |
|||||
162 | } |
||||||
163 | |||||||
164 | 13 | private function fromCache(array $cache) |
|||||
165 | { |
||||||
166 | 13 | foreach ($cache as $name => $value) { |
|||||
167 | 13 | $this->{$name} = $value; |
|||||
168 | } |
||||||
169 | } |
||||||
170 | |||||||
171 | 12 | public function save() |
|||||
172 | { |
||||||
173 | 12 | $adapter = $this->adapter; |
|||||
174 | 12 | $item = $adapter->getItem(static::CACHE_KEY); |
|||||
175 | 12 | $data = $this->toCache(); |
|||||
176 | |||||||
177 | 12 | $item->set($data); |
|||||
178 | 12 | $adapter->save($item); |
|||||
179 | } |
||||||
180 | |||||||
181 | 3 | public function reset() |
|||||
182 | { |
||||||
183 | 3 | $this->testCase = null; |
|||||
184 | 3 | $this->exceptions = []; |
|||||
185 | |||||||
186 | 3 | if($this->processor instanceof ProcessorInterface){ |
|||||
0 ignored issues
–
show
|
|||||||
187 | 3 | $this->processor->clear(); |
|||||
188 | } |
||||||
189 | } |
||||||
190 | |||||||
191 | 4 | public function hasExceptions() |
|||||
192 | { |
||||||
193 | 4 | return \count($this->exceptions) > 0; |
|||||
194 | } |
||||||
195 | |||||||
196 | 2 | public function getExceptions() |
|||||
197 | { |
||||||
198 | 2 | return $this->exceptions; |
|||||
199 | } |
||||||
200 | |||||||
201 | 4 | public function addException(\Exception $exception) |
|||||
202 | { |
||||||
203 | 4 | $message = $exception->getMessage(); |
|||||
204 | 4 | $id = md5($message); |
|||||
205 | |||||||
206 | 4 | if (!isset($this->exceptions[$id])) { |
|||||
207 | 4 | $this->exceptions[$id] = $exception; |
|||||
208 | } |
||||||
209 | } |
||||||
210 | |||||||
211 | 5 | public function setTestCase(TestCase $testCase) |
|||||
212 | { |
||||||
213 | 5 | $this->testCase = $testCase; |
|||||
214 | } |
||||||
215 | |||||||
216 | /** |
||||||
217 | * @throws SessionException If TestCase is null |
||||||
218 | */ |
||||||
219 | 6 | public function start() |
|||||
220 | { |
||||||
221 | 6 | if (null === $this->testCase) { |
|||||
222 | 2 | throw new SessionException('Can not start coverage with null TestCase'); |
|||||
223 | } |
||||||
224 | |||||||
225 | try { |
||||||
226 | 4 | $container = $this->container; |
|||||
227 | 4 | $testCase = $this->testCase; |
|||||
228 | 4 | $filter = $this->getProcessor()->getCodeCoverage()->filter(); |
|||||
229 | 4 | $class = $container->getParameter('coverage.driver.class'); |
|||||
0 ignored issues
–
show
The method
getParameter() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
230 | 4 | $coverage = new CodeCoverage(new $class, $filter); |
|||||
231 | 4 | $processor = new Processor($coverage); |
|||||
232 | |||||||
233 | 4 | $processor->start($testCase); |
|||||
234 | 5 | $this->currentProcessor = $processor; |
|||||
235 | 5 | $this->started = true; |
|||||
236 | 1 | } catch (\Exception $exception) { |
|||||
237 | 1 | $this->started = false; |
|||||
238 | 1 | $message = sprintf( |
|||||
239 | 1 | "Can not start coverage on session %s. Error message:\n%s", |
|||||
240 | 1 | $this->getName(), |
|||||
241 | 1 | $exception->getMessage() |
|||||
242 | ); |
||||||
243 | 1 | $exception = new SessionException($message); |
|||||
244 | 1 | $this->addException($exception); |
|||||
245 | } |
||||||
246 | } |
||||||
247 | |||||||
248 | 3 | public function stop() |
|||||
249 | { |
||||||
250 | try{ |
||||||
251 | 3 | $this->currentProcessor->stop(); |
|||||
0 ignored issues
–
show
The method
stop() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
252 | 3 | $this->processor->merge($this->currentProcessor); |
|||||
253 | 2 | $this->started = false; |
|||||
254 | 1 | }catch (\Exception $exception){ |
|||||
255 | 1 | $message = sprintf( |
|||||
256 | 1 | "Can not stop coverage on session <comment>%s</comment>. Error message:\n<error>%s</error>", |
|||||
257 | 1 | $this->name, |
|||||
258 | 1 | $exception->getMessage() |
|||||
259 | ); |
||||||
260 | 1 | $e = new SessionException($message); |
|||||
261 | 1 | $this->addException($e); |
|||||
262 | } |
||||||
263 | } |
||||||
264 | |||||||
265 | 1 | public function shutdown() |
|||||
266 | { |
||||||
267 | 1 | if ($this->started) { |
|||||
268 | 1 | $this->stop(); |
|||||
269 | } |
||||||
270 | 1 | $this->save(); |
|||||
271 | } |
||||||
272 | } |
||||||
273 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.