-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 59a67b4
Showing
9 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/test export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/.travis.yml export-ignore | ||
/phpunit.xml.dist export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
phpunit.xml | ||
/vendor/ | ||
composer.lock | ||
/build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
The MIT License (MIT) Copyright (c) 2016 Net Glue Ltd | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit | ||
persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the | ||
Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | ||
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Post/Redirect/Get Middleware for Zend Expressive | ||
|
||
WIP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "netglue/expressive-prg", | ||
"description": "Middleware to implement the Post/Redirect/Get pattern in Zend Expressive", | ||
"type": "library", | ||
"require": { | ||
"zendframework/zend-expressive": "^1.0", | ||
"zendframework/zend-session": "^2.7" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^5.5" | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "George Steel", | ||
"email": "george@net-glue.co.uk" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"NetglueExpressive\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"NetglueExpressiveTest\\": "test/NetglueExpressiveTest" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
bootstrap="./test/bootstrap.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
verbose="true" | ||
stopOnFailure="false" | ||
processIsolation="false" | ||
backupGlobals="false" | ||
syntaxCheck="true" | ||
> | ||
<testsuites> | ||
<testsuite name="PRG for Zend Expressive Test Suite"> | ||
<directory suffix="Test.php">./test/NetglueExpressiveTest</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
|
||
<logging> | ||
<!-- <log type="coverage-clover" target="build/logs/clover.xml"/> --> | ||
<!-- <log type="coverage-html" target="build/report" charset="UTF-8" highlight="true" /> --> | ||
</logging> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* This file is part of the Expressive PRG Package | ||
* Copyright 2016 Net Glue Ltd (https://netglue.uk). | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace NetglueExpressive\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Zend\Session\Container; | ||
use Zend\Diactoros\Response\RedirectResponse; | ||
|
||
class PostRedirectGet | ||
{ | ||
|
||
/** | ||
* Default Session Container Name | ||
*/ | ||
const SESSION_CONTAINER = 'netglue_prg'; | ||
|
||
/** | ||
* Request Attribute Key | ||
*/ | ||
const KEY = 'prg'; | ||
|
||
/** | ||
* Session Container | ||
* @var Container | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* Invoke Middleware | ||
* | ||
* @param Request $request | ||
* @param Response $response | ||
* @param callable $next | ||
* @return Response | ||
*/ | ||
public function __invoke(Request $request, Response $response, callable $next = null) | ||
{ | ||
$method = $request->getMethod(); | ||
$container = $this->getSessionContainer(); | ||
|
||
/** | ||
* If this is a POST request, store the data in the session | ||
* and return a redirect response with the original URI | ||
*/ | ||
if ($request->getMethod() === 'POST') { | ||
$container->setExpirationHops(1, 'post'); | ||
$container->post = $request->getParsedBody(); | ||
|
||
return new RedirectResponse($request->getUri(), 303); | ||
} | ||
|
||
/** | ||
* Modify the request to include an attribute set to either false | ||
* indicating that no post data is present, or set to the value of | ||
* the posted data | ||
*/ | ||
if ($request->getMethod() === 'GET') { | ||
$value = (null !== $container->post) | ||
? $container->post | ||
: false; | ||
unset($container->post); | ||
$request = $request->withAttribute(static::KEY, $value); | ||
} | ||
|
||
if ($next) { | ||
return $next($request, $response); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Return session container | ||
* @return Container | ||
*/ | ||
public function getSessionContainer() : Container | ||
{ | ||
if (!$this->container) { | ||
$this->container = new Container(static::SESSION_CONTAINER); | ||
} | ||
|
||
return $this->container; | ||
} | ||
|
||
/** | ||
* Set session container | ||
* @param Container $container | ||
* @return void | ||
*/ | ||
public function setSessionContainer(Container $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
test/NetglueExpressiveTest/Middleware/PostRedirectGetTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* This file is part of the Expressive PRG Package | ||
* Copyright 2016 Net Glue Ltd (https://netglue.uk). | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace NetglueExpressiveTest\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Zend\Session\Container; | ||
use NetglueExpressive\Middleware\PostRedirectGet as PRG; | ||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\ServerRequest; | ||
use Zend\Diactoros\Response\RedirectResponse; | ||
|
||
class PostRedirectGetTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testSetAndGetSessionContainer() | ||
{ | ||
$prg = new PRG; | ||
$this->assertInstanceOf(Container::class, $prg->getSessionContainer()); | ||
|
||
$container = new Container('foo'); | ||
$prg->setSessionContainer($container); | ||
$this->assertSame($container, $prg->getSessionContainer()); | ||
} | ||
|
||
public function testFirstGetRequestWillHaveFalseAttribute() | ||
{ | ||
$prg = new PRG; | ||
|
||
$req = new ServerRequest; | ||
$req = $req->withMethod('GET'); | ||
|
||
$res = $prg($req, new Response, function($req, $res) { | ||
$this->assertFalse($req->getAttribute(PRG::KEY)); | ||
}); | ||
} | ||
|
||
public function testPostRequestWillReturnRedirect() | ||
{ | ||
$prg = new PRG; | ||
|
||
$container = $prg->getSessionContainer(); | ||
$this->assertNull($container->post); | ||
|
||
$req = new ServerRequest; | ||
$req = $req->withMethod('POST'); | ||
$req = $req->withParsedBody([ | ||
'foo' => 'bar', | ||
]); | ||
|
||
$res = $prg($req, new Response, function($req, $res) { | ||
$this->fail('The next callable should not have been called. A redirect response should have been returned'); | ||
}); | ||
|
||
$this->assertInstanceOf(RedirectResponse::class, $res); | ||
} | ||
|
||
public function testGetRequestWithNonNullContainerWillHavePostAttribute() | ||
{ | ||
$prg = new PRG; | ||
|
||
$container = $prg->getSessionContainer(); | ||
$container->post = ['foo' => 'bar']; | ||
|
||
$req = new ServerRequest; | ||
|
||
$res = $prg($req, new Response, function($req, $res) { | ||
|
||
$attr = $req->getAttribute(PRG::KEY); | ||
$this->assertInternalType('array', $attr); | ||
$this->assertSame('bar', $attr['foo']); | ||
|
||
}); | ||
|
||
} | ||
|
||
public function testUnmodifiedResponseForGetRequests() | ||
{ | ||
$prg = new PRG; | ||
|
||
$req = new ServerRequest; | ||
$res = new Response; | ||
|
||
$return = $prg($req, $res); | ||
|
||
$this->assertSame($res, $return); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; |