Skip to content
This repository was archived by the owner on Jul 29, 2022. It is now read-only.

Commit 9b21c84

Browse files
author
Rodrigue Villetard
committed
Decouple phpunit tests
1 parent ae55309 commit 9b21c84

File tree

7 files changed

+148
-190
lines changed

7 files changed

+148
-190
lines changed

argumentsExt/Argument/ArgumentOrganiser.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ public function organiseArguments(ReflectionFunctionAbstract $function, array $m
6363

6464
foreach ($annotations as $annotation) {
6565
if ($annotation instanceof StepInjectorArgument &&
66-
in_array($annotation->getArgument(), $paramsKeys)
66+
in_array($argument = $annotation->getArgument(), $paramsKeys)
6767
) {
6868
/* @var StepInjectorArgument $annotation */
6969
foreach ($this->stepArgumentHolders as $hooker) {
7070
if ($hooker->doesHandleStepArgument($annotation)) {
71-
$match[$annotation->getArgument()]
71+
72+
$match[$argument]
7273
= $match[strval(++$i)]
7374
= $hooker->getStepArgumentValueFor($annotation)
7475
;

argumentsExt/Resolver/ArgumentsResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct($stepArgumentHolders, Reader $reader)
4949
*/
5050
public function resolve(\ReflectionMethod $function, array $arguments)
5151
{
52-
// No `@StepArgumentInjectorArgument` annotation found
52+
// No `@StepInjectorArgument` annotation found
5353
if (null === $this->reader->getMethodAnnotation($function, StepInjectorArgument::class)) {
5454
return $arguments;
5555
}
@@ -62,12 +62,12 @@ public function resolve(\ReflectionMethod $function, array $arguments)
6262
$annotations = $this->reader->getMethodAnnotations($function);
6363
foreach ($annotations as $annotation) {
6464
if ($annotation instanceof StepInjectorArgument &&
65-
in_array($annotation->getArgument(), $paramsKeys)
65+
in_array($argument = $annotation->getArgument(), $paramsKeys)
6666
) {
6767
/* @var StepArgumentInjectorArgument $annotation */
6868
foreach ($this->stepArgumentHolders as $hooker) {
6969
if ($hooker->doesHandleStepArgument($annotation)) {
70-
$arguments[$annotation->getArgument()] = $hooker->getStepArgumentValueFor($annotation);
70+
$arguments[$argument] = $hooker->getStepArgumentValueFor($annotation);
7171
}
7272
}
7373
}

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
<testsuite name="ScenarioStateBehatExtension Test Suite">
1515
<directory>tests</directory>
1616
</testsuite>
17+
<testsuite name="StepArgumentInjectorBehatExtension Test Suite">
18+
<directory>testsSAI</directory>
19+
</testsuite>
1720
</testsuites>
1821
</phpunit>

testsSAI/Annotation/ScenarioStateArgumentTest.php

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the StepArgumentInjectorBehatExtension project.
5+
*
6+
* (c) Rodrigue Villetard <rodrigue.villetard@gmail.com>
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\StepArgumentInjectorBehatExtension\Argument;
13+
14+
use Doctrine\Common\Annotations\Reader;
15+
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepArgumentInjectorArgument;
16+
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument;
17+
use Prophecy\Prophecy\ObjectProphecy;
18+
use Behat\Testwork\Argument\ArgumentOrganiser as BehatArgumentOrganiser;
19+
20+
/**
21+
* @author Vincent Chalamon <vincentchalamon@gmail.com>
22+
* @author Rodrigue Villetard <rodrigue.villetard@gmail.com>
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
38+
*/
39+
private $initializerMock;
40+
41+
/**
42+
* @var ObjectProphecy|\ReflectionMethod
43+
*/
44+
private $functionMock;
45+
46+
/**
47+
* @var ObjectProphecy|Reader
48+
*/
49+
private $readerMock;
50+
51+
/**
52+
* @var StepArgumentHolder
53+
*/
54+
private $holderMock;
55+
56+
/**
57+
* @var ObjectProphecy|StepArgumentInjectorArgument
58+
*/
59+
private $annotationMock;
60+
61+
protected function setUp()
62+
{
63+
$this->organiserMock = $this->prophesize(BehatArgumentOrganiser::class);
64+
$this->functionMock = $this->prophesize(\ReflectionMethod::class);
65+
$this->readerMock = $this->prophesize(Reader::class);
66+
$this->annotationMock = function () { return $this->prophesize(StepInjectorArgument::class); };
67+
$this->holderMock = $this->prophesize(StepArgumentHolder::class);
68+
69+
$this->organiser = new ArgumentOrganiser(
70+
$this->organiserMock->reveal(),
71+
[$this->holderMock->reveal()],
72+
$this->readerMock->reveal()
73+
);
74+
}
75+
76+
public function testOrganiseArguments()
77+
{
78+
$this->functionMock->getParameters()->willReturn([
79+
(object) ['name' => 'scenarioBanana'], // argument with injector annotation and **a service hold** value
80+
(object) ['name' => 'gorilla'], // argument with injector annotation but **no service hold** value
81+
(object) ['name' => 'foo'], // argument not handled by this extension
82+
])->shouldBeCalledTimes(1);
83+
84+
/*
85+
$this->annotationMock->getArgument()
86+
->willReturn('scenarioBanana', 'gorilla')
87+
->shouldBeCalled();
88+
*/
89+
90+
$annot1 = ($this->annotationMock)();
91+
$annot1->getArgument()->willReturn('scenarioBanana')->shouldBeCalledTimes(1);
92+
$annot1->reveal();
93+
94+
$annot2 = ($this->annotationMock)();
95+
$annot2->getArgument()->willReturn('gorilla')->shouldBeCalledTimes(1);
96+
$annot2->reveal();
97+
98+
$this->readerMock->getMethodAnnotations($this->functionMock->reveal())->willReturn([
99+
$annot1,
100+
$annot2,
101+
])->shouldBeCalledTimes(1);
102+
103+
$this->holderMock->doesHandleStepArgument($annot1)->willReturn(true);
104+
$this->holderMock->doesHandleStepArgument($annot2)->willReturn(false);
105+
106+
$this->holderMock->getStepArgumentValueFor($annot1)->willReturn('yammyBanana')->shouldBeCalledTimes(1);
107+
$this->holderMock->getStepArgumentValueFor($annot2)->shouldNotBeCalled();
108+
109+
$this->holderMock->getStepArgumentValueFor($annot2);
110+
111+
$this->organiserMock->organiseArguments($this->functionMock->reveal(), [
112+
0 => 'scenarioBanana',
113+
1 => 'gorilla',
114+
'scenarioBanana' => 'yammyBanana',
115+
2 => 'yammyBanana',
116+
])->shouldBeCalledTimes(1);
117+
118+
$this->organiser->organiseArguments($this->functionMock->reveal(), ['scenarioBanana', 'gorilla']);
119+
}
120+
}

testsSAI/Argument/ScenarioStateArgumentOrganiserTest.php

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)