Skip to content

Commit

Permalink
Simplify test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Apr 3, 2021
1 parent ea9d31e commit f2c98e8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 75 deletions.
29 changes: 14 additions & 15 deletions tests/Unit/Definitions/DynamicReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,18 @@
use Yiisoft\Factory\Tests\Support\EngineInterface;
use Yiisoft\Factory\Tests\Support\EngineMarkOne;
use Yiisoft\Injector\Injector;
use Yiisoft\Test\Support\Container\Exception\NotFoundException;
use Yiisoft\Test\Support\Container\SimpleContainer;

class DynamicReferenceTest extends TestCase
{
public function createContainer(): ContainerInterface
{
$container = new SimpleContainer(
[
EngineInterface::class => new EngineMarkOne(),
],
static function (string $id) use (&$container) {
if ($id === ContainerInterface::class) {
return $container;
}
if ($id === Injector::class) {
return new Injector($container);
}
throw new NotFoundException($id);
}
);
$container = new SimpleContainer([
ContainerInterface::class => &$container,
EngineInterface::class => new EngineMarkOne(),
Injector::class => &$injector,
]);
$injector = new Injector($container);
return $container;
}

Expand All @@ -48,6 +39,14 @@ public function testClosure(): void
$this->assertInstanceOf(EngineMarkOne::class, $ref->resolve($this->createContainer()));
}

public function testStaticClosure(): void
{
$ref = DynamicReference::to(
static fn (ContainerInterface $container) => $container->get(EngineInterface::class)
);
$this->assertInstanceOf(EngineMarkOne::class, $ref->resolve($this->createContainer()));
}

public function testCallable(): void
{
$ref = DynamicReference::to([static::class, 'callableDefinition']);
Expand Down
101 changes: 41 additions & 60 deletions tests/Unit/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Psr\Container\ContainerInterface;
use Yiisoft\Factory\Definitions\Reference;
use Yiisoft\Factory\Exceptions\InvalidConfigException;
use Yiisoft\Factory\Exceptions\NotFoundException;
use Yiisoft\Factory\Factory;
use Yiisoft\Factory\Tests\Support\Car;
use Yiisoft\Factory\Tests\Support\EngineInterface;
Expand All @@ -23,12 +22,11 @@ final class FactoryTest extends TestCase
public function testCanCreateByAlias(): void
{
$container = new SimpleContainer();
$factory = new Factory($container, ['engine' => EngineMarkOne::class]);

$factory = new Factory($container, [
'engine' => EngineMarkOne::class,
]);
$one = $factory->create('engine');
$two = $factory->create('engine');

$this->assertNotSame($one, $two);
$this->assertInstanceOf(EngineMarkOne::class, $one);
$this->assertInstanceOf(EngineMarkOne::class, $two);
Expand All @@ -37,12 +35,11 @@ public function testCanCreateByAlias(): void
public function testCanCreateByInterfaceAsStringDefinition(): void
{
$container = new SimpleContainer();
$factory = new Factory($container, [EngineInterface::class => EngineMarkOne::class]);

$factory = new Factory($container, [
EngineInterface::class => EngineMarkOne::class,
]);
$one = $factory->create(EngineInterface::class);
$two = $factory->create(EngineInterface::class);

$this->assertNotSame($one, $two);
$this->assertInstanceOf(EngineMarkOne::class, $one);
$this->assertInstanceOf(EngineMarkOne::class, $two);
Expand All @@ -51,12 +48,11 @@ public function testCanCreateByInterfaceAsStringDefinition(): void
public function testCanCreateByInterfaceAsReferenceDefinition(): void
{
$container = new SimpleContainer();
$factory = new Factory($container, [EngineInterface::class => EngineMarkOne::class]);

$factory = new Factory($container, [
EngineInterface::class => EngineMarkOne::class,
]);
$one = $factory->create(Reference::to(EngineInterface::class));
$two = $factory->create(Reference::to(EngineInterface::class));

$this->assertNotSame($one, $two);
$this->assertInstanceOf(EngineMarkOne::class, $one);
$this->assertInstanceOf(EngineMarkOne::class, $two);
Expand All @@ -69,12 +65,11 @@ public function testCanCreateByInterfaceAsReferenceDefinition(): void
public function testObjectIsCloned(): void
{
$container = new SimpleContainer();
$factory = new Factory($container, ['engine' => new EngineMarkOne()]);

$factory = new Factory($container, [
'engine' => new EngineMarkOne(),
]);
$one = $factory->create('engine');
$two = $factory->create('engine');

$this->assertNotSame($one, $two);
$this->assertInstanceOf(EngineMarkOne::class, $two);
}
Expand All @@ -85,10 +80,11 @@ public function testObjectIsCloned(): void
public function testCreateClassNotDefinedInConfig(): void
{
$container = new SimpleContainer();

$factory = new Factory($container);

$one = $factory->create(EngineMarkOne::class);
$two = $factory->create(EngineMarkOne::class);

$this->assertNotSame($one, $two);
$this->assertInstanceOf(EngineMarkOne::class, $one);
$this->assertInstanceOf(EngineMarkOne::class, $two);
Expand All @@ -100,7 +96,6 @@ public function testCreateClassNotDefinedInConfig(): void
public function testMergeFactoryConfig(): void
{
$container = new SimpleContainer();

$factory = new Factory($container, [
EngineMarkOne::class => [
'__class' => EngineMarkOne::class,
Expand All @@ -111,6 +106,7 @@ public function testMergeFactoryConfig(): void
$instance = $factory->create([
'__class' => EngineMarkOne::class,
]);

$this->assertInstanceOf(EngineMarkOne::class, $instance);
$this->assertEquals(42, $instance->getNumber());
}
Expand All @@ -121,7 +117,6 @@ public function testMergeFactoryConfig(): void
public function testOverrideFactoryConfig(): void
{
$container = new SimpleContainer();

$factory = new Factory($container, [
EngineMarkOne::class => [
'__class' => EngineMarkOne::class,
Expand All @@ -133,19 +128,21 @@ public function testOverrideFactoryConfig(): void
'__class' => EngineMarkOne::class,
'setNumber()' => [43],
]);

$this->assertInstanceOf(EngineMarkOne::class, $instance);
$this->assertEquals(43, $instance->getNumber());
}

public function testGetByAlias(): void
{
$container = new SimpleContainer();

$factory = new Factory($container, [
'engine' => EngineMarkOne::class,
]);

$one = $factory->get('engine');
$two = $factory->get('engine');

$this->assertNotSame($one, $two);
$this->assertInstanceOf(EngineMarkOne::class, $one);
$this->assertInstanceOf(EngineMarkOne::class, $two);
Expand All @@ -154,11 +151,12 @@ public function testGetByAlias(): void
public function testTrivialDefinition(): void
{
$container = new SimpleContainer();

$factory = new Factory($container);
$factory->set(EngineMarkOne::class, EngineMarkOne::class);

$one = $factory->get(EngineMarkOne::class);
$two = $factory->get(EngineMarkOne::class);

$this->assertNotSame($one, $two);
$this->assertInstanceOf(EngineMarkOne::class, $one);
$this->assertInstanceOf(EngineMarkOne::class, $two);
Expand All @@ -170,10 +168,11 @@ public function testTrivialDefinition(): void
public function testCreateWithParams(): void
{
$container = new SimpleContainer();

$factory = new Factory($container);

$one = $factory->create(Car::class, [$factory->get(EngineMarkOne::class)]);
$two = $factory->create(Car::class, [$factory->get(EngineMarkTwo::class)]);

$this->assertNotSame($one, $two);
$this->assertInstanceOf(Car::class, $one);
$this->assertInstanceOf(Car::class, $two);
Expand All @@ -184,10 +183,11 @@ public function testCreateWithParams(): void
public function testCreateWithNamedParams(): void
{
$container = new SimpleContainer();

$factory = new Factory($container);

$one = $factory->create(Car::class, ['engine' => $factory->get(EngineMarkOne::class)]);
$two = $factory->create(Car::class, ['engine' => $factory->get(EngineMarkTwo::class)]);

$this->assertNotSame($one, $two);
$this->assertInstanceOf(Car::class, $one);
$this->assertInstanceOf(Car::class, $two);
Expand All @@ -198,8 +198,8 @@ public function testCreateWithNamedParams(): void
public function testCreateWithCallableValuesInParams(): void
{
$container = new SimpleContainer();

$factory = new Factory($container);

$object = $factory->create(TwoParametersDependency::class, [
'firstParameter' => 'date',
'secondParameter' => 'time',
Expand All @@ -213,18 +213,18 @@ public function testCreateWithCallableValuesInParams(): void
public function testCreateWithInvalidParams(): void
{
$container = new SimpleContainer();

$factory = new Factory($container);

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

$factory->create(TwoParametersDependency::class, ['firstParam' => 'param1', 1 => 'param2']);
}

public function testCreateWithRandomOrderedParams(): void
{
$container = new SimpleContainer();

$factory = new Factory($container);

$object = $factory->create(TwoParametersDependency::class, [1 => 'param2', 0 => 'param1']);

$this->assertInstanceOf(TwoParametersDependency::class, $object);
Expand All @@ -237,17 +237,19 @@ public function testCreateWithRandomOrderedParams(): void
*/
public function testResolveDependenciesUsingContainer(): void
{
$container = new SimpleContainer([
EngineInterface::class => new EngineMarkOne(),
]);

$origin = new EngineMarkOne();
$container = new SimpleContainer([EngineInterface::class => $origin]);
$factory = new Factory($container);

$one = $factory->create(Car::class);
$two = $factory->create(Car::class);

$this->assertInstanceOf(Car::class, $one);
$this->assertInstanceOf(Car::class, $two);
$this->assertInstanceOf(EngineMarkOne::class, $two->getEngine());
$this->assertNotSame($one, $two);
$this->assertNotSame($origin, $one);
$this->assertNotSame($origin, $two);
$this->assertSame($one->getEngine(), $two->getEngine());
}

Expand All @@ -257,40 +259,24 @@ public function testResolveDependenciesUsingContainer(): void
*/
public function testDoNotFallbackToContainer(): void
{
$container = new SimpleContainer(
[],
static function (string $id) {
if ($id === EngineMarkOne::class) {
$engine = new EngineMarkOne();
$engine->setNumber(42);
return $engine;
}
throw new NotFoundException($id);
}
);

$engine = new EngineMarkOne();
$engine->setNumber(42);
$container = new SimpleContainer([EngineMarkOne::class => $engine]);
$factory = new Factory($container);

$instance = $factory->create(EngineMarkOne::class);

$this->assertInstanceOf(EngineMarkOne::class, $instance);
$this->assertNotEquals(42, $instance->getNumber());
$this->assertNotSame($engine, $instance);
}

/**
* When resolving dependencies, factory should rely on container only
*/
public function testDoNotResolveDependenciesFromFactory(): void
{
$container = new SimpleContainer(
[],
static function (string $id) {
if ($id === EngineInterface::class) {
return new EngineMarkOne();
}
throw new NotFoundException($id);
}
);

$container = new SimpleContainer([EngineInterface::class => new EngineMarkOne()]);
$factory = new Factory($container, [
EngineInterface::class => [
'__class' => EngineMarkOne::class,
Expand All @@ -299,24 +285,15 @@ static function (string $id) {
]);

$instance = $factory->create(Car::class);

$this->assertInstanceOf(Car::class, $instance);
$this->assertInstanceOf(EngineMarkOne::class, $instance->getEngine());

$this->assertEquals(0, $instance->getEngine()->getNumber());
}

public function testCreateFactory(): void
{
$container = new SimpleContainer(
[],
static function (string $id) use (&$container) {
if ($id === ContainerInterface::class) {
return $container;
}
throw new NotFoundException($id);
}
);

$container = new SimpleContainer([ContainerInterface::class => &$container]);
$factory = new Factory($container, [
'factoryObject' => [
'__class' => Factory::class,
Expand All @@ -326,8 +303,10 @@ static function (string $id) use (&$container) {
],
],
]);

$oneFactoryObject = $factory->create('factoryObject');
$otherFactoryObject = $factory->create('factoryObject');

$this->assertNotSame($oneFactoryObject, $otherFactoryObject);
$this->assertNotSame($oneFactoryObject, $factory);
$this->assertInstanceOf(Factory::class, $oneFactoryObject);
Expand All @@ -343,9 +322,11 @@ public function testCreateFactoryImmutable(): void
'fieldImmutable()' => ['testMe'],
],
]);

$oneImmutableObject = $factory->create('immutableObject');
$otherImmutableObject = (new Immutable())->fieldImmutable('testMe');
$otherImmutableObject->id('id-testMe');

$this->assertEquals($oneImmutableObject, $otherImmutableObject);
}
}

0 comments on commit f2c98e8

Please sign in to comment.