1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Tests\Bridge\Doctrine\ORM\Event\Subscriber; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use LAG\AdminBundle\Admin\ActionInterface; |
7
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
8
|
|
|
use LAG\AdminBundle\Bridge\Doctrine\ORM\Event\Subscriber\ORMSubscriber; |
9
|
|
|
use LAG\AdminBundle\Configuration\ActionConfiguration; |
10
|
|
|
use LAG\AdminBundle\Event\AdminEvents; |
11
|
|
|
use LAG\AdminBundle\Event\DoctrineOrmFilterEvent; |
12
|
|
|
use LAG\AdminBundle\Filter\FilterInterface; |
13
|
|
|
use LAG\AdminBundle\Tests\AdminTestBase; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
16
|
|
|
|
17
|
|
|
class ORMSubscriberTest extends AdminTestBase |
18
|
|
|
{ |
19
|
|
|
public function testGetSubscribedEvents() |
20
|
|
|
{ |
21
|
|
|
$events = ORMSubscriber::getSubscribedEvents(); |
22
|
|
|
$this->assertArrayHasKey(AdminEvents::DOCTRINE_ORM_FILTER, $events); |
23
|
|
|
$this->assertContains('addOrder', $events[AdminEvents::DOCTRINE_ORM_FILTER][0]); |
24
|
|
|
$this->assertContains('addFilters', $events[AdminEvents::DOCTRINE_ORM_FILTER][1]); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testAddOrder() |
28
|
|
|
{ |
29
|
|
|
$request = new Request(); |
30
|
|
|
|
31
|
|
|
$requestStack = $this->getMockWithoutConstructor(RequestStack::class); |
32
|
|
|
$requestStack |
33
|
|
|
->expects($this->atLeastOnce()) |
34
|
|
|
->method('getMasterRequest') |
35
|
|
|
->willReturn($request) |
36
|
|
|
; |
37
|
|
|
$admin = $this->getMockWithoutConstructor(AdminInterface::class); |
38
|
|
|
$action = $this->getMockWithoutConstructor(ActionInterface::class); |
39
|
|
|
|
40
|
|
|
$configuration = $this->getMockWithoutConstructor(ActionConfiguration::class); |
41
|
|
|
$configuration |
42
|
|
|
->expects($this->once()) |
43
|
|
|
->method('getParameter') |
44
|
|
|
->with('order') |
45
|
|
|
->willReturn([ |
46
|
|
|
'id' => 'desc', |
47
|
|
|
]) |
48
|
|
|
; |
49
|
|
|
|
50
|
|
|
$queryBuilder = $this->getMockWithoutConstructor(QueryBuilder::class); |
51
|
|
|
$queryBuilder |
52
|
|
|
->expects($this->once()) |
53
|
|
|
->method('getRootAliases') |
54
|
|
|
->willReturn([ |
55
|
|
|
'entity', |
56
|
|
|
]) |
57
|
|
|
; |
58
|
|
|
$queryBuilder |
59
|
|
|
->expects($this->once()) |
60
|
|
|
->method('addOrderBy') |
61
|
|
|
->with('entity.id', 'desc') |
62
|
|
|
; |
63
|
|
|
|
64
|
|
|
$event = $this->getMockWithoutConstructor(DoctrineOrmFilterEvent::class); |
65
|
|
|
$event |
66
|
|
|
->expects($this->once()) |
67
|
|
|
->method('getQueryBuilder') |
68
|
|
|
->willReturn($queryBuilder) |
69
|
|
|
; |
70
|
|
|
$event |
71
|
|
|
->expects($this->once()) |
72
|
|
|
->method('getAdmin') |
73
|
|
|
->willReturn($admin) |
74
|
|
|
; |
75
|
|
|
$admin |
76
|
|
|
->expects($this->once()) |
77
|
|
|
->method('getAction') |
78
|
|
|
->willReturn($action) |
79
|
|
|
; |
80
|
|
|
$action |
81
|
|
|
->expects($this->once()) |
82
|
|
|
->method('getConfiguration') |
83
|
|
|
->willReturn($configuration) |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
$subscriber = new ORMSubscriber($requestStack); |
87
|
|
|
$subscriber->addOrder($event); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testAddFilters() |
91
|
|
|
{ |
92
|
|
|
$requestStack = $this->getMockWithoutConstructor(RequestStack::class); |
93
|
|
|
$queryBuilder = $this->getMockWithoutConstructor(QueryBuilder::class); |
94
|
|
|
$queryBuilder |
95
|
|
|
->expects($this->once()) |
96
|
|
|
->method('getRootAliases') |
97
|
|
|
->willReturn([ |
98
|
|
|
'entity', |
99
|
|
|
]) |
100
|
|
|
; |
101
|
|
|
$queryBuilder |
102
|
|
|
->expects($this->once()) |
103
|
|
|
->method('andWhere') |
104
|
|
|
->with('entity.name like :filter_name') |
105
|
|
|
; |
106
|
|
|
$queryBuilder |
107
|
|
|
->expects($this->once()) |
108
|
|
|
->method('setParameter') |
109
|
|
|
->with('filter_name', '%test%') |
110
|
|
|
; |
111
|
|
|
|
112
|
|
|
$filter1 = $this->getMockWithoutConstructor(FilterInterface::class); |
113
|
|
|
$filter1 |
114
|
|
|
->method('getName') |
115
|
|
|
->willReturn('name') |
116
|
|
|
; |
117
|
|
|
$filter1 |
118
|
|
|
->method('getOperator') |
119
|
|
|
->willReturn('like') |
120
|
|
|
; |
121
|
|
|
$filter1 |
122
|
|
|
->expects($this->once()) |
123
|
|
|
->method('getValue') |
124
|
|
|
->willReturn('test') |
125
|
|
|
; |
126
|
|
|
|
127
|
|
|
$event = $this->getMockWithoutConstructor(DoctrineOrmFilterEvent::class); |
128
|
|
|
$event |
129
|
|
|
->expects($this->once()) |
130
|
|
|
->method('getQueryBuilder') |
131
|
|
|
->willReturn($queryBuilder) |
132
|
|
|
; |
133
|
|
|
$event |
134
|
|
|
->expects($this->once()) |
135
|
|
|
->method('getFilters') |
136
|
|
|
->willReturn([ |
137
|
|
|
$filter1, |
138
|
|
|
]) |
139
|
|
|
; |
140
|
|
|
|
141
|
|
|
$subscriber = new ORMSubscriber($requestStack); |
142
|
|
|
$subscriber->addFilters($event); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|