FavoriteManagerTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 81
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetFavoriteWithMultipleInstancesWithCookie() 0 7 1
A testSetFavoriteWithUser() 0 5 1
A setUp() 0 13 1
A testSetFavoriteWithCookie() 0 5 1
A testSetFavoriteWithMultipleInstancesWithUser() 0 7 1
A testGetFavoriteReturningNull() 0 5 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\Test\Model;
21
22
use DembeloMain\Document\Textnode;
23
use DembeloMain\Document\Topic;
24
use DembeloMain\Model\Repository\Doctrine\ODM\TextNodeRepository;
25
use DembeloMain\Document\User;
26
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
27
use DembeloMain\Model\FavoriteManager;
28
use Symfony\Component\HttpFoundation\Session\Session;
29
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
30
31
/**
32
 * Class FavoriteManagerTest
33
 */
34
class FavoriteManagerTest extends WebTestCase
35
{
36
    /* @var FavoriteManager */
37
    private $favMgr;
38
    private $topic;
39
    private $textnode;
40
    /* @var Session */
41
    private $session;
42
    private $user;
43
    private $textnodeRepository;
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function setUp()
49
    {
50
        parent::setUp(); // TODO: Change the autogenerated stub
51
        $this->topic = new Topic();
52
        $this->topic->setId('topicId');
53
        $this->textnode = new Textnode();
54
        $this->textnode->setArbitraryId('textnodeId');
55
        $this->textnode->setTopicId('topicId');
56
        $this->user = new User();
57
58
        $this->session = new Session(new MockArraySessionStorage());
59
60
        $this->favMgr = new FavoriteManager($this->session);
61
    }
62
63
    /**
64
     * tests setting of a favorite via cookie
65
     */
66
    public function testSetFavoriteWithCookie()
67
    {
68
        $this->favMgr->setFavorite($this->textnode);
69
        $this->assertEquals('textnodeId', $this->favMgr->getFavorite($this->topic));
70
        $this->assertEquals('textnodeId', $this->session->get('favorite_topicId'));
71
    }
72
73
    /**
74
     * tests setting of a favorite via user object
75
     */
76
    public function testSetFavoriteWithUser()
77
    {
78
        $this->favMgr->setFavorite($this->textnode, $this->user);
79
        $this->assertEquals('textnodeId', $this->favMgr->getFavorite($this->topic, $this->user));
80
        $this->assertNull($this->session->get('favorite_topicId'));
81
    }
82
83
    /**
84
     * tests setting of a favorite across multiple instances via user object
85
     */
86
    public function testSetFavoriteWithMultipleInstancesWithUser()
87
    {
88
        $this->favMgr->setFavorite($this->textnode, $this->user);
89
90
        $favMgr = new FavoriteManager($this->session, $this->textnodeRepository);
0 ignored issues
show
Unused Code introduced by
The call to DembeloMain\Model\FavoriteManager::__construct() has too many arguments starting with $this->textnodeRepository. ( Ignorable by Annotation )

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

90
        $favMgr = /** @scrutinizer ignore-call */ new FavoriteManager($this->session, $this->textnodeRepository);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
91
        $this->assertEquals('textnodeId', $favMgr->getFavorite($this->topic, $this->user));
92
        $this->assertNull($this->session->get('favorite_topicId'));
93
    }
94
95
    /**
96
     * tests setting of a favorite across multiple instances via cookie
97
     */
98
    public function testSetFavoriteWithMultipleInstancesWithCookie()
99
    {
100
        $this->favMgr->setFavorite($this->textnode);
101
102
        $favMgr = new FavoriteManager($this->session, $this->textnodeRepository);
0 ignored issues
show
Unused Code introduced by
The call to DembeloMain\Model\FavoriteManager::__construct() has too many arguments starting with $this->textnodeRepository. ( Ignorable by Annotation )

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

102
        $favMgr = /** @scrutinizer ignore-call */ new FavoriteManager($this->session, $this->textnodeRepository);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
103
        $this->assertEquals('textnodeId', $favMgr->getFavorite($this->topic));
104
        $this->assertEquals('textnodeId', $this->session->get('favorite_topicId'));
105
    }
106
107
    /**
108
     * tests getting a favorite via cookie when no favorite is set
109
     */
110
    public function testGetFavoriteReturningNull()
111
    {
112
        $favMgr = new FavoriteManager($this->session, $this->textnodeRepository);
0 ignored issues
show
Unused Code introduced by
The call to DembeloMain\Model\FavoriteManager::__construct() has too many arguments starting with $this->textnodeRepository. ( Ignorable by Annotation )

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

112
        $favMgr = /** @scrutinizer ignore-call */ new FavoriteManager($this->session, $this->textnodeRepository);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
113
        $this->assertNull($favMgr->getFavorite($this->topic));
114
        $this->assertNull($this->session->get('favorite_topicId'));
115
    }
116
}
117