diff --git a/.gitignore b/.gitignore index 0602d69..273fe7b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ phpunit.xml vendor bin/ .php_cs.cache +.phpunit.* diff --git a/README.md b/README.md index cbdacbe..94e26ab 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,16 @@ Swap allows you to retrieve currency exchange rates from various services such a $ composer require php-http/curl-client nyholm/psr7 php-http/message florianv/laravel-swap ``` +### Laravel 5.7 or lesser + +If you use cache, add also PSR-6 adapter and PSR-16 bridge cache dependencies : + +```bash +$ composer require cache/illuminate-adapter cache/simple-cache-bridge +``` + +These dependencies are not required with Laravel 5.8 or greater which [implements PSR-16](https://github.com/laravel/framework/pull/27217). + ### Laravel 5.5+ If you don't use auto-discovery, add the `ServiceProvider` to the providers array in `config/app.php`: diff --git a/composer.json b/composer.json index 0d481a5..f6a638c 100644 --- a/composer.json +++ b/composer.json @@ -32,12 +32,10 @@ }, "require": { "php": "^7.1.3", - "florianv/swap": "^4.0", - "cache/illuminate-adapter": "^0.2.0", - "cache/simple-cache-bridge": "^1.0" + "florianv/swap": "^4.0" }, "require-dev": { - "graham-campbell/testbench": "~5.2", + "graham-campbell/testbench": "^5.3", "php-http/guzzle6-adapter": "^1.0", "nyholm/psr7": "^1.0" }, diff --git a/doc/readme.md b/doc/readme.md index 6a8cf9a..838f754 100644 --- a/doc/readme.md +++ b/doc/readme.md @@ -56,6 +56,16 @@ Copy the package config to your local config with the publish command: $ php artisan vendor:publish --provider="Swap\Laravel\SwapServiceProvider" ``` +__Laravel 5.7 or lesser :__ + +If you use cache, add also PSR-6 adapter and PSR-16 bridge cache dependencies : + +```bash +$ composer require cache/illuminate-adapter cache/simple-cache-bridge +``` + +These dependencies are not required with Laravel 5.8 or greater which [implements PSR-16](https://github.com/laravel/framework/pull/27217). + ### Lumen Configure the Service Provider and alias: diff --git a/src/SwapServiceProvider.php b/src/SwapServiceProvider.php index 6bab127..9b47016 100644 --- a/src/SwapServiceProvider.php +++ b/src/SwapServiceProvider.php @@ -99,9 +99,26 @@ private function getConfigPath($path = '') private function getSimpleCache() { if ($cache = $this->app->config->get('swap.cache')) { - $store = $this->app['cache']->store($cache)->getStore(); + $store = $this->app['cache']->store($cache); - return new SimpleCacheBridge(new IlluminateCachePool($store)); + // Check simple cache PSR-16 compatibility + if ($store instanceof \Psr\SimpleCache\CacheInterface) { + return $store; + } + + // Ensure PSR-6 adapter class exists + if (! class_exists(IlluminateCachePool::class)) { + throw new \Exception("cache/illuminate-adapter dependency is missing"); + } + + // Ensure PSR-16 bridge class exists + if (! class_exists(SimpleCacheBridge::class)) { + throw new \Exception("cache/simple-cache-bridge dependency is missing"); + } + + return new SimpleCacheBridge( + new IlluminateCachePool($store->getStore()) + ); } return null; diff --git a/tests/SwapServiceProviderTest.php b/tests/SwapServiceProviderTest.php index abfe5b8..8c06915 100644 --- a/tests/SwapServiceProviderTest.php +++ b/tests/SwapServiceProviderTest.php @@ -21,12 +21,11 @@ class SwapServiceProviderTest extends AbstractTestCase { use ServiceProviderTrait; - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage The "access_key" option must be provided. - */ public function testMissingServiceConfig() { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The "access_key" option must be provided.'); + $this->app->config->set('swap.services', [ 'currency_layer' => true, ]); @@ -34,12 +33,11 @@ public function testMissingServiceConfig() $this->app['swap']; } - /** - * @expectedException \LogicException - * @expectedExceptionMessage The service "unknown" is not registered. - */ public function testUnknownService() { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('The service "unknown" is not registered.'); + $this->app->config->set('swap.services', [ 'unknown' => true, ]); @@ -49,7 +47,7 @@ public function testUnknownService() public function testEmptyServices() { - $this->app['swap']; + Assert::assertInstanceOf(Swap::class, $this->app['swap']); } public function testSwapIsInjectable()