These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\AdminListBundle\Tests\AdminList\FilterType\DBAL; |
||
4 | |||
5 | use Doctrine\DBAL\Query\QueryBuilder; |
||
6 | use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\DateTimeFilterType; |
||
7 | use ReflectionClass; |
||
8 | use Symfony\Component\HttpFoundation\Request; |
||
9 | |||
10 | View Code Duplication | class DateTimeFilterTypeTest extends BaseDbalFilterTest |
|
0 ignored issues
–
show
|
|||
11 | { |
||
12 | /** |
||
13 | * @var \UnitTester |
||
14 | */ |
||
15 | protected $tester; |
||
16 | |||
17 | /** |
||
18 | * @var DateTimeFilterType |
||
19 | */ |
||
20 | protected $object; |
||
21 | |||
22 | protected function setUp() |
||
23 | { |
||
24 | $this->object = new DateTimeFilterType('datetime', 'e'); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @return array |
||
29 | */ |
||
30 | public static function applyDataProvider() |
||
31 | { |
||
32 | return array( |
||
33 | array('before', '<= :var_datetime', array('date' => '14/04/2014', 'time' => '09:00'), '2014-04-14 09:00'), |
||
34 | array('after', '> :var_datetime', array('date' => '14/04/2014', 'time' => '10:00'), '2014-04-14 10:00'), |
||
35 | ); |
||
36 | } |
||
37 | |||
38 | public function testBindRequest() |
||
39 | { |
||
40 | $request = new Request(array( |
||
41 | 'filter_comparator_datetime' => 'before', |
||
42 | 'filter_value_datetime' => array('date' => '14/04/2014', 'time' => '09:00'), |
||
43 | )); |
||
44 | |||
45 | $data = array(); |
||
46 | $uniqueId = 'datetime'; |
||
47 | $this->object->bindRequest($request, $data, $uniqueId); |
||
48 | |||
49 | $this->assertEquals( |
||
50 | array('comparator' => 'before', 'value' => array('date' => '14/04/2014', 'time' => '09:00')), |
||
51 | $data |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param string $comparator The comparator |
||
57 | * @param string $whereClause The where clause |
||
58 | * @param mixed $value The value |
||
59 | * @param mixed $testValue The test value |
||
60 | * |
||
61 | * @dataProvider applyDataProvider |
||
62 | */ |
||
63 | public function testApply($comparator, $whereClause, $value, $testValue) |
||
64 | { |
||
65 | $qb = $this->getQueryBuilder(); |
||
66 | $qb->select('*') |
||
67 | ->from('entity', 'e'); |
||
68 | $this->object->setQueryBuilder($qb); |
||
69 | $this->object->apply(array('comparator' => $comparator, 'value' => $value), 'datetime'); |
||
70 | |||
71 | $this->assertEquals("SELECT * FROM entity e WHERE e.datetime $whereClause", $qb->getSQL()); |
||
72 | $this->assertEquals($testValue, $qb->getParameter('var_datetime')); |
||
73 | } |
||
74 | |||
75 | public function testGetTemplate() |
||
76 | { |
||
77 | $this->assertEquals( |
||
78 | '@KunstmaanAdminList/FilterType/dateTimeFilter.html.twig', |
||
79 | $this->object->getTemplate() |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @throws \ReflectionException |
||
85 | */ |
||
86 | public function testApplyReturnsNull() |
||
87 | { |
||
88 | $queryBuilder = $this->createMock(QueryBuilder::class); |
||
89 | $queryBuilder->expects($this->never())->method('setParameter'); |
||
90 | $mirror = new ReflectionClass(DateTimeFilterType::class); |
||
91 | $property = $mirror->getProperty('queryBuilder'); |
||
92 | $property->setAccessible(true); |
||
93 | $property->setValue($this->object, $queryBuilder); |
||
94 | |||
95 | $badData = [ |
||
96 | 'value' => [ |
||
97 | 'date' => 'oopsNotADate', |
||
98 | 'time' => 'oopsNotATime', |
||
99 | ], |
||
100 | 'comparator' => 'true', |
||
101 | ]; |
||
102 | $this->object->apply($badData, uniqid()); |
||
103 | } |
||
104 | } |
||
105 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.