Skip to content

Commit

Permalink
Use test.service_container for Symfony 4.1+ (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Gorbachev authored and theofidry committed Sep 24, 2018
1 parent 160c95a commit 3479e48
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -23,10 +23,9 @@ matrix:
- php: '7.2'
env: SYMFONY_VERSION='~4.0.0'
- php: '7.2'
env: SYMFONY_VERSION='~4.1.0@dev'
env: SYMFONY_VERSION='~4.1.0'
allow_failures:
- php: nightly
- env: SYMFONY_VERSION='~4.1.0@dev'

before_install:
- set -eo pipefail
Expand Down
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -65,6 +65,9 @@ php bin/console psysh

# Symfony < 3.0
php app/console psysh

# Symfony > 4.0
bin/console psysh
```

or
Expand Down
46 changes: 46 additions & 0 deletions resources/config/test.xml
@@ -0,0 +1,46 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="test.client.parameters" type="collection"></parameter>
</parameters>

<services>
<defaults public="false" />

<service id="test.client" class="Symfony\Bundle\FrameworkBundle\Client" shared="false" public="true">
<argument type="service" id="kernel" />
<argument>%test.client.parameters%</argument>
<argument type="service" id="test.client.history" />
<argument type="service" id="test.client.cookiejar" />
</service>

<service id="test.client.history" class="Symfony\Component\BrowserKit\History" shared="false" />

<service id="test.client.cookiejar" class="Symfony\Component\BrowserKit\CookieJar" shared="false" />

<service id="test.session.listener" class="Symfony\Component\HttpKernel\EventListener\TestSessionListener">
<tag name="kernel.event_subscriber" />
<argument type="service">
<service class="Symfony\Component\DependencyInjection\ServiceLocator">
<tag name="container.service_locator" />
<argument type="collection">
<argument key="session" type="service" id="session" on-invalid="ignore" />
</argument>
</service>
</argument>
</service>

<service id="test.service_container" class="Symfony\Bundle\FrameworkBundle\Test\TestContainer" public="true">
<argument type="service" id="kernel" />
<argument>test.private_services_locator</argument>
</service>

<service id="test.private_services_locator" class="Symfony\Component\DependencyInjection\ServiceLocator" public="true">
<argument type="collection" />
</service>
</services>
</container>
9 changes: 7 additions & 2 deletions src/DependencyInjection/PsyshExtension.php
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Bundle\FrameworkBundle\Test\TestContainer;

/**
* This is the class that loads and manages your bundle configuration.
Expand All @@ -35,6 +36,9 @@ public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../../resources/config'));
$loader->load('services.xml');
if (class_exists(TestContainer::class) && !$container->has('test.service_container')) {
$loader->load('test.xml');
}

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
Expand All @@ -43,12 +47,13 @@ public function load(array $configs, ContainerBuilder $container)
$value = new Reference(substr($value, 1));
}
}
$containerId = $container->has('test.service_container') ? 'test.service_container' : 'service_container';
$container->findDefinition('psysh.shell')
->addMethodCall('setScopeVariables', [$config['variables'] + [
'container' => new Reference('service_container'),
'container' => new Reference($containerId),
'kernel' => new Reference('kernel'),
'self' => new Reference('psysh.shell'),
'parameters' => new Expression("service('service_container').getParameterBag().all()")
'parameters' => new Expression(sprintf("service('%s').getParameterBag().all()", $containerId))
]]);

// Register Psysh commands for service autoconfiguration (Symfony 3.3+)
Expand Down
12 changes: 12 additions & 0 deletions tests/Command/PsyshCommandIntegrationTest.php
Expand Up @@ -14,6 +14,8 @@
use Psy\Shell;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Bundle\FrameworkBundle\Test\TestContainer;

/**
* @author Théo FIDRY <theo.fidry@gmail.com>
Expand Down Expand Up @@ -61,6 +63,16 @@ public function testScopeVariables()
);
}

public function testContainerInstance()
{
$container = $this->shell->getScopeVariable('container');
if (class_exists(TestContainer::class)) {
$this->assertInstanceOf(TestContainer::class, $container);
} else {
$this->assertInstanceOf(Container::class, $container);
}
}

public function testFindShell()
{
$application = new Application(self::$kernel);
Expand Down

0 comments on commit 3479e48

Please sign in to comment.