Skip to content

Commit

Permalink
Container tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philipobenito committed Apr 3, 2017
1 parent 5e0dfe3 commit d7d9f24
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function extend(string $id): DefinitionInterface
}

throw new NotFoundException(
sprintf('Unable to extend alias (%s) as it is not being managed as a definition', $alias)
sprintf('Unable to extend alias (%s) as it is not being managed as a definition', $id)
);
}

Expand Down
158 changes: 158 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php declare(strict_types=1);

namespace League\Container\Test;

use League\Container\Definition\DefinitionInterface;
use League\Container\Exception\NotFoundException;
use League\Container\ServiceProvider\AbstractServiceProvider;
use League\Container\Test\Asset\{Foo, Bar};
use League\Container\{Container, ReflectionContainer};
use PHPUnit\Framework\TestCase;

class ContainerTest extends TestCase
{
/**
* Asserts that the container can add and get a service.
*/
public function testContainerAddsAndGets()
{
$container = new Container;

$container->add(Foo::class);

$this->assertTrue($container->has(Foo::class));

$foo = $container->get(Foo::class);

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

/**
* Asserts that the container can add and get a service from service provider.
*/
public function testContainerAddsAndGetsWithServiceProvider()
{
$provider = new class extends AbstractServiceProvider
{
protected $provides = [
Foo::class
];

public function register()
{
$this->getContainer()->add(Foo::class);
}
};

$container = new Container;

$container->addServiceProvider($provider);

$this->assertTrue($container->has(Foo::class));

$foo = $container->get(Foo::class);

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

/**
* Asserts that the container can add and get a service from a delegate.
*/
public function testContainerAddsAndGetsFromDelegate()
{
$delegate = new ReflectionContainer;
$container = new Container;

$container->delegate($delegate);

$foo = $container->get(Foo::class);

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

/**
* Asserts that the container throws an exception when cannot find service.
*/
public function testContainerThrowsWhenCannotGetService()
{
$this->expectException(NotFoundException::class);

$container = new Container;

$this->assertFalse($container->has(Foo::class));

$container->get(Foo::class);
}

/**
* Asserts that the container can find a definition to extend.
*/
public function testContainerCanExtendDefinition()
{
$container = new Container;

$container->add(Foo::class);

$definition = $container->extend(Foo::class);

$this->assertInstanceOf(DefinitionInterface::class, $definition);
}

/**
* Asserts that the container can find a definition to extend from service provider.
*/
public function testContainerCanExtendDefinitionFromServiceProvider()
{
$provider = new class extends AbstractServiceProvider
{
protected $provides = [
Foo::class
];

public function register()
{
$this->getContainer()->add(Foo::class);
}
};

$container = new Container;

$container->addServiceProvider($provider);

$definition = $container->extend(Foo::class);

$this->assertInstanceOf(DefinitionInterface::class, $definition);
}

/**
* Asserts that the container throws an exception when can't find definition to extend.
*/
public function testContainerThrowsWhenCannotGetDefinitionToExtend()
{
$this->expectException(NotFoundException::class);

$container = new Container;

$this->assertFalse($container->has(Foo::class));

$container->extend(Foo::class);
}

/**
* Asserts that the container adds and invokes an inflector.
*/
public function testContainerAddsAndInvokesInflector()
{
$container = new Container;

$container->inflector(Foo::class)->setProperty('bar', Bar::class);

$container->add(Foo::class);
$container->add(Bar::class);

$foo = $container->get(Foo::class);

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

0 comments on commit d7d9f24

Please sign in to comment.