|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mcustiel\Phiremock\Codeception\Util; |
|
4
|
|
|
|
|
5
|
|
|
use Codeception\Exception\ParseException; |
|
6
|
|
|
use Codeception\Test\Cest; |
|
7
|
|
|
use Codeception\TestInterface; |
|
8
|
|
|
use Codeception\Util\Annotation; |
|
9
|
|
|
|
|
10
|
|
|
class ExpectationAnnotationParser |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var array */ |
|
13
|
|
|
private $expectationsPath; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(string $expectationsPath) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->expectationsPath = $expectationsPath; |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param TestInterface|Cest $test |
|
22
|
|
|
* |
|
23
|
|
|
* @return array |
|
24
|
|
|
* @throws ParseException |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getExpectations(TestInterface $test): array |
|
27
|
|
|
{ |
|
28
|
|
|
if (!$test instanceof Cest) { |
|
29
|
|
|
return []; |
|
30
|
|
|
} |
|
31
|
|
|
$expectations = Annotation::forMethod($test->getTestClass(), $test->getTestMethod())->fetchAll('expectation'); |
|
32
|
|
|
|
|
33
|
|
|
return array_map([$this, 'parseExpectation'], $expectations); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** @throws ParseException */ |
|
37
|
|
|
public function parseExpectation(string $expectationAnnotation): string |
|
38
|
|
|
{ |
|
39
|
|
|
$matches = []; |
|
40
|
|
|
$expectationRegex = '/\(?\"?(?<filePath>[a-zA-Z0-9_\\/]+)(.json)?\"?\)?/'; |
|
41
|
|
|
preg_match($expectationRegex, $expectationAnnotation, $matches); |
|
42
|
|
|
|
|
43
|
|
|
if (empty($matches)) { |
|
44
|
|
|
throw new ParseException("The 'expectation' annotation could not be parsed (found: '$expectationAnnotation')"); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$expectationPath = $this->getExpectationFullPath("{$matches['filePath']}.json"); |
|
48
|
|
|
if (!file_exists($expectationPath)) { |
|
49
|
|
|
throw new ParseException("The expectation at $expectationPath could not be found "); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $expectationPath; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function getExpectationFullPath($path): string |
|
56
|
|
|
{ |
|
57
|
|
|
return sprintf('%s/%s', $this->expectationsPath, $path); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
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..