Skip to content
This repository was archived by the owner on Feb 5, 2021. It is now read-only.

Commit 270fdb8

Browse files
authored
Merge pull request #24 from franzose/master
Glob patterns in imports
2 parents 52a19fa + 5a3477c commit 270fdb8

File tree

2 files changed

+152
-1
lines changed

2 files changed

+152
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
Feature: Registering contexts services using glob patterns
2+
In order to allow my contexts to be created using Symfony DI
3+
As a Behat Developer
4+
I want to be able to define them as services
5+
6+
Background:
7+
Given a context file "features/bootstrap/FeatureContext.php" containing:
8+
"""
9+
<?php
10+
11+
use Behat\Behat\Context\Context;
12+
13+
class FeatureContext implements Context
14+
{
15+
private $foobar;
16+
private $qux;
17+
private $doo;
18+
19+
public function __construct($foobar, $qux, $doo)
20+
{
21+
$this->foobar = $foobar;
22+
$this->qux = $qux;
23+
$this->doo = $doo;
24+
}
25+
26+
/**
27+
* @Given parameters were injected to the context
28+
*/
29+
public function parametersWereInjectedToTheContext()
30+
{
31+
$params = [
32+
'foobar' => $this->foobar,
33+
'qux' => $this->qux,
34+
'doo' => $this->doo
35+
];
36+
37+
foreach ($params as $name => $value) {
38+
if (null === $value) {
39+
throw new \DomainException(sprintf(
40+
'Parameter "%s" was not injected (or is null)!',
41+
$name
42+
));
43+
}
44+
}
45+
}
46+
47+
/**
48+
* @When I change them to :a, :b, and :c
49+
*/
50+
public function iChangeThemTo($a, $b, $c)
51+
{
52+
$this->foobar = $a;
53+
$this->qux = $b;
54+
$this->doo = $c;
55+
}
56+
57+
/**
58+
* @Then they should contain :a, :b, and :c
59+
*/
60+
public function theyShouldContain($a, $b, $c)
61+
{
62+
if ($a !== $this->foobar) {
63+
throw new \DomainException(sprintf('Expected to get "%s", got "%s"!', $a, $this->foobar));
64+
}
65+
66+
if ($b !== $this->qux) {
67+
throw new \DomainException(sprintf('Expected to get "%s", got "%s"!', $b, $this->qux));
68+
}
69+
70+
if ($c !== $this->doo) {
71+
throw new \DomainException(sprintf('Expected to get "%s", got "%s"!', $c, $this->doo));
72+
}
73+
}
74+
}
75+
"""
76+
And a Behat configuration containing:
77+
"""
78+
default:
79+
suites:
80+
default:
81+
contexts_services:
82+
- feature_context
83+
84+
extensions:
85+
FriendsOfBehat\ContextServiceExtension:
86+
imports:
87+
- features/bootstrap/config/**/*
88+
"""
89+
And a config file "features/bootstrap/config/qux/services.php" containing:
90+
"""
91+
<?php
92+
93+
use Symfony\Component\DependencyInjection\Definition;
94+
use Symfony\Component\DependencyInjection\Parameter;
95+
96+
$container->setParameter('qux', 'qux value');
97+
"""
98+
And a config file "features/bootstrap/config/doo/services.xml" containing:
99+
"""
100+
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services">
101+
<parameters>
102+
<parameter key="doo">doo value</parameter>
103+
</parameters>
104+
</container>
105+
"""
106+
And a config file "features/bootstrap/config/services.yml" containing:
107+
"""
108+
parameters:
109+
foobar: "shit happens"
110+
111+
services:
112+
feature_context:
113+
class: FeatureContext
114+
arguments:
115+
- "%foobar%"
116+
- "%qux%"
117+
- "%doo%"
118+
tags:
119+
- { name: "fob.context_service" }
120+
"""
121+
122+
Scenario: Using registered context service
123+
Given a feature file "features/registering_context_service.feature" containing:
124+
"""
125+
Feature: Registering context service
126+
127+
Scenario:
128+
Given parameters were injected to the context
129+
Then they should contain "shit happens", "qux value", and "doo value"
130+
"""
131+
When I run Behat
132+
Then it should pass
133+
134+
Scenario: New context service instance is used for each scenario
135+
Given a feature file "features/registering_context_service.feature" containing:
136+
"""
137+
Feature: Registering context service
138+
139+
Scenario:
140+
Given parameters were injected to the context
141+
When I change them to "shit does not happen", "ooops", and "foobarquxdoo"
142+
Then they should contain "shit does not happen", "ooops", and "foobarquxdoo"
143+
144+
Scenario:
145+
Given parameters were injected to the context
146+
Then they should contain "shit happens", "qux value", and "doo value"
147+
"""
148+
When I run Behat
149+
Then it should pass

Diff for: src/ServiceContainer/Scenario/ContainerFactory.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public function createContainer(string $basePath, array $importedFiles = []): Co
4141

4242
$loader = $this->createLoader($container, $basePath);
4343
foreach ($importedFiles as $file) {
44-
$loader->load($file);
44+
$type = false !== mb_strpos($file, '*') ? 'glob' : null;
45+
$loader->load($file, $type);
4546
}
4647

4748
return $container;
@@ -60,6 +61,7 @@ private function createLoader(ContainerBuilder $container, string $basePath): Lo
6061
new Loader\XmlFileLoader($container, $fileLocator),
6162
new Loader\YamlFileLoader($container, $fileLocator),
6263
new Loader\PhpFileLoader($container, $fileLocator),
64+
new Loader\GlobFileLoader($container, $fileLocator)
6365
]));
6466

6567
return $loader;

0 commit comments

Comments
 (0)