Skip to content

Commit

Permalink
Merge pull request #9 from filecage/#8-interface-factory-caching
Browse files Browse the repository at this point in the history
#8 fixes interface factory caching
  • Loading branch information
filecage authored Aug 13, 2018
2 parents dfaab07 + e26a5fa commit bd65995
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/Creation.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function create () {
*/
private function createInstanceWithRegistry (string $className, Creatable $creatable, ResourceRegistry $registry) {
// Does the registry already contain the resource?
$instance = $registry->getClassResource($className);
$instance = $registry->getClassResource($className) ?? $registry->findFulfillingInstance($creatable);
if ($instance !== null) {
return $instance;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/ArbitraryClassOnlyResolvableByFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Creator\Tests\Mocks;

class ArbitraryClassOnlyResolvableByFactory {
class ArbitraryClassOnlyResolvableByFactory implements ArbitraryInterface {

/**
* @var SimpleClass
Expand Down
7 changes: 7 additions & 0 deletions tests/Mocks/ArbitraryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Creator\Tests\Mocks;

interface ArbitraryInterface {

}
17 changes: 17 additions & 0 deletions tests/ResourceCachingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Creator\Tests;

use Creator\Creator;
use Creator\Tests\Mocks\ArbitraryClassOnlyResolvableByFactory;
use Creator\Tests\Mocks\ArbitraryFactory;
use Creator\Tests\Mocks\ArbitraryInterface;
use Creator\Tests\Mocks\ExtendedClass;
use Creator\Tests\Mocks\MoreExtendedClass;
use Creator\Tests\Mocks\SimpleClass;
Expand Down Expand Up @@ -57,4 +60,18 @@ function testExpectsInnerDependencyCachingWhenForcingInstance () {
$this->assertSame($a->getAnotherSimpleClass(), $b->getAnotherSimpleClass());
}

function testExpectsInterfaceFactoryResultCaching () {
$creator = new Creator();

$creator->registerFactory(function() {
return new ArbitraryClassOnlyResolvableByFactory(new SimpleClass(), ArbitraryFactory::PRIMITIVE_VALUE);
}, ArbitraryInterface::class);

$firstArbitraryImplementation = $creator->create(ArbitraryInterface::class);
$secondArbitraryImplementation = $creator->create(ArbitraryInterface::class);

$this->assertInstanceOf(ArbitraryInterface::class, $firstArbitraryImplementation);
$this->assertSame($firstArbitraryImplementation, $secondArbitraryImplementation);
}

}

0 comments on commit bd65995

Please sign in to comment.