ReadpathTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 187
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testGetCurrentTextnodeIdForSessionWithoutReadpath() 0 6 1
A testGetCurrentTextnodeIdForUserWithoutReadpath() 0 17 1
A testGetCurrentTextnodeIdForSessionWithReadpath() 0 8 1
A testGetCurrentTextnodeIdForUserWithReadpath() 0 17 1
A testGetCurrentTextnodeIdForSessionWithMultipleReadpath() 0 8 1
A getUserMock() 0 5 1
A getReadpathRepositoryMock() 0 8 1
A getTextnodeMock() 0 5 1
B testStoreReadpathWithUser() 0 31 1
A testStoreReadpathWithoutUser() 0 21 1
1
<?php
2
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace DembeloMain\Tests\Model;
21
22
use DembeloMain\Document\Textnode;
23
use DembeloMain\Document\User;
24
use DembeloMain\Model\Readpath;
25
use DembeloMain\Model\Repository\Doctrine\ODM\ReadPathRepository;
26
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
27
use DembeloMain\Document\Readpath as ReadpathDocument;
28
use Symfony\Component\HttpFoundation\Session\Session;
29
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
30
31
/**
32
 * Class ReadpathTest
33
 */
34
class ReadpathTest extends WebTestCase
35
{
36
    /* @var Session */
37
    private $session;
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function setUp()
43
    {
44
        $this->session = new Session(new MockArraySessionStorage());
45
    }
46
47
    /**
48
     * tests the saving of a readpath node without a given user
49
     */
50
    public function testStoreReadpathWithoutUser()
51
    {
52
        $textnodeMock1 = $this->getTextnodeMock();
53
        $textnodeMock1->expects($this->any())
54
            ->method('getId')
55
            ->willReturn('id1');
56
        $textnodeMock2 = $this->getTextnodeMock();
57
        $textnodeMock2->expects($this->any())
58
            ->method('getId')
59
            ->willReturn('id2');
60
        $readpathRepositoryMock = $this->getReadpathRepositoryMock();
61
        $readpathRepositoryMock->expects($this->never())
62
            ->method('save');
63
64
        $readpath = new Readpath($readpathRepositoryMock, $this->session);
65
        $readpath->storeReadpath($textnodeMock1);
66
        $this->assertContains($textnodeMock1->getId(), $this->session->get('readpath'));
0 ignored issues
show
Bug introduced by
The method getId() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        $this->assertContains($textnodeMock1->/** @scrutinizer ignore-call */ getId(), $this->session->get('readpath'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68
        $readpath->storeReadpath($textnodeMock2);
69
        $this->assertContains($textnodeMock1->getId(), $this->session->get('readpath'));
70
        $this->assertContains($textnodeMock2->getId(), $this->session->get('readpath'));
71
    }
72
73
    /**
74
     * tests the saving of readpath node
75
     */
76
    public function testStoreReadpathWithUser()
77
    {
78
        $textnodeMockId = 'textnodeId';
79
        $userMockId = 'userId';
80
81
        $textnodeMock = $this->getTextnodeMock();
82
        $userMock = $this->getUserMock();
83
        $readpathRepositoryMock = $this->getReadpathRepositoryMock();
84
85
        $textnodeMock->expects($this->any())
86
            ->method('getId')
87
            ->willReturn($textnodeMockId);
88
89
        $userMock->expects($this->any())
90
            ->method('getId')
91
            ->willReturn($userMockId);
92
93
        $readpathRepositoryMock->expects($this->once())
94
            ->method('save')
95
            ->willReturnCallback(function (ReadpathDocument $readpathDocument) use ($textnodeMockId, $userMockId) {
96
                $this->assertInstanceOf(ReadpathDocument::class, $readpathDocument);
97
                $this->assertEquals($textnodeMockId, $readpathDocument->getTextnodeId());
98
                $this->assertEquals($userMockId, $readpathDocument->getUserId());
99
                $this->assertInstanceOf(\MongoDate::class, $readpathDocument->getTimestamp());
100
                $this->assertLessThanOrEqual(1, abs($readpathDocument->getTimestamp()->sec-time()));
101
            });
102
103
        $readpath = new Readpath($readpathRepositoryMock, $this->session);
104
        $readpath->storeReadpath($textnodeMock, $userMock);
105
106
        $this->assertFalse($this->session->has('readpath'));
107
    }
108
109
    /**
110
     * tests getCurrentTextnodeId() for Session without readpath
111
     */
112
    public function testGetCurrentTextnodeIdForSessionWithoutReadpath()
113
    {
114
        $readpathRepositoryMock = $this->getReadpathRepositoryMock();
115
        $readpath = new Readpath($readpathRepositoryMock, $this->session);
116
        $returnValue = $readpath->getCurrentTextnodeId();
117
        $this->assertNull($returnValue);
118
    }
119
120
    /**
121
     * tests getCurrentTextnodeId for session with readpath
122
     */
123
    public function testGetCurrentTextnodeIdForSessionWithReadpath()
124
    {
125
        $readpathRepositoryMock = $this->getReadpathRepositoryMock();
126
        $this->session->set('readpath', ['id1']);
127
128
        $readpath = new Readpath($readpathRepositoryMock, $this->session);
129
        $returnValue = $readpath->getCurrentTextnodeId();
130
        $this->assertEquals('id1', $returnValue);
131
    }
132
133
    /**
134
     * tests getCurrentTextnodeId for session with multiple readpath
135
     */
136
    public function testGetCurrentTextnodeIdForSessionWithMultipleReadpath()
137
    {
138
        $readpathRepositoryMock = $this->getReadpathRepositoryMock();
139
        $this->session->set('readpath', ['id1', 'id2']);
140
141
        $readpath = new Readpath($readpathRepositoryMock, $this->session);
142
        $returnValue = $readpath->getCurrentTextnodeId();
143
        $this->assertEquals('id2', $returnValue);
144
    }
145
146
    /**
147
     * tests getCurrentTextnodeId for usser without readpath
148
     */
149
    public function testGetCurrentTextnodeIdForUserWithoutReadpath()
150
    {
151
        $userMockId = 'userId';
152
153
        $readpathRepositoryMock = $this->getReadpathRepositoryMock();
154
        $readpathRepositoryMock->expects($this->once())
155
            ->method('getCurrentTextnodeIdForUser')
156
            ->willReturn(null);
157
158
        $userMock = $this->getUserMock();
159
        $userMock->expects($this->any())
160
            ->method('getId')
161
            ->willReturn($userMockId);
162
163
        $readpath = new Readpath($readpathRepositoryMock, $this->session);
164
        $returnValue = $readpath->getCurrentTextnodeId($userMock);
165
        $this->assertNull($returnValue);
166
    }
167
168
    /**
169
     * tests getCurrentTextnodeId for user with readpath
170
     */
171
    public function testGetCurrentTextnodeIdForUserWithReadpath()
172
    {
173
        $userMockId = 'userId';
174
175
        $readpathRepositoryMock = $this->getReadpathRepositoryMock();
176
        $readpathRepositoryMock->expects($this->once())
177
            ->method('getCurrentTextnodeIdForUser')
178
            ->willReturn('someId');
179
180
        $userMock = $this->getUserMock();
181
        $userMock->expects($this->any())
182
            ->method('getId')
183
            ->willReturn($userMockId);
184
185
        $readpath = new Readpath($readpathRepositoryMock, $this->session);
186
        $returnValue = $readpath->getCurrentTextnodeId($userMock);
187
        $this->assertEquals('someId', $returnValue);
188
    }
189
190
    /**
191
     * @return \PHPUnit\Framework\MockObject\MockObject
192
     */
193
    private function getTextnodeMock()
194
    {
195
        $textnode = $this->getMockBuilder(Textnode::class)->getMock();
196
197
        return $textnode;
198
    }
199
200
    /**
201
     * @return \PHPUnit\Framework\MockObject\MockObject
202
     */
203
    private function getReadpathRepositoryMock()
204
    {
205
        $readpath = $this->getMockBuilder(ReadPathRepository::class)
206
            ->setMethods(['save', 'getCurrentTextnodeIdForUser'])
207
            ->disableOriginalConstructor()
208
            ->getMock();
209
210
        return $readpath;
211
    }
212
213
    /**
214
     * @return \PHPUnit\Framework\MockObject\MockObject
215
     */
216
    private function getUserMock()
217
    {
218
        $user = $this->getMockBuilder(User::class)->getMock();
219
220
        return $user;
221
    }
222
}
223