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\MediaBundle\Test\Entity; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
17
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
use Sonata\Doctrine\Test\EntityManagerMockFactoryTrait; |
20
|
|
|
use Sonata\MediaBundle\Entity\BaseGallery; |
21
|
|
|
use Sonata\MediaBundle\Entity\GalleryManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Benoit de Jacobet <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class GalleryManagerTest extends TestCase |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
use EntityManagerMockFactoryTrait; |
29
|
|
|
|
30
|
|
|
public function testGetPager(): void |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->getGalleryManager(function (MockObject $qb): void { |
34
|
|
|
$qb->expects($this->once())->method('getRootAliases')->willReturn(['g']); |
35
|
|
|
$qb->expects($this->never())->method('andWhere'); |
36
|
|
|
$qb->expects($this->once())->method('orderBy')->with( |
37
|
|
|
$this->equalTo('g.name'), |
38
|
|
|
$this->equalTo('ASC') |
39
|
|
|
); |
40
|
|
|
$qb->expects($this->once())->method('setParameters')->with($this->equalTo([])); |
41
|
|
|
}) |
42
|
|
|
->getPager([], 1); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testGetPagerWithInvalidSort(): void |
46
|
|
|
{ |
47
|
|
|
$this->expectException(\RuntimeException::class); |
48
|
|
|
$this->expectExceptionMessage('Invalid sort field \'invalid\' in \'Sonata\\MediaBundle\\Entity\\BaseGallery\' class'); |
49
|
|
|
|
50
|
|
|
$this |
51
|
|
|
->getGalleryManager(static function (MockObject $qb): void { |
|
|
|
|
52
|
|
|
}) |
53
|
|
|
->getPager([], 1, 10, ['invalid' => 'ASC']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetPagerWithMultipleSort(): void |
57
|
|
|
{ |
58
|
|
|
$this |
59
|
|
|
->getGalleryManager(function (MockObject $qb): void { |
60
|
|
|
$qb->expects($this->once())->method('getRootAliases')->willReturn(['g']); |
61
|
|
|
$qb->expects($this->never())->method('andWhere'); |
62
|
|
|
$qb->expects($this->exactly(2))->method('orderBy')->with( |
63
|
|
|
$this->logicalOr( |
64
|
|
|
$this->equalTo('g.name'), |
65
|
|
|
$this->equalTo('g.context') |
66
|
|
|
), |
67
|
|
|
$this->logicalOr( |
68
|
|
|
$this->equalTo('ASC'), |
69
|
|
|
$this->equalTo('DESC') |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
$qb->expects($this->once())->method('setParameters')->with($this->equalTo([])); |
73
|
|
|
}) |
74
|
|
|
->getPager([], 1, 10, [ |
75
|
|
|
'name' => 'ASC', |
76
|
|
|
'context' => 'DESC', |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testGetPagerWithEnabledGalleries(): void |
81
|
|
|
{ |
82
|
|
|
$this |
83
|
|
|
->getGalleryManager(function (MockObject $qb): void { |
84
|
|
|
$qb->expects($this->once())->method('getRootAliases')->willReturn(['g']); |
85
|
|
|
$qb->expects($this->once())->method('andWhere')->with($this->equalTo('g.enabled = :enabled')); |
86
|
|
|
$qb->expects($this->once())->method('setParameters')->with($this->equalTo(['enabled' => true])); |
87
|
|
|
}) |
88
|
|
|
->getPager(['enabled' => true], 1); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testGetPagerWithNoEnabledGalleries(): void |
92
|
|
|
{ |
93
|
|
|
$this |
94
|
|
|
->getGalleryManager(function (MockObject $qb): void { |
95
|
|
|
$qb->expects($this->once())->method('getRootAliases')->willReturn(['g']); |
96
|
|
|
$qb->expects($this->once())->method('andWhere')->with($this->equalTo('g.enabled = :enabled')); |
97
|
|
|
$qb->expects($this->once())->method('setParameters')->with($this->equalTo(['enabled' => false])); |
98
|
|
|
}) |
99
|
|
|
->getPager(['enabled' => false], 1); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function getGalleryManager(\Closure $qbCallback): GalleryManager |
103
|
|
|
{ |
104
|
|
|
$em = $this->createEntityManagerMock($qbCallback, [ |
105
|
|
|
'name', |
106
|
|
|
'context', |
107
|
|
|
'enabled', |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
111
|
|
|
$registry->method('getManagerForClass')->willReturn($em); |
112
|
|
|
|
113
|
|
|
return new GalleryManager(BaseGallery::class, $registry); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|