Passed
Push — master ( d9e25d...dc012b )
by Jan
05:48
created

php$0 ➔ testGetFullPath()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program 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 for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
namespace App\Tests\Entity\Base;
24
25
use App\Entity\Attachments\AttachmentType;
26
use App\Entity\Parts\Category;
27
use InvalidArgumentException;
28
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
30
/**
31
 * Test StructuralDBElement entities.
32
 * Note: Because StructuralDBElement is abstract we use AttachmentType here as a placeholder.
33
 */
34
class AbstractStructuralDBElementTest extends TestCase
35
{
36
    protected $root;
37
    protected $child1;
38
    protected $child2;
39
    protected $child3;
40
    protected $child1_1;
41
    protected $child1_2;
42
43
    protected function setUp(): void
44
    {
45
        parent::setUp(); // TODO: Change the autogenerated stub
46
47
        //Build a simple hierachy
48
        $this->root = new AttachmentType();
49
        $this->root->setName('root')->setParent(null);
50
        $this->child1 = new AttachmentType();
51
        $this->child1->setParent($this->root)->setName('child1');
52
        $this->child2 = new AttachmentType();
53
        $this->child2->setName('child2')->setParent($this->root);
54
        $this->child3 = new AttachmentType();
55
        $this->child3->setName('child3')->setParent($this->root);
56
        $this->child1_1 = new AttachmentType();
57
        $this->child1_1->setName('child1_1')->setParent($this->child1);
58
        $this->child1_2 = new AttachmentType();
59
        $this->child1_2->setName('child1_2')->setParent($this->child1);
60
    }
61
62
    public function testSetParent(): void
63
    {
64
        $el1 = new AttachmentType();
65
        $el2 = new AttachmentType();
66
67
        $el2->setParent($el1);
68
69
        //Check if parent was set correctly
70
        $this->assertSame($el1, $el2->getParent());
71
        //El2 must now be a child of el1
72
        $this->assertTrue($el1->getChildren()->contains($el2));
73
    }
74
75
    public function testAddChild(): void
76
    {
77
        $el1 = new AttachmentType();
78
        $el2 = new AttachmentType();
79
80
        $el1->addChild($el2);
81
82
        //Check if parent was set correctly
83
        $this->assertSame($el1, $el2->getParent());
84
        //El2 must now be a child of el1
85
        $this->assertTrue($el1->getChildren()->contains($el2));
86
    }
87
88
    public function testIsRoot(): void
89
    {
90
        $this->assertTrue($this->root->isRoot());
91
        $this->assertFalse($this->child1->isRoot());
92
        $this->assertFalse($this->child1_2->isRoot());
93
    }
94
95
    public function testIsChildOf(): void
96
    {
97
        //Root must not be the child of any other node
98
        $this->assertFalse($this->root->isChildOf($this->child1));
99
        $this->assertFalse($this->root->isChildOf($this->root));
100
101
        //Check for direct parents
102
        $this->assertTrue($this->child1->isChildOf($this->root));
103
        $this->assertTrue($this->child1_2->isChildOf($this->child1));
104
105
        //Check for inheritance
106
        $this->assertTrue($this->child1_2->isChildOf($this->root));
107
    }
108
109
    public function testChildOfDifferentClasses(): void
110
    {
111
        $this->expectException(InvalidArgumentException::class);
112
        $category = new Category();
113
        $this->root->isChildOf($category);
114
    }
115
116
    public function testChildOfExtendedClass(): void
117
    {
118
        //Doctrine extends the entities for proxy classes so the isChildOf mus also work for inheritance types
119
        $inheritance = new class() extends AttachmentType {
120
        };
121
        $inheritance->setParent($this->root);
122
        $this->assertTrue($inheritance->isChildOf($this->root));
123
        $this->assertFalse($this->root->isChildOf($inheritance));
124
    }
125
126
    public function testGetLevel(): void
127
    {
128
        $this->assertSame(0, $this->root->getLevel());
129
        $this->assertSame(1, $this->child1->getLevel());
130
        $this->assertSame(1, $this->child2->getLevel());
131
        $this->assertSame(2, $this->child1_2->getLevel());
132
        $this->assertSame(2, $this->child1_1->getLevel());
133
    }
134
135
    public function testGetFullPath(): void
136
    {
137
        $this->assertSame('root/child1/child1_1', $this->child1_1->getFullPath('/'));
138
        $this->assertSame('root#child2', $this->child2->getFullPath('#'));
139
    }
140
141
    public function testGetPathArray(): void
142
    {
143
        $this->assertSame([$this->root, $this->child1, $this->child1_1], $this->child1_1->getPathArray());
144
        $this->assertSame([$this->root, $this->child1], $this->child1->getPathArray());
145
        $this->assertSame([$this->root], $this->root->getPathArray());
146
    }
147
}
148