Skip to content

Commit

Permalink
add AcceptFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Kleine committed May 20, 2012
1 parent 9a1db5a commit 8b310a9
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/main/php/net/stubbles/input/filter/AcceptFilter.php
@@ -0,0 +1,41 @@
<?php
/**
* This file is part of stubbles.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package net\stubbles\input
*/
namespace net\stubbles\input\filter;
use net\stubbles\input\Filter;
use net\stubbles\input\Param;
use net\stubbles\lang\BaseObject;
use net\stubbles\peer\http\AcceptHeader;
/**
* Filters accept headers.
*
* @since 2.0.0
*/
class AcceptFilter extends BaseObject implements Filter
{
/**
* apply filter on given param
*
* @param Param $param
* @return AcceptHeader
*/
public function apply(Param $param)
{
if ($param->isNull() || $param->isEmpty()) {
return new AcceptHeader();
}

try {
return AcceptHeader::parse($param->getValue());
} catch (\net\stubbles\lang\exception\IllegalArgumentException $iae) {
return new AcceptHeader();
}
}
}
?>
64 changes: 64 additions & 0 deletions src/test/php/net/stubbles/input/filter/AcceptFilterTestCase.php
@@ -0,0 +1,64 @@
<?php
/**
* This file is part of stubbles.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package net\stubbles\input
*/
namespace net\stubbles\input\filter;
use net\stubbles\input\Param;
/**
* Tests for net\stubbles\input\filter\AcceptFilter.
*
* @since 2.0.0
* @group filter
*/
class AcceptFilterTestCase extends \PHPUnit_Framework_TestCase
{
/**
* apply filter on given value
*
* @param string $value
* @return net\stubbles\peer\http\AcceptHeader
*/
private function apply($value)
{
$acceptFilter = new AcceptFilter();
return $acceptFilter->apply(new Param('Accept', $value));
}

/**
* @test
*/
public function returnsEmptyAcceptHeaderWhenParamValueIsNull()
{
$this->assertEquals(0, $this->apply(null)->count());
}

/**
* @test
*/
public function returnsEmptyAcceptHeaderWhenParamValueIsEmpty()
{
$this->assertEquals(0, $this->apply('')->count());
}

/**
* @test
*/
public function returnsEmptyAcceptHeaderWhenParamValueIsInvalid()
{
$this->assertEquals(0, $this->apply('text/plain;q=5')->count());
}

/**
* @test
*/
public function returnsFilledAcceptHeaderWhenParamValueIsValid()
{
$this->assertEquals(1, $this->apply('text/plain;q=0.5')->count());
}
}
?>

0 comments on commit 8b310a9

Please sign in to comment.