-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from mikey179/master
add net\stubbles\input\ValueReader::asMonth()
- Loading branch information
Showing
7 changed files
with
267 additions
and
9 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,52 @@ | ||
<?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\types\datespan\Month; | ||
use net\stubbles\lang\exception\IllegalArgumentException; | ||
/** | ||
* Class for filtering months. | ||
* | ||
* The following rules apply: | ||
* - If given value is empty the returned value is null. | ||
* - If given value is not a valid month the returned value is null. | ||
* - If given value is a valid month the returned value is an instance of | ||
* net\stubbles\lang\types\datespan\Month. | ||
* | ||
* @since 2.5.1 | ||
*/ | ||
class MonthFilter implements Filter | ||
{ | ||
/** | ||
* apply filter on given param | ||
* | ||
* In case the given value can not be transformed into the target type | ||
* the return value is null. Additionally the $param instance is filled | ||
* with a FilterError. | ||
* | ||
* @param Param $param | ||
* @return Date | ||
*/ | ||
public function apply(Param $param) | ||
{ | ||
if ($param->isEmpty()) { | ||
return null; | ||
} | ||
|
||
try { | ||
return Month::fromString($param->getValue()); | ||
} catch (IllegalArgumentException $iae) { | ||
$param->addErrorWithId('MONTH_INVALID'); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
173 changes: 173 additions & 0 deletions
173
src/test/php/net/stubbles/input/filter/MonthFilterTestCase.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,173 @@ | ||
<?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\range\DatespanRange; | ||
use net\stubbles\lang\types\Date; | ||
use net\stubbles\lang\types\datespan\Month; | ||
require_once __DIR__ . '/FilterTestCase.php'; | ||
/** | ||
* Tests for net\stubbles\input\filter\MonthFilter. | ||
* | ||
* @group filter | ||
* @since 2.5.1 | ||
*/ | ||
class MonthFilterTestCase extends FilterTestCase | ||
{ | ||
/** | ||
* instance to test | ||
* | ||
* @type MonthFilter | ||
*/ | ||
private $monthFilter; | ||
|
||
/** | ||
* set up test environment | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->monthFilter = new MonthFilter(); | ||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* @return scalar | ||
*/ | ||
public function getEmptyValues() | ||
{ | ||
return array(array(''), | ||
array(null) | ||
); | ||
} | ||
|
||
/** | ||
* @param scalar $value | ||
* @test | ||
* @dataProvider getEmptyValues | ||
*/ | ||
public function emptyParamsAreReturnedAsNull($value) | ||
{ | ||
$this->assertNull($this->monthFilter->apply($this->createParam($value))); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function validParamsAreReturnedAsDateInstance() | ||
{ | ||
$month = $this->monthFilter->apply($this->createParam('2008-09-27')); | ||
$this->assertInstanceOf('net\stubbles\lang\types\datespan\Month', $month); | ||
$this->assertEquals('2008-09', $month->asString()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function applyReturnsNullForInvalidMonth() | ||
{ | ||
$this->assertNull($this->monthFilter->apply($this->createParam('invalid day'))); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function applyAddsErrorForInvalidDay() | ||
{ | ||
$param = $this->createParam('invalid day'); | ||
$this->monthFilter->apply($param); | ||
$this->assertTrue($param->hasError('MONTH_INVALID')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function asMonthReturnsNullIfParamIsNullAndNotRequired() | ||
{ | ||
$this->assertNull($this->createValueReader(null)->asMonth()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function asMonthReturnsDefaultIfParamIsNullAndNotRequired() | ||
{ | ||
$default = new Month(); | ||
$this->assertEquals($default, | ||
$this->createValueReader(null) | ||
->asMonth($default) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function asMonthReturnsNullIfParamIsNullAndRequired() | ||
{ | ||
$this->assertNull($this->createValueReader(null)->required()->asMonth()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function asMonthAddsParamErrorIfParamIsNullAndRequired() | ||
{ | ||
$this->createValueReader(null)->required()->asMonth(); | ||
$this->assertTrue($this->paramErrors->existForWithId('bar', 'FIELD_EMPTY')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function asMonthReturnsNullIfParamIsInvalid() | ||
{ | ||
$this->assertNull($this->createValueReader('foo')->asMonth()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function asDayAddsParamErrorIfParamIsInvalid() | ||
{ | ||
$this->createValueReader('foo')->asMonth(); | ||
$this->assertTrue($this->paramErrors->existFor('bar')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function asMonthReturnsValidValue() | ||
{ | ||
$this->assertEquals('2012-03', | ||
$this->createValueReader('2012-03-11') | ||
->asMonth() | ||
->asString() | ||
); | ||
|
||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function asMonthReturnsNullIfParamIsOutOfRange() | ||
{ | ||
$this->assertNull($this->createValueReader(new Month()) | ||
->asMonth(null, new DatespanRange(Date::now(), null)) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function asMonthAddsParamErrorIfParamIsOutOfRange() | ||
{ | ||
$this->createValueReader(new Month()) | ||
->asMonth(null, new DatespanRange(Date::now(), null)); | ||
$this->assertTrue($this->paramErrors->existFor('bar')); | ||
} | ||
} |