testGetPublicationDateQueryParts()   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 Doctrine\Common\Persistence\ManagerRegistry;
17
use PHPUnit\Framework\TestCase;
18
use Sonata\Doctrine\Test\EntityManagerMockFactoryTrait;
19
use Sonata\NewsBundle\Entity\BasePost;
20
use Sonata\NewsBundle\Entity\PostManager;
21
use Sonata\NewsBundle\Model\BlogInterface;
22
use Sonata\NewsBundle\Permalink\PermalinkInterface;
23
24
class PostManagerTest 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...
25
{
26
    use EntityManagerMockFactoryTrait;
27
28
    public function assertRelationsEnabled($qb): void
29
    {
30
        $qb
31
            ->expects($this->exactly(2))
32
            ->method('leftJoin')
33
            ->with(
34
                $this->logicalOr(
35
                    $this->equalTo('p.tags'),
36
                    $this->equalTo('p.author')
37
                ),
38
                $this->logicalOr(
39
                    $this->equalTo('t'),
40
                    $this->equalTo('a')
41
                ),
42
                'WITH',
43
                $this->stringEndsWith('.enabled = true')
44
            )
45
            ->willReturn($qb)
46
        ;
47
    }
48
49
    public function assertRelationsJoined($qb): void
50
    {
51
        $qb
52
            ->expects($this->exactly(2))
53
            ->method('leftJoin')
54
            ->with(
55
                $this->logicalOr(
56
                    $this->equalTo('p.tags'),
57
                    $this->equalTo('p.author')
58
                ),
59
                $this->logicalOr(
60
                    $this->equalTo('t'),
61
                    $this->equalTo('a')
62
                ),
63
                $this->isNull(),
64
                $this->isNull()
65
            )
66
            ->willReturn($qb)
67
        ;
68
    }
69
70
    public function assertPostEnabled($qb, $flag): void
71
    {
72
        $qb->expects($this->once())->method('andWhere')->with($this->equalTo('p.enabled = :enabled'));
73
        $qb->expects($this->once())->method('setParameters')->with($this->equalTo(['enabled' => $flag]));
74
    }
75
76
    public function testFindOneByPermalinkSlug(): void
77
    {
78
        $permalink = $this->createMock(PermalinkInterface::class);
79
        $permalink->expects($this->once())->method('getParameters')
80
            ->with($this->equalTo('foo/bar'))
81
            ->willReturn([
82
                'slug' => 'bar',
83
            ]);
84
85
        $blog = $this->createMock(BlogInterface::class);
86
        $blog->expects($this->once())->method('getPermalinkGenerator')->willReturn($permalink);
87
88
        $self = $this;
89
        $this
90
            ->getPostManager(static function ($qb) use ($self): void {
91
                $qb->expects($self->once())->method('andWhere')->with($self->equalTo('p.slug = :slug'));
92
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo(['slug' => 'bar']));
93
            })
94
            ->findOneByPermalink('foo/bar', $blog);
95
    }
96
97
    public function testFindOneByPermalinkException(): void
98
    {
99
        $permalink = $this->createMock(PermalinkInterface::class);
100
        $permalink->expects($this->once())->method('getParameters')
101
            ->with($this->equalTo(''))
102
            ->willThrowException(new \InvalidArgumentException());
103
104
        $blog = $this->createMock(BlogInterface::class);
105
        $blog->expects($this->once())->method('getPermalinkGenerator')->willReturn($permalink);
106
107
        $self = $this;
0 ignored issues
show
Unused Code introduced by
$self is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
108
        $result = $this
109
            ->getPostManager(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...
110
            })
111
            ->findOneByPermalink('', $blog);
112
113
        $this->assertNull($result);
114
    }
115
116
    public function testGetPagerWithoutMode(): void
117
    {
118
        $self = $this;
119
        $this
120
            ->getPostManager(static function ($qb) use ($self): void {
121
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p']));
122
                $self->assertRelationsEnabled($qb);
123
                $self->assertPostEnabled($qb, 1);
124
            })
125
            ->getPager([], 1);
126
    }
127
128
    public function testGetPagerWithoutModeEnabled(): void
129
    {
130
        $self = $this;
131
        $this
132
            ->getPostManager(static function ($qb) use ($self): void {
133
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p']));
134
                $self->assertRelationsEnabled($qb);
135
                $self->assertPostEnabled($qb, 1);
136
            })
137
            ->getPager([], 1);
138
    }
139
140
    public function testGetPagerWithoutModeDisabled(): void
141
    {
142
        $self = $this;
143
        $this
144
            ->getPostManager(static function ($qb) use ($self): void {
145
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p']));
146
                $self->assertRelationsEnabled($qb);
147
                $self->assertPostEnabled($qb, 0);
148
            })
149
            ->getPager([
150
                'enabled' => 0,
151
            ], 1);
152
    }
153
154
    public function testGetPagerWithPublicMode(): void
155
    {
156
        $self = $this;
157
        $this
158
            ->getPostManager(static function ($qb) use ($self): void {
159
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p']));
160
                $self->assertRelationsEnabled($qb);
161
                $self->assertPostEnabled($qb, 1);
162
            })
163
            ->getPager([
164
                'mode' => 'public',
165
            ], 1);
166
    }
167
168
    public function testGetPagerWithPublicModeEnabled(): void
169
    {
170
        $self = $this;
171
        $this
172
            ->getPostManager(static function ($qb) use ($self): void {
173
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p']));
174
                $self->assertRelationsEnabled($qb);
175
                $self->assertPostEnabled($qb, 1);
176
            })
177
            ->getPager([
178
                'mode' => 'public',
179
                'enabled' => 1,
180
            ], 1);
181
    }
182
183
    public function testGetPagerWithPublicModeDisabled(): void
184
    {
185
        $self = $this;
186
        $this
187
            ->getPostManager(static function ($qb) use ($self): void {
188
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p']));
189
                $self->assertRelationsEnabled($qb);
190
                $self->assertPostEnabled($qb, 0);
191
            })
192
            ->getPager([
193
                'mode' => 'public',
194
                'enabled' => 0,
195
            ], 1);
196
    }
197
198
    public function testGetPagerWithAdminMode(): void
199
    {
200
        $self = $this;
201
        $this
202
            ->getPostManager(static function ($qb) use ($self): void {
203
                $self->assertRelationsJoined($qb);
204
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p']));
205
                $qb->expects($self->never())->method('andWhere');
206
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo([]));
207
            })
208
            ->getPager([
209
                'mode' => 'admin',
210
            ], 1);
211
    }
212
213
    public function testGetPagerWithAdminModeEnabled(): void
214
    {
215
        $self = $this;
216
        $this
217
            ->getPostManager(static function ($qb) use ($self): void {
218
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p']));
219
                $self->assertRelationsJoined($qb);
220
                $self->assertPostEnabled($qb, 1);
221
            })
222
            ->getPager([
223
                'mode' => 'admin',
224
                'enabled' => 1,
225
            ], 1);
226
    }
227
228
    public function testGetPagerWithAdminModeDisabled(): void
229
    {
230
        $self = $this;
231
        $this
232
            ->getPostManager(static function ($qb) use ($self): void {
233
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['p']));
234
                $self->assertRelationsJoined($qb);
235
                $self->assertPostEnabled($qb, 0);
236
            })
237
            ->getPager([
238
                'mode' => 'admin',
239
                'enabled' => 0,
240
            ], 1);
241
    }
242
243
    public function testGetPublicationDateQueryParts(): void
244
    {
245
        $result = $this
246
            ->getPostManager(static function (): void {
247
            })
248
            ->getPublicationDateQueryParts('2010-02-10', 'month', 'n');
249
250
        $this->assertNotNull($result);
251
        $this->assertInstanceOf(\DateTimeInterface::class, $result['params']['startDate']);
252
        $this->assertInstanceOf(\DateTimeInterface::class, $result['params']['endDate']);
253
        $this->assertSame('2010-02-10', $result['params']['startDate']->format('Y-m-d'));
254
        $this->assertSame('2010-03-10', $result['params']['endDate']->format('Y-m-d'));
255
    }
256
257
    protected function getPostManager($qbCallback)
258
    {
259
        $em = $this->createEntityManagerMock($qbCallback, []);
260
261
        $registry = $this->createMock(ManagerRegistry::class);
262
        $registry->expects($this->any())->method('getManagerForClass')->willReturn($em);
263
264
        return new PostManager(BasePost::class, $registry);
265
    }
266
}
267