CommentManagerTest::testGetPagerWithStatus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
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\NewsBundle\Tests\Entity;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\Doctrine\Test\EntityManagerMockFactoryTrait;
18
use Sonata\NewsBundle\Entity\CommentManager;
19
use Sonata\NewsBundle\Model\CommentInterface;
20
21
/**
22
 * Tests the comment manager entity.
23
 *
24
 * @author Romain Mouillard <[email protected]>
25
 */
26
class CommentManagerTest 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
            ->getCommentManager(static function ($qb) use ($self): void {
35
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['c']));
36
                $qb->expects($self->once())->method('andWhere');
37
                $qb->expects($self->once())->method('setParameters')->with(['status' => CommentInterface::STATUS_VALID]);
38
            })
39
            ->getPager([], 1);
40
    }
41
42
    public function testGetPagerWithAdminMode(): void
43
    {
44
        $self = $this;
45
        $this
46
            ->getCommentManager(static function ($qb) use ($self): void {
47
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['c']));
48
                $qb->expects($self->never())->method('andWhere');
49
                $qb->expects($self->once())->method('setParameters')->with([]);
50
            })
51
            ->getPager([
52
                'mode' => 'admin',
53
            ], 1);
54
    }
55
56
    public function testGetPagerWithStatus(): void
57
    {
58
        $self = $this;
59
        $this
60
            ->getCommentManager(static function ($qb) use ($self): void {
61
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['c']));
62
                $qb->expects($self->once())->method('andWhere');
63
                $qb->expects($self->once())->method('setParameters')->with(['status' => CommentInterface::STATUS_INVALID]);
64
            })
65
            ->getPager([
66
                'status' => CommentInterface::STATUS_INVALID,
67
            ], 1);
68
    }
69
70
    public function testGetPagerWithPostId(): void
71
    {
72
        $self = $this;
73
        $this
74
            ->getCommentManager(static function ($qb) use ($self): void {
75
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['c']));
76
                $qb->expects($self->exactly(2))->method('andWhere')->with($self->logicalOr('c.post = :postId', 'c.status = :status'));
77
                $qb->expects($self->once())->method('setParameters')->with(['postId' => 50, 'status' => CommentInterface::STATUS_VALID]);
78
            })
79
            ->getPager([
80
                'postId' => 50,
81
            ], 1);
82
    }
83
84
    protected function getCommentManager($qbCallback)
85
    {
86
        $em = $this->createEntityManagerMock($qbCallback, []);
87
88
        $registry = $this->createMock('Doctrine\Common\Persistence\ManagerRegistry');
89
        $registry->expects($this->any())->method('getManagerForClass')->willReturn($em);
90
91
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
92
93
        return new CommentManager('Sonata\NewsBundle\Entity\BasePost', $registry, $postManager);
94
    }
95
}
96