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\Argument; |
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 Gorghoa\ScenarioStateBehatExtension\ScenarioStateInterface; |
19
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Vincent Chalamon <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ArgumentOrganiserTest extends \PHPUnit_Framework_TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ArgumentOrganiser |
28
|
|
|
*/ |
29
|
|
|
private $organiser; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ObjectProphecy|ArgumentOrganiser |
33
|
|
|
*/ |
34
|
|
|
private $organiserMock; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ObjectProphecy|ScenarioStateInitializer |
38
|
|
|
*/ |
39
|
|
|
private $initializerMock; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ObjectProphecy|ScenarioStateInterface |
43
|
|
|
*/ |
44
|
|
|
private $storeMock; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var ObjectProphecy|\ReflectionMethod |
48
|
|
|
*/ |
49
|
|
|
private $functionMock; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ObjectProphecy|Reader |
53
|
|
|
*/ |
54
|
|
|
private $readerMock; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var ObjectProphecy|ScenarioStateArgument |
58
|
|
|
*/ |
59
|
|
|
private $annotationMock; |
60
|
|
|
|
61
|
|
|
protected function setUp() |
62
|
|
|
{ |
63
|
|
|
$this->organiserMock = $this->prophesize(ArgumentOrganiser::class); |
64
|
|
|
$this->initializerMock = $this->prophesize(ScenarioStateInitializer::class); |
65
|
|
|
$this->storeMock = $this->prophesize(ScenarioStateInterface::class); |
66
|
|
|
$this->functionMock = $this->prophesize(\ReflectionMethod::class); |
67
|
|
|
$this->readerMock = $this->prophesize(Reader::class); |
68
|
|
|
$this->annotationMock = $this->prophesize(ScenarioStateArgument::class); |
69
|
|
|
|
70
|
|
|
$this->organiser = new ArgumentOrganiser( |
71
|
|
|
$this->organiserMock->reveal(), |
|
|
|
|
72
|
|
|
$this->initializerMock->reveal(), |
73
|
|
|
$this->readerMock->reveal() |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testOrganiseArguments() |
78
|
|
|
{ |
79
|
|
|
$this->functionMock->getParameters()->willReturn([ |
80
|
|
|
(object) ['name' => 'scenarioBanana'], |
81
|
|
|
(object) ['name' => 'gorilla'], |
82
|
|
|
(object) ['name' => 'foo'], |
83
|
|
|
])->shouldBeCalledTimes(1); |
84
|
|
|
|
85
|
|
|
$this->initializerMock->getStore()->willReturn($this->storeMock->reveal())->shouldBeCalledTimes(1); |
86
|
|
|
$this->readerMock->getMethodAnnotations($this->functionMock->reveal())->willReturn([ |
87
|
|
|
$this->annotationMock->reveal(), |
88
|
|
|
$this->annotationMock->reveal(), |
89
|
|
|
])->shouldBeCalledTimes(1); |
90
|
|
|
$this->annotationMock->getArgument() |
91
|
|
|
->willReturn('scenarioBanana', 'scenarioBanana', 'gorilla', 'gorilla') |
92
|
|
|
->shouldBeCalled(); |
93
|
|
|
$this->annotationMock->getName() |
94
|
|
|
->willReturn('scenarioBanana', 'scenarioBanana', 'scenarioBanana', 'scenarioGorilla', 'scenarioGorilla', 'scenarioGorilla') |
95
|
|
|
->shouldBeCalled(); |
96
|
|
|
$this->storeMock->hasStateFragment('scenarioBanana')->willReturn(true)->shouldBeCalledTimes(1); |
97
|
|
|
$this->storeMock->hasStateFragment('scenarioGorilla')->willReturn(true)->shouldBeCalledTimes(1); |
98
|
|
|
$this->storeMock->hasStateFragment('foo')->shouldNotBeCalled(); |
99
|
|
|
$this->storeMock->getStateFragment('scenarioBanana')->willReturn('Yummy banana!')->shouldBeCalledTimes(2); |
100
|
|
|
$this->storeMock->getStateFragment('scenarioGorilla')->willReturn('Bonobo')->shouldBeCalledTimes(2); |
101
|
|
|
$this->storeMock->getStateFragment('foo')->shouldNotBeCalled(); |
102
|
|
|
|
103
|
|
|
$this->organiserMock->organiseArguments($this->functionMock->reveal(), [ |
104
|
|
|
0 => 'scenarioBanana', |
105
|
|
|
1 => 'gorilla', |
106
|
|
|
'scenarioBanana' => 'Yummy banana!', |
107
|
|
|
2 => 'Yummy banana!', |
108
|
|
|
'gorilla' => 'Bonobo', |
109
|
|
|
3 => 'Bonobo', |
110
|
|
|
])->shouldBeCalledTimes(1); |
111
|
|
|
|
112
|
|
|
$this->organiser->organiseArguments($this->functionMock->reveal(), ['scenarioBanana', 'gorilla']); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.