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

Commit ae55309

Browse files
author
Rodrigue Villetard
committed
Delegate annotation handling to hooked services
1 parent a7bbc31 commit ae55309

File tree

7 files changed

+88
-29
lines changed

7 files changed

+88
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor
22
composer.lock
3+
.php_cs.cache

argumentsExt/Annotation/StepInjectorArgument.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
interface StepInjectorArgument
1818
{
1919
/**
20-
* @return string
21-
*/
22-
public function getName();
23-
24-
/**
20+
* The argument name this annotation is targeting to inject.
21+
*
2522
* @return string
2623
*/
2724
public function getArgument();

argumentsExt/Argument/ArgumentOrganiser.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Behat\Testwork\Argument\ArgumentOrganiser as BehatArgumentOrganiser;
1515
use Doctrine\Common\Annotations\Reader;
16-
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepArgumentInjectorArgument;
1716
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument;
1817
// use Gorghoa\StepArgumentInjectorBehatExtension\Context\Initializer\StepArgumentInjectorInitializer;
1918
use ReflectionFunctionAbstract;
@@ -30,19 +29,19 @@ final class ArgumentOrganiser implements BehatArgumentOrganiser
3029
private $baseOrganiser;
3130

3231
/**
33-
* @var StepArgumentInjectorInitializer
32+
* @var StepArgumentHolder[]
3433
*/
35-
private $hookers;
34+
private $stepArgumentHolders;
3635

3736
/**
3837
* @var Reader
3938
*/
4039
private $reader;
4140

42-
public function __construct(BehatArgumentOrganiser $organiser, array $hookers, Reader $reader)
41+
public function __construct(BehatArgumentOrganiser $organiser, array $stepArgumentHolders, Reader $reader)
4342
{
4443
$this->baseOrganiser = $organiser;
45-
$this->hookers = $hookers;
44+
$this->stepArgumentHolders = $stepArgumentHolders;
4645
$this->reader = $reader;
4746
}
4847

@@ -60,18 +59,18 @@ public function organiseArguments(ReflectionFunctionAbstract $function, array $m
6059
return $this->baseOrganiser->organiseArguments($function, $match);
6160
}
6261

63-
/** @var StepArgumentInjectorArgument[] $annotations */
6462
$annotations = $this->reader->getMethodAnnotations($function);
6563

6664
foreach ($annotations as $annotation) {
6765
if ($annotation instanceof StepInjectorArgument &&
6866
in_array($annotation->getArgument(), $paramsKeys)
6967
) {
70-
foreach ($this->hookers as $hooker) {
71-
if ($hooker->hasStepArgumentFor($annotation->getName())) {
68+
/* @var StepInjectorArgument $annotation */
69+
foreach ($this->stepArgumentHolders as $hooker) {
70+
if ($hooker->doesHandleStepArgument($annotation)) {
7271
$match[$annotation->getArgument()]
7372
= $match[strval(++$i)]
74-
= $hooker->getStepArgumentFor($annotation->getName())
73+
= $hooker->getStepArgumentValueFor($annotation)
7574
;
7675
}
7776
}

argumentsExt/Argument/StepArgumentHolder.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,28 @@
1111

1212
namespace Gorghoa\StepArgumentInjectorBehatExtension\Argument;
1313

14+
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument;
15+
1416
/**
1517
* @author Rodrigue Villetard <rodrigue.villetard@gmail.com>
1618
*/
1719
interface StepArgumentHolder
1820
{
19-
public function hasStepArgumentFor($key);
21+
/**
22+
* Check if an annotation is handled by the service.
23+
*
24+
* @param StepInjectorArgument $annotation
25+
*
26+
* @return bool
27+
*/
28+
public function doesHandleStepArgument(StepInjectorArgument $annotation);
2029

21-
public function getStepArgumentFor($key);
30+
/**
31+
* Get value to inject for a step argument.
32+
*
33+
* @param StepInjectorArgument $annotation
34+
*
35+
* @return mixed
36+
*/
37+
public function getStepArgumentValueFor(StepInjectorArgument $annotation);
2238
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\Exception;
13+
14+
/**
15+
* @author Rodrigue Villetard <rodrigue.villetard@gmail.com>
16+
*/
17+
class RejectedAnnotationException extends \LogicException
18+
{
19+
}

argumentsExt/Resolver/ArgumentsResolver.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ class ArgumentsResolver
2525
private $reader;
2626

2727
/**
28-
* @var array
28+
* @var StepArgumentHolder[]
2929
*/
30-
private $hookers;
30+
private $stepArgumentHolders;
3131

3232
/**
33-
* @param Reader $reader
33+
* ArgumentsResolver constructor.
34+
*
35+
* @param StepArgumentHolder[] $stepArgumentHolders
36+
* @param Reader $reader
3437
*/
35-
public function __construct(array $hookers, Reader $reader)
38+
public function __construct($stepArgumentHolders, Reader $reader)
3639
{
37-
$this->hookers = $hookers;
40+
$this->stepArgumentHolders = $stepArgumentHolders;
3841
$this->reader = $reader;
3942
}
4043

@@ -56,15 +59,15 @@ public function resolve(\ReflectionMethod $function, array $arguments)
5659
}, $function->getParameters());
5760

5861
// Prepare arguments from annotations
59-
/** @var StepArgumentInjectorArgument[] $annotations */
6062
$annotations = $this->reader->getMethodAnnotations($function);
6163
foreach ($annotations as $annotation) {
6264
if ($annotation instanceof StepInjectorArgument &&
6365
in_array($annotation->getArgument(), $paramsKeys)
6466
) {
65-
foreach ($this->hookers as $hooker) {
66-
if ($hooker->hasStepArgumentFor($annotation->getName())) {
67-
$arguments[$annotation->getArgument()] = $hooker->getStepArgumentFor($annotation->getName());
67+
/* @var StepArgumentInjectorArgument $annotation */
68+
foreach ($this->stepArgumentHolders as $hooker) {
69+
if ($hooker->doesHandleStepArgument($annotation)) {
70+
$arguments[$annotation->getArgument()] = $hooker->getStepArgumentValueFor($annotation);
6871
}
6972
}
7073
}

src/Context/Initializer/ScenarioStateInitializer.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
use Behat\Behat\Context\Context;
1515
use Behat\Behat\Context\Initializer\ContextInitializer;
1616
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
17+
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument;
1718
use Gorghoa\ScenarioStateBehatExtension\Context\ScenarioStateAwareContext;
1819
use Gorghoa\ScenarioStateBehatExtension\ScenarioState;
1920
use Gorghoa\ScenarioStateBehatExtension\ScenarioStateInterface;
21+
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument;
2022
use Gorghoa\StepArgumentInjectorBehatExtension\Argument\StepArgumentHolder;
23+
use Gorghoa\StepArgumentInjectorBehatExtension\Exception\RejectedAnnotationException;
2124
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2225

2326
/**
@@ -70,13 +73,34 @@ public function getStore()
7073
return $this->store;
7174
}
7275

73-
public function hasStepArgumentFor($key)
76+
/**
77+
* Test if this service should handle specific argument injection.
78+
*
79+
* @param StepInjectorArgument $annotation
80+
*
81+
* @return bool
82+
*/
83+
public function doesHandleStepArgument(StepInjectorArgument $annotation)
7484
{
75-
return $this->store->hasStateFragment($key);
85+
return $annotation instanceof ScenarioStateArgument && $this->store->hasStateFragment($annotation->getName());
7686
}
7787

78-
public function getStepArgumentFor($key)
88+
/**
89+
* Get an argument value to inject.
90+
*
91+
* @param StepInjectorArgument $annotation
92+
*
93+
* @throws RejectedAnnotationException
94+
*
95+
* @return mixed
96+
*/
97+
public function getStepArgumentValueFor(StepInjectorArgument $annotation)
7998
{
80-
return $this->store->getStateFragment($key);
99+
if (!($annotation instanceof ScenarioStateArgument)) {
100+
$class = get_class($annotation);
101+
throw new RejectedAnnotationException("$class not handled by ScenarioStateBehatExtension");
102+
}
103+
104+
return $this->store->getStateFragment($annotation->getName());
81105
}
82106
}

0 commit comments

Comments
 (0)