-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use arachne/service-collections and eloquent/phony
- Loading branch information
Showing
14 changed files
with
543 additions
and
487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,10 @@ | |
|
||
namespace Arachne\EntityLoader\DI; | ||
|
||
use Arachne\DIHelpers\CompilerExtension; | ||
use Arachne\EventDispatcher\DI\EventDispatcherExtension; | ||
use Arachne\ServiceCollections\DI\ServiceCollectionsExtension; | ||
use Nette\DI\CompilerExtension; | ||
use Nette\Utils\AssertionException; | ||
|
||
/** | ||
* @author Jáchym Toušek <[email protected]> | ||
|
@@ -43,9 +45,20 @@ public function loadConfiguration() | |
{ | ||
$builder = $this->getContainerBuilder(); | ||
|
||
$extension = $this->getExtension('Arachne\DIHelpers\DI\ResolversExtension'); | ||
$extension->add(self::TAG_FILTER_IN, 'Arachne\EntityLoader\FilterInInterface'); | ||
$extension->add(self::TAG_FILTER_OUT, 'Arachne\EntityLoader\FilterOutInterface'); | ||
/* @var $serviceCollectionsExtension ServiceCollectionsExtension */ | ||
$serviceCollectionsExtension = $this->getExtension('Arachne\ServiceCollections\DI\ServiceCollectionsExtension'); | ||
|
||
$filterInResolver = $serviceCollectionsExtension->getCollection( | ||
ServiceCollectionsExtension::TYPE_RESOLVER, | ||
self::TAG_FILTER_IN, | ||
'Arachne\EntityLoader\FilterInInterface' | ||
); | ||
|
||
$filterOutResolver = $serviceCollectionsExtension->getCollection( | ||
ServiceCollectionsExtension::TYPE_RESOLVER, | ||
self::TAG_FILTER_OUT, | ||
'Arachne\EntityLoader\FilterOutInterface' | ||
); | ||
|
||
foreach ($this->filters as $class => $type) { | ||
$builder->addDefinition($this->prefix('filterIn.'.$type)) | ||
|
@@ -54,10 +67,20 @@ public function loadConfiguration() | |
} | ||
|
||
$builder->addDefinition($this->prefix('entityLoader')) | ||
->setClass('Arachne\EntityLoader\EntityLoader'); | ||
->setClass('Arachne\EntityLoader\EntityLoader') | ||
->setArguments( | ||
[ | ||
'filterInResolver' => '@'.$filterInResolver, | ||
] | ||
); | ||
|
||
$builder->addDefinition($this->prefix('entityUnloader')) | ||
->setClass('Arachne\EntityLoader\EntityUnloader'); | ||
->setClass('Arachne\EntityLoader\EntityUnloader') | ||
->setArguments( | ||
[ | ||
'filterOutResolver' => '@'.$filterOutResolver, | ||
] | ||
); | ||
|
||
$builder->addDefinition($this->prefix('application.parameterFinder')) | ||
->setClass('Arachne\EntityLoader\Application\ParameterFinder'); | ||
|
@@ -73,24 +96,21 @@ public function loadConfiguration() | |
->addTag(EventDispatcherExtension::TAG_SUBSCRIBER); | ||
} | ||
|
||
public function beforeCompile() | ||
/** | ||
* @param string $class | ||
* | ||
* @return CompilerExtension | ||
*/ | ||
private function getExtension($class) | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
|
||
$extension = $this->getExtension('Arachne\DIHelpers\DI\ResolversExtension'); | ||
$extensions = $this->compiler->getExtensions($class); | ||
|
||
$builder->getDefinition($this->prefix('entityLoader')) | ||
->setArguments( | ||
[ | ||
'filterInResolver' => '@'.$extension->get(self::TAG_FILTER_IN), | ||
] | ||
if (!$extensions) { | ||
throw new AssertionException( | ||
sprintf('Extension "%s" requires "%s" to be installed.', get_class($this), $class) | ||
); | ||
} | ||
|
||
$builder->getDefinition($this->prefix('entityUnloader')) | ||
->setArguments( | ||
[ | ||
'filterOutResolver' => '@'.$extension->get(self::TAG_FILTER_OUT), | ||
] | ||
); | ||
return reset($extensions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
error_level: "E_ALL" | ||
|
||
class_name: UnitSuiteTester | ||
|
||
modules: | ||
enabled: | ||
- Mockery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,103 +2,92 @@ | |
|
||
namespace Tests\Unit; | ||
|
||
use Arachne\DIHelpers\ResolverInterface; | ||
use Arachne\EntityLoader\EntityLoader; | ||
use Arachne\EntityLoader\FilterInInterface; | ||
use Codeception\MockeryModule\Test; | ||
use Mockery; | ||
use Mockery\MockInterface; | ||
use Codeception\Test\Unit; | ||
use DateTime; | ||
use Eloquent\Phony\Mock\Handle\InstanceHandle; | ||
use Eloquent\Phony\Phpunit\Phony; | ||
use Eloquent\Phony\Stub\StubVerifier; | ||
|
||
/** | ||
* @author Jáchym Toušek <[email protected]> | ||
*/ | ||
class EntityLoaderTest extends Test | ||
class EntityLoaderTest extends Unit | ||
{ | ||
/** | ||
* @var EntityLoader | ||
*/ | ||
private $entityLoader; | ||
|
||
/** | ||
* @var MockInterface | ||
* @var InstanceHandle | ||
*/ | ||
private $filter; | ||
private $filterHandle; | ||
|
||
/** | ||
* @var MockInterface | ||
* @var StubVerifier | ||
*/ | ||
private $filterResolver; | ||
|
||
protected function _before() | ||
{ | ||
$this->filter = Mockery::mock(FilterInInterface::class); | ||
$this->filterResolver = Mockery::mock(ResolverInterface::class); | ||
$this->filterHandle = Phony::mock(FilterInInterface::class); | ||
$this->filterResolver = Phony::stub(); | ||
$this->entityLoader = new EntityLoader($this->filterResolver); | ||
} | ||
|
||
public function testFilterIn() | ||
{ | ||
$this->filterResolver | ||
->shouldReceive('resolve') | ||
->once() | ||
->with('Type1') | ||
->andReturn($this->filter); | ||
->returns($this->filterHandle->get()); | ||
|
||
$mock1 = Mockery::mock('Type1'); | ||
$mock1 = Phony::mock(DateTime::class)->get(); | ||
|
||
$this->filter | ||
->shouldReceive('filterIn') | ||
->once() | ||
->with(1) | ||
->andReturn($mock1); | ||
$this->filterHandle | ||
->filterIn | ||
->returns($mock1); | ||
|
||
$this->assertSame($mock1, $this->entityLoader->filterIn('Type1', 1)); | ||
$this->assertSame($mock1, $this->entityLoader->filterIn(DateTime::class, 1)); | ||
|
||
$this->filterHandle | ||
->filterIn | ||
->calledWith(1); | ||
} | ||
|
||
/** | ||
* @expectedException Arachne\EntityLoader\Exception\UnexpectedValueException | ||
* @expectedExceptionMessage FilterIn did not return an instance of 'Type1'. | ||
* @expectedException \Arachne\EntityLoader\Exception\UnexpectedValueException | ||
* @expectedExceptionMessage FilterIn did not return an instance of 'DateTime'. | ||
*/ | ||
public function testFilterInFail() | ||
{ | ||
$this->filterResolver | ||
->shouldReceive('resolve') | ||
->once() | ||
->with('Type1') | ||
->andReturn($this->filter); | ||
|
||
$this->filter | ||
->shouldReceive('filterIn') | ||
->once() | ||
->with(1) | ||
->andReturn(null); | ||
|
||
$this->entityLoader->filterIn('Type1', 1); | ||
->returns($this->filterHandle->get()); | ||
|
||
$this->filterHandle | ||
->filterIn | ||
->returns(null); | ||
|
||
$this->entityLoader->filterIn(DateTime::class, 1); | ||
} | ||
|
||
public function testFilterInIgnore() | ||
{ | ||
// Make sure that the converter is not called at all if the parameter already has the desired type. | ||
$mock1 = Mockery::mock('Type1'); | ||
$this->assertSame($mock1, $this->entityLoader->filterIn('Type1', $mock1)); | ||
$mock1 = Phony::mock(DateTime::class)->get(); | ||
$this->assertSame($mock1, $this->entityLoader->filterIn(DateTime::class, $mock1)); | ||
} | ||
|
||
/** | ||
* @expectedException Arachne\EntityLoader\Exception\UnexpectedValueException | ||
* @expectedExceptionMessage No filter in found for type 'Type1'. | ||
* @expectedException \Arachne\EntityLoader\Exception\UnexpectedValueException | ||
* @expectedExceptionMessage No filter in found for type 'DateTime'. | ||
*/ | ||
public function testFilterNotFound() | ||
{ | ||
$parameters = [ | ||
'entity' => 'value1', | ||
]; | ||
|
||
$this->filterResolver | ||
->shouldReceive('resolve') | ||
->once() | ||
->with('Type1') | ||
->andReturn(); | ||
|
||
$this->entityLoader->filterIn('Type1', $parameters); | ||
$this->entityLoader->filterIn(DateTime::class, $parameters); | ||
} | ||
} |
Oops, something went wrong.