Completed
Push — master ( ba8ed9...770316 )
by Jeroen
06:11
created

NodeBundle/Tests/unit/Entity/NodeTest.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeBundle\Tests\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
7
use Kunstmaan\NodeBundle\Entity\Node;
8
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
9
use PHPUnit\Framework\TestCase;
10
11
class NodeTest extends TestCase
12
{
13
    /**
14
     * @var Node
15
     */
16
    protected $object;
17
18
    /**
19
     * Sets up the fixture, for example, opens a network connection.
20
     * This method is called before a test is executed.
21
     */
22
    protected function setUp()
23
    {
24
        $this->object = new Node();
25
    }
26
27
    public function testIsGetSetHiddenFromNav()
28
    {
29
        $this->assertFalse($this->object->getHiddenFromNav());
30
        $this->object->setHiddenFromNav(true);
31
        $this->assertTrue($this->object->isHiddenFromNav());
32
        $this->assertTrue($this->object->getHiddenFromNav());
33
    }
34
35
    public function testGetSetChildren()
36
    {
37
        $children = new ArrayCollection();
38
        $child = new Node();
39
        $children->add($child);
40
        $this->object->setChildren($children);
41
42
        $this->assertEquals(1, $this->object->getChildren()->count());
43
        $this->assertEquals($children, $this->object->getChildren());
44
        $this->assertTrue($this->object->getChildren()->contains($child));
45
    }
46
47
    public function testGetSetChildrenWithDeletedChildren()
48
    {
49
        $children = new ArrayCollection();
50
        $child = new Node();
51
        $deletedChild = new Node();
52
        $deletedChild->setDeleted(true);
53
        $children->add($child);
54
        $children->add($deletedChild);
55
        $this->object->setChildren($children);
56
57
        $this->assertEquals(1, $this->object->getChildren()->count());
58
        $this->assertTrue($this->object->getChildren()->contains($child));
59
        $this->assertFalse($this->object->getChildren()->contains($deletedChild));
60
    }
61
62
    public function testAddNode()
63
    {
64
        $child = new Node();
65
        $this->object->addNode($child);
66
        $this->assertEquals($this->object, $child->getParent());
67
        $this->assertEquals(1, $this->object->getChildren()->count());
68
    }
69
70 View Code Duplication
    public function testGetSetNodeTranslations()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $translations = new ArrayCollection();
73
        $translation = new NodeTranslation();
74
        $translations->add($translation);
75
        $this->object->setNodeTranslations($translations);
76
77
        $this->assertEquals(1, $this->object->getNodeTranslations(true)->count());
78
        $this->assertEquals($translations, $this->object->getNodeTranslations(true));
79
        $this->assertTrue($this->object->getNodeTranslations(true)->contains($translation));
80
    }
81
82 View Code Duplication
    public function testGetNodeTranslationsWithOfflineNodes()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $translation1 = new NodeTranslation();
85
        $translation1->setOnline(true);
86
        $this->object->addNodeTranslation($translation1);
87
88
        $translation2 = new NodeTranslation();
89
        $translation2->setOnline(false);
90
        $this->object->addNodeTranslation($translation2);
91
92
        $this->assertEquals(2, $this->object->getNodeTranslations(true)->count());
93
        $this->assertEquals(1, $this->object->getNodeTranslations()->count());
94
    }
95
96
    public function testGetNodeTranslation()
97
    {
98
        $translation1 = new NodeTranslation();
99
        $translation1->setLang('nl');
100
        $translation1->setOnline(true);
101
        $this->object->addNodeTranslation($translation1);
102
103
        $translation2 = new NodeTranslation();
104
        $translation2->setLang('fr');
105
        $translation2->setOnline(true);
106
        $this->object->addNodeTranslation($translation2);
107
108
        $this->assertEquals($translation1, $this->object->getNodeTranslation('nl'));
109
        $this->assertEquals($translation2, $this->object->getNodeTranslation('fr'));
110
        $this->assertNotEquals($translation1, $this->object->getNodeTranslation('fr'));
111
        $this->assertNotEquals($translation2, $this->object->getNodeTranslation('nl'));
112
        $this->assertNull($this->object->getNodeTranslation('en'));
113
    }
114
115
    public function testGetParents()
116
    {
117
        $child = new Node();
118
        $grandChild = new Node();
119
        $child->addNode($grandChild);
120
        $this->object->addNode($child);
121
        $parents = $grandChild->getParents();
122
123
        $this->assertCount(2, $parents);
124
        $this->assertEquals($child, $parents[1]);
125
        $this->assertEquals($this->object, $parents[0]);
126
    }
127
128
    public function testIsSetDeleted()
129
    {
130
        $this->assertFalse($this->object->isDeleted());
131
        $this->object->setDeleted(true);
132
        $this->assertTrue($this->object->isDeleted());
133
    }
134
135
    public function testSetRefAndGetRefEntityName()
136
    {
137
        $entity = $this->createMock(HasNodeInterface::class);
138
        $this->object->setRef($entity);
0 ignored issues
show
$entity is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\NodeBun...ntity\HasNodeInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
        $this->assertEquals(\get_class($entity), $this->object->getRefEntityName());
140
    }
141
142
    public function testSetInternalName()
143
    {
144
        $this->object->setInternalName('AnInternalName');
145
        $this->assertEquals('AnInternalName', $this->object->getInternalName());
146
    }
147
148
    public function testGetDefaultAdminType()
149
    {
150
        $this->assertEquals('Kunstmaan\NodeBundle\Form\NodeAdminType', $this->object->getDefaultAdminType());
151
    }
152
153
    public function testToString()
154
    {
155
        $entity = $this->createMock(HasNodeInterface::class);
156
        $this->object->setId(1);
157
        $this->object->setRef($entity);
158
159
        $this->assertEquals('node 1, refEntityName: '. \get_class($entity), $this->object->__toString());
160
    }
161
162
    public function testGetSetLeftRightLevel()
163
    {
164
        $mirror = new \ReflectionClass(Node::class);
165
        $property = $mirror->getProperty('lft');
166
        $property->setAccessible(true);
167
        $property->setValue($this->object, 11);
168
        $property = $mirror->getProperty('rgt');
169
        $property->setAccessible(true);
170
        $property->setValue($this->object, 12);
171
        $property = $mirror->getProperty('lvl');
172
        $property->setAccessible(true);
173
        $property->setValue($this->object, 13);
174
175
        $this->assertEquals(11, $this->object->getLeft());
176
        $this->assertEquals(12, $this->object->getRight());
177
        $this->assertEquals(13, $this->object->getLevel());
178
    }
179
}
180