From 2cfa0eb89c131743c1b55ee8eb69f1be589cbda2 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 5 Jan 2019 13:38:31 +0100 Subject: [PATCH 1/9] Added tests and fixed issues with NoCandidateFoundException --- .travis.yml | 7 +- composer.json | 3 +- phpunit.xml.dist | 21 ++++++ src/Exception/NoCandidateFoundException.php | 15 +++- src/Strategy/CommonClassesStrategy.php | 7 +- tests/Exception/InitializeExceptionTest.php | 25 +++++++ .../NoCandidateFoundExceptionTest.php | 69 +++++++++++++++++++ tests/Psr18ClientDiscoveryTest.php | 37 ++++++++++ 8 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 phpunit.xml.dist create mode 100644 tests/Exception/InitializeExceptionTest.php create mode 100644 tests/Exception/NoCandidateFoundExceptionTest.php create mode 100644 tests/Psr18ClientDiscoveryTest.php diff --git a/.travis.yml b/.travis.yml index a764af4..8763cc2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,8 +29,11 @@ matrix: fast_finish: true include: - php: 5.5 - env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" PULI_VERSION=1.0.0-beta9 - + env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" PULI_VERSION=1.0.0-beta9 DEPENDENCIES="phpspec/phpspec:^2.4 henrikbjorn/phpspec-code-coverage:^2.0.2" + - php: 7.2 + 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" + - php: 7.2 + env: TEST_COMMAND="./vendor/bin/phpunit --group=NothingInstalled" DEPENDENCIES="phpunit/phpunit:^7.5" before_install: - if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi diff --git a/composer.json b/composer.json index df1e31a..96542c2 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,7 @@ "php-http/httplug": "^1.0 || ^2.0", "php-http/message-factory": "^1.0", "puli/composer-plugin": "1.0.0-beta10", - "phpspec/phpspec": "^2.4", - "henrikbjorn/phpspec-code-coverage" : "^2.0.2" + "phpspec/phpspec": "^5.1" }, "suggest": { "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.", diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..e95cdd3 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,21 @@ + + + + + + + ./tests + + + + + + + NothingInstalled + + + diff --git a/src/Exception/NoCandidateFoundException.php b/src/Exception/NoCandidateFoundException.php index 240b568..a7d35b1 100644 --- a/src/Exception/NoCandidateFoundException.php +++ b/src/Exception/NoCandidateFoundException.php @@ -27,9 +27,22 @@ function ($a) { $message = sprintf( 'No valid candidate found using strategy "%s". We tested the following candidates: %s.', $strategy, - implode(', ', $classes) + implode(', ', array_map([$this, 'stringify'], $classes)) ); parent::__construct($message); } + + private function stringify($mixed) + { + if (is_string($mixed)) { + return $mixed; + } + + if (is_array($mixed) && count($mixed) === 2) { + return sprintf('%s::%s', $this->stringify($mixed[0]), $mixed[1]); + } + + return is_object($mixed) ? get_class($mixed) : gettype($mixed); + } } diff --git a/src/Strategy/CommonClassesStrategy.php b/src/Strategy/CommonClassesStrategy.php index c2b7e09..ab7e4c0 100644 --- a/src/Strategy/CommonClassesStrategy.php +++ b/src/Strategy/CommonClassesStrategy.php @@ -81,7 +81,12 @@ final class CommonClassesStrategy implements DiscoveryStrategy 'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class], ], ], - Psr18Client::class => [], + Psr18Client::class => [ + [ + 'class' => [self::class, 'buzzInstantiate'], + 'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class], + ], + ], ]; /** diff --git a/tests/Exception/InitializeExceptionTest.php b/tests/Exception/InitializeExceptionTest.php new file mode 100644 index 0000000..c93ca9f --- /dev/null +++ b/tests/Exception/InitializeExceptionTest.php @@ -0,0 +1,25 @@ +assertInstanceOf(\Http\Discovery\Exception::class, $exception); + } + } +} diff --git a/tests/Exception/NoCandidateFoundExceptionTest.php b/tests/Exception/NoCandidateFoundExceptionTest.php new file mode 100644 index 0000000..73d1a87 --- /dev/null +++ b/tests/Exception/NoCandidateFoundExceptionTest.php @@ -0,0 +1,69 @@ + 'Foo', 'condition' => true], + ['class' => 'Bar', 'condition' => false], + ]; + $exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates); + $this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); + $this->assertStringStartsWith('No valid candidate found using strategy "Foobar".', $exception->getMessage()); + } + + public function testInitializeWithArrayCallable() + { + $candidates = [ + ['class' => ['AnyClass', 'anyFunction'], 'condition' => true], + ]; + + $exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates); + $this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); + } + + public function testInitializeWithObjectCallable() + { + $obj = new \stdClass(); + $candidates = [ + ['class' => [$obj, 'anyFunction'], 'condition' => true], + ]; + + $exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates); + $this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); + } + + public function testInitializeWithClosure() + { + $obj = \Closure::fromCallable(function() { + $x = 2; + }); + $candidates = [ + ['class' => $obj, 'condition' => true], + ]; + + $exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates); + $this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); + } + + public function testInitializeWithAnonymousFunction() + { + $func = function() { + $x = 2; + }; + $candidates = [ + ['class' => $func, 'condition' => true], + ]; + + $exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates); + $this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); + } +} diff --git a/tests/Psr18ClientDiscoveryTest.php b/tests/Psr18ClientDiscoveryTest.php new file mode 100644 index 0000000..1f7ac96 --- /dev/null +++ b/tests/Psr18ClientDiscoveryTest.php @@ -0,0 +1,37 @@ +assertInstanceOf(ClientInterface::class, $client); + } + + /** + * @group NothingInstalled + */ + public function testNotFound() + { + $this->expectException(DiscoveryFailedException::class); + $client = Psr18ClientDiscovery::find(); + } +} From 82bc7398bbd32d66ef89c95c4af3cfbb4e9287e5 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 5 Jan 2019 13:40:18 +0100 Subject: [PATCH 2/9] cs --- src/Exception/NoCandidateFoundException.php | 2 +- tests/Exception/InitializeExceptionTest.php | 2 -- tests/Exception/NoCandidateFoundExceptionTest.php | 6 ++---- tests/Psr18ClientDiscoveryTest.php | 11 ----------- 4 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/Exception/NoCandidateFoundException.php b/src/Exception/NoCandidateFoundException.php index a7d35b1..e53fb79 100644 --- a/src/Exception/NoCandidateFoundException.php +++ b/src/Exception/NoCandidateFoundException.php @@ -39,7 +39,7 @@ private function stringify($mixed) return $mixed; } - if (is_array($mixed) && count($mixed) === 2) { + if (is_array($mixed) && 2 === count($mixed)) { return sprintf('%s::%s', $this->stringify($mixed[0]), $mixed[1]); } diff --git a/tests/Exception/InitializeExceptionTest.php b/tests/Exception/InitializeExceptionTest.php index c93ca9f..a9cdc56 100644 --- a/tests/Exception/InitializeExceptionTest.php +++ b/tests/Exception/InitializeExceptionTest.php @@ -3,8 +3,6 @@ namespace tests\Http\Discovery\Exception; use Http\Discovery\Exception as DiscoveryException; -use Http\Discovery\Strategy\CommonClassesStrategy; -use Http\Client\HttpClient; use PHPUnit\Framework\TestCase; class InitializeExceptionTest extends TestCase diff --git a/tests/Exception/NoCandidateFoundExceptionTest.php b/tests/Exception/NoCandidateFoundExceptionTest.php index 73d1a87..a6c2a6b 100644 --- a/tests/Exception/NoCandidateFoundExceptionTest.php +++ b/tests/Exception/NoCandidateFoundExceptionTest.php @@ -3,8 +3,6 @@ namespace tests\Http\Discovery\Exception; use Http\Discovery\Exception as DiscoveryException; -use Http\Discovery\Strategy\CommonClassesStrategy; -use Http\Client\HttpClient; use PHPUnit\Framework\TestCase; class NoCandidateFoundExceptionTest extends TestCase @@ -43,7 +41,7 @@ public function testInitializeWithObjectCallable() public function testInitializeWithClosure() { - $obj = \Closure::fromCallable(function() { + $obj = \Closure::fromCallable(function () { $x = 2; }); $candidates = [ @@ -56,7 +54,7 @@ public function testInitializeWithClosure() public function testInitializeWithAnonymousFunction() { - $func = function() { + $func = function () { $x = 2; }; $candidates = [ diff --git a/tests/Psr18ClientDiscoveryTest.php b/tests/Psr18ClientDiscoveryTest.php index 1f7ac96..39964f3 100644 --- a/tests/Psr18ClientDiscoveryTest.php +++ b/tests/Psr18ClientDiscoveryTest.php @@ -2,21 +2,10 @@ namespace tests\Http\Discovery; -use Http\Client\Common\Plugin; -use Http\Client\Common\Plugin\HeaderAppendPlugin; -use Http\Client\Common\Plugin\RedirectPlugin; -use Http\Client\Common\PluginClient; -use Http\Client\HttpAsyncClient; -use Http\Client\Promise\HttpFulfilledPromise; use Http\Discovery\Exception\DiscoveryFailedException; use Http\Discovery\Psr18ClientDiscovery; -use Http\Promise\Promise; -use Nyholm\Psr7\Request; -use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use Psr\Http\Client\ClientInterface; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; class Psr18ClientDiscoveryTest extends TestCase { From 784a0273e92eb0bdaf04cf84afdcac394408076c Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 5 Jan 2019 13:42:44 +0100 Subject: [PATCH 3/9] minor --- composer.json | 2 +- phpunit.xml.dist | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 96542c2..e6b6414 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "php-http/httplug": "^1.0 || ^2.0", "php-http/message-factory": "^1.0", "puli/composer-plugin": "1.0.0-beta10", - "phpspec/phpspec": "^5.1" + "phpspec/phpspec": "^2.4" }, "suggest": { "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.", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e95cdd3..72a6c47 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,7 +12,6 @@ - NothingInstalled From 478cb3de5b1ba98290fd108bd77c4c3892a0186c Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 5 Jan 2019 13:45:58 +0100 Subject: [PATCH 4/9] Added names on builds --- .travis.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8763cc2..90368f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,11 +28,14 @@ matrix: - php: 7.3 fast_finish: true include: - - php: 5.5 + - name: PHPSpec code coverage + php: 5.5 env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" PULI_VERSION=1.0.0-beta9 DEPENDENCIES="phpspec/phpspec:^2.4 henrikbjorn/phpspec-code-coverage:^2.0.2" - - php: 7.2 + - name: PHPUnit tests + php: 7.2 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" - - php: 7.2 + - name: PHPUnit test with nothing installed + php: 7.2 env: TEST_COMMAND="./vendor/bin/phpunit --group=NothingInstalled" DEPENDENCIES="phpunit/phpunit:^7.5" before_install: From 98a4089390f68892880157fc9b3164d77c467dc0 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 5 Jan 2019 13:52:01 +0100 Subject: [PATCH 5/9] Added test for PSR-17 --- .travis.yml | 2 +- src/Psr17FactoryDiscovery.php | 2 +- tests/Psr17FactoryDiscoveryTest.php | 49 +++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 tests/Psr17FactoryDiscoveryTest.php diff --git a/.travis.yml b/.travis.yml index 90368f6..d767065 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,7 +30,7 @@ matrix: include: - name: PHPSpec code coverage php: 5.5 - env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" PULI_VERSION=1.0.0-beta9 DEPENDENCIES="phpspec/phpspec:^2.4 henrikbjorn/phpspec-code-coverage:^2.0.2" + 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" - name: PHPUnit tests php: 7.2 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" diff --git a/src/Psr17FactoryDiscovery.php b/src/Psr17FactoryDiscovery.php index 1aa833a..7c70fb4 100644 --- a/src/Psr17FactoryDiscovery.php +++ b/src/Psr17FactoryDiscovery.php @@ -20,7 +20,7 @@ final class Psr17FactoryDiscovery extends ClassDiscovery private static function createException($type, Exception $e) { return new \Http\Discovery\Exception\NotFoundException( - 'No psr-17 '.$type.' found. Install a package from this list: https://packagist.org/providers/psr/http-factory-implementation', + 'No PSR-17 '.$type.' found. Install a package from this list: https://packagist.org/providers/psr/http-factory-implementation', 0, $e ); diff --git a/tests/Psr17FactoryDiscoveryTest.php b/tests/Psr17FactoryDiscoveryTest.php new file mode 100644 index 0000000..c3d6c39 --- /dev/null +++ b/tests/Psr17FactoryDiscoveryTest.php @@ -0,0 +1,49 @@ +assertInstanceOf($interface, $client); + } + + /** + * @group NothingInstalled + * @dataProvider getFactories + */ + public function testNotFound($method) + { + $callable = [Psr17FactoryDiscovery::class, $method]; + $this->expectException(DiscoveryFailedException::class); + $callable(); + } + + public function getFactories() + { + yield ['findRequestFactory', RequestFactoryInterface::class]; + yield ['findResponseFactory', ResponseFactoryInterface::class]; + yield ['findServerRequestFactory', ServerRequestFactoryInterface::class]; + yield ['findStreamFactory', StreamFactoryInterface::class]; + yield ['findUploadedFileFactory', UploadedFileFactoryInterface::class]; + yield ['findUrlFactory', UriFactoryInterface::class]; + } +} From 731f9d0dc3a829433922566fc08e6603174c8ab5 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 5 Jan 2019 13:53:32 +0100 Subject: [PATCH 6/9] Make sure we actually install stuff --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d767065..31842bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,13 +40,15 @@ matrix: before_install: - if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi + - if ! [ -z "$DEPENDENCIES" ]; then composer require --no-update ${DEPENDENCIES}; fi; install: - wget https://github.com/puli/cli/releases/download/1.0.0-beta10/puli.phar && chmod +x puli.phar - composer require puli/composer-plugin:${PULI_VERSION} --no-update - - travis_retry composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction + - composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction script: + - composer validate --strict --no-check-lock - $TEST_COMMAND after_success: From f52bd79db9d5de12a7dc60a20b970a196de3c444 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 5 Jan 2019 13:56:35 +0100 Subject: [PATCH 7/9] Dont validate comopser.json --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 31842bb..77c5ea3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,7 +48,6 @@ install: - composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction script: - - composer validate --strict --no-check-lock - $TEST_COMMAND after_success: From 112a673a03fd1043569c21a8b327373d54c2775a Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 5 Jan 2019 14:01:57 +0100 Subject: [PATCH 8/9] fixed tests --- tests/Psr17FactoryDiscoveryTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Psr17FactoryDiscoveryTest.php b/tests/Psr17FactoryDiscoveryTest.php index c3d6c39..a9d8797 100644 --- a/tests/Psr17FactoryDiscoveryTest.php +++ b/tests/Psr17FactoryDiscoveryTest.php @@ -2,11 +2,9 @@ namespace tests\Http\Discovery; -use Http\Discovery\Exception\DiscoveryFailedException; +use Http\Discovery\Exception\NotFoundException; use Http\Discovery\Psr17FactoryDiscovery; -use Http\Discovery\Psr18ClientDiscovery; use PHPUnit\Framework\TestCase; -use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ServerRequestFactoryInterface; @@ -33,7 +31,7 @@ public function testFind($method, $interface) public function testNotFound($method) { $callable = [Psr17FactoryDiscovery::class, $method]; - $this->expectException(DiscoveryFailedException::class); + $this->expectException(NotFoundException::class); $callable(); } From c49e712f543700fb1a1feae551b20005b4e72a56 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 5 Jan 2019 14:08:36 +0100 Subject: [PATCH 9/9] fixed test (again) --- tests/Psr18ClientDiscoveryTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Psr18ClientDiscoveryTest.php b/tests/Psr18ClientDiscoveryTest.php index 39964f3..30657b8 100644 --- a/tests/Psr18ClientDiscoveryTest.php +++ b/tests/Psr18ClientDiscoveryTest.php @@ -2,7 +2,7 @@ namespace tests\Http\Discovery; -use Http\Discovery\Exception\DiscoveryFailedException; +use Http\Discovery\Exception\NotFoundException; use Http\Discovery\Psr18ClientDiscovery; use PHPUnit\Framework\TestCase; use Psr\Http\Client\ClientInterface; @@ -20,7 +20,7 @@ public function testFind() */ public function testNotFound() { - $this->expectException(DiscoveryFailedException::class); + $this->expectException(NotFoundException::class); $client = Psr18ClientDiscovery::find(); } }