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\DateFilterType; |
||
7 | use ReflectionClass; |
||
8 | use Symfony\Component\HttpFoundation\Request; |
||
9 | |||
10 | class DateFilterTypeTest extends BaseDbalFilterTest |
||
11 | { |
||
12 | /** |
||
13 | * @var DateFilterType |
||
14 | */ |
||
15 | protected $object; |
||
16 | |||
17 | protected function setUp() |
||
18 | { |
||
19 | $this->object = new DateFilterType('date', 'e'); |
||
20 | } |
||
21 | |||
22 | public function testBindRequest() |
||
23 | { |
||
24 | $request = new Request(['filter_comparator_date' => 'before', 'filter_value_date' => '01/01/2012']); |
||
25 | |||
26 | $data = []; |
||
27 | $uniqueId = 'date'; |
||
28 | $this->object->bindRequest($request, $data, $uniqueId); |
||
29 | |||
30 | $this->assertEquals(['comparator' => 'before', 'value' => '01/01/2012'], $data); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param string $comparator The comparator |
||
35 | * @param string $whereClause The where clause |
||
36 | * @param mixed $value The value |
||
37 | * @param mixed $testValue The test value |
||
38 | * |
||
39 | * @dataProvider applyDataProvider |
||
40 | */ |
||
41 | public function testApply($comparator, $whereClause, $value, $testValue) |
||
42 | { |
||
43 | $qb = $this->getQueryBuilder(); |
||
44 | $qb->select('*') |
||
45 | ->from('entity', 'e'); |
||
46 | $this->object->setQueryBuilder($qb); |
||
47 | $this->object->apply(['comparator' => $comparator, 'value' => $value], 'date'); |
||
48 | |||
49 | $this->assertEquals("SELECT * FROM entity e WHERE e.date $whereClause", $qb->getSQL()); |
||
50 | $this->assertEquals($testValue, $qb->getParameter('var_date')); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return array |
||
55 | */ |
||
56 | View Code Duplication | public static function applyDataProvider() |
|
57 | { |
||
58 | return [ |
||
59 | ['before', '<= :var_date', '20/12/2012', '2012-12-20'], |
||
60 | ['after', '> :var_date', '21/12/2012', '2012-12-21'], |
||
61 | ]; |
||
62 | } |
||
63 | |||
64 | public function testGetTemplate() |
||
65 | { |
||
66 | $this->assertEquals('@KunstmaanAdminList/FilterType/dateFilter.html.twig', $this->object->getTemplate()); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @throws \ReflectionException |
||
71 | */ |
||
72 | View Code Duplication | public function testApplyReturnsNull() |
|
0 ignored issues
–
show
|
|||
73 | { |
||
74 | $queryBuilder = $this->createMock(QueryBuilder::class); |
||
75 | $queryBuilder->expects($this->never())->method('setParameter'); |
||
76 | $mirror = new ReflectionClass(DateFilterType::class); |
||
77 | $property = $mirror->getProperty('queryBuilder'); |
||
78 | $property->setAccessible(true); |
||
79 | $property->setValue($this->object, $queryBuilder); |
||
80 | $badData = [ |
||
81 | 'value' => 'oopsNotADate', |
||
82 | 'comparator' => 'true', |
||
83 | ]; |
||
84 | $this->object->apply($badData, uniqid()); |
||
85 | } |
||
86 | } |
||
87 |
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.