Merge pull request #74 from tonicospinelli/test-strategy

testing Strategy Pattern
This commit is contained in:
Dominik Liebler 2014-04-08 20:43:57 +02:00
commit edcc74be25
3 changed files with 72 additions and 23 deletions

View File

@ -12,8 +12,8 @@ class DateComparator implements ComparatorInterface
*/
public function compare($a, $b)
{
$aDate = strtotime($a['date']);
$bDate = strtotime($b['date']);
$aDate = new \DateTime($a['date']);
$bDate = new \DateTime($b['date']);
if ($aDate == $bDate) {
return 0;

View File

@ -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();

View 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);
}
}