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

Commit

Permalink
Bump version for packagist
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon authored and Rodrigue Villetard committed Jul 26, 2017
1 parent 10de6c0 commit f5c2672
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 96 deletions.
29 changes: 29 additions & 0 deletions argumentsExt/Annotation/StepInjectorArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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 Vincent Chalamon <[email protected]>
*
*/
interface StepInjectorArgument
{
/**
* @return string
*/
public function getName();

/**
* @return string
*/
public function getArgument();
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
<?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\StepArgumentInjectorArgument;
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 StepArgumentInjectorInitializer
*/
private $store;
private $hookers;

/**
* @var Reader
*/
private $reader;

public function __construct(ArgumentOrganiser $organiser, ScenarioStateInitializer $store, Reader $reader)
public function __construct(BehatArgumentOrganiser $organiser, array $hookers, Reader $reader)
{
$this->baseOrganiser = $organiser;
$this->store = $store;
$this->hookers = $hookers;
$this->reader = $reader;
}

Expand All @@ -59,16 +60,21 @@ public function organiseArguments(ReflectionFunctionAbstract $function, array $m
return $this->baseOrganiser->organiseArguments($function, $match);
}

/** @var ScenarioStateArgument[] $annotations */
/** @var StepArgumentInjectorArgument[] $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($annotation->getArgument(), $paramsKeys)
) {
$match[$annotation->getArgument()] = $store->getStateFragment($annotation->getName());
$match[strval(++$i)] = $store->getStateFragment($annotation->getName());
foreach ($this->hookers as $hooker) {
if ($hooker->hasStepArgumentFor($annotation->getName())) {
$match[$annotation->getArgument()]
= $match[strval(++$i)]
= $hooker->getStepArgumentFor($annotation->getName())
;
}
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions argumentsExt/Argument/StepArgumentHolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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;


/**
* @author Rodrigue Villetard <[email protected]>
*/
interface StepArgumentHolder
{
public function hasStepArgumentFor($key);
public function getStepArgumentFor($key);
}
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
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
<?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;
use Gorghoa\StepArgumentInjectorBehatExtension\Argument\StepArgumentHolder;

/**
* @author Vincent Chalamon <[email protected]>
*/
class ArgumentsResolver
{
/**
* @var ScenarioStateInitializer
* @var Reader
*/
private $store;
private $reader;

/**
* @var Reader
* @var array
*/
private $reader;
private $hookers;

/**
* @param ScenarioStateInitializer $store
* @param Reader $reader
*/
public function __construct(ScenarioStateInitializer $store, Reader $reader)
public function __construct(array $hookers, Reader $reader)
{
$this->store = $store;
$this->hookers = $hookers;
$this->reader = $reader;
}

Expand All @@ -48,25 +47,28 @@ 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 `@StepArgumentInjectorArgument` annotation found
if (null === $this->reader->getMethodAnnotation($function, StepInjectorArgument::class)) {
return $arguments;
}

$paramsKeys = array_map(function (\ReflectionParameter $element) {
return $element->getName();
}, $function->getParameters());
$store = $this->store->getStore();


// Prepare arguments from annotations
/** @var ScenarioStateArgument[] $annotations */
/** @var StepArgumentInjectorArgument[] $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 &&
in_array($annotation->getArgument(), $paramsKeys)
) {
$arguments[$annotation->getArgument()] = $store->getStateFragment($annotation->getName());
foreach ($this->hookers as $hooker) {
if ($hooker->hasStepArgumentFor($annotation->getName())) {
$arguments[$annotation->getArgument()] = $hooker->getStepArgumentFor($annotation->getName());
}
}
}
}

Expand Down
133 changes: 133 additions & 0 deletions argumentsExt/ServiceContainer/StepArgumentInjectorExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?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\ServiceContainer;

use Behat\Behat\Context\ServiceContainer\ContextExtension;
use Behat\Behat\Tester\ServiceContainer\TesterExtension;
use Behat\Testwork\Argument\ServiceContainer\ArgumentExtension;
use Behat\Testwork\Call\ServiceContainer\CallExtension;
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
use Behat\Testwork\Hook\ServiceContainer\HookExtension;
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
use Behat\Testwork\ServiceContainer\ExtensionManager;
use Doctrine\Common\Annotations\AnnotationReader;
use Gorghoa\ScenarioStateBehatExtension\ServiceContainer\ScenarioStateExtension;
use Gorghoa\StepArgumentInjectorBehatExtension\Argument\ArgumentOrganiser;
use Gorghoa\StepArgumentInjectorBehatExtension\Argument\StepArgumentInjectorArgumentOrganiser;
use Gorghoa\StepArgumentInjectorBehatExtension\Call\Handler\RuntimeCallHandler;
use Gorghoa\StepArgumentInjectorBehatExtension\Hook\Dispatcher\StepArgumentInjectorHookDispatcher;
use Gorghoa\StepArgumentInjectorBehatExtension\Hook\Tester\StepArgumentInjectorHookableScenarioTester;
use Gorghoa\StepArgumentInjectorBehatExtension\Resolver\ArgumentsResolver;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Behat store for Behat contexts.
*
* @author Rodrigue Villetard <[email protected]>
* @author Vincent Chalamon <[email protected]>
*/
class StepArgumentInjectorExtension implements ExtensionInterface
{
const STEP_ARGUMENT_INJECTOR_ARGUMENT_ORGANISER_ID = 'argument.step_argument_injector.organiser';
const STEP_ARGUMENT_INJECTOR_DISPATCHER_ID = 'hook.step_argument_injector.dispatcher';
const STEP_ARGUMENT_INJECTOR_TESTER_ID = 'tester.step_argument_injector.wrapper';
const STEP_ARGUMENT_INJECTOR_CALL_HANDLER_ID = 'call.step_argument_injector.call_handler';
const STEP_ARGUMENT_INJECTOR_ARGUMENTS_RESOLVER_ID = 'step_argument_injector.arguments_resolver';
const STEP_ARGUMENT_INJECTOR_STORE_ID = 'behatstore.context_initializer.store_aware';
const STEP_ARGUMENT_INJECTOR_DOCTRINE_READER_ID = 'doctrine.reader.annotation';
const STEP_ARGUMENT_INJECTOR_HOOK_TAG_ID = 'step_argument_injector.hook_tag_id';

/**
* {@inheritdoc}
*/
public function getConfigKey()
{
return 'stepargumentinjector';
}

/**
* {@inheritdoc}
*/
public function initialize(ExtensionManager $extensionManager)
{
}

/**
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder)
{
}

/**
* {@inheritdoc}
*/
public function load(ContainerBuilder $container, array $config)
{
// Declare Doctrine annotation reader as service
$container->register(self::STEP_ARGUMENT_INJECTOR_DOCTRINE_READER_ID, AnnotationReader::class)
// Ignore Behat annotations in reader
->addMethodCall('addGlobalIgnoredName', ['Given'])
->addMethodCall('addGlobalIgnoredName', ['When'])
->addMethodCall('addGlobalIgnoredName', ['Then'])
->addMethodCall('addGlobalIgnoredName', ['Transform'])
->addMethodCall('addGlobalIgnoredName', ['BeforeStep'])
->addMethodCall('addGlobalIgnoredName', ['BeforeScenario'])
->addMethodCall('addGlobalIgnoredName', ['BeforeFeature'])
->addMethodCall('addGlobalIgnoredName', ['BeforeSuite'])
->addMethodCall('addGlobalIgnoredName', ['AfterStep'])
->addMethodCall('addGlobalIgnoredName', ['AfterScenario'])
->addMethodCall('addGlobalIgnoredName', ['AfterFeature'])
->addMethodCall('addGlobalIgnoredName', ['AfterSuite']);


$taggedServices = array_map(function ($serviceId) {
return new Reference($serviceId);
}, array_keys($container->findTaggedServiceIds(self::STEP_ARGUMENT_INJECTOR_HOOK_TAG_ID)));

// Arguments resolver: resolve StepArgumentInjector arguments from annotation
$container->register(self::STEP_ARGUMENT_INJECTOR_ARGUMENTS_RESOLVER_ID, ArgumentsResolver::class)
->setArguments([
$taggedServices,
new Reference(self::STEP_ARGUMENT_INJECTOR_DOCTRINE_READER_ID),
]);


// Argument organiser
$container->register(self::STEP_ARGUMENT_INJECTOR_ARGUMENT_ORGANISER_ID, ArgumentOrganiser::class)
->setDecoratedService(ArgumentExtension::PREG_MATCH_ARGUMENT_ORGANISER_ID)
->setPublic(false)
->setArguments([
new Reference(sprintf('%s.inner', self::STEP_ARGUMENT_INJECTOR_ARGUMENT_ORGANISER_ID)),
$taggedServices,
new Reference(self::STEP_ARGUMENT_INJECTOR_DOCTRINE_READER_ID),
new Reference(self::STEP_ARGUMENT_INJECTOR_ARGUMENTS_RESOLVER_ID),
]);

// Override calls process
$container->register(self::STEP_ARGUMENT_INJECTOR_CALL_HANDLER_ID, RuntimeCallHandler::class)
->setDecoratedService(CallExtension::CALL_HANDLER_TAG.'.runtime')
->setArguments([
new Reference(self::STEP_ARGUMENT_INJECTOR_CALL_HANDLER_ID.'.inner'),
new Reference(self::STEP_ARGUMENT_INJECTOR_ARGUMENTS_RESOLVER_ID),
]);
}

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
}
}
Loading

0 comments on commit f5c2672

Please sign in to comment.