Skip to content

Commit

Permalink
fix isset to avoind null vs. missing value
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Hejna authored and Petr Hejna committed Mar 29, 2018
1 parent 6f32709 commit 81334dd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getOptionalField($name, $default = null)
*/
public function hasField($name)
{
return isset($this->getDecodedJsonFromBody()->$name);
return array_key_exists($name, (array)$this->getDecodedJsonFromBody());
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/Request/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,31 @@
use Mockery;
use Mockery\MockInterface;
use PHPUnit_Framework_TestCase;
use Psr\Http\Message\StreamInterface;
use Slim\Http\Body;
use Slim\Http\Headers;
use Slim\Http\Request as SlimRequest;
use Slim\Http\Uri;

final class RequestTest extends PHPUnit_Framework_TestCase
{

const PARAM_NAME = 'dateFrom';
const DATE_TIME_STRING = '2017-06-10T01:00:00+01:00';

public function testShouldDistinguishBetweenNullAndEmptyOption()
{
$body = new Body(fopen('php://temp', 'rb+'));
$body->write('{"thisIsNull": null, "thisIsGandalf": "gandalf"}');
$body->rewind();

$request = $this->createRequest($body);

$this->assertTrue($request->hasField('thisIsNull'));
$this->assertFalse($request->hasField('nonExistingField'));
$this->assertTrue($request->hasField('thisIsGandalf'));
}

public function testGettingDateTimeQueryParam()
{
$arguments = [self::PARAM_NAME => self::DATE_TIME_STRING];
Expand Down Expand Up @@ -77,4 +94,16 @@ private function createMockSlimRequest(array $arguments)
return $mock;
}

/**
* @param StreamInterface $body
* @return Request
*/
private function createRequest(StreamInterface $body)
{
$url = new Uri('https', 'example.com');
$slimRequest = new SlimRequest('POST', $url, new Headers(), [], [], $body);

return new Request($slimRequest);
}

}

0 comments on commit 81334dd

Please sign in to comment.