Skip to content

Commit 3b1a5c3

Browse files
authored
Add Tests (#7)
* add tests * Add travis-ci config * Rollback phpunit to support php 5.6+ * Add badges to README
1 parent 271016d commit 3b1a5c3

File tree

8 files changed

+153
-4
lines changed

8 files changed

+153
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor/
2+
.idea

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.0
6+
- 7.1
7+
- 7.2
8+
9+
env:
10+
global:
11+
- setup=basic
12+
13+
sudo: false
14+
15+
before_install:
16+
- travis_retry composer self-update
17+
18+
install:
19+
- composer install --no-interaction --prefer-dist
20+
21+
script: vendor/bin/phpunit

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# laravel-xml-middleware
2+
3+
[![Latest Stable Version](https://poser.pugx.org/tucker-eric/laravel-xml-middleware/v/stable)](https://packagist.org/packages/tucker-eric/laravel-xml-middleware)
4+
[![Total Downloads](https://poser.pugx.org/tucker-eric/laravel-xml-middleware/downloads)](https://packagist.org/packages/tucker-eric/laravel-xml-middleware)
5+
[![License](https://poser.pugx.org/tucker-eric/laravel-xml-middleware/license)](https://packagist.org/packages/tucker-eric/laravel-xml-middleware)
6+
[![Build Status](https://travis-ci.org/Tucker-Eric/laravel-xml-middleware.svg?branch=master)](https://travis-ci.org/Tucker-Eric/laravel-xml-middleware)
7+
28
A Laravel Middleware to accept XML requests
39

410
## Configuration

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"illuminate/http": "~5.0",
1414
"illuminate/support": "~5.0"
1515
},
16+
"require-dev": {
17+
"phpunit/phpunit": "~5.4.0"
18+
},
1619
"license": "MIT",
1720
"authors": [
1821
{

phpunit.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">src/</directory>
19+
</whitelist>
20+
</filter>
21+
<php>
22+
<env name="APP_ENV" value="testing"/>
23+
<env name="CACHE_DRIVER" value="array"/>
24+
<env name="SESSION_DRIVER" value="array"/>
25+
<env name="QUEUE_DRIVER" value="sync"/>
26+
<env name="DB_CONNECTION" value="sqlite"/>
27+
</php>
28+
</phpunit>

src/XmlRequestMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class XmlRequestMiddleware
1515
*/
1616
public function handle($request, Closure $next)
1717
{
18-
$request->merge($request->xml());
18+
$request->merge($request->xml(true));
1919

2020
return $next($request);
2121
}

src/XmlRequestServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public function register()
2020
});
2121

2222
Request::macro('xml', function ($assoc = true) {
23-
if (!$this->isXml()) {
24-
return [];
23+
if (!$this->isXml() || !$content = $this->getContent()) {
24+
return $assoc ? [] : new \stdClass;
2525
}
2626
// Returns the xml input from a request
27-
$xml = simplexml_load_string($this->getContent(), null, LIBXML_NOCDATA);
27+
$xml = simplexml_load_string($content, null, LIBXML_NOCDATA);
2828
$json = json_encode($xml);
2929

3030
return json_decode($json, $assoc);

tests/XmlRequestMacrosTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
use Illuminate\Http\Request;
4+
use PHPUnit\Framework\TestCase;
5+
use XmlMiddleware\XmlRequestMiddleware;
6+
use XmlMiddleware\XmlRequestServiceProvider;
7+
8+
class XmlRequestMacrosTest extends TestCase
9+
{
10+
public function setup()
11+
{
12+
if (!Request::hasMacro('xml') || !Request::hasMacro('isXml')) {
13+
(new XmlRequestServiceProvider(null))->register();
14+
}
15+
}
16+
17+
protected function createRequest($headers = [], $content = null)
18+
{
19+
return new Request([], [], [], [], [], $headers, $content);
20+
}
21+
22+
public function contentTypeDataProvider()
23+
{
24+
return [
25+
['application/xml', true, '<xml><person>human</person></xml>', ['person' => 'human']],
26+
['text/xml', true, '', []],
27+
['application/json', false, '{test: true}', []],
28+
['application/x-www-form-urlencoded', false, '', []]
29+
];
30+
}
31+
32+
/**
33+
* @dataProvider contentTypeDataProvider
34+
*
35+
* @param string $contentType
36+
* @param bool $assertion
37+
*/
38+
public function testIsXmlMethod($contentType, $assertion)
39+
{
40+
$this->assertEquals($assertion, $this->createRequest(['CONTENT_TYPE' => $contentType])->isXml());
41+
}
42+
43+
/**
44+
* @dataProvider contentTypeDataProvider
45+
*
46+
* @param $contentType
47+
* @param $typeAssertion
48+
* @param $content
49+
* @param string $expectedContent
50+
*/
51+
public function testXmlMethod($contentType, $typeAssertion, $content, $expectedContent = '')
52+
{
53+
$request = $this->createRequest(['CONTENT_TYPE' => $contentType], $content);
54+
if ($typeAssertion) {
55+
$this->assertEquals($expectedContent, $request->xml());
56+
$this->assertEquals((object)$expectedContent, $request->xml(false));
57+
} else {
58+
$this->assertEquals([], $request->xml());
59+
$this->assertEquals(new \stdClass, $request->xml(false));
60+
}
61+
}
62+
63+
/**
64+
* @dataProvider contentTypeDataProvider
65+
*
66+
* @param $contentType
67+
* @param $isXml
68+
* @param $content
69+
* @param string $expectedContent
70+
*/
71+
public function testXmlMiddleware($contentType, $isXml, $content, $expectedContent = '')
72+
{
73+
$request = $this->createRequest(['CONTENT_TYPE' => $contentType], $content);
74+
// Make sure we have an empty array before middleware
75+
$this->assertEquals([], $request->all());
76+
77+
// Apply the middleware
78+
(new XmlRequestMiddleware)->handle($request, function ($request) {
79+
return $request;
80+
});
81+
82+
// If this is xml we want to make sure the content is there
83+
// If not then it's gonna be an empty array
84+
if ($isXml) {
85+
$this->assertEquals($expectedContent, $request->all());
86+
} else {
87+
$this->assertEquals([], $request->all());
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)