This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
wip: Decoupling argument injection from store business #31
Open
gorghoa
wants to merge
4
commits into
master
Choose a base branch
from
research/30-decoupling
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
vendor | ||
composer.lock | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the StepArgumentInjectorBehatExtension project. | ||
* | ||
* (c) Rodrigue Villetard <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gorghoa\StepArgumentInjectorBehatExtension\Annotation; | ||
|
||
/** | ||
* @author Rodrigue Villetard <[email protected]> | ||
*/ | ||
interface StepInjectorArgument | ||
{ | ||
/** | ||
* The argument name this annotation is targeting to inject. | ||
* | ||
* @return string | ||
*/ | ||
public function getArgument(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ScenarioStateBehatExtension project. | ||
* This file is part of the StepArgumentInjectorBehatExtension project. | ||
* | ||
* (c) Rodrigue Villetard <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gorghoa\ScenarioStateBehatExtension\Argument; | ||
namespace Gorghoa\StepArgumentInjectorBehatExtension\Argument; | ||
|
||
use Behat\Testwork\Argument\ArgumentOrganiser; | ||
use Behat\Testwork\Argument\ArgumentOrganiser as BehatArgumentOrganiser; | ||
use Doctrine\Common\Annotations\Reader; | ||
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument; | ||
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer; | ||
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument; | ||
// use Gorghoa\StepArgumentInjectorBehatExtension\Context\Initializer\StepArgumentInjectorInitializer; | ||
use ReflectionFunctionAbstract; | ||
|
||
/** | ||
* @author Rodrigue Villetard <[email protected]> | ||
* @author Vincent Chalamon <[email protected]> | ||
*/ | ||
final class ScenarioStateArgumentOrganiser implements ArgumentOrganiser | ||
final class ArgumentOrganiser implements BehatArgumentOrganiser | ||
{ | ||
/** | ||
* @var ArgumentOrganiser | ||
* @var BehatArgumentOrganiser | ||
*/ | ||
private $baseOrganiser; | ||
|
||
/** | ||
* @var ScenarioStateInitializer | ||
* @var StepArgumentHolder[] | ||
*/ | ||
private $store; | ||
private $stepArgumentHolders; | ||
|
||
/** | ||
* @var Reader | ||
*/ | ||
private $reader; | ||
|
||
public function __construct(ArgumentOrganiser $organiser, ScenarioStateInitializer $store, Reader $reader) | ||
public function __construct(BehatArgumentOrganiser $organiser, array $stepArgumentHolders, Reader $reader) | ||
{ | ||
$this->baseOrganiser = $organiser; | ||
$this->store = $store; | ||
$this->stepArgumentHolders = $stepArgumentHolders; | ||
$this->reader = $reader; | ||
} | ||
|
||
|
@@ -59,16 +59,22 @@ public function organiseArguments(ReflectionFunctionAbstract $function, array $m | |
return $this->baseOrganiser->organiseArguments($function, $match); | ||
} | ||
|
||
/** @var ScenarioStateArgument[] $annotations */ | ||
$annotations = $this->reader->getMethodAnnotations($function); | ||
$store = $this->store->getStore(); | ||
|
||
foreach ($annotations as $annotation) { | ||
if ($annotation instanceof ScenarioStateArgument && | ||
in_array($annotation->getArgument(), $paramsKeys) && | ||
$store->hasStateFragment($annotation->getName()) | ||
if ($annotation instanceof StepInjectorArgument && | ||
in_array($argument = $annotation->getArgument(), $paramsKeys) | ||
) { | ||
$match[$annotation->getArgument()] = $store->getStateFragment($annotation->getName()); | ||
$match[strval(++$i)] = $store->getStateFragment($annotation->getName()); | ||
/* @var StepInjectorArgument $annotation */ | ||
foreach ($this->stepArgumentHolders as $hooker) { | ||
if ($hooker->doesHandleStepArgument($annotation)) { | ||
|
||
$match[$argument] | ||
= $match[strval(++$i)] | ||
= $hooker->getStepArgumentValueFor($annotation) | ||
; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the StepArgumentInjectorBehatExtension project. | ||
* | ||
* (c) Rodrigue Villetard <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gorghoa\StepArgumentInjectorBehatExtension\Argument; | ||
|
||
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument; | ||
|
||
/** | ||
* @author Rodrigue Villetard <[email protected]> | ||
*/ | ||
interface StepArgumentHolder | ||
{ | ||
/** | ||
* Check if an annotation is handled by the service. | ||
* | ||
* @param StepInjectorArgument $annotation | ||
* | ||
* @return bool | ||
*/ | ||
public function doesHandleStepArgument(StepInjectorArgument $annotation); | ||
|
||
/** | ||
* Get value to inject for a step argument. | ||
* | ||
* @param StepInjectorArgument $annotation | ||
* | ||
* @return mixed | ||
*/ | ||
public function getStepArgumentValueFor(StepInjectorArgument $annotation); | ||
} |
6 changes: 3 additions & 3 deletions
6
src/Call/Handler/RuntimeCallHandler.php → ...tsExt/Call/Handler/RuntimeCallHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ScenarioStateBehatExtension project. | ||
* This file is part of the StepArgumentInjectorBehatExtension project. | ||
* | ||
* (c) Rodrigue Villetard <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gorghoa\ScenarioStateBehatExtension\Call\Handler; | ||
namespace Gorghoa\StepArgumentInjectorBehatExtension\Call\Handler; | ||
|
||
use Behat\Behat\Transformation\Call\TransformationCall; | ||
use Behat\Testwork\Call\Call; | ||
use Behat\Testwork\Call\Handler\CallHandler; | ||
use Behat\Testwork\Environment\Call\EnvironmentCall; | ||
use Behat\Testwork\Hook\Call\HookCall; | ||
use Gorghoa\ScenarioStateBehatExtension\Resolver\ArgumentsResolver; | ||
use Gorghoa\StepArgumentInjectorBehatExtension\Resolver\ArgumentsResolver; | ||
|
||
/** | ||
* @author Vincent Chalamon <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the StepArgumentInjectorBehatExtension project. | ||
* | ||
* (c) Rodrigue Villetard <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gorghoa\StepArgumentInjectorBehatExtension\Exception; | ||
|
||
/** | ||
* @author Rodrigue Villetard <[email protected]> | ||
*/ | ||
class RejectedAnnotationException extends \LogicException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ScenarioStateBehatExtension project. | ||
* This file is part of the StepArgumentInjectorBehatExtension project. | ||
* | ||
* (c) Rodrigue Villetard <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gorghoa\ScenarioStateBehatExtension\Resolver; | ||
namespace Gorghoa\StepArgumentInjectorBehatExtension\Resolver; | ||
|
||
use Doctrine\Common\Annotations\Reader; | ||
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument; | ||
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer; | ||
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument; | ||
|
||
/** | ||
* @author Vincent Chalamon <[email protected]> | ||
*/ | ||
class ArgumentsResolver | ||
{ | ||
/** | ||
* @var ScenarioStateInitializer | ||
* @var Reader | ||
*/ | ||
private $store; | ||
private $reader; | ||
|
||
/** | ||
* @var Reader | ||
* @var StepArgumentHolder[] | ||
*/ | ||
private $reader; | ||
private $stepArgumentHolders; | ||
|
||
/** | ||
* @param ScenarioStateInitializer $store | ||
* @param Reader $reader | ||
* ArgumentsResolver constructor. | ||
* | ||
* @param StepArgumentHolder[] $stepArgumentHolders | ||
* @param Reader $reader | ||
*/ | ||
public function __construct(ScenarioStateInitializer $store, Reader $reader) | ||
public function __construct($stepArgumentHolders, Reader $reader) | ||
{ | ||
$this->store = $store; | ||
$this->stepArgumentHolders = $stepArgumentHolders; | ||
$this->reader = $reader; | ||
} | ||
|
||
|
@@ -48,25 +49,27 @@ public function __construct(ScenarioStateInitializer $store, Reader $reader) | |
*/ | ||
public function resolve(\ReflectionMethod $function, array $arguments) | ||
{ | ||
// No `@ScenarioStateArgument` annotation found | ||
if (null === $this->reader->getMethodAnnotation($function, ScenarioStateArgument::class)) { | ||
// No `@StepInjectorArgument` annotation found | ||
if (null === $this->reader->getMethodAnnotation($function, StepInjectorArgument::class)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return $arguments; | ||
} | ||
|
||
$paramsKeys = array_map(function (\ReflectionParameter $element) { | ||
return $element->getName(); | ||
}, $function->getParameters()); | ||
$store = $this->store->getStore(); | ||
|
||
// Prepare arguments from annotations | ||
/** @var ScenarioStateArgument[] $annotations */ | ||
$annotations = $this->reader->getMethodAnnotations($function); | ||
foreach ($annotations as $annotation) { | ||
if ($annotation instanceof ScenarioStateArgument && | ||
in_array($annotation->getArgument(), $paramsKeys) && | ||
$store->hasStateFragment($annotation->getName()) | ||
if ($annotation instanceof StepInjectorArgument && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (!$annotation instanceof StepInjectorArgument ||
!in_array($argument = $annotation->getArgument(), $paramsKeys)
) {
continue;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicated code from ArgumentOrganiser |
||
in_array($argument = $annotation->getArgument(), $paramsKeys) | ||
) { | ||
$arguments[$annotation->getArgument()] = $store->getStateFragment($annotation->getName()); | ||
/* @var StepArgumentInjectorArgument $annotation */ | ||
foreach ($this->stepArgumentHolders as $hooker) { | ||
if ($hooker->doesHandleStepArgument($annotation)) { | ||
$arguments[$argument] = $hooker->getStepArgumentValueFor($annotation); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.