1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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 Sylius\Behat\PageObjectExtension\Context\Argument; |
13
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Argument\ArgumentResolver; |
15
|
|
|
use SensioLabs\Behat\PageObjectExtension\PageObject\Element; |
16
|
|
|
use SensioLabs\Behat\PageObjectExtension\PageObject\Factory; |
17
|
|
|
use Sylius\Behat\PageObjectExtension\PageObject\Page; |
18
|
|
|
|
19
|
|
|
class PageObjectArgumentResolver implements ArgumentResolver |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Factory |
23
|
|
|
*/ |
24
|
|
|
private $factory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Factory $factory |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Factory $factory) |
30
|
|
|
{ |
31
|
|
|
$this->factory = $factory; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function resolveArguments(\ReflectionClass $classReflection, array $arguments) |
38
|
|
|
{ |
39
|
|
|
$parameters = $this->getConstructorParameters($classReflection); |
40
|
|
|
|
41
|
|
|
foreach ($parameters as $i => $parameter) { |
42
|
|
|
$parameterClassName = $this->getClassName($parameter); |
43
|
|
|
|
44
|
|
|
if (null !== $parameterClassName && $this->isPageOrElement($parameterClassName)) { |
45
|
|
|
$arguments[$i] = $this->factory->create($parameterClassName); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $arguments; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $className |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
private function isPageOrElement($className) |
58
|
|
|
{ |
59
|
|
|
return $this->isPage($className) || $this->isElement($className); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $className |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
private function isPage($className) |
68
|
|
|
{ |
69
|
|
|
return is_subclass_of($className, Page::class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $className |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
private function isElement($className) |
78
|
|
|
{ |
79
|
|
|
return is_subclass_of($className, Element::class); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param \ReflectionClass $classReflection |
84
|
|
|
* |
85
|
|
|
* @return \ReflectionParameter[] |
86
|
|
|
*/ |
87
|
|
|
private function getConstructorParameters(\ReflectionClass $classReflection) |
88
|
|
|
{ |
89
|
|
|
return $classReflection->getConstructor() ? $classReflection->getConstructor()->getParameters() : []; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \ReflectionParameter $parameter |
94
|
|
|
* |
95
|
|
|
* @return string|null |
96
|
|
|
*/ |
97
|
|
|
private function getClassName(\ReflectionParameter $parameter) |
98
|
|
|
{ |
99
|
|
|
return $parameter->getClass() ? $parameter->getClass()->getName() : null; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|