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
Changes from 3 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
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