Skip to content

Commit edfd1aa

Browse files
committed
Refactor to use arachne/service-collections and eloquent/phony
1 parent b1e9383 commit edfd1aa

14 files changed

+543
-487
lines changed

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
],
1414
"require": {
1515
"php": "^5.4.0|^7.0.0",
16-
"arachne/di-helpers": "^0.4.0|^0.5.0",
1716
"arachne/event-dispatcher": "^0.1.0",
17+
"arachne/service-collections": "^0.1.0",
1818
"nette/application": "^2.3.0",
1919
"nette/caching": "^2.3.0",
2020
"nette/di": "^2.3.0",
@@ -27,9 +27,8 @@
2727
"php": "^5.5.0|^7.0.0",
2828
"arachne/codeception": "^0.7.3",
2929
"codeception/codeception": "~2.2.0",
30-
"codeception/mockery-module": "^0.2.2",
31-
"enumag/application": "^0.3.5",
32-
"mockery/mockery": "^0.9.0"
30+
"eloquent/phony": "^0.13.5",
31+
"enumag/application": "^0.3.5"
3332
},
3433
"autoload": {
3534
"psr-4": {

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Now you need to register the necessary extensions using your [neon](http://ne-on
1818
```
1919
extensions:
2020
- Oops\CacheFactory\DI\CacheFactoryExtension
21-
- Arachne\DIHelpers\DI\ResolversExtension
21+
- Arachne\ServiceCollections\DI\ServiceCollectionsExtension
2222
- Arachne\ContainerAdapter\DI\ContainerAdapterExtension
2323
- Arachne\EventDispatcher\DI\EventDispatcherExtension
2424
- Arachne\EntityLoader\DI\EntityLoaderExtension

src/DI/EntityLoaderExtension.php

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
namespace Arachne\EntityLoader\DI;
1212

13-
use Arachne\DIHelpers\CompilerExtension;
1413
use Arachne\EventDispatcher\DI\EventDispatcherExtension;
14+
use Arachne\ServiceCollections\DI\ServiceCollectionsExtension;
15+
use Nette\DI\CompilerExtension;
16+
use Nette\Utils\AssertionException;
1517

1618
/**
1719
* @author Jáchym Toušek <enumag@gmail.com>
@@ -43,9 +45,20 @@ public function loadConfiguration()
4345
{
4446
$builder = $this->getContainerBuilder();
4547

46-
$extension = $this->getExtension('Arachne\DIHelpers\DI\ResolversExtension');
47-
$extension->add(self::TAG_FILTER_IN, 'Arachne\EntityLoader\FilterInInterface');
48-
$extension->add(self::TAG_FILTER_OUT, 'Arachne\EntityLoader\FilterOutInterface');
48+
/* @var $serviceCollectionsExtension ServiceCollectionsExtension */
49+
$serviceCollectionsExtension = $this->getExtension('Arachne\ServiceCollections\DI\ServiceCollectionsExtension');
50+
51+
$filterInResolver = $serviceCollectionsExtension->getCollection(
52+
ServiceCollectionsExtension::TYPE_RESOLVER,
53+
self::TAG_FILTER_IN,
54+
'Arachne\EntityLoader\FilterInInterface'
55+
);
56+
57+
$filterOutResolver = $serviceCollectionsExtension->getCollection(
58+
ServiceCollectionsExtension::TYPE_RESOLVER,
59+
self::TAG_FILTER_OUT,
60+
'Arachne\EntityLoader\FilterOutInterface'
61+
);
4962

5063
foreach ($this->filters as $class => $type) {
5164
$builder->addDefinition($this->prefix('filterIn.'.$type))
@@ -54,10 +67,20 @@ public function loadConfiguration()
5467
}
5568

5669
$builder->addDefinition($this->prefix('entityLoader'))
57-
->setClass('Arachne\EntityLoader\EntityLoader');
70+
->setClass('Arachne\EntityLoader\EntityLoader')
71+
->setArguments(
72+
[
73+
'filterInResolver' => '@'.$filterInResolver,
74+
]
75+
);
5876

5977
$builder->addDefinition($this->prefix('entityUnloader'))
60-
->setClass('Arachne\EntityLoader\EntityUnloader');
78+
->setClass('Arachne\EntityLoader\EntityUnloader')
79+
->setArguments(
80+
[
81+
'filterOutResolver' => '@'.$filterOutResolver,
82+
]
83+
);
6184

6285
$builder->addDefinition($this->prefix('application.parameterFinder'))
6386
->setClass('Arachne\EntityLoader\Application\ParameterFinder');
@@ -73,24 +96,21 @@ public function loadConfiguration()
7396
->addTag(EventDispatcherExtension::TAG_SUBSCRIBER);
7497
}
7598

76-
public function beforeCompile()
99+
/**
100+
* @param string $class
101+
*
102+
* @return CompilerExtension
103+
*/
104+
private function getExtension($class)
77105
{
78-
$builder = $this->getContainerBuilder();
79-
80-
$extension = $this->getExtension('Arachne\DIHelpers\DI\ResolversExtension');
106+
$extensions = $this->compiler->getExtensions($class);
81107

82-
$builder->getDefinition($this->prefix('entityLoader'))
83-
->setArguments(
84-
[
85-
'filterInResolver' => '@'.$extension->get(self::TAG_FILTER_IN),
86-
]
108+
if (!$extensions) {
109+
throw new AssertionException(
110+
sprintf('Extension "%s" requires "%s" to be installed.', get_class($this), $class)
87111
);
112+
}
88113

89-
$builder->getDefinition($this->prefix('entityUnloader'))
90-
->setArguments(
91-
[
92-
'filterOutResolver' => '@'.$extension->get(self::TAG_FILTER_OUT),
93-
]
94-
);
114+
return reset($extensions);
95115
}
96116
}

src/EntityLoader.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace Arachne\EntityLoader;
1212

13-
use Arachne\DIHelpers\ResolverInterface;
1413
use Arachne\EntityLoader\Exception\UnexpectedValueException;
1514

1615
/**
@@ -19,11 +18,11 @@
1918
class EntityLoader
2019
{
2120
/**
22-
* @var ResolverInterface
21+
* @var callable
2322
*/
2423
private $filterInResolver;
2524

26-
public function __construct(ResolverInterface $filterInResolver)
25+
public function __construct(callable $filterInResolver)
2726
{
2827
$this->filterInResolver = $filterInResolver;
2928
}
@@ -54,7 +53,7 @@ public function filterIn($type, $parameter)
5453
*/
5554
private function getFilter($type)
5655
{
57-
$filter = $this->filterInResolver->resolve($type);
56+
$filter = call_user_func($this->filterInResolver, $type);
5857
if (!$filter) {
5958
throw new UnexpectedValueException("No filter in found for type '$type'.");
6059
}

src/EntityUnloader.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace Arachne\EntityLoader;
1212

13-
use Arachne\DIHelpers\ResolverInterface;
1413
use Arachne\EntityLoader\Exception\UnexpectedValueException;
1514

1615
/**
@@ -19,11 +18,11 @@
1918
class EntityUnloader
2019
{
2120
/**
22-
* @var ResolverInterface
21+
* @var callable
2322
*/
2423
private $filterOutResolver;
2524

26-
public function __construct(ResolverInterface $filterOutResolver)
25+
public function __construct(callable $filterOutResolver)
2726
{
2827
$this->filterOutResolver = $filterOutResolver;
2928
}
@@ -47,7 +46,7 @@ public function filterOut($object)
4746
*/
4847
private function getFilter($type)
4948
{
50-
$filter = $this->filterOutResolver->resolve($type);
49+
$filter = call_user_func($this->filterOutResolver, $type);
5150
if (!$filter) {
5251
throw new UnexpectedValueException("No filter out found for type '$type'.");
5352
}

tests/functional/config/config.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extensions:
22
codeception.http: Arachne\Codeception\DI\HttpExtension
33
arachne.containeradaper: Arachne\ContainerAdapter\DI\ContainerAdapterExtension
4-
arachne.dihelpers.resolvers: Arachne\DIHelpers\DI\ResolversExtension
4+
arachne.servicecollections: Arachne\ServiceCollections\DI\ServiceCollectionsExtension
55
arachne.entityloader: Arachne\EntityLoader\DI\EntityLoaderExtension
66
arachne.eventdispatcher: Arachne\EventDispatcher\DI\EventDispatcherExtension
77
enumag.application: Enumag\Application\DI\ApplicationExtension

tests/unit.suite.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
error_level: "E_ALL"
22

33
class_name: UnitSuiteTester
4-
5-
modules:
6-
enabled:
7-
- Mockery

tests/unit/src/EntityLoaderTest.php

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,103 +2,92 @@
22

33
namespace Tests\Unit;
44

5-
use Arachne\DIHelpers\ResolverInterface;
65
use Arachne\EntityLoader\EntityLoader;
76
use Arachne\EntityLoader\FilterInInterface;
8-
use Codeception\MockeryModule\Test;
9-
use Mockery;
10-
use Mockery\MockInterface;
7+
use Codeception\Test\Unit;
8+
use DateTime;
9+
use Eloquent\Phony\Mock\Handle\InstanceHandle;
10+
use Eloquent\Phony\Phpunit\Phony;
11+
use Eloquent\Phony\Stub\StubVerifier;
1112

1213
/**
1314
* @author Jáchym Toušek <enumag@gmail.com>
1415
*/
15-
class EntityLoaderTest extends Test
16+
class EntityLoaderTest extends Unit
1617
{
1718
/**
1819
* @var EntityLoader
1920
*/
2021
private $entityLoader;
2122

2223
/**
23-
* @var MockInterface
24+
* @var InstanceHandle
2425
*/
25-
private $filter;
26+
private $filterHandle;
2627

2728
/**
28-
* @var MockInterface
29+
* @var StubVerifier
2930
*/
3031
private $filterResolver;
3132

3233
protected function _before()
3334
{
34-
$this->filter = Mockery::mock(FilterInInterface::class);
35-
$this->filterResolver = Mockery::mock(ResolverInterface::class);
35+
$this->filterHandle = Phony::mock(FilterInInterface::class);
36+
$this->filterResolver = Phony::stub();
3637
$this->entityLoader = new EntityLoader($this->filterResolver);
3738
}
3839

3940
public function testFilterIn()
4041
{
4142
$this->filterResolver
42-
->shouldReceive('resolve')
43-
->once()
44-
->with('Type1')
45-
->andReturn($this->filter);
43+
->returns($this->filterHandle->get());
4644

47-
$mock1 = Mockery::mock('Type1');
45+
$mock1 = Phony::mock(DateTime::class)->get();
4846

49-
$this->filter
50-
->shouldReceive('filterIn')
51-
->once()
52-
->with(1)
53-
->andReturn($mock1);
47+
$this->filterHandle
48+
->filterIn
49+
->returns($mock1);
5450

55-
$this->assertSame($mock1, $this->entityLoader->filterIn('Type1', 1));
51+
$this->assertSame($mock1, $this->entityLoader->filterIn(DateTime::class, 1));
52+
53+
$this->filterHandle
54+
->filterIn
55+
->calledWith(1);
5656
}
5757

5858
/**
59-
* @expectedException Arachne\EntityLoader\Exception\UnexpectedValueException
60-
* @expectedExceptionMessage FilterIn did not return an instance of 'Type1'.
59+
* @expectedException \Arachne\EntityLoader\Exception\UnexpectedValueException
60+
* @expectedExceptionMessage FilterIn did not return an instance of 'DateTime'.
6161
*/
6262
public function testFilterInFail()
6363
{
6464
$this->filterResolver
65-
->shouldReceive('resolve')
66-
->once()
67-
->with('Type1')
68-
->andReturn($this->filter);
69-
70-
$this->filter
71-
->shouldReceive('filterIn')
72-
->once()
73-
->with(1)
74-
->andReturn(null);
75-
76-
$this->entityLoader->filterIn('Type1', 1);
65+
->returns($this->filterHandle->get());
66+
67+
$this->filterHandle
68+
->filterIn
69+
->returns(null);
70+
71+
$this->entityLoader->filterIn(DateTime::class, 1);
7772
}
7873

7974
public function testFilterInIgnore()
8075
{
8176
// Make sure that the converter is not called at all if the parameter already has the desired type.
82-
$mock1 = Mockery::mock('Type1');
83-
$this->assertSame($mock1, $this->entityLoader->filterIn('Type1', $mock1));
77+
$mock1 = Phony::mock(DateTime::class)->get();
78+
$this->assertSame($mock1, $this->entityLoader->filterIn(DateTime::class, $mock1));
8479
}
8580

8681
/**
87-
* @expectedException Arachne\EntityLoader\Exception\UnexpectedValueException
88-
* @expectedExceptionMessage No filter in found for type 'Type1'.
82+
* @expectedException \Arachne\EntityLoader\Exception\UnexpectedValueException
83+
* @expectedExceptionMessage No filter in found for type 'DateTime'.
8984
*/
9085
public function testFilterNotFound()
9186
{
9287
$parameters = [
9388
'entity' => 'value1',
9489
];
9590

96-
$this->filterResolver
97-
->shouldReceive('resolve')
98-
->once()
99-
->with('Type1')
100-
->andReturn();
101-
102-
$this->entityLoader->filterIn('Type1', $parameters);
91+
$this->entityLoader->filterIn(DateTime::class, $parameters);
10392
}
10493
}

0 commit comments

Comments
 (0)