mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 09:42:24 +01:00
Merge pull request #74 from tonicospinelli/test-strategy
testing Strategy Pattern
This commit is contained in:
commit
edcc74be25
@ -12,8 +12,8 @@ class DateComparator implements ComparatorInterface
|
|||||||
*/
|
*/
|
||||||
public function compare($a, $b)
|
public function compare($a, $b)
|
||||||
{
|
{
|
||||||
$aDate = strtotime($a['date']);
|
$aDate = new \DateTime($a['date']);
|
||||||
$bDate = strtotime($b['date']);
|
$bDate = new \DateTime($b['date']);
|
||||||
|
|
||||||
if ($aDate == $bDate) {
|
if ($aDate == $bDate) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Strategy;
|
|
||||||
|
|
||||||
$elements = array(
|
|
||||||
array(
|
|
||||||
'id' => 2,
|
|
||||||
'date' => '2011-01-01',
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'id' => 1,
|
|
||||||
'date' => '2011-02-01'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
$collection = new ObjectCollection($elements);
|
|
||||||
$collection->setComparator(new IdComparator());
|
|
||||||
$collection->sort();
|
|
||||||
|
|
||||||
$collection->setComparator(new DateComparator());
|
|
||||||
$collection->sort();
|
|
70
Tests/Strategy/StrategyTest.php
Normal file
70
Tests/Strategy/StrategyTest.php
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Tests\Strategy;
|
||||||
|
|
||||||
|
use DesignPatterns\Strategy\DateComparator;
|
||||||
|
use DesignPatterns\Strategy\IdComparator;
|
||||||
|
use DesignPatterns\Strategy\ObjectCollection;
|
||||||
|
use DesignPatterns\Strategy\Strategy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for Static Factory pattern
|
||||||
|
|
||||||
|
*/
|
||||||
|
class StrategyTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
public function getIdCollection()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(
|
||||||
|
array(array('id' => 2), array('id' => 1), array('id' => 3)),
|
||||||
|
array('id' => 1)
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array(array('id' => 3), array('id' => 2), array('id' => 1)),
|
||||||
|
array('id' => 1)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDateCollection()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(
|
||||||
|
array(array('date' => '2014-03-03'), array('date' => '2015-03-02'), array('date' => '2013-03-01')),
|
||||||
|
array('date' => '2013-03-01')
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array(array('date' => '2014-02-03'), array('date' => '2013-02-01'), array('date' => '2015-02-02')),
|
||||||
|
array('date' => '2013-02-01')
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getIdCollection
|
||||||
|
*/
|
||||||
|
public function testIdComparator($collection, $expected)
|
||||||
|
{
|
||||||
|
$obj = new ObjectCollection($collection);
|
||||||
|
$obj->setComparator(new IdComparator());
|
||||||
|
$elements = $obj->sort();
|
||||||
|
|
||||||
|
$firstElement = array_shift($elements);
|
||||||
|
$this->assertEquals($expected, $firstElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getDateCollection
|
||||||
|
*/
|
||||||
|
public function testDateComparator($collection, $expected)
|
||||||
|
{
|
||||||
|
$obj = new ObjectCollection($collection);
|
||||||
|
$obj->setComparator(new DateComparator());
|
||||||
|
$elements = $obj->sort();
|
||||||
|
|
||||||
|
$firstElement = array_shift($elements);
|
||||||
|
$this->assertEquals($expected, $firstElement);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user