Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71d894e

Browse files
creating a test to Strategy and DateComparator refactored
1 parent b0b0d4a commit 71d894e

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

Strategy/DateComparator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class DateComparator implements ComparatorInterface
1212
*/
1313
public function compare($a, $b)
1414
{
15-
$aDate = strtotime($a['date']);
16-
$bDate = strtotime($b['date']);
15+
$aDate = new \DateTime($a['date']);
16+
$bDate = new \DateTime($b['date']);
1717

1818
if ($aDate == $bDate) {
1919
return 0;

Strategy/index.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

Tests/Strategy/StrategyTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace DesignPatterns\Tests\Strategy;
4+
5+
use DesignPatterns\Strategy\DateComparator;
6+
use DesignPatterns\Strategy\IdComparator;
7+
use DesignPatterns\Strategy\ObjectCollection;
8+
use DesignPatterns\Strategy\Strategy;
9+
10+
/**
11+
* Tests for Static Factory pattern
12+
13+
*/
14+
class StrategyTest extends \PHPUnit_Framework_TestCase
15+
{
16+
17+
public function getIdCollection()
18+
{
19+
return array(
20+
array(
21+
array(array('id' => 2), array('id' => 1), array('id' => 3)),
22+
array('id' => 1)
23+
),
24+
array(
25+
array(array('id' => 3), array('id' => 2), array('id' => 1)),
26+
array('id' => 1)
27+
),
28+
);
29+
}
30+
31+
public function getDateCollection()
32+
{
33+
return array(
34+
array(
35+
array(array('date' => '2014-03-03'), array('date' => '2015-03-02'), array('date' => '2013-03-01')),
36+
array('date' => '2013-03-01')
37+
),
38+
array(
39+
array(array('date' => '2014-02-03'), array('date' => '2013-02-01'), array('date' => '2015-02-02')),
40+
array('date' => '2013-02-01')
41+
),
42+
);
43+
}
44+
45+
/**
46+
* @dataProvider getIdCollection
47+
*/
48+
public function testIdComparator($collection, $expected)
49+
{
50+
$obj = new ObjectCollection($collection);
51+
$obj->setComparator(new IdComparator());
52+
$elements = $obj->sort();
53+
54+
$firstElement = array_shift($elements);
55+
$this->assertEquals($expected, $firstElement);
56+
}
57+
58+
/**
59+
* @dataProvider getDateCollection
60+
*/
61+
public function testDateComparator($collection, $expected)
62+
{
63+
$obj = new ObjectCollection($collection);
64+
$obj->setComparator(new DateComparator());
65+
$elements = $obj->sort();
66+
67+
$firstElement = array_shift($elements);
68+
$this->assertEquals($expected, $firstElement);
69+
}
70+
}

0 commit comments

Comments
 (0)