Skip to content

Bugfixes #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions spec/ClassDiscoverySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Puli\Repository\Api\ResourceRepository;
use PhpSpec\ObjectBehavior;
use spec\Http\Discovery\Helper\DiscoveryHelper;
use spec\Http\Discovery\Helper\FailedDiscoveryStrategy;
use spec\Http\Discovery\Helper\SuccessfullDiscoveryStrategy;

class ClassDiscoverySpec extends ObjectBehavior
{
Expand All @@ -33,24 +35,53 @@ function it_throws_an_exception_when_no_candidate_found(DiscoveryStrategy $strat
$this->shouldThrow(DiscoveryFailedException::class)->duringFind('NoCandidate');
}

function it_returns_a_class(DiscoveryStrategy $strategy)
function it_returns_a_class()
{
$candidate = ['class' => 'ClassName', 'condition' => true];
DiscoveryHelper::setClasses('Foobar', [$candidate]);

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

function it_validates_conditions(DiscoveryStrategy $strategy) {
function it_validates_conditions() {
$c0 = ['class' => 'ClassName0', 'condition' => false];
$c1 = ['class' => 'ClassName1', 'condition' => true];
$c2 = ['class' => 'ClassName2', 'condition' => false];
DiscoveryHelper::setClasses('Foobar', [$c0, $c1, $c2]);

$this->find('Foobar')->shouldReturn('ClassName1');
}

function it_prepends_strategies() {
$candidate = ['class' => 'Added'];
DiscoveryHelper::setClasses('Foobar', [$candidate]);

ClassDiscovery::setStrategies([SuccessfullDiscoveryStrategy::class]);
ClassDiscovery::prependStrategy(DiscoveryHelper::class);

$this->find('Foobar')->shouldReturn('Added');
}

function it_appends_strategies() {
$candidate = ['class' => 'Added'];
DiscoveryHelper::setClasses('Foobar', [$candidate]);

// Make sure our strategy is added to the list
ClassDiscovery::setStrategies([FailedDiscoveryStrategy::class]);
ClassDiscovery::appendStrategy(DiscoveryHelper::class);

$this->find('Foobar')->shouldReturn('Added');

// Make sure it is added last in the list
ClassDiscovery::setStrategies([SuccessfullDiscoveryStrategy::class]);
ClassDiscovery::appendStrategy(DiscoveryHelper::class);

$this->find('Foobar')->shouldReturn('Success');
}
}



class ClassDiscoveryStub extends ClassDiscovery
{
public static function find($type)
Expand Down
21 changes: 21 additions & 0 deletions spec/Helper/FailedDiscoveryStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace spec\Http\Discovery\Helper;

use Http\Discovery\Strategy\DiscoveryStrategy;

/**
* This is a discovery helper used in tests. It will always pass an empty array of candidates.
*
* @author Tobias Nyholm <[email protected]>
*/
class FailedDiscoveryStrategy implements DiscoveryStrategy
{
/**
* {@inheritdoc}
*/
public static function getCandidates($type)
{
return [];
}
}
21 changes: 21 additions & 0 deletions spec/Helper/SuccessfullDiscoveryStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace spec\Http\Discovery\Helper;

use Http\Discovery\Strategy\DiscoveryStrategy;

/**
* This is a discovery helper used in tests. It will always pass a class named "Sucess"
*
* @author Tobias Nyholm <[email protected]>
*/
class SuccessfullDiscoveryStrategy implements DiscoveryStrategy
{
/**
* {@inheritdoc}
*/
public static function getCandidates($type)
{
return [['class'=>'Success']];
}
}
19 changes: 10 additions & 9 deletions src/ClassDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Http\Discovery\Exception\DiscoveryFailedException;
use Http\Discovery\Exception\StrategyUnavailableException;
use Http\Discovery\Strategy\DiscoveryStrategy;

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

$candidate = self::$cache[$type];
if (!self::evaluateCondition($candidate['condition'])) {
return;
if (isset($candidate['condition'])) {
if (!self::evaluateCondition($candidate['condition'])) {
return;
}
}

return $candidate['class'];
Expand All @@ -109,7 +110,7 @@ private static function storeInCache($type, $class)
/**
* Set new strategies and clear the cache.
*
* @param DiscoveryStrategy[] $strategies
* @param array $strategies string array of fully qualified class name to a DiscoveryStrategy
*/
public static function setStrategies(array $strategies)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a similar test could be done here, setting the strategies to only that test strategy that returns a mock client

{
Expand All @@ -120,9 +121,9 @@ public static function setStrategies(array $strategies)
/**
* Append a strategy at the end of the strategy queue.
*
* @param DiscoveryStrategy $strategy
* @param string $strategy Fully qualified class name to a DiscoveryStrategy
*/
public static function appendStrategy(DiscoveryStrategy $strategy)
public static function appendStrategy($strategy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a test? i think we should have a test that shows this was a problem.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. But Im not sure how to write a test for that. self::$strategies is private.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a resource ONLY in an appended strategy and see if you can resolve that resource.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and to test this, first setStrategies to empty or something that never works, then append the one that returns a mock client.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct me if Im wrong. But the problem here is that appendStrategy($strategy) takes a FQCN not an object. I could add another DiscoveryHelper but it feels weird.. doesn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this is not possible.

    function it_prepends_strategies(DiscoveryStrategy $successfulStrategy, DiscoveryStrategy $addedStrategy) {
        $successfulStrategy->getCandidates('Foobar')->willReturn([['class'=>'Success']]);
        $addedStrategy->getCandidates('Foobar')->shouldBeCalled()->willReturn([['class'=>'Added']]);

        ClassDiscovery::setStrategies([$successfulStrategy]);
        ClassDiscovery::prependStrategy($addedStrategy);

        $this->find('Foobar')->shouldReturn('Added');
    }

    function it_appends_strategies(DiscoveryStrategy $failedStrategy, DiscoveryStrategy $addedStrategy) {
        $failedStrategy->getCandidates('Foobar')->shouldBeCalled()->willReturn([]);
        $addedStrategy->getCandidates('Foobar')->shouldBeCalled()->willReturn([['class'=>'Added']]);

        ClassDiscovery::setStrategies([$failedStrategy]);
        ClassDiscovery::appendStrategy($addedStrategy);

        $this->find('Foobar')->shouldReturn('Added');
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe like this:

ClassDiscovery::setStrategies(['Http\Discovery\MockDiscovery']);
...
class MockDiscovery implements Discovery
{
    ...
}
class MockClient implements Client
{
    public function ... () {} // do nothing
}

but indeed with passing strings, you would need quite a few mock classes.

{
self::$strategies[] = $strategy;
self::clearCache();
Expand All @@ -131,11 +132,11 @@ public static function appendStrategy(DiscoveryStrategy $strategy)
/**
* Prepend a strategy at the beginning of the strategy queue.
*
* @param DiscoveryStrategy $strategy
* @param string $strategy Fully qualified class name to a DiscoveryStrategy
*/
public static function prependStrategy(DiscoveryStrategy $strategy)
public static function prependStrategy($strategy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a full functional test with a prepend strategy that returns a mock?

{
self::$strategies = array_unshift(self::$strategies, $strategy);
array_unshift(self::$strategies, $strategy);
self::clearCache();
}

Expand Down