From d72487399c15177a3c17e10172e2ec2e5ff930d2 Mon Sep 17 00:00:00 2001 From: Eric Tucker Date: Wed, 27 Jun 2018 21:45:20 -0700 Subject: [PATCH 1/4] add tests --- .gitignore | 1 + composer.json | 3 ++ phpunit.xml | 28 ++++++++++ src/XmlRequestMiddleware.php | 2 +- src/XmlRequestServiceProvider.php | 6 +-- tests/XmlRequestMacrosTest.php | 90 +++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/XmlRequestMacrosTest.php diff --git a/.gitignore b/.gitignore index 57872d0..0dca145 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +.idea diff --git a/composer.json b/composer.json index b8b0eea..bd50aa5 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,9 @@ "illuminate/http": "~5.0", "illuminate/support": "~5.0" }, + "require-dev": { + "phpunit/phpunit": "^7.2" + }, "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()); + } + } +} From 0d99798d5864fdba9f2ce48bcae17ebbf6f06145 Mon Sep 17 00:00:00 2001 From: Eric Tucker Date: Wed, 27 Jun 2018 21:49:14 -0700 Subject: [PATCH 2/4] Add travis-ci config --- .travis.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .travis.yml 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 From 06666c134c64ff370da69fa37a5f7bef6d583d38 Mon Sep 17 00:00:00 2001 From: Eric Tucker Date: Wed, 27 Jun 2018 21:53:34 -0700 Subject: [PATCH 3/4] Rollback phpunit to support php 5.6+ --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index bd50aa5..45db3b5 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "illuminate/support": "~5.0" }, "require-dev": { - "phpunit/phpunit": "^7.2" + "phpunit/phpunit": "~5.4.0" }, "license": "MIT", "authors": [ From eaaa9eb31b9c5f5ab9f585b16382bc659d2bdef6 Mon Sep 17 00:00:00 2001 From: Eric Tucker Date: Wed, 27 Jun 2018 22:06:18 -0700 Subject: [PATCH 4/4] Add badges to README --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) 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