Completed
Pull Request — master (#1352)
by Jordi Sala
02:42
created

GalleryManagerTest::testGetPagerWithMultipleSort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Test\Entity;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\CoreBundle\Test\EntityManagerMockFactory;
16
use Sonata\MediaBundle\Entity\GalleryManager;
17
18
/**
19
 * @author Benoit de Jacobet <[email protected]>
20
 */
21
class GalleryManagerTest extends TestCase
22
{
23
    public function testGetPager()
24
    {
25
        $this
26
            ->getGalleryManager(function ($qb) {
27
                $qb->expects($this->once())->method('getRootAliases')->will($this->returnValue(['g']));
28
                $qb->expects($this->never())->method('andWhere');
29
                $qb->expects($this->once())->method('orderBy')->with(
30
                    $this->equalTo('g.name'),
31
                    $this->equalTo('ASC')
32
                );
33
                $qb->expects($this->once())->method('setParameters')->with($this->equalTo([]));
34
            })
35
            ->getPager([], 1);
36
    }
37
38
    public function testGetPagerWithInvalidSort()
39
    {
40
        $this->expectException(\RuntimeException::class);
41
        $this->expectExceptionMessage('Invalid sort field \'invalid\' in \'Sonata\\MediaBundle\\Entity\\BaseGallery\' class');
42
43
        $this
44
            ->getGalleryManager(function ($qb) {
0 ignored issues
show
Unused Code introduced by
The parameter $qb is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
            })
46
            ->getPager([], 1, 10, ['invalid' => 'ASC']);
47
    }
48
49
    public function testGetPagerWithMultipleSort()
50
    {
51
        $this
52
            ->getGalleryManager(function ($qb) {
53
                $qb->expects($this->once())->method('getRootAliases')->will($this->returnValue(['g']));
54
                $qb->expects($this->never())->method('andWhere');
55
                $qb->expects($this->exactly(2))->method('orderBy')->with(
56
                    $this->logicalOr(
57
                        $this->equalTo('g.name'),
58
                        $this->equalTo('g.context')
59
                    ),
60
                    $this->logicalOr(
61
                        $this->equalTo('ASC'),
62
                        $this->equalTo('DESC')
63
                    )
64
                );
65
                $qb->expects($this->once())->method('setParameters')->with($this->equalTo([]));
66
            })
67
            ->getPager([], 1, 10, [
68
                'name' => 'ASC',
69
                'context' => 'DESC',
70
            ]);
71
    }
72
73
    public function testGetPagerWithEnabledGalleries()
74
    {
75
        $this
76
            ->getGalleryManager(function ($qb) {
77
                $qb->expects($this->once())->method('getRootAliases')->will($this->returnValue(['g']));
78
                $qb->expects($this->once())->method('andWhere')->with($this->equalTo('g.enabled = :enabled'));
79
                $qb->expects($this->once())->method('setParameters')->with($this->equalTo(['enabled' => true]));
80
            })
81
            ->getPager(['enabled' => true], 1);
82
    }
83
84
    public function testGetPagerWithNoEnabledGalleries()
85
    {
86
        $this
87
            ->getGalleryManager(function ($qb) {
88
                $qb->expects($this->once())->method('getRootAliases')->will($this->returnValue(['g']));
89
                $qb->expects($this->once())->method('andWhere')->with($this->equalTo('g.enabled = :enabled'));
90
                $qb->expects($this->once())->method('setParameters')->with($this->equalTo(['enabled' => false]));
91
            })
92
            ->getPager(['enabled' => false], 1);
93
    }
94
95
    protected function getGalleryManager($qbCallback)
96
    {
97
        $em = EntityManagerMockFactory::create($this, $qbCallback, [
98
            'name',
99
            'context',
100
            'enabled',
101
        ]);
102
103
        $registry = $this->createMock('Doctrine\Common\Persistence\ManagerRegistry');
104
        $registry->expects($this->any())->method('getManagerForClass')->will($this->returnValue($em));
105
106
        return new GalleryManager('Sonata\MediaBundle\Entity\BaseGallery', $registry);
107
    }
108
}
109