Skip to content

Commit a2a00ec

Browse files
Nyholmdbu
authored andcommitted
Added tests and fixed issues with NoCandidateFoundException (#132)
1 parent be7a6a0 commit a2a00ec

10 files changed

+216
-9
lines changed

.travis.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,24 @@ matrix:
2828
- php: 7.3
2929
fast_finish: true
3030
include:
31-
- php: 5.5
32-
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" PULI_VERSION=1.0.0-beta9
33-
31+
- name: PHPSpec code coverage
32+
php: 5.5
33+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" PULI_VERSION=1.0.0-beta9 DEPENDENCIES="henrikbjorn/phpspec-code-coverage:^2.0.2"
34+
- name: PHPUnit tests
35+
php: 7.2
36+
env: TEST_COMMAND="./vendor/bin/phpunit" DEPENDENCIES="phpunit/phpunit:^7.5 nyholm/psr7:^1.0 kriswallsmith/buzz:^1.0@beta php-http/curl-client:^1.0 php-http/message"
37+
- name: PHPUnit test with nothing installed
38+
php: 7.2
39+
env: TEST_COMMAND="./vendor/bin/phpunit --group=NothingInstalled" DEPENDENCIES="phpunit/phpunit:^7.5"
3440

3541
before_install:
3642
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
43+
- if ! [ -z "$DEPENDENCIES" ]; then composer require --no-update ${DEPENDENCIES}; fi;
3744

3845
install:
3946
- wget https://github.com/puli/cli/releases/download/1.0.0-beta10/puli.phar && chmod +x puli.phar
4047
- composer require puli/composer-plugin:${PULI_VERSION} --no-update
41-
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction
48+
- composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction
4249

4350
script:
4451
- $TEST_COMMAND

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
"php-http/httplug": "^1.0 || ^2.0",
1818
"php-http/message-factory": "^1.0",
1919
"puli/composer-plugin": "1.0.0-beta10",
20-
"phpspec/phpspec": "^2.4",
21-
"henrikbjorn/phpspec-code-coverage" : "^2.0.2"
20+
"phpspec/phpspec": "^2.4"
2221
},
2322
"suggest": {
2423
"puli/composer-plugin": "Sets up Puli which is recommended for Discovery to work. Check http://docs.php-http.org/en/latest/discovery.html for more details.",

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap="./vendor/autoload.php"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true">
8+
9+
<testsuites>
10+
<testsuite name="HTTPlug unit tests">
11+
<directory suffix="Test.php">./tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
15+
<groups>
16+
<exclude>
17+
<group>NothingInstalled</group>
18+
</exclude>
19+
</groups>
20+
</phpunit>

src/Exception/NoCandidateFoundException.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,22 @@ function ($a) {
2727
$message = sprintf(
2828
'No valid candidate found using strategy "%s". We tested the following candidates: %s.',
2929
$strategy,
30-
implode(', ', $classes)
30+
implode(', ', array_map([$this, 'stringify'], $classes))
3131
);
3232

3333
parent::__construct($message);
3434
}
35+
36+
private function stringify($mixed)
37+
{
38+
if (is_string($mixed)) {
39+
return $mixed;
40+
}
41+
42+
if (is_array($mixed) && 2 === count($mixed)) {
43+
return sprintf('%s::%s', $this->stringify($mixed[0]), $mixed[1]);
44+
}
45+
46+
return is_object($mixed) ? get_class($mixed) : gettype($mixed);
47+
}
3548
}

src/Psr17FactoryDiscovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class Psr17FactoryDiscovery extends ClassDiscovery
2020
private static function createException($type, Exception $e)
2121
{
2222
return new \Http\Discovery\Exception\NotFoundException(
23-
'No psr-17 '.$type.' found. Install a package from this list: https://packagist.org/providers/psr/http-factory-implementation',
23+
'No PSR-17 '.$type.' found. Install a package from this list: https://packagist.org/providers/psr/http-factory-implementation',
2424
0,
2525
$e
2626
);

src/Strategy/CommonClassesStrategy.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ final class CommonClassesStrategy implements DiscoveryStrategy
8181
'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class],
8282
],
8383
],
84-
Psr18Client::class => [],
84+
Psr18Client::class => [
85+
[
86+
'class' => [self::class, 'buzzInstantiate'],
87+
'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class],
88+
],
89+
],
8590
];
8691

8792
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace tests\Http\Discovery\Exception;
4+
5+
use Http\Discovery\Exception as DiscoveryException;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class InitializeExceptionTest extends TestCase
9+
{
10+
public function testInitialize()
11+
{
12+
$e[] = new DiscoveryException\ClassInstantiationFailedException();
13+
$e[] = new DiscoveryException\NotFoundException();
14+
$e[] = new DiscoveryException\PuliUnavailableException();
15+
$e[] = new DiscoveryException\StrategyUnavailableException();
16+
$e[] = new DiscoveryException\NoCandidateFoundException('CommonClasses', []);
17+
$e[] = DiscoveryException\DiscoveryFailedException::create($e);
18+
19+
foreach ($e as $exception) {
20+
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception);
21+
}
22+
}
23+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace tests\Http\Discovery\Exception;
4+
5+
use Http\Discovery\Exception as DiscoveryException;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class NoCandidateFoundExceptionTest extends TestCase
9+
{
10+
public function testInitialize()
11+
{
12+
$candidates = [
13+
['class' => 'Foo', 'condition' => true],
14+
['class' => 'Bar', 'condition' => false],
15+
];
16+
$exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates);
17+
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception);
18+
$this->assertStringStartsWith('No valid candidate found using strategy "Foobar".', $exception->getMessage());
19+
}
20+
21+
public function testInitializeWithArrayCallable()
22+
{
23+
$candidates = [
24+
['class' => ['AnyClass', 'anyFunction'], 'condition' => true],
25+
];
26+
27+
$exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates);
28+
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception);
29+
}
30+
31+
public function testInitializeWithObjectCallable()
32+
{
33+
$obj = new \stdClass();
34+
$candidates = [
35+
['class' => [$obj, 'anyFunction'], 'condition' => true],
36+
];
37+
38+
$exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates);
39+
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception);
40+
}
41+
42+
public function testInitializeWithClosure()
43+
{
44+
$obj = \Closure::fromCallable(function () {
45+
$x = 2;
46+
});
47+
$candidates = [
48+
['class' => $obj, 'condition' => true],
49+
];
50+
51+
$exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates);
52+
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception);
53+
}
54+
55+
public function testInitializeWithAnonymousFunction()
56+
{
57+
$func = function () {
58+
$x = 2;
59+
};
60+
$candidates = [
61+
['class' => $func, 'condition' => true],
62+
];
63+
64+
$exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates);
65+
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception);
66+
}
67+
}

tests/Psr17FactoryDiscoveryTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace tests\Http\Discovery;
4+
5+
use Http\Discovery\Exception\NotFoundException;
6+
use Http\Discovery\Psr17FactoryDiscovery;
7+
use PHPUnit\Framework\TestCase;
8+
use Psr\Http\Message\RequestFactoryInterface;
9+
use Psr\Http\Message\ResponseFactoryInterface;
10+
use Psr\Http\Message\ServerRequestFactoryInterface;
11+
use Psr\Http\Message\StreamFactoryInterface;
12+
use Psr\Http\Message\UploadedFileFactoryInterface;
13+
use Psr\Http\Message\UriFactoryInterface;
14+
15+
class Psr17FactoryDiscoveryTest extends TestCase
16+
{
17+
/**
18+
* @dataProvider getFactories
19+
*/
20+
public function testFind($method, $interface)
21+
{
22+
$callable = [Psr17FactoryDiscovery::class, $method];
23+
$client = $callable();
24+
$this->assertInstanceOf($interface, $client);
25+
}
26+
27+
/**
28+
* @group NothingInstalled
29+
* @dataProvider getFactories
30+
*/
31+
public function testNotFound($method)
32+
{
33+
$callable = [Psr17FactoryDiscovery::class, $method];
34+
$this->expectException(NotFoundException::class);
35+
$callable();
36+
}
37+
38+
public function getFactories()
39+
{
40+
yield ['findRequestFactory', RequestFactoryInterface::class];
41+
yield ['findResponseFactory', ResponseFactoryInterface::class];
42+
yield ['findServerRequestFactory', ServerRequestFactoryInterface::class];
43+
yield ['findStreamFactory', StreamFactoryInterface::class];
44+
yield ['findUploadedFileFactory', UploadedFileFactoryInterface::class];
45+
yield ['findUrlFactory', UriFactoryInterface::class];
46+
}
47+
}

tests/Psr18ClientDiscoveryTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace tests\Http\Discovery;
4+
5+
use Http\Discovery\Exception\NotFoundException;
6+
use Http\Discovery\Psr18ClientDiscovery;
7+
use PHPUnit\Framework\TestCase;
8+
use Psr\Http\Client\ClientInterface;
9+
10+
class Psr18ClientDiscoveryTest extends TestCase
11+
{
12+
public function testFind()
13+
{
14+
$client = Psr18ClientDiscovery::find();
15+
$this->assertInstanceOf(ClientInterface::class, $client);
16+
}
17+
18+
/**
19+
* @group NothingInstalled
20+
*/
21+
public function testNotFound()
22+
{
23+
$this->expectException(NotFoundException::class);
24+
$client = Psr18ClientDiscovery::find();
25+
}
26+
}

0 commit comments

Comments
 (0)