ImporterTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 71
c 2
b 0
f 0
dl 0
loc 125
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testImportThrowsWithTooManyData() 0 12 1
A createExcel() 0 13 1
A testImportThrowsWithTooManyFiles() 0 14 1
A testImport() 0 41 1
A testImportThrowsWithWrongHeaders() 0 9 1
A createImages() 0 7 1
A testImportThrowsWithInvalidData() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Service;
6
7
use Application\Enum\Site;
8
use Application\Handler\TemplateHandler;
9
use Application\Model\Card;
10
use Application\Model\Collection;
11
use Application\Service\Importer;
12
use Laminas\Diactoros\Stream;
13
use Laminas\Diactoros\UploadedFile;
14
use PhpOffice\PhpSpreadsheet\Spreadsheet;
15
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
16
use PHPUnit\Framework\TestCase;
17
18
class ImporterTest extends TestCase
19
{
20
    private function createExcel(array $data): UploadedFile
21
    {
22
        $s = new Spreadsheet();
23
        $s->getActiveSheet()->fromArray($data);
24
25
        $writer = new Xlsx($s);
26
        $filename = tempnam(sys_get_temp_dir(), 'test');
27
28
        $writer->save($filename);
29
30
        $excel = new UploadedFile(new Stream($filename), 999, UPLOAD_ERR_OK, 'foo.xlsx', 'text/plain');
31
32
        return $excel;
33
    }
34
35
    private function createImages(array $paths): array
36
    {
37
        return array_map(function (string $path) {
38
            $basename = pathinfo($path, PATHINFO_BASENAME);
39
40
            return new UploadedFile(new Stream($path), 999, UPLOAD_ERR_OK, $basename, 'text/plain');
0 ignored issues
show
Bug introduced by
It seems like $basename can also be of type array; however, parameter $clientFilename of Laminas\Diactoros\UploadedFile::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

40
            return new UploadedFile(new Stream($path), 999, UPLOAD_ERR_OK, /** @scrutinizer ignore-type */ $basename, 'text/plain');
Loading history...
41
        }, $paths);
42
    }
43
44
    public function testImport(): void
45
    {
46
        $excel = $this->createExcel([
47
            TemplateHandler::HEADERS,
48
            ['5da49355cbcff.jpeg', 'Super name', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
49
            ['dw4jV3zYSPsqE2CB8BcP8ABD0', 'My Name', 'Long description', 'Test domain 9001', 'Test root material 8000 > Test child material 8001', 'Test root period 7000', '123', '456', 'Anguilla', 'The Valley', 'Paris', 'REF972', 'Test document type 11001', 'John Rambo', '1997', '1.1231', '123.132', 'building'],
50
        ]);
51
52
        $images = $this->createImages([
53
            'data/images/5da49355cbcff.jpeg',
54
            'data/images/dw4jV3zYSPsqE2CB8BcP8ABD0.jpg',
55
        ]);
56
57
        $collection = new Collection();
58
59
        $importer = new Importer(Site::Tiresias);
60
        $cards = $importer->import($excel, $images, $collection);
61
62
        self::assertCount(1, $cards[0]->getCollections());
63
        self::assertCount(1, $cards[1]->getCollections());
64
        self::assertSame($collection, $cards[0]->getCollections()->first());
65
        self::assertSame($collection, $cards[1]->getCollections()->first());
66
67
        /** @var Card $c */
68
        $c = $cards[1];
69
        self::assertSame('My Name', $c->getName());
70
        self::assertSame('Long description', $c->getExpandedName());
71
        self::assertSame('Test child material 8001', $c->getMaterials()->first()->getName());
72
        self::assertSame('Test root period 7000', $c->getPeriods()->first()->getName());
73
        self::assertSame(123, $c->getFrom());
74
        self::assertSame(456, $c->getTo());
75
        self::assertSame('Anguilla', $c->getCountry()->getName());
76
        self::assertSame('The Valley', $c->getLocality());
77
        self::assertSame('Paris', $c->getProductionPlace());
78
        self::assertSame('REF972', $c->getObjectReference());
79
        self::assertSame('Test domain 9001', $c->getDomains()->first()->getName());
80
        self::assertSame('John Rambo', $c->getTechniqueAuthor());
81
        self::assertSame('1997', $c->getTechniqueDate());
82
        self::assertSame(1.1231, $c->getLatitude());
83
        self::assertSame(123.132, $c->getLongitude());
84
        self::assertSame('building', $c->getPrecision()->value);
85
    }
86
87
    public function testImportThrowsWithWrongHeaders(): void
88
    {
89
        $excel = $this->createExcel([
90
            ['invalid header'],
91
        ]);
92
93
        $importer = new Importer(Site::Tiresias);
94
        $this->expectExceptionMessage('Erreur dans la cellule A1: S\'attend à "Fichier image (avec ou sans extension)", mais a vu "invalid header"');
95
        $importer->import($excel, [], null);
96
    }
97
98
    public function testImportThrowsWithTooManyFiles(): void
99
    {
100
        $excel = $this->createExcel([
101
            TemplateHandler::HEADERS,
102
        ]);
103
104
        $images = $this->createImages([
105
            'data/images/5da49355cbcff.jpeg',
106
            'data/images/dw4jV3zYSPsqE2CB8BcP8ABD0.jpg',
107
        ]);
108
109
        $importer = new Importer(Site::Tiresias);
110
        $this->expectExceptionMessage('Erreur dans la cellule A2: 2 images ont été uploadé pour lesquelles aucune information ont été trouvée dans le fichier Excel: 5da49355cbcff, dw4jV3zYSPsqE2CB8BcP8ABD0');
111
        $importer->import($excel, $images, null);
112
    }
113
114
    public function testImportThrowsWithTooManyData(): void
115
    {
116
        $excel = $this->createExcel([
117
            TemplateHandler::HEADERS,
118
            ['5da49355cbcff.jpeg', 'Super mario', 'tre etneruentuendu', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
119
            ['dw4jV3zYSPsqE2CB8BcP8ABD0', 'My Name', 'Long description', 'Test domain 9001', 'Test root material 8000 > Test child material 8001', 'Test root period 7000', '123', '456', 'Anguilla', 'The Valley', 'Paris', 'REF972', 'Test document type 11001', 'John Rambo', '1997', '1.1231', '1231.132', 'building'],
120
121
        ]);
122
123
        $importer = new Importer(Site::Tiresias);
124
        $this->expectExceptionMessage('Erreur dans la cellule A2: Image présente dans le fichier Excel, mais pas retrouvée dans les images uploadées: 5da49355cbcff.jpeg');
125
        $importer->import($excel, [], null);
126
    }
127
128
    public function testImportThrowsWithInvalidData(): void
129
    {
130
        $excel = $this->createExcel([
131
            TemplateHandler::HEADERS,
132
            ['dw4jV3zYSPsqE2CB8BcP8ABD0', 'My Name', 'Long description', 'non-existing-domain', 'Test root material 8000 > Test child material 8001', 'Test root period 7000', '123', '456', 'Anguilla', 'The Valley', 'Paris', 'REF972', 'Test document type 11001', 'John Rambo', '1997', '1.1231', '1231.132', 'building'],
133
134
        ]);
135
136
        $images = $this->createImages([
137
            'data/images/dw4jV3zYSPsqE2CB8BcP8ABD0.jpg',
138
        ]);
139
140
        $importer = new Importer(Site::Tiresias);
141
        $this->expectExceptionMessage('Erreur dans la cellule D2: Domaine introuvable: non-existing-domain');
142
        $importer->import($excel, $images, null);
143
    }
144
}
145