Skip to content

Commit

Permalink
Use PSR-11 container
Browse files Browse the repository at this point in the history
The dependency to `container-interop/service-provider:^0.4` changes the
signature of the container, from the container-interop container to
PSR-11 container.

This also updates tests and version of phpunit.
  • Loading branch information
romm committed Apr 12, 2020
1 parent 529ebf1 commit ecba7bc
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 85 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,7 @@ language: php

php:
- 7.2
- 7.3
- 7.4

before_script:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -20,7 +20,7 @@
},
"require-dev": {
"php": ">=5.6",
"phpunit/phpunit": "~5.0",
"phpunit/phpunit": "^8.5",
"php-coveralls/php-coveralls": "^2.2"
},
"autoload": {
Expand Down
5 changes: 0 additions & 5 deletions phpunit.xml.dist
Expand Up @@ -8,7 +8,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
Expand All @@ -21,8 +20,4 @@
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
38 changes: 0 additions & 38 deletions src/Exception/ContainerException.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/Exception/NotFoundException.php

This file was deleted.

5 changes: 3 additions & 2 deletions src/ServiceProviderCompilationPass.php
Expand Up @@ -117,7 +117,7 @@ private function extendService($serviceName, $serviceProviderKey, $callable, Con
$container->setDefinition($serviceName, $factoryDefinition);
$container->setDefinition($innerName, $innerDefinition);

$container->setAlias($oldServiceName, new Alias($serviceName));
$container->setAlias($oldServiceName, (new Alias($serviceName))->setPublic(true));
}
}

Expand All @@ -135,11 +135,12 @@ private function getServiceDefinitionFromCallable($serviceName, $serviceProvider
// TODO: plug the definition-interop converter here!
}*/
$factoryDefinition = new Definition('Class'); // TODO: in PHP7, we can get the return type of the function!
$factoryDefinition->setPublic(true);
$containerDefinition = new Reference('interop_service_provider_acclimated_container');

if ((is_array($callable) && is_string($callable[0])) || is_string($callable)) {
$factoryDefinition->setFactory($callable);
$factoryDefinition->addArgument(new Reference('interop_service_provider_acclimated_container'));
$factoryDefinition->addArgument($containerDefinition);
} else {
$factoryDefinition->setFactory([ new Reference('service_provider_registry_'.$this->registryId), 'createService' ]);
$factoryDefinition->addArgument($serviceProviderKey);
Expand Down
14 changes: 3 additions & 11 deletions src/SymfonyContainerAdapter.php
Expand Up @@ -2,11 +2,8 @@

namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle;

use TheCodingMachine\Interop\ServiceProviderBridgeBundle\Exception\ContainerException as BridgeContainerException;
use TheCodingMachine\Interop\ServiceProviderBridgeBundle\Exception\NotFoundException as BridgeNotFoundException;
use Interop\Container\ContainerInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException as SymfonyNotFoundException;

/**
* An adapter from a Symfony Container to the standardized ContainerInterface
Expand All @@ -33,13 +30,8 @@ public function get($id)
if ($this->container->hasParameter($id)) {
return $this->container->getParameter($id);
}
try {
return $this->container->get($id);
} catch (SymfonyNotFoundException $prev) {
throw BridgeNotFoundException::fromPrevious($id, $prev);
} catch (\Exception $prev) {
throw BridgeContainerException::fromPrevious($id, $prev);
}

return $this->container->get($id);
}

public function has($id)
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Fixtures/TestServiceProvider.php
@@ -1,8 +1,8 @@
<?php
namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle\Tests\Fixtures;

use Interop\Container\ContainerInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;

function myFunctionFactory()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Fixtures/TestServiceProviderOverride.php
@@ -1,8 +1,8 @@
<?php
namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle\Tests\Fixtures;

use Interop\Container\ContainerInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;

class TestServiceProviderOverride implements ServiceProviderInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Fixtures/TestServiceProviderOverride2.php
Expand Up @@ -2,8 +2,8 @@

namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle\Tests\Fixtures;

use Interop\Container\ContainerInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;

class TestServiceProviderOverride2 implements ServiceProviderInterface
{
Expand Down
5 changes: 3 additions & 2 deletions tests/ServiceProviderCompilationPassTest.php
Expand Up @@ -4,6 +4,7 @@
namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle;


use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -12,15 +13,15 @@
use TheCodingMachine\Interop\ServiceProviderBridgeBundle\Tests\Fixtures\TestServiceProviderOverride;
use TheCodingMachine\Interop\ServiceProviderBridgeBundle\Tests\Fixtures\TestServiceProviderOverride2;

class ServiceProviderCompilationPassTest extends \PHPUnit_Framework_TestCase
class ServiceProviderCompilationPassTest extends TestCase
{
protected function getContainer(array $lazyArray, $useDiscovery = false)
{
$bundle = new InteropServiceProviderBridgeBundle($lazyArray, $useDiscovery);

$container = new ContainerBuilder();
$container->setParameter('database_host', 'localhost');
$container->setDefinition('logger', new Definition(NullLogger::class));
$container->setDefinition('logger', (new Definition(NullLogger::class))->setPublic(true));

$bundle->build($container);
$container->compile();
Expand Down
17 changes: 9 additions & 8 deletions tests/SymfonyContainerAdapterTest.php
Expand Up @@ -4,10 +4,13 @@
namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle;


use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class SymfonyContainerAdapterTest extends \PHPUnit_Framework_TestCase
class SymfonyContainerAdapterTest extends TestCase
{

public function testContainer()
Expand All @@ -23,30 +26,28 @@ public function testContainer()
$this->assertFalse($containerAdapter->has('not_exists'));
}

/**
* @expectedException \TheCodingMachine\Interop\ServiceProviderBridgeBundle\Exception\NotFoundException
*/
public function testContainerNotFound()
{
$container = new ContainerBuilder();
$container->compile();
$containerAdapter = new SymfonyContainerAdapter($container);

$this->expectException(NotFoundExceptionInterface::class);

$containerAdapter->get('not_found');
}

/**
* @expectedException \TheCodingMachine\Interop\ServiceProviderBridgeBundle\Exception\ContainerException
*/
public function testContainerWithException()
{
$container = new ContainerBuilder();
$definition = new Definition(\stdClass::class);
$definition->setFactory(self::class, "exceptionFactory");
$definition->setFactory([self::class, 'exceptionFactory']);
$container->setDefinition('mydef', $definition);
$container->compile();
$containerAdapter = new SymfonyContainerAdapter($container);

$this->expectException(ContainerExceptionInterface::class);

$containerAdapter->get('mydef');
}

Expand Down

0 comments on commit ecba7bc

Please sign in to comment.