Passed
Push — master ( f2239e...07f95b )
by Jan
05:19
created

EntityImporterTest::testMassCreationNested()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 35
nc 1
nop 0
dl 0
loc 44
rs 9.36
c 0
b 0
f 0
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\Services\ImportExportSystem;
24
25
use App\Entity\Attachments\AttachmentType;
26
use App\Entity\UserSystem\User;
27
use App\Services\Formatters\AmountFormatter;
28
use App\Services\ImportExportSystem\EntityImporter;
29
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
30
31
/**
32
 * @group DB
33
 */
34
class EntityImporterTest extends WebTestCase
35
{
36
    /**
37
     * @var AmountFormatter
38
     */
39
    protected $service;
40
41
    protected function setUp(): void
42
    {
43
        parent::setUp();
44
45
        //Get an service instance.
46
        self::bootKernel();
47
        $this->service = self::getContainer()->get(EntityImporter::class);
48
    }
49
50
    public function testMassCreationResults(): void
51
    {
52
        $errors = [];
53
        $results = $this->service->massCreation('', AttachmentType::class, null, $errors);
0 ignored issues
show
Bug introduced by
The method massCreation() does not exist on App\Services\Formatters\AmountFormatter. ( Ignorable by Annotation )

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

53
        /** @scrutinizer ignore-call */ 
54
        $results = $this->service->massCreation('', AttachmentType::class, null, $errors);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
        $this->assertEmpty($results);
55
        $this->assertEmpty($errors);
56
57
        $errors = [];
58
        $lines = "Test 1\nTest 2   \nTest 3";
59
        $results = $this->service->massCreation($lines, AttachmentType::class, null, $errors);
60
        $this->assertCount(0, $errors);
61
        $this->assertCount(3, $results);
62
        //Check type
63
        $this->assertInstanceOf(AttachmentType::class, $results[0]);
64
        //Check names
65
        $this->assertSame('Test 1', $results[0]->getName());
66
        $this->assertSame('Test 2', $results[1]->getName());
67
        //Check parent
68
        $this->assertNull($results[0]->getMasterPictureAttachment());
69
70
        $parent = new AttachmentType();
71
        $results = $this->service->massCreation($lines, AttachmentType::class, $parent, $errors);
72
        $this->assertCount(3, $results);
73
        $this->assertSame($parent, $results[0]->getParent());
74
    }
75
76
    public function testNonStructuralClass(): void
77
    {
78
        $input = <<<EOT
79
Test1
80
   Test1.1
81
Test2
82
EOT;
83
84
        $errors = [];
85
        $results = $this->service->massCreation($input, User::class, null, $errors);
86
87
        //Import must not fail, even with non-structural classes
88
        $this->assertCount(3, $results);
89
        $this->assertCount(0, $errors);
90
91
        $this->assertSame('Test1', $results[0]->getName());
92
        $this->assertSame('Test1.1', $results[1]->getName());
93
        $this->assertSame('Test2', $results[2]->getName());
94
95
    }
96
97
    public function testMassCreationNested(): void
98
    {
99
        $input = <<<EOT
100
Test 1
101
   Test 1.1
102
    Test 1.1.1
103
    Test 1.1.2
104
   Test 1.2
105
      Test 1.2.1
106
Test 2
107
EOT;
108
109
        $errors = [];
110
        $parent = new AttachmentType();
111
        $results = $this->service->massCreation($input, AttachmentType::class, $parent, $errors);
112
113
        //We have 7 elements, an now errros
114
        $this->assertCount(0, $errors);
115
        $this->assertCount(7, $results);
116
117
        $element1 = $results[0];
118
        $element11 = $results[1];
119
        $element111 = $results[2];
120
        $element112 = $results[3];
121
        $element12 = $results[4];
122
        $element121 = $results[5];
123
        $element2 = $results[6];
124
125
        $this->assertSame('Test 1', $element1->getName());
126
        $this->assertSame('Test 1.1', $element11->getName());
127
        $this->assertSame('Test 1.1.1', $element111->getName());
128
        $this->assertSame('Test 1.1.2', $element112->getName());
129
        $this->assertSame('Test 1.2', $element12->getName());
130
        $this->assertSame('Test 1.2.1', $element121->getName());
131
        $this->assertSame('Test 2', $element2->getName());
132
133
        //Check parents
134
        $this->assertSame($parent, $element1->getParent());
135
        $this->assertSame($element1, $element11->getParent());
136
        $this->assertSame($element11, $element111->getParent());
137
        $this->assertSame($element11, $element112->getParent());
138
        $this->assertSame($element1, $element12->getParent());
139
        $this->assertSame($element12, $element121->getParent());
140
        $this->assertSame($parent, $element2->getParent());
141
    }
142
143
    public function testMassCreationErrors(): void
144
    {
145
        $errors = [];
146
        //Node 1 and Node 2 are created in datafixtures, so their attemp to create them again must fail.
147
        $lines = "Test 1\nNode 1\nNode 2";
148
        $results = $this->service->massCreation($lines, AttachmentType::class, null, $errors);
149
        $this->assertCount(1, $results);
150
        $this->assertSame('Test 1', $results[0]->getName());
151
        $this->assertCount(2, $errors);
152
        $this->assertSame('Node 1', $errors[0]['entity']->getName());
153
    }
154
}
155