Skip to content
Snippets Groups Projects
Commit 50fc5e87 authored by Thomas Tanghus's avatar Thomas Tanghus
Browse files

Check if accessor matched request method.

It's easier to find errors in the code if an exception is thrown.
parent 480aeb80
No related branches found
No related tags found
No related merge requests found
......@@ -152,6 +152,9 @@ class Request implements \ArrayAccess, \Countable, IRequest {
switch($name) {
case 'get':
case 'post':
if($this->method !== strtoupper($name)) {
throw new \BadMethodCallException(sprintf('%s cannot be accessed in a %s request.', $name, $this->method));
}
case 'files':
case 'server':
case 'env':
......
......@@ -14,6 +14,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase {
public function testRequestAccessors() {
$vars = array(
'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
'method' => 'GET',
);
$request = new Request($vars);
......@@ -73,4 +74,30 @@ class RequestTest extends \PHPUnit_Framework_TestCase {
$request->{'nickname'} = 'Janey';
}
/**
* @expectedException BadMethodCallException
*/
public function testGetTheMethodRight() {
$vars = array(
'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
'method' => 'GET',
);
$request = new Request($vars);
$result = $request->post;
}
public function testTheMethodIsRight() {
$vars = array(
'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
'method' => 'GET',
);
$request = new Request($vars);
$this->assertEquals('GET', $request->method);
$result = $request->get;
$this->assertEquals('John Q. Public', $result['name']);
$this->assertEquals('Joey', $result['nickname']);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment