diff --git a/Strategy/DateComparator.php b/Strategy/DateComparator.php index 9025acca4..199716be6 100644 --- a/Strategy/DateComparator.php +++ b/Strategy/DateComparator.php @@ -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; diff --git a/Strategy/index.php b/Strategy/index.php deleted file mode 100644 index d04caa231..000000000 --- a/Strategy/index.php +++ /dev/null @@ -1,21 +0,0 @@ - 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(); diff --git a/Tests/Strategy/StrategyTest.php b/Tests/Strategy/StrategyTest.php new file mode 100644 index 000000000..7e587b106 --- /dev/null +++ b/Tests/Strategy/StrategyTest.php @@ -0,0 +1,70 @@ + 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); + } +}