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

wip: Decoupling argument injection from store business #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
composer.lock
.php_cs.cache
25 changes: 25 additions & 0 deletions argumentsExt/Annotation/StepInjectorArgument.php
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();
}
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;
}

Expand All @@ -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 &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!$annotation instanceof StepInjectorArgument ||
    !in_array($argument = $annotation->getArgument(), $paramsKeys)
) {
    continue;
}

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)
;
}
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions argumentsExt/Argument/StepArgumentHolder.php
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);
}
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]>
Expand Down
19 changes: 19 additions & 0 deletions argumentsExt/Exception/RejectedAnnotationException.php
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
{
}
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;
}

Expand All @@ -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)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StepInjectorArgument is not an annotation

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 &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!$annotation instanceof StepInjectorArgument ||
    !in_array($argument = $annotation->getArgument(), $paramsKeys)
) {
    continue;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
}
}
}

Expand Down
Loading