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

testGetNewEntityMixed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
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\Repository;
24
25
use App\Entity\Attachments\AttachmentType;
26
use App\Helpers\Trees\TreeViewNode;
27
use App\Repository\StructuralDBElementRepository;
28
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
29
30
/**
31
 * @Group DB
32
 */
33
class StructuralDBElementRepositoryTest extends WebTestCase
34
{
35
    private $entityManager;
36
    /**
37
     * @var StructuralDBElementRepository
38
     */
39
    private $repo;
40
41
    protected function setUp(): void
42
    {
43
        $kernel = self::bootKernel();
44
45
        $this->entityManager = $kernel->getContainer()
46
            ->get('doctrine')
47
            ->getManager();
48
49
        $this->repo = $this->entityManager->getRepository(AttachmentType::class);
50
    }
51
52
    public function testFindRootNodes(): void
53
    {
54
        $root_nodes = $this->repo->findRootNodes();
55
        $this->assertCount(3, $root_nodes);
56
        $this->assertContainsOnlyInstancesOf(AttachmentType::class, $root_nodes);
57
58
        //Asc sorting
59
        $this->assertSame('Node 1', $root_nodes[0]->getName());
60
        $this->assertSame('Node 2', $root_nodes[1]->getName());
61
        $this->assertSame('Node 3', $root_nodes[2]->getName());
62
    }
63
64
    public function testGetGenericTree(): void
65
    {
66
        $tree = $this->repo->getGenericNodeTree();
67
        $this->assertIsArray($tree);
68
        $this->assertContainsOnlyInstancesOf(TreeViewNode::class, $tree);
69
70
        $this->assertCount(3, $tree);
71
        $this->assertCount(2, $tree[0]->getNodes());
72
        $this->assertCount(1, $tree[0]->getNodes()[0]->getNodes());
73
        $this->assertEmpty($tree[2]->getNodes());
74
        $this->assertEmpty($tree[1]->getNodes()[0]->getNodes());
75
76
        //Check text
77
        $this->assertSame('Node 1', $tree[0]->getText());
78
        $this->assertSame('Node 2', $tree[1]->getText());
79
        $this->assertSame('Node 3', $tree[2]->getText());
80
        $this->assertSame('Node 1.1', $tree[0]->getNodes()[0]->getText());
81
        $this->assertSame('Node 1.1.1', $tree[0]->getNodes()[0]->getNodes()[0]->getText());
82
83
        //Check that IDs were set correctly
84
        $this->assertSame(1, $tree[0]->getId());
85
        $this->assertSame(2, $tree[1]->getId());
86
        $this->assertSame(7, $tree[0]->getNodes()[0]->getNodes()[0]->getId());
87
    }
88
89
    /**
90
     * Test $repo->toNodesList() for null as parameter.
91
     */
92
    public function testToNodesListRoot(): void
93
    {
94
        //List all root nodes and their children
95
        $nodes = $this->repo->toNodesList();
96
97
        $this->assertCount(7, $nodes);
98
        $this->assertContainsOnlyInstancesOf(AttachmentType::class, $nodes);
99
        $this->assertSame('Node 1', $nodes[0]->getName());
100
        $this->assertSame('Node 1.1', $nodes[1]->getName());
101
        $this->assertSame('Node 1.1.1', $nodes[2]->getName());
102
        $this->assertSame('Node 1.2', $nodes[3]->getName());
103
        $this->assertSame('Node 2', $nodes[4]->getName());
104
        $this->assertSame('Node 2.1', $nodes[5]->getName());
105
        $this->assertSame('Node 3', $nodes[6]->getName());
106
    }
107
108
    public function testToNodesListElement(): void
109
    {
110
        //List all nodes that are children to Node 1
111
        $node1 = $this->repo->find(1);
112
        $nodes = $this->repo->toNodesList($node1);
113
114
        $this->assertCount(3, $nodes);
115
        $this->assertContainsOnlyInstancesOf(AttachmentType::class, $nodes);
116
        $this->assertSame('Node 1.1', $nodes[0]->getName());
117
        $this->assertSame('Node 1.1.1', $nodes[1]->getName());
118
        $this->assertSame('Node 1.2', $nodes[2]->getName());
119
    }
120
121
    public function testGetNewEntityFromDB(): void
122
    {
123
        $path = 'Node 1/ Node 1.1 /Node 1.1.1';
124
125
        $nodes = $this->repo->getNewEntityFromPath($path, '/');
126
127
        $this->assertSame('Node 1', $nodes[0]->getName());
128
        //Node must be from DB
129
        $this->assertNotNull( $nodes[0]->getID());
130
131
        $this->assertSame('Node 1.1', $nodes[1]->getName());
132
        //Node must be from DB
133
        $this->assertNotNull( $nodes[1]->getID());
134
135
        $this->assertSame('Node 1.1.1', $nodes[2]->getName());
136
        //Node must be from DB
137
        $this->assertNotNull( $nodes[2]->getID());
138
    }
139
140
    public function testGetNewEntityNew(): void
141
    {
142
        $path = 'Element 1-> Element 1.1 ->Element 1.1.1';
143
144
        $nodes = $this->repo->getNewEntityFromPath($path);
145
146
        $this->assertSame('Element 1', $nodes[0]->getName());
147
        //Node must not be from DB
148
        $this->assertNull( $nodes[0]->getID());
149
150
        $this->assertSame('Element 1.1', $nodes[1]->getName());
151
        //Node must not be from DB
152
        $this->assertNull( $nodes[1]->getID());
153
154
        $this->assertSame('Element 1.1.1', $nodes[2]->getName());
155
        //Node must not be from DB
156
        $this->assertNull( $nodes[2]->getID());
157
    }
158
159
    public function testGetNewEntityMixed(): void
160
    {
161
        $path = 'Node 1-> Node 1.1 -> New Node';
162
163
        $nodes = $this->repo->getNewEntityFromPath($path);
164
165
        $this->assertSame('Node 1', $nodes[0]->getName());
166
        //Node must be from DB
167
        $this->assertNotNull( $nodes[0]->getID());
168
169
        $this->assertSame('Node 1.1', $nodes[1]->getName());
170
        //Node must be from DB
171
        $this->assertNotNull( $nodes[1]->getID());
172
173
        $this->assertSame('New Node', $nodes[2]->getName());
174
        //Node must not be from DB
175
        $this->assertNull( $nodes[2]->getID());
176
    }
177
}
178