diff --git a/README.md b/README.md index 4305830..6e895a0 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/neomerx/cors-illuminate/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/neomerx/cors-illuminate/?branch=master) +[![Code Coverage](https://scrutinizer-ci.com/g/neomerx/cors-illuminate/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/neomerx/cors-illuminate/?branch=master) +[![Build Status](https://travis-ci.org/neomerx/cors-illuminate.svg?branch=master)](https://travis-ci.org/neomerx/cors-illuminate) +[![HHVM](https://img.shields.io/hhvm/neomerx/cors-illuminate.svg)](https://travis-ci.org/neomerx/cors-illuminate) +[![License](https://img.shields.io/packagist/l/neomerx/cors-illuminate.svg)](https://packagist.org/packages/neomerx/cors-illuminate) + ## Description This package adds [Cross-Origin Resource Sharing](http://www.w3.org/TR/cors/) (CORS) support to your Laravel application. diff --git a/src/Adapters/IlluminateRequestToPsr7.php b/src/Adapters/IlluminateRequestToPsr7.php index 3108a9d..41ab891 100644 --- a/src/Adapters/IlluminateRequestToPsr7.php +++ b/src/Adapters/IlluminateRequestToPsr7.php @@ -77,7 +77,7 @@ public function getHeader($name) */ public function getProtocolVersion() { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -85,7 +85,7 @@ public function getProtocolVersion() */ public function withProtocolVersion($version) { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -93,7 +93,7 @@ public function withProtocolVersion($version) */ public function getHeaders() { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -101,7 +101,7 @@ public function getHeaders() */ public function getHeaderLine($name) { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -109,7 +109,7 @@ public function getHeaderLine($name) */ public function withHeader($name, $value) { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -117,7 +117,7 @@ public function withHeader($name, $value) */ public function withAddedHeader($name, $value) { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -125,7 +125,7 @@ public function withAddedHeader($name, $value) */ public function withoutHeader($name) { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -133,7 +133,7 @@ public function withoutHeader($name) */ public function getBody() { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -141,7 +141,7 @@ public function getBody() */ public function withBody(StreamInterface $body) { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -149,7 +149,7 @@ public function withBody(StreamInterface $body) */ public function getRequestTarget() { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -157,7 +157,7 @@ public function getRequestTarget() */ public function withRequestTarget($requestTarget) { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -165,7 +165,7 @@ public function withRequestTarget($requestTarget) */ public function withMethod($method) { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** @@ -173,21 +173,13 @@ public function withMethod($method) */ public function getUri() { - $this->notImplemented(); + throw new LogicException('Method is not implemented'); } /** * @inheritdoc */ public function withUri(UriInterface $uri, $preserveHost = false) - { - $this->notImplemented(); - } - - /** - * Throws exception. This method should never be called. - */ - private function notImplemented() { throw new LogicException('Method is not implemented'); } diff --git a/src/Providers/LaravelServiceProvider.php b/src/Providers/LaravelServiceProvider.php index 27e67dc..6788550 100644 --- a/src/Providers/LaravelServiceProvider.php +++ b/src/Providers/LaravelServiceProvider.php @@ -17,6 +17,7 @@ */ use \Illuminate\Support\ServiceProvider; +use \Illuminate\Contracts\Config\Repository; use \Neomerx\CorsIlluminate\Settings\Settings; /** @@ -48,13 +49,15 @@ public function register() public function boot() { // publish config - $publishPath = $this->app->make('path.config') . DIRECTORY_SEPARATOR . static::CONFIG_FILE_NAME_WO_EXT . '.php'; + $publishPath = $this->app['path.config'] . DIRECTORY_SEPARATOR . static::CONFIG_FILE_NAME_WO_EXT . '.php'; $this->publishes([ $this->getConfigPath() => $publishPath, ]); // load settings - $settings = $this->app->make('config')->get(static::CONFIG_FILE_NAME_WO_EXT); + /** @var Repository $config */ + $config = $this->app['config']; + $settings = $config->get(static::CONFIG_FILE_NAME_WO_EXT); Settings::setSettings($settings); } diff --git a/tests/Adapters/IlluminateRequestToPsr7Test.php b/tests/Adapters/IlluminateRequestToPsr7Test.php index f5c272a..3fcfb4e 100644 --- a/tests/Adapters/IlluminateRequestToPsr7Test.php +++ b/tests/Adapters/IlluminateRequestToPsr7Test.php @@ -16,13 +16,18 @@ * limitations under the License. */ +use \Mockery; use \Illuminate\Http\Request; +use \Psr\Http\Message\UriInterface; +use \Psr\Http\Message\StreamInterface; use \Psr\Http\Message\RequestInterface; use \Neomerx\Tests\CorsIlluminate\BaseTestCase; use \Neomerx\CorsIlluminate\Adapters\IlluminateRequestToPsr7; /** * @package Neomerx\Tests\CorsIlluminate + * + * @SuppressWarnings(PHPMD.TooManyMethods) */ class IlluminateRequestToPsr7Test extends BaseTestCase { @@ -84,4 +89,120 @@ public function testGetHeader() $this->request->headers->set($name, ['value 2'], false); $this->assertEquals(['value 1', 'value 2'], $this->adapter->getHeader($name)); } + + /** + * @expectedException \LogicException + */ + public function testGetProtocolVersion() + { + $this->adapter->getProtocolVersion(); + } + + /** + * @expectedException \LogicException + */ + public function testWithProtocolVersion() + { + $this->adapter->withProtocolVersion(null); + } + + /** + * @expectedException \LogicException + */ + public function testGetHeaders() + { + $this->adapter->getHeaders(); + } + + /** + * @expectedException \LogicException + */ + public function testGetHeaderLine() + { + $this->adapter->getHeaderLine(null); + } + + /** + * @expectedException \LogicException + */ + public function testWithHeader() + { + $this->adapter->withHeader(null, null); + } + + /** + * @expectedException \LogicException + */ + public function testWithAddedHeader() + { + $this->adapter->withAddedHeader(null, null); + } + + /** + * @expectedException \LogicException + */ + public function testWithoutHeader() + { + $this->adapter->withoutHeader(null); + } + + /** + * @expectedException \LogicException + */ + public function testGetBody() + { + $this->adapter->getBody(); + } + + /** + * @expectedException \LogicException + */ + public function testWithBody() + { + /** @var StreamInterface $body */ + $body = Mockery::mock(StreamInterface::class); + $this->adapter->withBody($body); + } + + /** + * @expectedException \LogicException + */ + public function testGetRequestTarget() + { + $this->adapter->getRequestTarget(); + } + + /** + * @expectedException \LogicException + */ + public function testWithRequestTarget() + { + $this->adapter->withRequestTarget(null); + } + + /** + * @expectedException \LogicException + */ + public function testWithMethod() + { + $this->adapter->withMethod(null); + } + + /** + * @expectedException \LogicException + */ + public function testGetUri() + { + $this->adapter->getUri(); + } + + /** + * @expectedException \LogicException + */ + public function testWithUri() + { + /** @var UriInterface $uri */ + $uri = Mockery::mock(UriInterface::class); + $this->adapter->withUri($uri); + } } diff --git a/tests/CorsMiddlewareTest.php b/tests/CorsMiddlewareTest.php index d5e506a..14f03ec 100644 --- a/tests/CorsMiddlewareTest.php +++ b/tests/CorsMiddlewareTest.php @@ -23,6 +23,7 @@ use \Illuminate\Http\Response; use \Neomerx\CorsIlluminate\CorsMiddleware; use \Neomerx\Cors\Contracts\AnalyzerInterface; +use \Neomerx\CorsIlluminate\Settings\Settings; use \Neomerx\Cors\Contracts\AnalysisResultInterface; /** @@ -57,6 +58,32 @@ protected function setUp() $this->analysisResult = Mockery::mock(AnalysisResultInterface::class); } + /** + * Test can be created without input args. + */ + public function testCreate() + { + $middleware = new CorsMiddleware(); + + $oldOrigin = Settings::$serverOrigin; + $oldAllowed = Settings::$allowedOrigins; + try { + Settings::$serverOrigin = [ + 'scheme' => 'http', + 'host' => 'localhost', + 'port' => 8080, + ]; + Settings::$allowedOrigins = []; + + $middleware->handle(new Request(), function () { + return null; + }); + } finally { + Settings::$serverOrigin = $oldOrigin; + Settings::$allowedOrigins = $oldAllowed; + } + } + /** * Test middleware handling. */ diff --git a/tests/Providers/LaravelServiceProviderTest.php b/tests/Providers/LaravelServiceProviderTest.php index 089b351..d165a5e 100644 --- a/tests/Providers/LaravelServiceProviderTest.php +++ b/tests/Providers/LaravelServiceProviderTest.php @@ -16,7 +16,11 @@ * limitations under the License. */ +use \Mockery; +use \Mockery\MockInterface; use \Neomerx\Tests\CorsIlluminate\BaseTestCase; +use \Neomerx\CorsIlluminate\Providers\LaravelServiceProvider; +use \Illuminate\Contracts\Foundation\Application as ApplicationInterface; /** * @package Neomerx\Tests\CorsIlluminate @@ -24,13 +28,57 @@ class LaravelServiceProviderTest extends BaseTestCase { /** - * Test provider. + * @var ApplicationInterface */ - public function testProvider() + private $app; + + /** + * @var MockInterface + */ + private $config; + + /** + * @var LaravelServiceProvider + */ + private $provider; + + /** + * @inheritDoc + */ + protected function setUp() { - // nothing to test here. - // LaravelServiceProvider is a basic Laravel specific integration code - // which IMO can't be tested unless very heavily mocked. - // It just doesn't make any sense. + parent::setUp(); + + $this->config = Mockery::mock(); + $this->app = [ + 'path.config' => '/some/config/path', + 'config' => $this->config, + ]; + + $this->provider = new LaravelServiceProvider($this->app); + } + + /** + * Test register provider. + */ + public function testRegister() + { + /** @noinspection PhpMethodParametersCountMismatchInspection */ + $this->config->shouldReceive('get')->withAnyArgs()->once()->andReturn([]); + /** @noinspection PhpMethodParametersCountMismatchInspection */ + $this->config->shouldReceive('set')->withAnyArgs()->once()->andReturnUndefined(); + + $this->provider->register(); + } + + /** + * Test boot provider. + */ + public function testBoot() + { + /** @noinspection PhpMethodParametersCountMismatchInspection */ + $this->config->shouldReceive('get')->withAnyArgs()->once()->andReturn([]); + + $this->provider->boot(); } }