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

Commit a7bbc31

Browse files
author
Rodrigue Villetard
committed
wip: behat tests are dirty passing, keep going
1 parent c3260ae commit a7bbc31

16 files changed

+304
-106
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 Rodrigue Villetard <rodrigue.villetard@gmail.com>
16+
*/
17+
interface StepInjectorArgument
18+
{
19+
/**
20+
* @return string
21+
*/
22+
public function getName();
23+
24+
/**
25+
* @return string
26+
*/
27+
public function getArgument();
28+
}

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

1817
/**
1918
* @author Vincent Chalamon <vincent@les-tilleuls.coop>
2019
*/
2120
class ArgumentsResolver
2221
{
2322
/**
24-
* @var ScenarioStateInitializer
23+
* @var Reader
2524
*/
26-
private $store;
25+
private $reader;
2726

2827
/**
29-
* @var Reader
28+
* @var array
3029
*/
31-
private $reader;
30+
private $hookers;
3231

3332
/**
34-
* @param ScenarioStateInitializer $store
35-
* @param Reader $reader
33+
* @param Reader $reader
3634
*/
37-
public function __construct(ScenarioStateInitializer $store, Reader $reader)
35+
public function __construct(array $hookers, Reader $reader)
3836
{
39-
$this->store = $store;
37+
$this->hookers = $hookers;
4038
$this->reader = $reader;
4139
}
4240

@@ -48,25 +46,27 @@ public function __construct(ScenarioStateInitializer $store, Reader $reader)
4846
*/
4947
public function resolve(\ReflectionMethod $function, array $arguments)
5048
{
51-
// No `@ScenarioStateArgument` annotation found
52-
if (null === $this->reader->getMethodAnnotation($function, ScenarioStateArgument::class)) {
49+
// No `@StepArgumentInjectorArgument` annotation found
50+
if (null === $this->reader->getMethodAnnotation($function, StepInjectorArgument::class)) {
5351
return $arguments;
5452
}
5553

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

6158
// Prepare arguments from annotations
62-
/** @var ScenarioStateArgument[] $annotations */
59+
/** @var StepArgumentInjectorArgument[] $annotations */
6360
$annotations = $this->reader->getMethodAnnotations($function);
6461
foreach ($annotations as $annotation) {
65-
if ($annotation instanceof ScenarioStateArgument &&
66-
in_array($annotation->getArgument(), $paramsKeys) &&
67-
$store->hasStateFragment($annotation->getName())
62+
if ($annotation instanceof StepInjectorArgument &&
63+
in_array($annotation->getArgument(), $paramsKeys)
6864
) {
69-
$arguments[$annotation->getArgument()] = $store->getStateFragment($annotation->getName());
65+
foreach ($this->hookers as $hooker) {
66+
if ($hooker->hasStepArgumentFor($annotation->getName())) {
67+
$arguments[$annotation->getArgument()] = $hooker->getStepArgumentFor($annotation->getName());
68+
}
69+
}
7070
}
7171
}
7272

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

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
"autoload": {
2929
"psr-4": {
30+
"Gorghoa\\StepArgumentInjectorBehatExtension\\": "argumentsExt/",
3031
"Gorghoa\\ScenarioStateBehatExtension\\": "src/",
3132
"Symfony\\Component\\Process\\": "vendor/symfony/process/"
3233
}

src/.php_cs.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"php":"7.1.7-1+0~20170711133214.5+stretch~1.gbp5284f4","version":"2.4.0:v2.4.0#63661f3add3609e90e4ab8115113e189ae547bb4","rules":{"binary_operator_spaces":{"align_double_arrow":false,"align_equals":false},"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"braces":{"allow_single_line_closure":true},"cast_spaces":true,"class_definition":{"singleLine":true},"concat_space":{"spacing":"none"},"declare_equal_normalize":true,"function_typehint_space":true,"hash_to_slash_comment":true,"include":true,"lowercase_cast":true,"magic_constant_casing":true,"method_argument_space":true,"method_separation":true,"native_function_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_consecutive_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":{"use":"echo"},"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"php_unit_fqcn_annotation":true,"phpdoc_align":true,"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_empty_return":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_types":true,"phpdoc_var_without_name":true,"pre_increment":true,"protected_to_private":true,"return_type_declaration":true,"self_accessor":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_class_element_per_statement":true,"single_quote":true,"space_after_semicolon":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"blank_line_after_namespace":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"\/ScenarioStateInterface.php":2824836438,"\/Annotation\/ScenarioStateArgument.php":2979727378,"\/Context\/Initializer\/ScenarioStateInitializer.php":781987277,"\/Context\/ScenarioStateAwareTrait.php":2441401241,"\/Context\/ScenarioStateAwareContext.php":1438795547,"\/ScenarioState.php":3549039778,"\/ServiceContainer\/ScenarioStateExtension.php":1698856486,"\/Exception\/MissingStateException.php":1656618837}}

0 commit comments

Comments
 (0)