1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ScenarioStateBehatExtension project. |
5
|
|
|
* |
6
|
|
|
* (c) Rodrigue Villetard <[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 Gorghoa\ScenarioStateBehatExtension; |
13
|
|
|
|
14
|
|
|
use Behat\Testwork\Argument\ArgumentOrganiser; |
15
|
|
|
use Doctrine\Common\Annotations\Reader; |
16
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument; |
17
|
|
|
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer; |
18
|
|
|
use ReflectionFunctionAbstract; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Rodrigue Villetard <[email protected]> |
22
|
|
|
* @author Vincent Chalamon <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class ScenarioStateArgumentOrganiser implements ArgumentOrganiser |
25
|
|
|
{ |
26
|
|
|
private $baseOrganiser; |
27
|
|
|
private $store; |
28
|
|
|
private $reader; |
29
|
|
|
|
30
|
|
|
public function __construct(ArgumentOrganiser $organiser, ScenarioStateInitializer $store, Reader $reader) |
31
|
|
|
{ |
32
|
|
|
$this->baseOrganiser = $organiser; |
33
|
|
|
$this->store = $store; |
34
|
|
|
$this->reader = $reader; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function organiseArguments(ReflectionFunctionAbstract $function, array $match) |
41
|
|
|
{ |
42
|
|
|
$i = array_slice(array_keys($match), -1, 1)[0]; |
43
|
|
|
$paramsKeys = array_map(function ($element) { |
44
|
|
|
return $element->name; |
45
|
|
|
}, $function->getParameters()); |
46
|
|
|
|
47
|
|
|
$store = $this->store->getStore(); |
48
|
|
|
|
49
|
|
|
/** @var ScenarioStateArgument[] $annotations */ |
50
|
|
|
$annotations = $this->reader->getMethodAnnotations($function); |
|
|
|
|
51
|
|
|
foreach ($annotations as $annotation) { |
52
|
|
|
if ($annotation instanceof ScenarioStateArgument && |
53
|
|
|
in_array($annotation->getArgument(), $paramsKeys) && |
54
|
|
|
$store->hasStateFragment($annotation->getName()) |
55
|
|
|
) { |
56
|
|
|
$match[$annotation->getArgument()] = $store->getStateFragment($annotation->getName()); |
57
|
|
|
$match[strval(++$i)] = $store->getStateFragment($annotation->getName()); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->baseOrganiser->organiseArguments($function, $match); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.