Completed
Push — master ( 1bdade...d7c1bf )
by Jordi Sala
12s
created

PostManagerTest::testFindOneByPermalinkException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NewsBundle\Tests\Entity;
13
14
use Sonata\CoreBundle\Test\EntityManagerMockFactory;
15
use Sonata\NewsBundle\Entity\PostManager;
16
use Sonata\NewsBundle\Tests\PHPUnit_Framework_TestCase;
17
18
/**
19
 * Tests the post manager entity.
20
 */
21
class PostManagerTest extends PHPUnit_Framework_TestCase
22
{
23
    public function assertRelationsEnabled($qb)
24
    {
25
        $qb
26
            ->expects($this->exactly(2))
27
            ->method('leftJoin')
28
            ->with(
29
                $this->logicalOr(
30
                    $this->equalTo('p.tags'),
31
                    $this->equalTo('p.author')
32
                ),
33
                $this->logicalOr(
34
                    $this->equalTo('t'),
35
                    $this->equalTo('a')
36
                ),
37
                'WITH',
38
                $this->stringEndsWith('.enabled = true')
39
            )
40
            ->will($this->returnValue($qb))
41
        ;
42
    }
43
44
    public function assertRelationsJoined($qb)
45
    {
46
        $qb
47
            ->expects($this->exactly(2))
48
            ->method('leftJoin')
49
            ->with(
50
                $this->logicalOr(
51
                    $this->equalTo('p.tags'),
52
                    $this->equalTo('p.author')
53
                ),
54
                $this->logicalOr(
55
                    $this->equalTo('t'),
56
                    $this->equalTo('a')
57
                ),
58
                $this->isNull(),
59
                $this->isNull()
60
            )
61
            ->will($this->returnValue($qb))
62
        ;
63
    }
64
65
    public function assertPostEnabled($qb, $flag)
66
    {
67
        $qb->expects($this->once())->method('andWhere')->with($this->equalTo('p.enabled = :enabled'));
68
        $qb->expects($this->once())->method('setParameters')->with($this->equalTo(array('enabled' => $flag)));
69
    }
70
71
    public function testFindOneByPermalinkSlug()
72
    {
73
        $permalink = $this->getMock('Sonata\NewsBundle\Permalink\PermalinkInterface');
74
        $permalink->expects($this->once())->method('getParameters')
75
            ->with($this->equalTo('foo/bar'))
76
            ->will($this->returnValue(array(
77
                'slug' => 'bar',
78
            )));
79
80
        $blog = $this->getMock('Sonata\NewsBundle\Model\BlogInterface');
81
        $blog->expects($this->once())->method('getPermalinkGenerator')->will($this->returnValue($permalink));
82
83
        $self = $this;
84
        $this
85
            ->getPostManager(function ($qb) use ($self) {
86
                $qb->expects($self->once())->method('andWhere')->with($self->equalTo('p.slug = :slug'));
87
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo(array('slug' => 'bar')));
88
            })
89
            ->findOneByPermalink('foo/bar', $blog);
90
    }
91
92
    public function testFindOneByPermalinkException()
93
    {
94
        $permalink = $this->getMock('Sonata\NewsBundle\Permalink\PermalinkInterface');
95
        $permalink->expects($this->once())->method('getParameters')
96
            ->with($this->equalTo(''))
97
            ->willThrowException(new \InvalidArgumentException());
98
99
        $blog = $this->getMock('Sonata\NewsBundle\Model\BlogInterface');
100
        $blog->expects($this->once())->method('getPermalinkGenerator')->will($this->returnValue($permalink));
101
102
        $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...
103
        $result = $this
104
            ->getPostManager(function ($qb) {
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...
105
            })
106
            ->findOneByPermalink('', $blog);
107
108
        $this->assertNull($result);
109
    }
110
111
    public function testGetPagerWithoutMode()
112
    {
113
        $self = $this;
114
        $this
115
            ->getPostManager(function ($qb) use ($self) {
116
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(array('p')));
117
                $self->assertRelationsEnabled($qb);
118
                $self->assertPostEnabled($qb, 1);
119
            })
120
            ->getPager(array(), 1);
121
    }
122
123
    public function testGetPagerWithoutModeEnabled()
124
    {
125
        $self = $this;
126
        $this
127
            ->getPostManager(function ($qb) use ($self) {
128
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(array('p')));
129
                $self->assertRelationsEnabled($qb);
130
                $self->assertPostEnabled($qb, 1);
131
            })
132
            ->getPager(array(), 1);
133
    }
134
135
    public function testGetPagerWithoutModeDisabled()
136
    {
137
        $self = $this;
138
        $this
139
            ->getPostManager(function ($qb) use ($self) {
140
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(array('p')));
141
                $self->assertRelationsEnabled($qb);
142
                $self->assertPostEnabled($qb, 0);
143
            })
144
            ->getPager(array(
145
                'enabled' => 0,
146
            ), 1);
147
    }
148
149
    public function testGetPagerWithPublicMode()
150
    {
151
        $self = $this;
152
        $this
153
            ->getPostManager(function ($qb) use ($self) {
154
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(array('p')));
155
                $self->assertRelationsEnabled($qb);
156
                $self->assertPostEnabled($qb, 1);
157
            })
158
            ->getPager(array(
159
                'mode' => 'public',
160
            ), 1);
161
    }
162
163
    public function testGetPagerWithPublicModeEnabled()
164
    {
165
        $self = $this;
166
        $this
167
            ->getPostManager(function ($qb) use ($self) {
168
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(array('p')));
169
                $self->assertRelationsEnabled($qb);
170
                $self->assertPostEnabled($qb, 1);
171
            })
172
            ->getPager(array(
173
                'mode' => 'public',
174
                'enabled' => 1,
175
            ), 1);
176
    }
177
178
    public function testGetPagerWithPublicModeDisabled()
179
    {
180
        $self = $this;
181
        $this
182
            ->getPostManager(function ($qb) use ($self) {
183
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(array('p')));
184
                $self->assertRelationsEnabled($qb);
185
                $self->assertPostEnabled($qb, 0);
186
            })
187
            ->getPager(array(
188
                'mode' => 'public',
189
                'enabled' => 0,
190
            ), 1);
191
    }
192
193
    public function testGetPagerWithAdminMode()
194
    {
195
        $self = $this;
196
        $this
197
            ->getPostManager(function ($qb) use ($self) {
198
                $self->assertRelationsJoined($qb);
199
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(array('p')));
200
                $qb->expects($self->never())->method('andWhere');
201
                $qb->expects($self->once())->method('setParameters')->with($self->equalTo(array()));
202
            })
203
            ->getPager(array(
204
                'mode' => 'admin',
205
            ), 1);
206
    }
207
208
    public function testGetPagerWithAdminModeEnabled()
209
    {
210
        $self = $this;
211
        $this
212
            ->getPostManager(function ($qb) use ($self) {
213
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(array('p')));
214
                $self->assertRelationsJoined($qb);
215
                $self->assertPostEnabled($qb, 1);
216
            })
217
            ->getPager(array(
218
                'mode' => 'admin',
219
                'enabled' => 1,
220
            ), 1);
221
    }
222
223
    public function testGetPagerWithAdminModeDisabled()
224
    {
225
        $self = $this;
226
        $this
227
            ->getPostManager(function ($qb) use ($self) {
228
                $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(array('p')));
229
                $self->assertRelationsJoined($qb);
230
                $self->assertPostEnabled($qb, 0);
231
            })
232
            ->getPager(array(
233
                'mode' => 'admin',
234
                'enabled' => 0,
235
            ), 1);
236
    }
237
238
    public function testGetPublicationDateQueryParts()
239
    {
240
        $result = $this
241
            ->getPostManager(function () {
242
            })
243
            ->getPublicationDateQueryParts('2010-02-10', 'month', 'n');
244
245
        $this->assertNotNull($result);
246
        $this->assertEquals(new \DateTime('2010-02-10'), $result['params']['startDate']);
247
        $this->assertEquals(new \DateTime('2010-03-10'), $result['params']['endDate']);
248
    }
249
250
    protected function getPostManager($qbCallback)
251
    {
252
        $em = EntityManagerMockFactory::create($this, $qbCallback, array());
253
254
        $registry = $this->createMock('Doctrine\Common\Persistence\ManagerRegistry');
255
        $registry->expects($this->any())->method('getManagerForClass')->will($this->returnValue($em));
256
257
        return new PostManager('Sonata\NewsBundle\Entity\BasePost', $registry);
258
    }
259
}
260