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

Commit f5c2672

Browse files
vincentchalamonRodrigue Villetard
authored and
Rodrigue Villetard
committed
Bump version for packagist
1 parent 10de6c0 commit f5c2672

File tree

14 files changed

+315
-96
lines changed

14 files changed

+315
-96
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Annotation;
13+
14+
/**
15+
* @author Vincent Chalamon <vincentchalamon@gmail.com>
16+
*
17+
*/
18+
interface StepInjectorArgument
19+
{
20+
/**
21+
* @return string
22+
*/
23+
public function getName();
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getArgument();
29+
}

src/Argument/ScenarioStateArgumentOrganiser.php renamed to argumentsExt/Argument/ArgumentOrganiser.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
11
<?php
22

33
/*
4-
* This file is part of the ScenarioStateBehatExtension project.
4+
* This file is part of the StepArgumentInjectorBehatExtension project.
55
*
66
* (c) Rodrigue Villetard <rodrigue.villetard@gmail.com>
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Gorghoa\ScenarioStateBehatExtension\Argument;
12+
namespace Gorghoa\StepArgumentInjectorBehatExtension\Argument;
1313

14-
use Behat\Testwork\Argument\ArgumentOrganiser;
14+
use Behat\Testwork\Argument\ArgumentOrganiser as BehatArgumentOrganiser;
1515
use Doctrine\Common\Annotations\Reader;
16-
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument;
17-
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer;
16+
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepArgumentInjectorArgument;
17+
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument;
18+
// use Gorghoa\StepArgumentInjectorBehatExtension\Context\Initializer\StepArgumentInjectorInitializer;
1819
use ReflectionFunctionAbstract;
1920

2021
/**
2122
* @author Rodrigue Villetard <rodrigue.villetard@gmail.com>
2223
* @author Vincent Chalamon <vincentchalamon@gmail.com>
2324
*/
24-
final class ScenarioStateArgumentOrganiser implements ArgumentOrganiser
25+
final class ArgumentOrganiser implements BehatArgumentOrganiser
2526
{
2627
/**
27-
* @var ArgumentOrganiser
28+
* @var BehatArgumentOrganiser
2829
*/
2930
private $baseOrganiser;
3031

3132
/**
32-
* @var ScenarioStateInitializer
33+
* @var StepArgumentInjectorInitializer
3334
*/
34-
private $store;
35+
private $hookers;
3536

3637
/**
3738
* @var Reader
3839
*/
3940
private $reader;
4041

41-
public function __construct(ArgumentOrganiser $organiser, ScenarioStateInitializer $store, Reader $reader)
42+
public function __construct(BehatArgumentOrganiser $organiser, array $hookers, Reader $reader)
4243
{
4344
$this->baseOrganiser = $organiser;
44-
$this->store = $store;
45+
$this->hookers = $hookers;
4546
$this->reader = $reader;
4647
}
4748

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

62-
/** @var ScenarioStateArgument[] $annotations */
63+
/** @var StepArgumentInjectorArgument[] $annotations */
6364
$annotations = $this->reader->getMethodAnnotations($function);
64-
$store = $this->store->getStore();
65+
6566
foreach ($annotations as $annotation) {
66-
if ($annotation instanceof ScenarioStateArgument &&
67-
in_array($annotation->getArgument(), $paramsKeys) &&
68-
$store->hasStateFragment($annotation->getName())
67+
if ($annotation instanceof StepInjectorArgument &&
68+
in_array($annotation->getArgument(), $paramsKeys)
6969
) {
70-
$match[$annotation->getArgument()] = $store->getStateFragment($annotation->getName());
71-
$match[strval(++$i)] = $store->getStateFragment($annotation->getName());
70+
foreach ($this->hookers as $hooker) {
71+
if ($hooker->hasStepArgumentFor($annotation->getName())) {
72+
$match[$annotation->getArgument()]
73+
= $match[strval(++$i)]
74+
= $hooker->getStepArgumentFor($annotation->getName())
75+
;
76+
}
77+
}
7278
}
7379
}
7480

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
15+
/**
16+
* @author Rodrigue Villetard <rodrigue.villetard@gmail.com>
17+
*/
18+
interface StepArgumentHolder
19+
{
20+
public function hasStepArgumentFor($key);
21+
public function getStepArgumentFor($key);
22+
}

src/Call/Handler/RuntimeCallHandler.php renamed to argumentsExt/Call/Handler/RuntimeCallHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<?php
22

33
/*
4-
* This file is part of the ScenarioStateBehatExtension project.
4+
* This file is part of the StepArgumentInjectorBehatExtension project.
55
*
66
* (c) Rodrigue Villetard <rodrigue.villetard@gmail.com>
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Gorghoa\ScenarioStateBehatExtension\Call\Handler;
12+
namespace Gorghoa\StepArgumentInjectorBehatExtension\Call\Handler;
1313

1414
use Behat\Behat\Transformation\Call\TransformationCall;
1515
use Behat\Testwork\Call\Call;
1616
use Behat\Testwork\Call\Handler\CallHandler;
1717
use Behat\Testwork\Environment\Call\EnvironmentCall;
1818
use Behat\Testwork\Hook\Call\HookCall;
19-
use Gorghoa\ScenarioStateBehatExtension\Resolver\ArgumentsResolver;
19+
use Gorghoa\StepArgumentInjectorBehatExtension\Resolver\ArgumentsResolver;
2020

2121
/**
2222
* @author Vincent Chalamon <vincent@les-tilleuls.coop>

src/Resolver/ArgumentsResolver.php renamed to argumentsExt/Resolver/ArgumentsResolver.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
11
<?php
22

33
/*
4-
* This file is part of the ScenarioStateBehatExtension project.
4+
* This file is part of the StepArgumentInjectorBehatExtension project.
55
*
66
* (c) Rodrigue Villetard <rodrigue.villetard@gmail.com>
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Gorghoa\ScenarioStateBehatExtension\Resolver;
12+
namespace Gorghoa\StepArgumentInjectorBehatExtension\Resolver;
1313

1414
use Doctrine\Common\Annotations\Reader;
15-
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument;
16-
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer;
15+
use Gorghoa\StepArgumentInjectorBehatExtension\Annotation\StepInjectorArgument;
16+
use Gorghoa\StepArgumentInjectorBehatExtension\Argument\StepArgumentHolder;
1717

1818
/**
1919
* @author Vincent Chalamon <vincent@les-tilleuls.coop>
2020
*/
2121
class ArgumentsResolver
2222
{
2323
/**
24-
* @var ScenarioStateInitializer
24+
* @var Reader
2525
*/
26-
private $store;
26+
private $reader;
2727

2828
/**
29-
* @var Reader
29+
* @var array
3030
*/
31-
private $reader;
31+
private $hookers;
3232

3333
/**
34-
* @param ScenarioStateInitializer $store
3534
* @param Reader $reader
3635
*/
37-
public function __construct(ScenarioStateInitializer $store, Reader $reader)
36+
public function __construct(array $hookers, Reader $reader)
3837
{
39-
$this->store = $store;
38+
$this->hookers = $hookers;
4039
$this->reader = $reader;
4140
}
4241

@@ -48,25 +47,28 @@ public function __construct(ScenarioStateInitializer $store, Reader $reader)
4847
*/
4948
public function resolve(\ReflectionMethod $function, array $arguments)
5049
{
51-
// No `@ScenarioStateArgument` annotation found
52-
if (null === $this->reader->getMethodAnnotation($function, ScenarioStateArgument::class)) {
50+
// No `@StepArgumentInjectorArgument` annotation found
51+
if (null === $this->reader->getMethodAnnotation($function, StepInjectorArgument::class)) {
5352
return $arguments;
5453
}
5554

5655
$paramsKeys = array_map(function (\ReflectionParameter $element) {
5756
return $element->getName();
5857
}, $function->getParameters());
59-
$store = $this->store->getStore();
58+
6059

6160
// Prepare arguments from annotations
62-
/** @var ScenarioStateArgument[] $annotations */
61+
/** @var StepArgumentInjectorArgument[] $annotations */
6362
$annotations = $this->reader->getMethodAnnotations($function);
6463
foreach ($annotations as $annotation) {
65-
if ($annotation instanceof ScenarioStateArgument &&
66-
in_array($annotation->getArgument(), $paramsKeys) &&
67-
$store->hasStateFragment($annotation->getName())
64+
if ($annotation instanceof StepInjectorArgument &&
65+
in_array($annotation->getArgument(), $paramsKeys)
6866
) {
69-
$arguments[$annotation->getArgument()] = $store->getStateFragment($annotation->getName());
67+
foreach ($this->hookers as $hooker) {
68+
if ($hooker->hasStepArgumentFor($annotation->getName())) {
69+
$arguments[$annotation->getArgument()] = $hooker->getStepArgumentFor($annotation->getName());
70+
}
71+
}
7072
}
7173
}
7274

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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\ServiceContainer;
13+
14+
use Behat\Behat\Context\ServiceContainer\ContextExtension;
15+
use Behat\Behat\Tester\ServiceContainer\TesterExtension;
16+
use Behat\Testwork\Argument\ServiceContainer\ArgumentExtension;
17+
use Behat\Testwork\Call\ServiceContainer\CallExtension;
18+
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
19+
use Behat\Testwork\Hook\ServiceContainer\HookExtension;
20+
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
21+
use Behat\Testwork\ServiceContainer\ExtensionManager;
22+
use Doctrine\Common\Annotations\AnnotationReader;
23+
use Gorghoa\ScenarioStateBehatExtension\ServiceContainer\ScenarioStateExtension;
24+
use Gorghoa\StepArgumentInjectorBehatExtension\Argument\ArgumentOrganiser;
25+
use Gorghoa\StepArgumentInjectorBehatExtension\Argument\StepArgumentInjectorArgumentOrganiser;
26+
use Gorghoa\StepArgumentInjectorBehatExtension\Call\Handler\RuntimeCallHandler;
27+
use Gorghoa\StepArgumentInjectorBehatExtension\Hook\Dispatcher\StepArgumentInjectorHookDispatcher;
28+
use Gorghoa\StepArgumentInjectorBehatExtension\Hook\Tester\StepArgumentInjectorHookableScenarioTester;
29+
use Gorghoa\StepArgumentInjectorBehatExtension\Resolver\ArgumentsResolver;
30+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
31+
use Symfony\Component\DependencyInjection\ContainerBuilder;
32+
use Symfony\Component\DependencyInjection\Reference;
33+
34+
/**
35+
* Behat store for Behat contexts.
36+
*
37+
* @author Rodrigue Villetard <rodrigue.villetard@gmail.com>
38+
* @author Vincent Chalamon <vincentchalamon@gmail.com>
39+
*/
40+
class StepArgumentInjectorExtension implements ExtensionInterface
41+
{
42+
const STEP_ARGUMENT_INJECTOR_ARGUMENT_ORGANISER_ID = 'argument.step_argument_injector.organiser';
43+
const STEP_ARGUMENT_INJECTOR_DISPATCHER_ID = 'hook.step_argument_injector.dispatcher';
44+
const STEP_ARGUMENT_INJECTOR_TESTER_ID = 'tester.step_argument_injector.wrapper';
45+
const STEP_ARGUMENT_INJECTOR_CALL_HANDLER_ID = 'call.step_argument_injector.call_handler';
46+
const STEP_ARGUMENT_INJECTOR_ARGUMENTS_RESOLVER_ID = 'step_argument_injector.arguments_resolver';
47+
const STEP_ARGUMENT_INJECTOR_STORE_ID = 'behatstore.context_initializer.store_aware';
48+
const STEP_ARGUMENT_INJECTOR_DOCTRINE_READER_ID = 'doctrine.reader.annotation';
49+
const STEP_ARGUMENT_INJECTOR_HOOK_TAG_ID = 'step_argument_injector.hook_tag_id';
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function getConfigKey()
55+
{
56+
return 'stepargumentinjector';
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function initialize(ExtensionManager $extensionManager)
63+
{
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function configure(ArrayNodeDefinition $builder)
70+
{
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function load(ContainerBuilder $container, array $config)
77+
{
78+
// Declare Doctrine annotation reader as service
79+
$container->register(self::STEP_ARGUMENT_INJECTOR_DOCTRINE_READER_ID, AnnotationReader::class)
80+
// Ignore Behat annotations in reader
81+
->addMethodCall('addGlobalIgnoredName', ['Given'])
82+
->addMethodCall('addGlobalIgnoredName', ['When'])
83+
->addMethodCall('addGlobalIgnoredName', ['Then'])
84+
->addMethodCall('addGlobalIgnoredName', ['Transform'])
85+
->addMethodCall('addGlobalIgnoredName', ['BeforeStep'])
86+
->addMethodCall('addGlobalIgnoredName', ['BeforeScenario'])
87+
->addMethodCall('addGlobalIgnoredName', ['BeforeFeature'])
88+
->addMethodCall('addGlobalIgnoredName', ['BeforeSuite'])
89+
->addMethodCall('addGlobalIgnoredName', ['AfterStep'])
90+
->addMethodCall('addGlobalIgnoredName', ['AfterScenario'])
91+
->addMethodCall('addGlobalIgnoredName', ['AfterFeature'])
92+
->addMethodCall('addGlobalIgnoredName', ['AfterSuite']);
93+
94+
95+
$taggedServices = array_map(function ($serviceId) {
96+
return new Reference($serviceId);
97+
}, array_keys($container->findTaggedServiceIds(self::STEP_ARGUMENT_INJECTOR_HOOK_TAG_ID)));
98+
99+
// Arguments resolver: resolve StepArgumentInjector arguments from annotation
100+
$container->register(self::STEP_ARGUMENT_INJECTOR_ARGUMENTS_RESOLVER_ID, ArgumentsResolver::class)
101+
->setArguments([
102+
$taggedServices,
103+
new Reference(self::STEP_ARGUMENT_INJECTOR_DOCTRINE_READER_ID),
104+
]);
105+
106+
107+
// Argument organiser
108+
$container->register(self::STEP_ARGUMENT_INJECTOR_ARGUMENT_ORGANISER_ID, ArgumentOrganiser::class)
109+
->setDecoratedService(ArgumentExtension::PREG_MATCH_ARGUMENT_ORGANISER_ID)
110+
->setPublic(false)
111+
->setArguments([
112+
new Reference(sprintf('%s.inner', self::STEP_ARGUMENT_INJECTOR_ARGUMENT_ORGANISER_ID)),
113+
$taggedServices,
114+
new Reference(self::STEP_ARGUMENT_INJECTOR_DOCTRINE_READER_ID),
115+
new Reference(self::STEP_ARGUMENT_INJECTOR_ARGUMENTS_RESOLVER_ID),
116+
]);
117+
118+
// Override calls process
119+
$container->register(self::STEP_ARGUMENT_INJECTOR_CALL_HANDLER_ID, RuntimeCallHandler::class)
120+
->setDecoratedService(CallExtension::CALL_HANDLER_TAG.'.runtime')
121+
->setArguments([
122+
new Reference(self::STEP_ARGUMENT_INJECTOR_CALL_HANDLER_ID.'.inner'),
123+
new Reference(self::STEP_ARGUMENT_INJECTOR_ARGUMENTS_RESOLVER_ID),
124+
]);
125+
}
126+
127+
/**
128+
* {@inheritdoc}
129+
*/
130+
public function process(ContainerBuilder $container)
131+
{
132+
}
133+
}

0 commit comments

Comments
 (0)