Skip to content

Commit 388f134

Browse files
committed
Adds proper testing for discoveries, removes object caching, discovery caches class name now
1 parent 5cc2a6f commit 388f134

8 files changed

+135
-105
lines changed

phpspec.yml.ci

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ suites:
22
discovery_suite:
33
namespace: Http\Discovery
44
psr4_prefix: Http\Discovery
5+
bootstrap: spec/bootstrap.php
56
extensions:
67
- PhpSpec\Extension\CodeCoverageExtension
78
formatter.name: pretty

phpspec.yml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ suites:
22
discovery_suite:
33
namespace: Http\Discovery
44
psr4_prefix: Http\Discovery
5+
bootstrap: spec/bootstrap.php
56
formatter.name: pretty

spec/ClassDiscoverySpec.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace spec\Http\Discovery;
4+
5+
use Http\Discovery\ClassDiscovery;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class ClassDiscoverySpec extends ObjectBehavior
9+
{
10+
function let()
11+
{
12+
$this->beAnInstanceOf('spec\Http\Discovery\DiscoveryStub');
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
18+
}
19+
20+
function it_registers_a_class()
21+
{
22+
$this->reset();
23+
24+
$this->register('class1', 'spec\Http\Discovery\AnotherClassToFind');
25+
26+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
27+
}
28+
29+
function it_registers_a_class_with_a_condition()
30+
{
31+
$this->reset();
32+
33+
$this->register('class1', 'spec\Http\Discovery\ClassToFind', 'invalid');
34+
$this->register('class2', 'spec\Http\Discovery\AnotherClassToFind', 'spec\Http\Discovery\TestClass');
35+
36+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
37+
}
38+
39+
function it_resets_cache_when_a_class_is_registered()
40+
{
41+
$this->reset();
42+
43+
$this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind');
44+
45+
$this->register('class1', 'spec\Http\Discovery\AnotherClassToFind');
46+
47+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
48+
}
49+
50+
function it_caches_a_found_class()
51+
{
52+
$this->reset();
53+
54+
$this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind');
55+
56+
$this->registerWithoutCacheReset('class1', 'spec\Http\Discovery\AnotherClassToFind');
57+
58+
$this->find()->shouldhaveType('spec\Http\Discovery\ClassToFind');
59+
}
60+
61+
function it_throws_an_exception_when_no_class_is_found()
62+
{
63+
$this->reset();
64+
65+
$this->register('class1', 'invalid');
66+
67+
$this->shouldThrow('Http\Discovery\NotFoundException')->duringFind();
68+
}
69+
}
70+
71+
class DiscoveryStub extends ClassDiscovery
72+
{
73+
protected static $cache;
74+
75+
/**
76+
* @var array
77+
*/
78+
protected static $classes;
79+
80+
/**
81+
* Reset classes
82+
*/
83+
public function reset()
84+
{
85+
static::$cache = null;
86+
87+
static::$classes = [
88+
'class1' => [
89+
'class' => 'spec\Http\Discovery\ClassToFind',
90+
'condition' => 'spec\Http\Discovery\ClassToFind'
91+
],
92+
];
93+
}
94+
95+
public function registerWithoutCacheReset($name, $class, $condition = null)
96+
{
97+
static::$classes[$name] = [
98+
'class' => $class,
99+
'condition' => $condition ?: $class,
100+
];
101+
}
102+
}
103+
104+
class ClassToFind {}
105+
class AnotherClassToFind {}
106+
class TestClass {}

spec/HttpAdapterDiscoverySpec.php

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace spec\Http\Discovery;
44

5-
use Http\Discovery\MessageFactoryDiscovery;
6-
use Http\Discovery\UriFactoryDiscovery;
75
use PhpSpec\ObjectBehavior;
86

97
class HttpAdapterDiscoverySpec extends ObjectBehavior
@@ -13,63 +11,13 @@ function it_is_initializable()
1311
$this->shouldHaveType('Http\Discovery\HttpAdapterDiscovery');
1412
}
1513

16-
function it_registers_a_factory()
14+
function it_is_a_class_discovery()
1715
{
18-
$this->reset();
19-
20-
$this->register('guzzle6', 'spec\Http\Discovery\AnotherGuzzle6HttpAdapter');
21-
22-
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherGuzzle6HttpAdapter');
16+
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
2317
}
2418

25-
function it_resets_cache_when_a_factory_is_registered()
19+
function it_finds_an_http_adapter()
2620
{
27-
$this->reset();
28-
29-
$firstMatch = $this->find();
30-
31-
$this->register('guzzle6', 'spec\Http\Discovery\AnotherGuzzle6HttpAdapter');
32-
33-
$this->find()->shouldNotBe($firstMatch);
34-
}
35-
36-
function it_caches_a_found_adapter()
37-
{
38-
$this->reset();
39-
40-
$firstMatch = $this->find()->shouldHaveType('spec\Http\Discovery\Guzzle6HttpAdapter');
41-
42-
$this->find()->shouldReturn($firstMatch);
43-
}
44-
45-
function it_finds_guzzle6_then_guzzle5_by_default()
46-
{
47-
$this->reset();
48-
49-
$this->find()->shouldHaveType('spec\Http\Discovery\Guzzle6HttpAdapter');
50-
51-
$this->register('guzzle6', 'invalid', '');
52-
53-
$this->find()->shouldHaveType('spec\Http\Discovery\Guzzle5HttpAdapter');
54-
}
55-
56-
function it_throws_an_exception_when_no_adapter_is_found()
57-
{
58-
$this->reset();
59-
60-
$this->register('guzzle6', 'invalid', '');
61-
$this->register('guzzle5', 'invalid', '');
62-
63-
$this->shouldThrow('Http\Discovery\NotFoundException')->duringFind();
64-
}
65-
66-
function reset()
67-
{
68-
$this->register('guzzle5', 'spec\Http\Discovery\Guzzle5HttpAdapter');
69-
$this->register('guzzle6', 'spec\Http\Discovery\Guzzle6HttpAdapter');
21+
$this->find()->shouldHaveType('Http\Adapter\HttpAdapter');
7022
}
7123
}
72-
73-
class Guzzle5HttpAdapter {}
74-
class Guzzle6HttpAdapter {}
75-
class AnotherGuzzle6HttpAdapter {}

spec/MessageFactoryDiscoverySpec.php

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,13 @@ function it_is_initializable()
1111
$this->shouldHaveType('Http\Discovery\MessageFactoryDiscovery');
1212
}
1313

14-
function it_registers_a_factory()
14+
function it_is_a_class_discovery()
1515
{
16-
$this->reset();
17-
18-
$this->register('guzzle', 'spec\Http\Discovery\Factory', 'spec\Http\Discovery\TestClass');
19-
20-
$this->find()->shouldHaveType('spec\Http\Discovery\Factory');
21-
}
22-
23-
function it_resets_cache_when_a_factory_is_registered()
24-
{
25-
$this->reset();
26-
27-
$firstGuess = $this->find();
28-
29-
$this->register('guzzle', 'spec\Http\Discovery\Factory', 'spec\Http\Discovery\TestClass');
30-
31-
$this->find()->shouldNotBe($firstGuess);
32-
}
33-
34-
function it_caches_a_found_message_factory()
35-
{
36-
$this->reset();
37-
38-
$firstGuess = $this->find()->shouldHaveType('Http\Discovery\MessageFactory\GuzzleFactory');
39-
40-
$this->find()->shouldReturn($firstGuess);
16+
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
4117
}
4218

4319
function it_finds_guzzle_then_zend_by_default()
4420
{
45-
$this->reset();
46-
4721
$this->find()->shouldHaveType('Http\Discovery\MessageFactory\GuzzleFactory');
4822

4923
$this->register('guzzle', 'invalid', '');
@@ -52,23 +26,4 @@ function it_finds_guzzle_then_zend_by_default()
5226
$this->find()->shouldHaveType('Http\Discovery\MessageFactory\DiactorosFactory');
5327
}
5428
}
55-
56-
function it_throws_an_exception_when_no_message_factory_is_found()
57-
{
58-
$this->reset();
59-
60-
$this->register('guzzle', 'invalid', '');
61-
$this->register('diactoros', 'invalid', '');
62-
63-
$this->shouldThrow('Http\Discovery\NotFoundException')->duringFind();
64-
}
65-
66-
function reset()
67-
{
68-
$this->register('guzzle', 'Http\Discovery\MessageFactory\GuzzleFactory', 'GuzzleHttp\Psr7\Request');
69-
$this->register('diactoros', 'Http\Discovery\MessageFactory\DiactorosFactory', 'Zend\Diactoros\Request');
70-
}
7129
}
72-
73-
class TestClass {}
74-
class Factory {}

spec/UriFactoryDiscoverySpec.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ function it_is_initializable()
1212
$this->shouldHaveType('Http\Discovery\UriFactoryDiscovery');
1313
}
1414

15+
function it_is_a_class_discovery()
16+
{
17+
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
18+
}
19+
1520
function it_finds_guzzle_then_zend_by_default()
1621
{
1722
$this->find()->shouldHaveType('Http\Discovery\UriFactory\GuzzleFactory');

spec/bootstrap.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
// Used for real return type check
4+
5+
if (!interface_exists('Http\Adapter\HttpAdapter')) {
6+
eval('namespace Http\Adapter; interface HttpAdapter {}');
7+
}
8+
9+
if (!class_exists('Http\Adapter\Guzzle6HttpAdapter')) {
10+
eval('namespace Http\Adapter; class Guzzle6HttpAdapter implements HttpAdapter {}');
11+
}
12+

src/ClassDiscovery.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ public static function find()
4646
{
4747
// We have a cache
4848
if (isset(static::$cache)) {
49-
return static::$cache;
49+
return new static::$cache;
5050
}
5151

5252
foreach (static::$classes as $name => $definition) {
5353
if (class_exists($definition['condition'])) {
54-
return static::$cache = new $definition['class'];
54+
static::$cache = $definition['class'];
55+
56+
return new $definition['class'];
5557
}
5658
}
5759

0 commit comments

Comments
 (0)