diff --git a/.gitignore b/.gitignore index 57872d0..0dca145 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +.idea diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..224d49e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,21 @@ +language: php + +php: + - 5.6 + - 7.0 + - 7.1 + - 7.2 + +env: + global: + - setup=basic + +sudo: false + +before_install: + - travis_retry composer self-update + +install: + - composer install --no-interaction --prefer-dist + +script: vendor/bin/phpunit diff --git a/README.md b/README.md index d1756e3..90bf3f1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,10 @@ # laravel-xml-middleware + +[![Latest Stable Version](https://poser.pugx.org/tucker-eric/laravel-xml-middleware/v/stable)](https://packagist.org/packages/tucker-eric/laravel-xml-middleware) +[![Total Downloads](https://poser.pugx.org/tucker-eric/laravel-xml-middleware/downloads)](https://packagist.org/packages/tucker-eric/laravel-xml-middleware) +[![License](https://poser.pugx.org/tucker-eric/laravel-xml-middleware/license)](https://packagist.org/packages/tucker-eric/laravel-xml-middleware) +[![Build Status](https://travis-ci.org/Tucker-Eric/laravel-xml-middleware.svg?branch=master)](https://travis-ci.org/Tucker-Eric/laravel-xml-middleware) + A Laravel Middleware to accept XML requests ## Configuration diff --git a/composer.json b/composer.json index b8b0eea..45db3b5 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,9 @@ "illuminate/http": "~5.0", "illuminate/support": "~5.0" }, + "require-dev": { + "phpunit/phpunit": "~5.4.0" + }, "license": "MIT", "authors": [ { diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..2286247 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,28 @@ + + + + + ./tests/ + + + + + src/ + + + + + + + + + + diff --git a/src/XmlRequestMiddleware.php b/src/XmlRequestMiddleware.php index 7e1dfba..46f3be6 100644 --- a/src/XmlRequestMiddleware.php +++ b/src/XmlRequestMiddleware.php @@ -15,7 +15,7 @@ class XmlRequestMiddleware */ public function handle($request, Closure $next) { - $request->merge($request->xml()); + $request->merge($request->xml(true)); return $next($request); } diff --git a/src/XmlRequestServiceProvider.php b/src/XmlRequestServiceProvider.php index cfda810..7d82641 100644 --- a/src/XmlRequestServiceProvider.php +++ b/src/XmlRequestServiceProvider.php @@ -20,11 +20,11 @@ public function register() }); Request::macro('xml', function ($assoc = true) { - if (!$this->isXml()) { - return []; + if (!$this->isXml() || !$content = $this->getContent()) { + return $assoc ? [] : new \stdClass; } // Returns the xml input from a request - $xml = simplexml_load_string($this->getContent(), null, LIBXML_NOCDATA); + $xml = simplexml_load_string($content, null, LIBXML_NOCDATA); $json = json_encode($xml); return json_decode($json, $assoc); diff --git a/tests/XmlRequestMacrosTest.php b/tests/XmlRequestMacrosTest.php new file mode 100644 index 0000000..d089f53 --- /dev/null +++ b/tests/XmlRequestMacrosTest.php @@ -0,0 +1,90 @@ +register(); + } + } + + protected function createRequest($headers = [], $content = null) + { + return new Request([], [], [], [], [], $headers, $content); + } + + public function contentTypeDataProvider() + { + return [ + ['application/xml', true, 'human', ['person' => 'human']], + ['text/xml', true, '', []], + ['application/json', false, '{test: true}', []], + ['application/x-www-form-urlencoded', false, '', []] + ]; + } + + /** + * @dataProvider contentTypeDataProvider + * + * @param string $contentType + * @param bool $assertion + */ + public function testIsXmlMethod($contentType, $assertion) + { + $this->assertEquals($assertion, $this->createRequest(['CONTENT_TYPE' => $contentType])->isXml()); + } + + /** + * @dataProvider contentTypeDataProvider + * + * @param $contentType + * @param $typeAssertion + * @param $content + * @param string $expectedContent + */ + public function testXmlMethod($contentType, $typeAssertion, $content, $expectedContent = '') + { + $request = $this->createRequest(['CONTENT_TYPE' => $contentType], $content); + if ($typeAssertion) { + $this->assertEquals($expectedContent, $request->xml()); + $this->assertEquals((object)$expectedContent, $request->xml(false)); + } else { + $this->assertEquals([], $request->xml()); + $this->assertEquals(new \stdClass, $request->xml(false)); + } + } + + /** + * @dataProvider contentTypeDataProvider + * + * @param $contentType + * @param $isXml + * @param $content + * @param string $expectedContent + */ + public function testXmlMiddleware($contentType, $isXml, $content, $expectedContent = '') + { + $request = $this->createRequest(['CONTENT_TYPE' => $contentType], $content); + // Make sure we have an empty array before middleware + $this->assertEquals([], $request->all()); + + // Apply the middleware + (new XmlRequestMiddleware)->handle($request, function ($request) { + return $request; + }); + + // If this is xml we want to make sure the content is there + // If not then it's gonna be an empty array + if ($isXml) { + $this->assertEquals($expectedContent, $request->all()); + } else { + $this->assertEquals([], $request->all()); + } + } +}