MediaManagerTest::testGetPagerWithMultipleSort()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\BaseMedia;
21
use Sonata\MediaBundle\Entity\MediaManager;
22
23
/**
24
 * @author Benoit de Jacobet <[email protected]>
25
 */
26
class MediaManagerTest extends TestCase
0 ignored issues
show
Bug introduced by
There is one abstract method createMock in this class; you could implement it, or declare this class as abstract.
Loading history...
27
{
28
    use EntityManagerMockFactoryTrait;
29
30
    public function testGetPager(): void
31
    {
32
        $self = $this;
33
        $this
34
            ->getMediaManager(static function (MockObject $qb) use ($self): void {
35
                $qb->expects($self->once())->method('getRootAliases')->willReturn(['g']);
36
                $qb->expects($self->never())->method('andWhere');
37
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([]));
38
            })
39
            ->getPager([], 1);
40
    }
41
42
    public function testGetPagerWithInvalidSort(): void
43
    {
44
        $this->expectException(\RuntimeException::class);
45
        $this->expectExceptionMessage('Invalid sort field \'invalid\' in \'Sonata\\MediaBundle\\Entity\\BaseMedia\' class');
46
47
        $this
48
            ->getMediaManager(static function ($qb): void {
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...
49
            })
50
            ->getPager([], 1, 10, ['invalid' => 'ASC']);
51
    }
52
53
    public function testGetPagerWithMultipleSort(): void
54
    {
55
        $self = $this;
56
        $this
57
            ->getMediaManager(static function (MockObject $qb) use ($self): void {
58
                $qb->expects($self->once())->method('getRootAliases')->willReturn(['g']);
59
                $qb->expects($self->never())->method('andWhere');
60
                $qb->expects($self->exactly(2))->method('orderBy')->with(
61
                    $self->logicalOr(
62
                        $self->equalTo('m.name'),
63
                        $self->equalTo('m.description')
64
                    ),
65
                    $self->logicalOr(
66
                        $self->equalTo('ASC'),
67
                        $self->equalTo('DESC')
68
                    )
69
                );
70
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([]));
71
            })
72
            ->getPager([], 1, 10, [
73
                'name' => 'ASC',
74
                'description' => 'DESC',
75
            ]);
76
    }
77
78
    public function testGetPagerWithEnabledMedia(): void
79
    {
80
        $self = $this;
81
        $this
82
            ->getMediaManager(static function (MockObject $qb) use ($self): void {
83
                $qb->expects($self->once())->method('getRootAliases')->willReturn(['g']);
84
                $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.enabled = :enabled'));
85
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo(['enabled' => true]));
86
            })
87
            ->getPager(['enabled' => true], 1);
88
    }
89
90
    public function testGetPagerWithNoEnabledMedias(): void
91
    {
92
        $self = $this;
93
        $this
94
            ->getMediaManager(static function (MockObject $qb) use ($self): void {
95
                $qb->expects($self->once())->method('getRootAliases')->willReturn(['g']);
96
                $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.enabled = :enabled'));
97
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo(['enabled' => false]));
98
            })
99
            ->getPager(['enabled' => false], 1);
100
    }
101
102
    protected function getMediaManager(\Closure $qbCallback): MediaManager
103
    {
104
        $em = $this->createEntityManagerMock($qbCallback, [
105
            'name',
106
            'description',
107
            'enabled',
108
        ]);
109
110
        $registry = $this->createMock(ManagerRegistry::class);
111
        $registry->method('getManagerForClass')->willReturn($em);
112
113
        return new MediaManager(BaseMedia::class, $registry);
114
    }
115
}
116