Completed
Pull Request — master (#362)
by
unknown
02:29
created

MessageManagerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 3
dl 0
loc 213
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testCancel() 0 10 1
A testRestart() 0 17 1
A testGetPager() 0 15 1
A testGetPagerWithInvalidSort() 0 11 1
A testGetPagerWithMultipleSort() 0 25 1
A testGetPagerWithOpenedMessages() 0 13 1
A testGetPagerWithCanceledMessages() 0 13 1
A testGetPagerWithInProgressMessages() 0 13 1
A testFindByTypes() 0 15 1
A getMessageManagerMock() 0 8 1
A getMessageManager() 0 40 1
A getMessage() 0 8 1
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->assertEquals(MessageInterface::STATE_OPEN, $newMessage->getState());
55
        $this->assertEquals(13, $newMessage->getRestartCount());
56
    }
57
58
    public function testGetPager(): void
59
    {
60
        $self = $this;
61
        $this
62
            ->getMessageManager(function ($qb) use ($self): void {
63
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['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
        $self = $this;
80
        $this
81
            ->getMessageManager(function ($qb) use ($self): 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...
82
            })
83
            ->getPager([], 1, 10, ['invalid' => 'ASC']);
84
    }
85
86
    public function testGetPagerWithMultipleSort(): void
87
    {
88
        $self = $this;
89
        $this
90
            ->getMessageManager(function ($qb) use ($self): void {
91
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m']));
92
                $qb->expects($self->never())->method('andWhere');
93
                $qb->expects($self->once())->method('setParameters')->with([]);
94
                $qb->expects($self->exactly(2))->method('orderBy')->with(
95
                    $self->logicalOr(
96
                        $self->equalTo('m.type'),
97
                        $self->equalTo('m.state')
98
                    ),
99
                    $self->logicalOr(
100
                        $self->equalTo('ASC'),
101
                        $self->equalTo('DESC')
102
                    )
103
                );
104
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([]));
105
            })
106
            ->getPager([], 1, 10, [
107
                'type' => 'ASC',
108
                'state' => 'DESC',
109
            ]);
110
    }
111
112
    public function testGetPagerWithOpenedMessages(): void
113
    {
114
        $self = $this;
115
        $this
116
            ->getMessageManager(function ($qb) use ($self): void {
117
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m']));
118
                $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state'));
119
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([
120
                    'state' => MessageInterface::STATE_OPEN,
121
                ]));
122
            })
123
            ->getPager(['state' => MessageInterface::STATE_OPEN], 1);
124
    }
125
126
    public function testGetPagerWithCanceledMessages(): void
127
    {
128
        $self = $this;
129
        $this
130
            ->getMessageManager(function ($qb) use ($self): void {
131
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m']));
132
                $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state'));
133
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([
134
                    'state' => MessageInterface::STATE_CANCELLED,
135
                ]));
136
            })
137
            ->getPager(['state' => MessageInterface::STATE_CANCELLED], 1);
138
    }
139
140
    public function testGetPagerWithInProgressMessages(): void
141
    {
142
        $self = $this;
143
        $this
144
            ->getMessageManager(function ($qb) use ($self): void {
145
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m']));
146
                $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state'));
147
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([
148
                    'state' => MessageInterface::STATE_IN_PROGRESS,
149
                ]));
150
            })
151
            ->getPager(['state' => MessageInterface::STATE_IN_PROGRESS], 1);
152
    }
153
154
    public function testFindByTypes(): void
155
    {
156
        $self = $this;
157
        $this
158
            ->getMessageManager(function ($qb) use ($self): void {
159
                $qb->expects($self->never())->method('andWhere');
160
                $qb->expects($self->once())->method('where')->willReturnSelf();
161
                $qb->expects($self->once())->method('setParameters')->with(['state' => MessageInterface::STATE_OPEN]);
162
                $qb->expects($self->once())->method('orderBy')->with(
163
                    $self->equalTo('m.createdAt'),
164
                    $self->equalTo('DESC')
165
                )->willReturnSelf();
166
            })
167
            ->findByTypes([], MessageInterface::STATE_OPEN, 10);
168
    }
169
170
    /**
171
     * @return MessageManagerMock
172
     */
173
    protected function getMessageManagerMock()
174
    {
175
        $registry = $this->createMock(ManagerRegistry::class);
176
177
        $manager = new MessageManagerMock(Message::class, $registry);
178
179
        return $manager;
180
    }
181
182
    /**
183
     * @return MessageManager
184
     */
185
    protected function getMessageManager($qbCallback)
186
    {
187
        $query = $this->getMockForAbstractClass(
188
            AbstractQuery::class,
189
            [],
190
            '',
191
            false,
192
            true,
193
            true,
194
            ['execute']
195
        );
196
        $query->expects($this->any())->method('execute')->will($this->returnValue(true));
197
198
        $qb = $this->getMockBuilder(QueryBuilder::class)
199
            ->setConstructorArgs([$this->createMock(EntityManager::class)])
200
            ->getMock();
201
202
        $qb->expects($this->any())->method('select')->will($this->returnValue($qb));
203
        $qb->expects($this->any())->method('getQuery')->will($this->returnValue($query));
204
205
        $qbCallback($qb);
206
207
        $repository = $this->createMock(EntityRepository::class);
208
        $repository->expects($this->any())->method('createQueryBuilder')->will($this->returnValue($qb));
209
210
        $metadata = $this->createMock(ClassMetadata::class);
211
        $metadata->expects($this->any())->method('getFieldNames')->will($this->returnValue([
212
            'state',
213
            'type',
214
        ]));
215
216
        $em = $this->createMock(EntityManager::class);
217
        $em->expects($this->any())->method('getRepository')->will($this->returnValue($repository));
218
        $em->expects($this->any())->method('getClassMetadata')->will($this->returnValue($metadata));
219
220
        $registry = $this->createMock(ManagerRegistry::class);
221
        $registry->expects($this->any())->method('getManagerForClass')->will($this->returnValue($em));
222
223
        return  new MessageManager(BaseMessage::class, $registry);
224
    }
225
226
    /**
227
     * @param int $state
228
     *
229
     * @return Message
230
     */
231
    protected function getMessage($state = MessageInterface::STATE_OPEN)
232
    {
233
        $message = new Message();
234
235
        $message->setState($state);
236
237
        return $message;
238
    }
239
}
240