MessageManagerTest::testGetPager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
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\NotificationBundle\Tests\Entity;
15
16
use Doctrine\Common\Persistence\ManagerRegistry;
17
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
18
use Doctrine\ORM\AbstractQuery;
19
use Doctrine\ORM\EntityManager;
20
use Doctrine\ORM\EntityRepository;
21
use Doctrine\ORM\QueryBuilder;
22
use PHPUnit\Framework\TestCase;
23
use Sonata\NotificationBundle\Entity\BaseMessage;
24
use Sonata\NotificationBundle\Entity\MessageManager;
25
use Sonata\NotificationBundle\Model\MessageInterface;
26
27
class MessageManagerTest extends TestCase
28
{
29
    public function testCancel(): void
30
    {
31
        $manager = $this->getMessageManagerMock();
32
33
        $message = $this->getMessage();
34
35
        $manager->cancel($message);
36
37
        $this->assertTrue($message->isCancelled());
38
    }
39
40
    public function testRestart(): void
41
    {
42
        $manager = $this->getMessageManagerMock();
43
44
        // test un-restartable status
45
        $this->assertNull($manager->restart($this->getMessage(MessageInterface::STATE_OPEN)));
46
        $this->assertNull($manager->restart($this->getMessage(MessageInterface::STATE_CANCELLED)));
47
        $this->assertNull($manager->restart($this->getMessage(MessageInterface::STATE_IN_PROGRESS)));
48
49
        $message = $this->getMessage(MessageInterface::STATE_ERROR);
50
        $message->setRestartCount(12);
51
52
        $newMessage = $manager->restart($message);
53
54
        $this->assertSame(MessageInterface::STATE_OPEN, $newMessage->getState());
55
        $this->assertSame(13, $newMessage->getRestartCount());
56
    }
57
58
    public function testGetPager(): void
59
    {
60
        $self = $this;
61
        $this
62
            ->getMessageManager(static function ($qb) use ($self): void {
63
                $qb->expects($self->once())->method('getRootAliases')->willReturn(['m']);
64
                $qb->expects($self->never())->method('andWhere');
65
                $qb->expects($self->once())->method('setParameters')->with([]);
66
                $qb->expects($self->once())->method('orderBy')->with(
67
                    $self->equalTo('m.type'),
68
                    $self->equalTo('ASC')
69
                );
70
            })
71
            ->getPager([], 1);
72
    }
73
74
    public function testGetPagerWithInvalidSort(): void
75
    {
76
        $this->expectException(\RuntimeException::class);
77
        $this->expectExceptionMessage('Invalid sort field \'invalid\' in \'Sonata\\NotificationBundle\\Entity\\BaseMessage\' class');
78
79
        $this
80
            ->getMessageManager(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...
81
            })
82
            ->getPager([], 1, 10, ['invalid' => 'ASC']);
83
    }
84
85
    public function testGetPagerWithMultipleSort(): void
86
    {
87
        $self = $this;
88
        $this
89
            ->getMessageManager(static function ($qb) use ($self): void {
90
                $qb->expects($self->once())->method('getRootAliases')->willReturn(['m']);
91
                $qb->expects($self->never())->method('andWhere');
92
                $qb->expects($self->once())->method('setParameters')->with([]);
93
                $qb->expects($self->exactly(2))->method('orderBy')->with(
94
                    $self->logicalOr(
95
                        $self->equalTo('m.type'),
96
                        $self->equalTo('m.state')
97
                    ),
98
                    $self->logicalOr(
99
                        $self->equalTo('ASC'),
100
                        $self->equalTo('DESC')
101
                    )
102
                );
103
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([]));
104
            })
105
            ->getPager([], 1, 10, [
106
                'type' => 'ASC',
107
                'state' => 'DESC',
108
            ]);
109
    }
110
111
    public function testGetPagerWithOpenedMessages(): void
112
    {
113
        $self = $this;
114
        $this
115
            ->getMessageManager(static function ($qb) use ($self): void {
116
                $qb->expects($self->once())->method('getRootAliases')->willReturn(['m']);
117
                $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state'));
118
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([
119
                    'state' => MessageInterface::STATE_OPEN,
120
                ]));
121
            })
122
            ->getPager(['state' => MessageInterface::STATE_OPEN], 1);
123
    }
124
125
    public function testGetPagerWithCanceledMessages(): void
126
    {
127
        $self = $this;
128
        $this
129
            ->getMessageManager(static function ($qb) use ($self): void {
130
                $qb->expects($self->once())->method('getRootAliases')->willReturn(['m']);
131
                $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state'));
132
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([
133
                    'state' => MessageInterface::STATE_CANCELLED,
134
                ]));
135
            })
136
            ->getPager(['state' => MessageInterface::STATE_CANCELLED], 1);
137
    }
138
139
    public function testGetPagerWithInProgressMessages(): void
140
    {
141
        $self = $this;
142
        $this
143
            ->getMessageManager(static function ($qb) use ($self): void {
144
                $qb->expects($self->once())->method('getRootAliases')->willReturn(['m']);
145
                $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state'));
146
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([
147
                    'state' => MessageInterface::STATE_IN_PROGRESS,
148
                ]));
149
            })
150
            ->getPager(['state' => MessageInterface::STATE_IN_PROGRESS], 1);
151
    }
152
153
    public function testFindByTypes(): void
154
    {
155
        $this
156
            ->getMessageManager(function ($qb): void {
157
                $qb->expects($this->never())->method('andWhere');
158
                $qb->expects($this->once())->method('where')->willReturnSelf();
159
                $qb->expects($this->once())->method('setParameters')
160
                    ->with(['state' => MessageInterface::STATE_OPEN]);
161
                $qb->expects($this->once())->method('orderBy')->with(
162
                    $this->equalTo('m.createdAt')
163
                )->willReturnSelf();
164
            })
165
            ->findByTypes([], MessageInterface::STATE_OPEN, 10);
166
    }
167
168
    protected function getMessageManagerMock(): MessageManagerMock
169
    {
170
        $registry = $this->createMock(ManagerRegistry::class);
171
172
        return new MessageManagerMock(Message::class, $registry);
173
    }
174
175
    protected function getMessageManager($qbCallback): MessageManager
176
    {
177
        $query = $this->getMockForAbstractClass(
178
            AbstractQuery::class,
179
            [],
180
            '',
181
            false,
182
            true,
183
            true,
184
            ['execute']
185
        );
186
        $query->method('execute')->willReturn(true);
187
188
        $qb = $this->getMockBuilder(QueryBuilder::class)
189
            ->setConstructorArgs([$this->createMock(EntityManager::class)])
190
            ->getMock();
191
192
        $qb->method('select')->willReturn($qb);
193
        $qb->method('getQuery')->willReturn($query);
194
195
        $qbCallback($qb);
196
197
        $repository = $this->createMock(EntityRepository::class);
198
        $repository->method('createQueryBuilder')->willReturn($qb);
199
200
        $metadata = $this->createMock(ClassMetadata::class);
201
        $metadata->method('getFieldNames')->willReturn([
202
            'state',
203
            'type',
204
        ]);
205
206
        $em = $this->createMock(EntityManager::class);
207
        $em->method('getRepository')->willReturn($repository);
208
        $em->method('getClassMetadata')->willReturn($metadata);
209
210
        $registry = $this->createMock(ManagerRegistry::class);
211
        $registry->method('getManagerForClass')->willReturn($em);
212
213
        return  new MessageManager(BaseMessage::class, $registry);
214
    }
215
216
    /**
217
     * @param int $state
218
     */
219
    protected function getMessage($state = MessageInterface::STATE_OPEN): Message
220
    {
221
        $message = new Message();
222
223
        $message->setState($state);
224
225
        return $message;
226
    }
227
}
228