Check if accessor matched request method.

It's easier to find errors in the code if an exception is thrown.
This commit is contained in:
Thomas Tanghus 2013-09-27 14:36:19 +02:00
parent 480aeb804f
commit 50fc5e8792
2 changed files with 30 additions and 0 deletions

View File

@ -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':

View File

@ -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']);
}
}