Skip to content

Commit

Permalink
ServerRequest: introduce trait, cover more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Aug 10, 2017
1 parent f74daf4 commit 4e972dd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
44 changes: 44 additions & 0 deletions src/Extra/ExtraServerRequestTrait.php
@@ -0,0 +1,44 @@
<?php

namespace Contributte\Psr7\Extra;

use Contributte\Psr7\Exception\Logical\InvalidStateException;

trait ExtraServerRequestTrait
{

use ExtraRequestTrait;

/**
* QUERY PARAM *************************************************************
*/

/**
* @param string $name
*
* @return bool
*/
public function hasQueryParam($name)
{
return array_key_exists($name, $this->getQueryParams());
}

/**
* @param string $name
* @param mixed $default
* @return mixed
*/
public function getQueryParam($name, $default = NULL)
{
if (!$this->hasQueryParam($name)) {
if (func_num_args() < 2) {
throw new InvalidStateException(sprintf('No query parameter "%s" found', $name));
}

return $default;
}

return $this->getQueryParams()[$name];
}

}
6 changes: 4 additions & 2 deletions src/Psr7ServerRequest.php
Expand Up @@ -2,6 +2,7 @@

namespace Contributte\Psr7;

use Contributte\Psr7\Extra\ExtraServerRequestTrait;
use Contributte\Psr7\Nette\NetteRequestTrait;
use GuzzleHttp\Psr7\LazyOpenStream;
use GuzzleHttp\Psr7\ServerRequest;
Expand All @@ -13,13 +14,14 @@
* @author Milan Felix Sulc <sulcmil@gmail.com>
*
* @method Psr7UploadedFile[] getUploadedFiles()
* @method self withAttribute($name, $value)
* @method self|static withAttribute($name, $value)
* @method self|static withHeader($header, $value)
*/
class Psr7ServerRequest extends ServerRequest
{

use NetteRequestTrait;
use ExtraRequestTrait;
use ExtraServerRequestTrait;

/**
* @param array $files
Expand Down
23 changes: 17 additions & 6 deletions tests/cases/Psr7ServerRequest.phpt
Expand Up @@ -4,6 +4,7 @@
* Test: Psr7ServerRequest
*/

use Contributte\Psr7\Exception\Logical\InvalidStateException;
use Contributte\Psr7\Psr7ServerRequest;
use Contributte\Psr7\Psr7ServerRequestFactory;
use Tester\Assert;
Expand Down Expand Up @@ -33,13 +34,23 @@ test(function () {
Assert::equal('baz', $request->getAttribute('X-Bar'));
});

// hasQueryParam
// normalizeNetteFiles
test(function () {
$_GET['FOO'] = 'bar';
Assert::throws(function () {
Psr7ServerRequest::normalizeNetteFiles([new stdClass()]);
}, InvalidArgumentException::class, 'Invalid value in files specification');
});

// QueryParams
test(function () {
$_GET['foo'] = 'bar';
$request = Psr7ServerRequestFactory::fromSuperGlobal();

Assert::true($request->hasQueryParam('FOO'));
Assert::false($request->hasQueryParam('BAR'));
Assert::equal('bar', $request->getQueryParam('FOO'));
Assert::equal('baz', $request->getQueryParam('BAR', 'baz'));
Assert::true($request->hasQueryParam('foo'));
Assert::equal('bar', $request->getQueryParam('foo'));
Assert::equal('baz', $request->getQueryParam('foobar', 'baz'));

Assert::throws(function () use ($request) {
$request->getQueryParam('baz');
}, InvalidStateException::class, 'No query parameter "baz" found');
});

0 comments on commit 4e972dd

Please sign in to comment.