Skip to content

Commit

Permalink
Added integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Scholten committed Jul 16, 2016
1 parent b34229e commit 0aa2b16
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions tests/Integration/Psr7IntegrationTest.php
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();
}
}

0 comments on commit 0aa2b16

Please sign in to comment.