├── .github
└── workflows
│ └── build.yml
├── LICENSE
├── README.md
├── composer.json
├── psalm.xml
└── src
└── ServiceContainer
└── ServiceContainerExtension.php
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 |
3 | on:
4 | push: ~
5 | pull_request: ~
6 | release:
7 | types: [created]
8 | schedule:
9 | -
10 | cron: "0 1 * * 6" # Run at 1am every Saturday
11 |
12 | jobs:
13 | tests:
14 | runs-on: ubuntu-latest
15 | name: "PHP ${{ matrix.php }}"
16 | strategy:
17 | fail-fast: false
18 | matrix:
19 | php: ["7.3", "7.4", "8.0"]
20 |
21 | steps:
22 | -
23 | uses: actions/checkout@v2
24 |
25 | -
26 | name: Setup PHP
27 | uses: shivammathur/setup-php@v2
28 | with:
29 | php-version: "${{ matrix.php }}"
30 | coverage: none
31 |
32 | -
33 | name: Install dependencies
34 | run: composer update
35 |
36 | -
37 | name: Composer validate
38 | run: composer validate --strict --ansi
39 |
40 | -
41 | name: Run Psalm
42 | run: vendor/bin/psalm src --no-progress
43 |
44 | -
45 | name: Run tests
46 | run: vendor/bin/behat -f progress --strict -vvv --no-interaction --colors
47 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2016-2017 Kamil Kokot
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is furnished
8 | to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Service Container Extension [](https://packagist.org/packages/friends-of-behat/service-container-extension) [](https://packagist.org/packages/friends-of-behat/service-container-extension) [](http://travis-ci.org/FriendsOfBehat/ServiceContainerExtension) [](https://scrutinizer-ci.com/g/FriendsOfBehat/ServiceContainerExtension/)
2 |
3 | Allows to declare own services inside Behat container without writing an extension.
4 |
5 | ## Usage
6 |
7 | 1. Install it:
8 |
9 | ```bash
10 | $ composer require friends-of-behat/service-container-extension --dev
11 | ```
12 |
13 | 2. Enable this extension and configure Behat to use it:
14 |
15 | ```yaml
16 | # behat.yml
17 | default:
18 | # ...
19 | extensions:
20 | FriendsOfBehat\ServiceContainerExtension:
21 | imports:
22 | - "features/bootstrap/config/services.xml"
23 | - "features/bootstrap/config/services.yml"
24 | - "features/bootstrap/config/services.php"
25 | ```
26 |
27 | 3. Write services files definitions:
28 |
29 | ```xml
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 | ```
38 |
39 | ```yaml
40 | # features/bootstrap/config/services.yml
41 | services:
42 | acme.my_service:
43 | class: Acme\MyService
44 | ```
45 |
46 | ```php
47 | // features/bootstrap/config/services.php
48 | use Symfony\Component\DependencyInjection\Definition;
49 |
50 | $container->setDefinition('acme.my_service', new Definition(\Acme\MyService::class));
51 | ```
52 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "friends-of-behat/service-container-extension",
3 | "description": "Allows to declare own services inside Behat container without writing an extension.",
4 | "license": "MIT",
5 | "authors": [
6 | {
7 | "name": "Kamil Kokot",
8 | "email": "kamil@kokot.me",
9 | "homepage": "https://kamilkokot.com"
10 | }
11 | ],
12 | "require": {
13 | "php": "^7.3 || ^8.0",
14 | "behat/behat": "^3.4"
15 | },
16 | "require-dev": {
17 | "friends-of-behat/test-context": "^1.2",
18 | "vimeo/psalm": "4.1.1"
19 | },
20 | "autoload": {
21 | "psr-4": {
22 | "FriendsOfBehat\\ServiceContainerExtension\\": "src/"
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/psalm.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/ServiceContainer/ServiceContainerExtension.php:
--------------------------------------------------------------------------------
1 |
9 | *
10 | * For the full copyright and license information, please view the LICENSE
11 | * file that was distributed with this source code.
12 | */
13 |
14 | namespace FriendsOfBehat\ServiceContainerExtension\ServiceContainer;
15 |
16 | use Behat\Testwork\ServiceContainer\Extension;
17 | use Behat\Testwork\ServiceContainer\ExtensionManager;
18 | use FriendsOfBehat\CrossContainerExtension\ServiceContainer\CrossContainerExtension;
19 | use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20 | use Symfony\Component\Config\FileLocator;
21 | use Symfony\Component\Config\Loader\DelegatingLoader;
22 | use Symfony\Component\Config\Loader\LoaderInterface;
23 | use Symfony\Component\Config\Loader\LoaderResolver;
24 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
25 | use Symfony\Component\DependencyInjection\ContainerBuilder;
26 | use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
27 | use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
28 | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
29 |
30 | /**
31 | * @internal
32 | */
33 | final class ServiceContainerExtension implements Extension
34 | {
35 | /**
36 | * {@inheritdoc}
37 | */
38 | public function getConfigKey(): string
39 | {
40 | return 'fob_service_container';
41 | }
42 |
43 | /**
44 | * {@inheritdoc}
45 | */
46 | public function initialize(ExtensionManager $extensionManager): void
47 | {
48 |
49 | }
50 |
51 | /**
52 | * {@inheritdoc}
53 | */
54 | public function configure(ArrayNodeDefinition $builder): void
55 | {
56 | $builder
57 | ->children()
58 | ->arrayNode('imports')
59 | ->performNoDeepMerging()
60 | ->prototype('scalar')->end()
61 | ->end()
62 | ->end()
63 | ;
64 | }
65 |
66 | /**
67 | * {@inheritdoc}
68 | */
69 | public function load(ContainerBuilder $container, array $config): void
70 | {
71 | $loader = $this->createLoader($container, $config);
72 |
73 | foreach ($config['imports'] as $file) {
74 | $loader->load($file);
75 | }
76 | }
77 |
78 | /**
79 | * {@inheritdoc}
80 | */
81 | public function process(ContainerBuilder $container): void
82 | {
83 |
84 | }
85 |
86 | private function createLoader(ContainerBuilder $container, array $config): LoaderInterface
87 | {
88 | $fileLocator = new FileLocator($container->getParameter('paths.base'));
89 |
90 | return new DelegatingLoader(new LoaderResolver([
91 | new XmlFileLoader($container, $fileLocator),
92 | new YamlFileLoader($container, $fileLocator),
93 | new PhpFileLoader($container, $fileLocator),
94 | ]));
95 | }
96 | }
97 |
--------------------------------------------------------------------------------