Skip to content

Commit 9db2184

Browse files
committed
Merge pull request #34 from php-http/puli_discovery
Use Puli discovery
2 parents 05c48ec + 2d3bc8d commit 9db2184

21 files changed

+422
-338
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
.puli/
12
build/
23
vendor/
34
composer.lock
45
phpspec.yml
56
phpunit.xml
7+
puli.json

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Change Log
22

33

4+
## Unreleased
5+
6+
### Changed
7+
8+
- Use [Puli](http://puli.io) for discovery
9+
10+
411
## 0.5.0 - 2015-12-25
512

613
### Changed

composer.json

+11-9
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,33 @@
1616
],
1717
"require": {
1818
"php": ">=5.4",
19-
"php-http/message-factory": "^1.0"
19+
"puli/composer-plugin": "1.0.0-beta8",
20+
"puli/discovery": "1.0.0-beta8"
2021
},
2122
"require-dev": {
22-
"php-http/message": "^0.1",
23-
"zendframework/zend-diactoros": "^1.0",
24-
"guzzlehttp/psr7": "^1.0",
25-
"php-http/guzzle6-adapter": "^0.2",
23+
"php-http/httplug": "1.0.0-beta",
24+
"php-http/message-factory": "^1.0",
25+
"puli/cli": "1.0.0-beta9",
2626
"phpspec/phpspec": "^2.4",
2727
"henrikbjorn/phpspec-code-coverage" : "^1.0"
2828
},
29-
"suggest": {
30-
"php-http/utils": "To use Guzzle or Diactoros factories"
31-
},
3229
"autoload": {
3330
"psr-4": {
3431
"Http\\Discovery\\": "src/"
3532
}
3633
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"spec\\Http\\Discovery\\": "spec/"
37+
}
38+
},
3739
"scripts": {
3840
"test": "vendor/bin/phpspec run",
3941
"test-ci": "vendor/bin/phpspec run -c phpspec.yml.ci"
4042
},
4143
"extra": {
4244
"branch-alias": {
43-
"dev-master": "0.5-dev"
45+
"dev-master": "0.6-dev"
4446
}
4547
},
4648
"prefer-stable": true,

spec/ClassDiscoverySpec.php

+45-125
Original file line numberDiff line numberDiff line change
@@ -3,163 +3,83 @@
33
namespace spec\Http\Discovery;
44

55
use Http\Discovery\ClassDiscovery;
6+
use Puli\Discovery\Binding\ClassBinding;
7+
use Puli\GeneratedPuliFactory;
8+
use Puli\Discovery\Api\Discovery;
9+
use Puli\Repository\Api\ResourceRepository;
610
use PhpSpec\ObjectBehavior;
711

812
class ClassDiscoverySpec extends ObjectBehavior
913
{
10-
function let()
11-
{
12-
$this->beAnInstanceOf('spec\Http\Discovery\DiscoveryStub');
14+
function let(
15+
GeneratedPuliFactory $puliFactory,
16+
ResourceRepository $repository,
17+
Discovery $discovery
18+
) {
19+
$puliFactory->createRepository()->willReturn($repository);
20+
$puliFactory->createDiscovery($repository)->willReturn($discovery);
21+
22+
$this->beAnInstanceOf('spec\Http\Discovery\ClassDiscoveryStub');
23+
$this->setPuliFactory($puliFactory);
1324
}
1425

15-
function it_is_initializable()
26+
function letgo()
1627
{
17-
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
28+
$this->resetPuliFactory();
1829
}
1930

20-
function it_registers_a_class()
31+
function it_is_initializable()
2132
{
22-
$this->reset();
23-
24-
$this->register('spec\Http\Discovery\AnotherClassToFind');
25-
26-
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
33+
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
2734
}
2835

29-
function it_registers_a_class_with_a_condition()
36+
function it_has_a_puli_factory(GeneratedPuliFactory $puliFactory)
3037
{
31-
$this->reset();
32-
33-
$this->register('spec\Http\Discovery\AnotherClassToFind', 'spec\Http\Discovery\TestClass');
34-
$this->register('spec\Http\Discovery\ClassToFind', false);
35-
36-
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
38+
$this->getPuliFactory()->shouldReturn($puliFactory);
3739
}
3840

39-
function it_registers_a_class_with_a_callable_condition()
41+
function it_has_a_puli_discovery(Discovery $discovery)
4042
{
41-
$this->reset();
42-
43-
$this->register('spec\Http\Discovery\AnotherClassToFind', function() { return true; });
44-
$this->register('spec\Http\Discovery\ClassToFind', false);
45-
46-
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
43+
$this->getPuliDiscovery()->shouldReturn($discovery);
4744
}
4845

49-
function it_registers_a_class_with_a_boolean_condition()
46+
function it_throws_an_exception_when_binding_not_found(Discovery $discovery)
5047
{
51-
$this->reset();
52-
53-
$this->register('spec\Http\Discovery\AnotherClassToFind', true);
54-
$this->register('spec\Http\Discovery\ClassToFind', false);
48+
$discovery->findBindings('InvalidBinding')->willReturn([]);
5549

56-
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
57-
}
58-
59-
function it_registers_a_class_with_an_array_condition()
60-
{
61-
$this->reset();
62-
63-
$this->register(
64-
'spec\Http\Discovery\AnotherClassToFind',
65-
[
66-
true,
67-
'spec\Http\Discovery\AnotherClassToFind',
68-
]
69-
);
70-
$this->register(
71-
'spec\Http\Discovery\ClassToFind',
72-
[
73-
false,
74-
'spec\Http\Discovery\ClassToFind',
75-
]
76-
);
77-
78-
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
50+
$this->shouldThrow('Http\Discovery\NotFoundException')->duringFindOneByType('InvalidBinding');
7951
}
8052

81-
function it_registers_a_class_with_an_invalid_condition()
53+
function it_returns_a_class_binding(Discovery $discovery, ClassBinding $binding)
8254
{
83-
$this->reset();
55+
$binding->hasParameterValue('depends')->willReturn(false);
56+
$binding->getClassName()->willReturn('ClassName');
8457

85-
$this->register('spec\Http\Discovery\AnotherClassToFind', true);
86-
$this->register('spec\Http\Discovery\ClassToFind', new \stdClass);
58+
$discovery->findBindings('Binding')->willReturn([$binding]);
8759

88-
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
60+
$this->findOneByType('Binding')->shouldReturn('ClassName');
8961
}
9062

91-
function it_resets_cache_when_a_class_is_registered()
92-
{
93-
$this->reset();
63+
function it_returns_a_class_binding_with_dependency(
64+
Discovery $discovery,
65+
ClassBinding $binding1,
66+
ClassBinding $binding2
67+
) {
68+
$binding1->hasParameterValue('depends')->willReturn(true);
69+
$binding1->getParameterValue('depends')->willReturn(false);
9470

95-
$this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind');
71+
$binding2->hasParameterValue('depends')->willReturn(false);
72+
$binding2->getClassName()->willReturn('ClassName');
9673

97-
$this->register('spec\Http\Discovery\AnotherClassToFind');
74+
$discovery->findBindings('Binding')->willReturn([
75+
$binding1,
76+
$binding2,
77+
]);
9878

99-
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
100-
}
101-
102-
function it_caches_a_found_class()
103-
{
104-
$this->reset();
105-
106-
$this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind');
107-
108-
$this->registerWithoutCacheReset('spec\Http\Discovery\AnotherClassToFind');
109-
110-
$this->find()->shouldhaveType('spec\Http\Discovery\ClassToFind');
111-
}
112-
113-
function it_throws_an_exception_when_no_class_is_found()
114-
{
115-
$this->resetEmpty();
116-
117-
$this->shouldThrow('Http\Discovery\NotFoundException')->duringFind();
79+
$this->findOneByType('Binding')->shouldReturn('ClassName');
11880
}
11981
}
12082

121-
class DiscoveryStub extends ClassDiscovery
83+
class ClassDiscoveryStub extends ClassDiscovery
12284
{
123-
protected static $cache;
124-
125-
/**
126-
* @var array
127-
*/
128-
protected static $classes;
129-
130-
/**
131-
* Reset classes
132-
*/
133-
public function reset()
134-
{
135-
static::$cache = null;
136-
137-
static::$classes = [
138-
[
139-
'class' => 'spec\Http\Discovery\ClassToFind',
140-
'condition' => 'spec\Http\Discovery\ClassToFind'
141-
],
142-
];
143-
}
144-
145-
public function registerWithoutCacheReset($class, $condition = null)
146-
{
147-
$definition = [
148-
'class' => $class,
149-
'condition' => isset($condition) ? $condition : $class,
150-
];
151-
152-
array_unshift(static::$classes, $definition);
153-
}
154-
155-
public function resetEmpty()
156-
{
157-
static::$cache = null;
158-
159-
static::$classes = [];
160-
}
16185
}
162-
163-
class ClassToFind {}
164-
class AnotherClassToFind {}
165-
class TestClass {}

spec/HttpAsyncClientDiscoverySpec.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22

33
namespace spec\Http\Discovery;
44

5+
use Puli\GeneratedPuliFactory;
6+
use Puli\Discovery\Api\Discovery;
7+
use Puli\Discovery\Binding\ClassBinding;
8+
use Puli\Repository\Api\ResourceRepository;
59
use PhpSpec\ObjectBehavior;
610

711
class HttpAsyncClientDiscoverySpec extends ObjectBehavior
812
{
13+
function let(
14+
GeneratedPuliFactory $puliFactory,
15+
ResourceRepository $repository,
16+
Discovery $discovery
17+
) {
18+
$puliFactory->createRepository()->willReturn($repository);
19+
$puliFactory->createDiscovery($repository)->willReturn($discovery);
20+
21+
$this->setPuliFactory($puliFactory);
22+
}
23+
24+
function letgo()
25+
{
26+
$this->resetPuliFactory();
27+
}
28+
929
function it_is_initializable()
1030
{
1131
$this->shouldHaveType('Http\Discovery\HttpAsyncClientDiscovery');
@@ -16,8 +36,15 @@ function it_is_a_class_discovery()
1636
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
1737
}
1838

19-
function it_finds_an_http_client()
20-
{
39+
function it_finds_an_async_http_client(
40+
Discovery $discovery,
41+
ClassBinding $binding
42+
) {
43+
$binding->hasParameterValue('depends')->willReturn(false);
44+
$binding->getClassName()->willReturn('spec\Http\Discovery\Stub\HttpAsyncClientStub');
45+
46+
$discovery->findBindings('Http\Client\HttpAsyncClient')->willReturn([$binding]);
47+
2148
$this->find()->shouldImplement('Http\Client\HttpAsyncClient');
2249
}
2350
}

spec/HttpClientDiscoverySpec.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22

33
namespace spec\Http\Discovery;
44

5+
use Puli\GeneratedPuliFactory;
6+
use Puli\Discovery\Api\Discovery;
7+
use Puli\Discovery\Binding\ClassBinding;
8+
use Puli\Repository\Api\ResourceRepository;
59
use PhpSpec\ObjectBehavior;
610

711
class HttpClientDiscoverySpec extends ObjectBehavior
812
{
13+
function let(
14+
GeneratedPuliFactory $puliFactory,
15+
ResourceRepository $repository,
16+
Discovery $discovery
17+
) {
18+
$puliFactory->createRepository()->willReturn($repository);
19+
$puliFactory->createDiscovery($repository)->willReturn($discovery);
20+
21+
$this->setPuliFactory($puliFactory);
22+
}
23+
24+
function letgo()
25+
{
26+
$this->resetPuliFactory();
27+
}
28+
929
function it_is_initializable()
1030
{
1131
$this->shouldHaveType('Http\Discovery\HttpClientDiscovery');
@@ -16,8 +36,15 @@ function it_is_a_class_discovery()
1636
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
1737
}
1838

19-
function it_finds_an_http_client()
20-
{
39+
function it_finds_an_http_client(
40+
Discovery $discovery,
41+
ClassBinding $binding
42+
) {
43+
$binding->hasParameterValue('depends')->willReturn(false);
44+
$binding->getClassName()->willReturn('spec\Http\Discovery\Stub\HttpClientStub');
45+
46+
$discovery->findBindings('Http\Client\HttpClient')->willReturn([$binding]);
47+
2148
$this->find()->shouldImplement('Http\Client\HttpClient');
2249
}
2350
}

0 commit comments

Comments
 (0)