Skip to content

Commit

Permalink
Merge 0dc57b3 into 087e9a9
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Jan 26, 2019
2 parents 087e9a9 + 0dc57b3 commit e571a80
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Container.php
Expand Up @@ -3,7 +3,7 @@
namespace League\Container;

use League\Container\Definition\{DefinitionAggregate, DefinitionInterface, DefinitionAggregateInterface};
use League\Container\Exception\NotFoundException;
use League\Container\Exception\{NotFoundException, ContainerException};
use League\Container\Inflector\{InflectorAggregate, InflectorInterface, InflectorAggregateInterface};
use League\Container\ServiceProvider\{ServiceProviderAggregate, ServiceProviderAggregateInterface};
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -166,6 +166,11 @@ public function get($id, bool $new = false)

if ($this->providers->provides($id)) {
$this->providers->register($id);

if(!$this->definitions->has($id) && !$this->definitions->hasTag($id)) {
throw new ContainerException(sprintf('Service provider lied about providing (%s) service', $id));
}

return $this->get($id, $new);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Asset/LiarServiceProvider.php
@@ -0,0 +1,19 @@
<?php

namespace League\Container\Test\Asset;

use League\Container\ServiceProvider\AbstractServiceProvider;

class LiarServiceProvider extends AbstractServiceProvider
{
/**
* @var array
*/
protected $provides = [
'lie'
];

public function register()
{
}
}
28 changes: 28 additions & 0 deletions tests/ContainerTest.php
Expand Up @@ -132,6 +132,34 @@ public function register()

$this->assertInstanceOf(Foo::class, $foo);
}

/**
* Expect an exception to be thrown if a service provider lies
* about providing a specific service.
*/
public function testThrowsWhenServiceProviderLies()
{
$liar = new class extends AbstractServiceProvider
{
protected $provides = [
'lie'
];

public function register()
{
}
};

$container = new Container;

$container->addServiceProvider($provider);

$this->assertTrue($container->has('lie'));

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

$lie = $container->get('lie');
}

/**
* Asserts that the container can add and get a service from a delegate.
Expand Down

0 comments on commit e571a80

Please sign in to comment.