Skip to content

Commit 410465b

Browse files
Nyholmsagikazarmark
authored andcommitted
Bugfixes (#71)
* Prepend and append should handle FQN and not objects * Bugfixes * typo * Added tests
1 parent 96419ad commit 410465b

File tree

4 files changed

+85
-11
lines changed

4 files changed

+85
-11
lines changed

spec/ClassDiscoverySpec.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Puli\Repository\Api\ResourceRepository;
1212
use PhpSpec\ObjectBehavior;
1313
use spec\Http\Discovery\Helper\DiscoveryHelper;
14+
use spec\Http\Discovery\Helper\FailedDiscoveryStrategy;
15+
use spec\Http\Discovery\Helper\SuccessfullDiscoveryStrategy;
1416

1517
class ClassDiscoverySpec extends ObjectBehavior
1618
{
@@ -32,24 +34,53 @@ function it_throws_an_exception_when_no_candidate_found(DiscoveryStrategy $strat
3234
$this->shouldThrow(DiscoveryFailedException::class)->duringFind('NoCandidate');
3335
}
3436

35-
function it_returns_a_class(DiscoveryStrategy $strategy)
37+
function it_returns_a_class()
3638
{
3739
$candidate = ['class' => 'ClassName', 'condition' => true];
3840
DiscoveryHelper::setClasses('Foobar', [$candidate]);
3941

4042
$this->find('Foobar')->shouldReturn('ClassName');
4143
}
4244

43-
function it_validates_conditions(DiscoveryStrategy $strategy) {
45+
function it_validates_conditions() {
4446
$c0 = ['class' => 'ClassName0', 'condition' => false];
4547
$c1 = ['class' => 'ClassName1', 'condition' => true];
4648
$c2 = ['class' => 'ClassName2', 'condition' => false];
4749
DiscoveryHelper::setClasses('Foobar', [$c0, $c1, $c2]);
4850

4951
$this->find('Foobar')->shouldReturn('ClassName1');
5052
}
53+
54+
function it_prepends_strategies() {
55+
$candidate = ['class' => 'Added'];
56+
DiscoveryHelper::setClasses('Foobar', [$candidate]);
57+
58+
ClassDiscovery::setStrategies([SuccessfullDiscoveryStrategy::class]);
59+
ClassDiscovery::prependStrategy(DiscoveryHelper::class);
60+
61+
$this->find('Foobar')->shouldReturn('Added');
62+
}
63+
64+
function it_appends_strategies() {
65+
$candidate = ['class' => 'Added'];
66+
DiscoveryHelper::setClasses('Foobar', [$candidate]);
67+
68+
// Make sure our strategy is added to the list
69+
ClassDiscovery::setStrategies([FailedDiscoveryStrategy::class]);
70+
ClassDiscovery::appendStrategy(DiscoveryHelper::class);
71+
72+
$this->find('Foobar')->shouldReturn('Added');
73+
74+
// Make sure it is added last in the list
75+
ClassDiscovery::setStrategies([SuccessfullDiscoveryStrategy::class]);
76+
ClassDiscovery::appendStrategy(DiscoveryHelper::class);
77+
78+
$this->find('Foobar')->shouldReturn('Success');
79+
}
5180
}
5281

82+
83+
5384
class ClassDiscoveryStub extends ClassDiscovery
5485
{
5586
public static function find($type)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace spec\Http\Discovery\Helper;
4+
5+
use Http\Discovery\Strategy\DiscoveryStrategy;
6+
7+
/**
8+
* This is a discovery helper used in tests. It will always pass an empty array of candidates.
9+
*
10+
* @author Tobias Nyholm <[email protected]>
11+
*/
12+
class FailedDiscoveryStrategy implements DiscoveryStrategy
13+
{
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public static function getCandidates($type)
18+
{
19+
return [];
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace spec\Http\Discovery\Helper;
4+
5+
use Http\Discovery\Strategy\DiscoveryStrategy;
6+
7+
/**
8+
* This is a discovery helper used in tests. It will always pass a class named "Sucess"
9+
*
10+
* @author Tobias Nyholm <[email protected]>
11+
*/
12+
class SuccessfullDiscoveryStrategy implements DiscoveryStrategy
13+
{
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public static function getCandidates($type)
18+
{
19+
return [['class'=>'Success']];
20+
}
21+
}

src/ClassDiscovery.php

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

55
use Http\Discovery\Exception\DiscoveryFailedException;
66
use Http\Discovery\Exception\StrategyUnavailableException;
7-
use Http\Discovery\Strategy\DiscoveryStrategy;
87

98
/**
109
* Registry that based find results on class existence.
@@ -88,8 +87,10 @@ private static function getFromCache($type)
8887
}
8988

9089
$candidate = self::$cache[$type];
91-
if (!self::evaluateCondition($candidate['condition'])) {
92-
return;
90+
if (isset($candidate['condition'])) {
91+
if (!self::evaluateCondition($candidate['condition'])) {
92+
return;
93+
}
9394
}
9495

9596
return $candidate['class'];
@@ -109,7 +110,7 @@ private static function storeInCache($type, $class)
109110
/**
110111
* Set new strategies and clear the cache.
111112
*
112-
* @param DiscoveryStrategy[] $strategies
113+
* @param array $strategies string array of fully qualified class name to a DiscoveryStrategy
113114
*/
114115
public static function setStrategies(array $strategies)
115116
{
@@ -120,9 +121,9 @@ public static function setStrategies(array $strategies)
120121
/**
121122
* Append a strategy at the end of the strategy queue.
122123
*
123-
* @param DiscoveryStrategy $strategy
124+
* @param string $strategy Fully qualified class name to a DiscoveryStrategy
124125
*/
125-
public static function appendStrategy(DiscoveryStrategy $strategy)
126+
public static function appendStrategy($strategy)
126127
{
127128
self::$strategies[] = $strategy;
128129
self::clearCache();
@@ -131,11 +132,11 @@ public static function appendStrategy(DiscoveryStrategy $strategy)
131132
/**
132133
* Prepend a strategy at the beginning of the strategy queue.
133134
*
134-
* @param DiscoveryStrategy $strategy
135+
* @param string $strategy Fully qualified class name to a DiscoveryStrategy
135136
*/
136-
public static function prependStrategy(DiscoveryStrategy $strategy)
137+
public static function prependStrategy($strategy)
137138
{
138-
self::$strategies = array_unshift(self::$strategies, $strategy);
139+
array_unshift(self::$strategies, $strategy);
139140
self::clearCache();
140141
}
141142

0 commit comments

Comments
 (0)