forked from stubbles/stubbles-input
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Frank Kleine
committed
May 20, 2012
1 parent
9a1db5a
commit 8b310a9
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
64
src/test/php/net/stubbles/input/filter/AcceptFilterTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
?> |