Passed
Push — master ( 40feff...710ff4 )
by Michael
01:41
created

ReadPathRepositoryTest::createTextnode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/* Copyright (C) 2015 Michael Giesler, Stephan Kreutzer
4
 *
5
 * This file is part of Dembelo.
6
 *
7
 * Dembelo is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * Dembelo is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License 3 for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License 3
18
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
namespace DembeloMain\Tests\Model\Repository\Doctrine\ODM;
22
23
use DembeloMain\Document\Textnode;
24
use DembeloMain\Document\User;
25
use DembeloMain\Document\Readpath;
26
use DembeloMain\Model\Repository\Doctrine\ODM\TextNodeRepository;
27
28
/**
29
 * Class ReadPathRepositoryTest
30
 * @package DembeloMain\Tests\Model\Repository\Doctrine\ODM
31
 */
32
class ReadPathRepositoryTest extends AbstractRepositoryTest
33
{
34
    /**
35
     * @var \Doctrine\ODM\MongoDB\DocumentManager
36
     */
37
    private $em;
38
39
    /**
40
     * @var TextNodeRepository
41
     */
42
    private $repository;
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    protected function setUp(): void
48
    {
49
        self::bootKernel();
50
51
        $this->em = static::$kernel->getContainer()
52
            ->get('doctrine_mongodb')
53
            ->getManager();
54
55
        $collection = $this->em->getDocumentCollection(Readpath::class);
56
        $collection->remove(array());
57
        $collection = $this->em->getDocumentCollection(User::class);
58
        $collection->remove(array());
59
        $collection = $this->em->getDocumentCollection(Textnode::class);
60
        $collection->remove(array());
61
62
        $this->repository = $this->em->getRepository('DembeloMain:Readpath');
63
    }
64
65
    /**
66
     * tests getCurrentTextnodeId for user returns null when readpath is empty
67
     */
68
    public function testGetCurrentTextnodeIdForUserReturnsNullWhenReadpathIsEmpty(): void
69
    {
70
        $user = $this->createUser();
71
        $returnValue = $this->repository->getCurrentTextnodeIdForUser($user);
0 ignored issues
show
Bug introduced by
The method getCurrentTextnodeIdForUser() does not exist on DembeloMain\Model\Reposi...\ODM\TextNodeRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

71
        $returnValue = $this->repository->/** @scrutinizer ignore-call */ getCurrentTextnodeIdForUser($user);
Loading history...
72
73
        $this->assertNull($returnValue);
74
    }
75
76
    /**
77
     * tests getCurrentTextnodeId for user returning textnodeId
78
     */
79
    public function testGetCurrentTextnodeIdForUserReturnsTextnodeId(): void
80
    {
81
        $user1 = $this->createUser();
82
        $user2 = $this->createUser();
83
84
        $textnode1 = $this->createTextnode('a');
85
        $readpath1 = new Readpath();
86
        $readpath1->setUserId($user1->getId());
87
        $readpath1->setTextnodeId($textnode1->getId());
88
        $readpath1->setTimestamp(new \MongoDate(1000000));
89
        $this->repository->save($readpath1);
90
91
        $textnode2 = $this->createTextnode('b');
92
        $readpath2 = new Readpath();
93
        $readpath2->setUserId($user1->getId());
94
        $readpath2->setTextnodeId($textnode2->getId());
95
        $readpath2->setTimestamp(new \MongoDate(1000001));
96
        $this->repository->save($readpath2);
97
98
        $textnode3 = $this->createTextnode('c');
99
        $readpath3 = new Readpath();
100
        $readpath3->setUserId($user2->getId());
101
        $readpath3->setTextnodeId($textnode3->getId());
102
        $readpath3->setTimestamp(new \MongoDate(1000002));
103
        $this->repository->save($readpath3);
104
105
        $returnValue = $this->repository->getCurrentTextnodeIdForUser($user1);
106
107
        $this->assertEquals($textnode2->getId(), $returnValue);
108
    }
109
110
    private function createUser(): User
111
    {
112
        $user = new User();
113
        $this->em->getRepository('DembeloMain:User')->save($user);
114
115
        return $user;
116
    }
117
118
    private function createTextnode($arbitraryId): Textnode
119
    {
120
        $textnode = new Textnode();
121
        $textnode->setArbitraryId($arbitraryId);
122
        $this->em->getRepository('DembeloMain:Textnode')->save($textnode);
123
124
        return $textnode;
125
    }
126
}
127