-
Notifications
You must be signed in to change notification settings - Fork 3
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
Marc Scholten
committed
Jul 16, 2016
1 parent
b34229e
commit 0aa2b16
Showing
1 changed file
with
90 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,90 @@ | ||
<?php | ||
|
||
class Psr7IntegrationTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function controllersProvider() | ||
{ | ||
$parameters = [ | ||
'string' => 'Test', | ||
'int' => 1 | ||
]; | ||
|
||
$request = (new Zend\Diactoros\ServerRequest()); | ||
|
||
$getRequest = $request | ||
->withMethod('GET') | ||
->withQueryParams($parameters); | ||
|
||
$postRequest = $request | ||
->withMethod('POST') | ||
->withParsedBody($parameters); | ||
|
||
return [ | ||
[new Psr7Controller($getRequest)], | ||
[new Psr7Controller($postRequest)] | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider controllersProvider | ||
*/ | ||
public function testStringAction(Psr7Controller $controller) | ||
{ | ||
$this->assertEquals('Test', $controller->testString()); | ||
} | ||
|
||
/** | ||
* @dataProvider controllersProvider | ||
*/ | ||
public function testIntAction(Psr7Controller $controller) | ||
{ | ||
$this->assertEquals(1, $controller->testInt()); | ||
} | ||
|
||
/** | ||
* @dataProvider controllersProvider | ||
*/ | ||
public function testNotFoundActionThrowsException(Psr7Controller $controller) | ||
{ | ||
$this->setExpectedException(\MPScholten\RequestParser\NotFoundException::class, "Parameter notFound not found"); | ||
$controller->testNotFound(); | ||
} | ||
} | ||
|
||
class Psr7Controller | ||
{ | ||
use \MPScholten\RequestParser\Psr7\ControllerHelperTrait; | ||
|
||
private $request; | ||
|
||
public function __construct(Psr\Http\Message\ServerRequestInterface $request) | ||
{ | ||
$this->initRequestParser($request); | ||
$this->request = $request; | ||
} | ||
|
||
protected function parameter($name) | ||
{ | ||
if ($this->request->getMethod() === 'GET') { | ||
return $this->queryParameter($name); | ||
} else { | ||
return $this->bodyParameter($name); | ||
} | ||
} | ||
|
||
public function testString() | ||
{ | ||
return $this->parameter('string')->string()->required(); | ||
} | ||
|
||
public function testInt() | ||
{ | ||
return $this->parameter('int')->int()->required(); | ||
} | ||
|
||
public function testNotFound() | ||
{ | ||
return $this->parameter('notFound')->string()->required(); | ||
} | ||
} | ||
|