|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Codeception\Util; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Codeception\Configuration; |
|
8
|
|
|
use Codeception\Exception\ParseException; |
|
9
|
|
|
use Codeception\Test\Cest; |
|
10
|
|
|
use Codeception\TestInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ExpectationAnnotationParser |
|
13
|
|
|
{ |
|
14
|
|
|
const DEFAULT_EXPECTATIONS_PATH = "tests/_unique_expectations/"; |
|
15
|
|
|
const PATH_CONFIG_KEY = 'unique_expectations_path'; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* ExpectationAnnotationParser constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $config |
|
25
|
|
|
* |
|
26
|
|
|
* @throws \Codeception\Exception\ConfigurationException |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($config = []) |
|
29
|
|
|
{ |
|
30
|
|
|
if (empty($config)) { |
|
31
|
|
|
$config = Configuration::config(); |
|
32
|
|
|
} |
|
33
|
|
|
$this->config = $config; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param TestInterface|Cest $test |
|
39
|
|
|
* |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getExpectations(TestInterface $test) |
|
43
|
|
|
{ |
|
44
|
|
|
if (!$test instanceof Cest) { |
|
45
|
|
|
return []; |
|
46
|
|
|
} |
|
47
|
|
|
$expectations = Annotation::forMethod($test->getTestClass(), $test->getTestMethod())->fetchAll('expectation'); |
|
48
|
|
|
|
|
49
|
|
|
return array_map([$this, 'parseExpectation'], $expectations); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param $expectationsPath |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function getExpectationFullPath($path) |
|
58
|
|
|
{ |
|
59
|
|
|
$expectationsPath = isset($this->config['paths'][self::PATH_CONFIG_KEY]) ? $this->config['paths'][self::PATH_CONFIG_KEY] : self::DEFAULT_EXPECTATIONS_PATH; |
|
60
|
|
|
|
|
61
|
|
|
return codecept_root_dir($expectationsPath.$path); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $expectation |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
* @throws ParseException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function parseExpectation($expectation) |
|
71
|
|
|
{ |
|
72
|
|
|
$matches = []; |
|
73
|
|
|
$expectationRegex = '/\(?\"?(?<filePath>[a-zA-Z0-9_]+)(.json)?\"?\)?/'; |
|
74
|
|
|
preg_match($expectationRegex, $expectation, $matches); |
|
75
|
|
|
|
|
76
|
|
|
if (empty($matches)) { |
|
77
|
|
|
throw new ParseException("The 'expectation' annotation could not be parsed (found: '$expectation')"); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$expectationPath = $this->getExpectationFullPath("{$matches['filePath']}.json"); |
|
81
|
|
|
if (!file_exists($expectationPath)) { |
|
82
|
|
|
throw new ParseException("The expectation at $expectationPath could not be found "); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $expectationPath; |
|
86
|
|
|
} |
|
87
|
|
|
} |