These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\NewsBundle\Tests\Entity; |
||
15 | |||
16 | use Doctrine\Common\Persistence\ManagerRegistry; |
||
17 | use PHPUnit\Framework\TestCase; |
||
18 | use Sonata\Doctrine\Test\EntityManagerMockFactory; |
||
19 | use Sonata\NewsBundle\Entity\BasePost; |
||
20 | use Sonata\NewsBundle\Entity\PostManager; |
||
21 | use Sonata\NewsBundle\Model\BlogInterface; |
||
22 | use Sonata\NewsBundle\Permalink\PermalinkInterface; |
||
23 | |||
24 | class PostManagerTest extends TestCase |
||
25 | { |
||
26 | public function assertRelationsEnabled($qb): void |
||
27 | { |
||
28 | $qb |
||
29 | ->expects($this->exactly(2)) |
||
30 | ->method('leftJoin') |
||
31 | ->with( |
||
32 | $this->logicalOr( |
||
33 | $this->equalTo('p.tags'), |
||
34 | $this->equalTo('p.author') |
||
35 | ), |
||
36 | $this->logicalOr( |
||
37 | $this->equalTo('t'), |
||
38 | $this->equalTo('a') |
||
39 | ), |
||
40 | 'WITH', |
||
41 | $this->stringEndsWith('.enabled = true') |
||
42 | ) |
||
43 | ->willReturn($qb) |
||
44 | ; |
||
45 | } |
||
46 | |||
47 | public function assertRelationsJoined($qb): void |
||
48 | { |
||
49 | $qb |
||
50 | ->expects($this->exactly(2)) |
||
51 | ->method('leftJoin') |
||
52 | ->with( |
||
53 | $this->logicalOr( |
||
54 | $this->equalTo('p.tags'), |
||
55 | $this->equalTo('p.author') |
||
56 | ), |
||
57 | $this->logicalOr( |
||
58 | $this->equalTo('t'), |
||
59 | $this->equalTo('a') |
||
60 | ), |
||
61 | $this->isNull(), |
||
62 | $this->isNull() |
||
63 | ) |
||
64 | ->willReturn($qb) |
||
65 | ; |
||
66 | } |
||
67 | |||
68 | public function assertPostEnabled($qb, $flag): void |
||
69 | { |
||
70 | $qb->expects($this->once())->method('andWhere')->with($this->equalTo('p.enabled = :enabled')); |
||
71 | $qb->expects($this->once())->method('setParameters')->with($this->equalTo(['enabled' => $flag])); |
||
72 | } |
||
73 | |||
74 | public function testFindOneByPermalinkSlug(): void |
||
75 | { |
||
76 | $permalink = $this->createMock(PermalinkInterface::class); |
||
77 | $permalink->expects($this->once())->method('getParameters') |
||
78 | ->with($this->equalTo('foo/bar')) |
||
79 | ->willReturn([ |
||
80 | 'slug' => 'bar', |
||
81 | ]); |
||
82 | |||
83 | $blog = $this->createMock(BlogInterface::class); |
||
84 | $blog->expects($this->once())->method('getPermalinkGenerator')->willReturn($permalink); |
||
85 | |||
86 | $self = $this; |
||
87 | $this |
||
88 | ->getPostManager(static function ($qb) use ($self): void { |
||
89 | $qb->expects($self->once())->method('andWhere')->with($self->equalTo('p.slug = :slug')); |
||
90 | $qb->expects($self->once())->method('setParameters')->with($self->equalTo(['slug' => 'bar'])); |
||
91 | }) |
||
92 | ->findOneByPermalink('foo/bar', $blog); |
||
0 ignored issues
–
show
|
|||
93 | } |
||
94 | |||
95 | public function testFindOneByPermalinkException(): void |
||
96 | { |
||
97 | $permalink = $this->createMock(PermalinkInterface::class); |
||
98 | $permalink->expects($this->once())->method('getParameters') |
||
99 | ->with($this->equalTo('')) |
||
100 | ->willThrowException(new \InvalidArgumentException()); |
||
101 | |||
102 | $blog = $this->createMock(BlogInterface::class); |
||
103 | $blog->expects($this->once())->method('getPermalinkGenerator')->willReturn($permalink); |
||
104 | |||
105 | $self = $this; |
||
106 | $result = $this |
||
107 | ->getPostManager(static function ($qb): void { |
||
108 | }) |
||
109 | ->findOneByPermalink('', $blog); |
||
0 ignored issues
–
show
$blog is of type object<PHPUnit\Framework\MockObject\MockObject> , but the function expects a object<Sonata\NewsBundle\Model\BlogInterface> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
110 | |||
111 | $this->assertNull($result); |
||
112 | } |
||
113 | |||
114 | public function testGetPagerWithoutMode(): void |
||
115 | { |
||
116 | $self = $this; |
||
117 | $this |
||
118 | ->getPostManager(static function ($qb) use ($self): void { |
||
119 | $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p'])); |
||
120 | $self->assertRelationsEnabled($qb); |
||
121 | $self->assertPostEnabled($qb, 1); |
||
122 | }) |
||
123 | ->getPager([], 1); |
||
124 | } |
||
125 | |||
126 | public function testGetPagerWithoutModeEnabled(): void |
||
127 | { |
||
128 | $self = $this; |
||
129 | $this |
||
130 | ->getPostManager(static function ($qb) use ($self): void { |
||
131 | $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p'])); |
||
132 | $self->assertRelationsEnabled($qb); |
||
133 | $self->assertPostEnabled($qb, 1); |
||
134 | }) |
||
135 | ->getPager([], 1); |
||
136 | } |
||
137 | |||
138 | public function testGetPagerWithoutModeDisabled(): void |
||
139 | { |
||
140 | $self = $this; |
||
141 | $this |
||
142 | ->getPostManager(static function ($qb) use ($self): void { |
||
143 | $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p'])); |
||
144 | $self->assertRelationsEnabled($qb); |
||
145 | $self->assertPostEnabled($qb, 0); |
||
146 | }) |
||
147 | ->getPager([ |
||
148 | 'enabled' => 0, |
||
149 | ], 1); |
||
150 | } |
||
151 | |||
152 | public function testGetPagerWithPublicMode(): void |
||
153 | { |
||
154 | $self = $this; |
||
155 | $this |
||
156 | ->getPostManager(static function ($qb) use ($self): void { |
||
157 | $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p'])); |
||
158 | $self->assertRelationsEnabled($qb); |
||
159 | $self->assertPostEnabled($qb, 1); |
||
160 | }) |
||
161 | ->getPager([ |
||
162 | 'mode' => 'public', |
||
163 | ], 1); |
||
164 | } |
||
165 | |||
166 | public function testGetPagerWithPublicModeEnabled(): void |
||
167 | { |
||
168 | $self = $this; |
||
169 | $this |
||
170 | ->getPostManager(static function ($qb) use ($self): void { |
||
171 | $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p'])); |
||
172 | $self->assertRelationsEnabled($qb); |
||
173 | $self->assertPostEnabled($qb, 1); |
||
174 | }) |
||
175 | ->getPager([ |
||
176 | 'mode' => 'public', |
||
177 | 'enabled' => 1, |
||
178 | ], 1); |
||
179 | } |
||
180 | |||
181 | public function testGetPagerWithPublicModeDisabled(): void |
||
182 | { |
||
183 | $self = $this; |
||
184 | $this |
||
185 | ->getPostManager(static function ($qb) use ($self): void { |
||
186 | $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p'])); |
||
187 | $self->assertRelationsEnabled($qb); |
||
188 | $self->assertPostEnabled($qb, 0); |
||
189 | }) |
||
190 | ->getPager([ |
||
191 | 'mode' => 'public', |
||
192 | 'enabled' => 0, |
||
193 | ], 1); |
||
194 | } |
||
195 | |||
196 | public function testGetPagerWithAdminMode(): void |
||
197 | { |
||
198 | $self = $this; |
||
199 | $this |
||
200 | ->getPostManager(static function ($qb) use ($self): void { |
||
201 | $self->assertRelationsJoined($qb); |
||
202 | $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p'])); |
||
203 | $qb->expects($self->never())->method('andWhere'); |
||
204 | $qb->expects($self->once())->method('setParameters')->with($self->equalTo([])); |
||
205 | }) |
||
206 | ->getPager([ |
||
207 | 'mode' => 'admin', |
||
208 | ], 1); |
||
209 | } |
||
210 | |||
211 | public function testGetPagerWithAdminModeEnabled(): void |
||
212 | { |
||
213 | $self = $this; |
||
214 | $this |
||
215 | ->getPostManager(static function ($qb) use ($self): void { |
||
216 | $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p'])); |
||
217 | $self->assertRelationsJoined($qb); |
||
218 | $self->assertPostEnabled($qb, 1); |
||
219 | }) |
||
220 | ->getPager([ |
||
221 | 'mode' => 'admin', |
||
222 | 'enabled' => 1, |
||
223 | ], 1); |
||
224 | } |
||
225 | |||
226 | public function testGetPagerWithAdminModeDisabled(): void |
||
227 | { |
||
228 | $self = $this; |
||
229 | $this |
||
230 | ->getPostManager(static function ($qb) use ($self): void { |
||
231 | $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p'])); |
||
232 | $self->assertRelationsJoined($qb); |
||
233 | $self->assertPostEnabled($qb, 0); |
||
234 | }) |
||
235 | ->getPager([ |
||
236 | 'mode' => 'admin', |
||
237 | 'enabled' => 0, |
||
238 | ], 1); |
||
239 | } |
||
240 | |||
241 | public function testGetPublicationDateQueryParts(): void |
||
242 | { |
||
243 | $result = $this |
||
244 | ->getPostManager(static function (): void { |
||
245 | }) |
||
246 | ->getPublicationDateQueryParts('2010-02-10', 'month', 'n'); |
||
247 | |||
248 | $this->assertNotNull($result); |
||
249 | $this->assertInstanceOf(\DateTimeInterface::class, $result['params']['startDate']); |
||
250 | $this->assertInstanceOf(\DateTimeInterface::class, $result['params']['endDate']); |
||
251 | $this->assertSame('2010-02-10', $result['params']['startDate']->format('Y-m-d')); |
||
252 | $this->assertSame('2010-03-10', $result['params']['endDate']->format('Y-m-d')); |
||
253 | } |
||
254 | |||
255 | protected function getPostManager($qbCallback) |
||
256 | { |
||
257 | $em = EntityManagerMockFactory::create($this, $qbCallback, []); |
||
258 | |||
259 | $registry = $this->createMock(ManagerRegistry::class); |
||
260 | $registry->expects($this->any())->method('getManagerForClass')->willReturn($em); |
||
261 | |||
262 | return new PostManager(BasePost::class, $registry); |
||
0 ignored issues
–
show
$registry is of type object<PHPUnit\Framework\MockObject\MockObject> , but the function expects a object<Doctrine\Common\P...stence\ManagerRegistry> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
263 | } |
||
264 | } |
||
265 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: