Passed
Push — master ( d286b7...24e4a5 )
by Jan
04:36
created

AttachmentTest::externalDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 12
rs 9.9332
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 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (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 General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\Tests\Entity\Attachments;
23
24
use App\Entity\Attachments\Attachment;
25
use App\Entity\Attachments\AttachmentType;
26
use App\Entity\Attachments\AttachmentTypeAttachment;
27
use App\Entity\Attachments\CategoryAttachment;
28
use App\Entity\Attachments\CurrencyAttachment;
29
use App\Entity\Attachments\DeviceAttachment;
30
use App\Entity\Attachments\FootprintAttachment;
31
use App\Entity\Attachments\GroupAttachment;
32
use App\Entity\Attachments\ManufacturerAttachment;
33
use App\Entity\Attachments\MeasurementUnitAttachment;
34
use App\Entity\Attachments\PartAttachment;
35
use App\Entity\Attachments\StorelocationAttachment;
36
use App\Entity\Attachments\SupplierAttachment;
37
use App\Entity\Attachments\UserAttachment;
38
use App\Entity\Devices\Device;
39
use App\Entity\Parts\Category;
40
use App\Entity\Parts\Footprint;
41
use App\Entity\Parts\Manufacturer;
42
use App\Entity\Parts\MeasurementUnit;
43
use App\Entity\Parts\Part;
44
use App\Entity\Parts\Storelocation;
45
use App\Entity\Parts\Supplier;
46
use App\Entity\PriceInformations\Currency;
47
use App\Entity\UserSystem\Group;
48
use App\Entity\UserSystem\User;
49
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...
50
use ReflectionClass;
51
52
class AttachmentTest extends TestCase
53
{
54
    public function testEmptyState(): void
55
    {
56
        $attachment = new PartAttachment();
57
58
        $this->assertNull($attachment->getAttachmentType());
59
        $this->assertFalse($attachment->isPicture());
60
        $this->assertFalse($attachment->isExternal());
61
        $this->assertFalse($attachment->isSecure());
62
        $this->assertFalse($attachment->isBuiltIn());
63
        $this->assertFalse($attachment->is3DModel());
64
        $this->assertFalse($attachment->getShowInTable());
65
        $this->assertEmpty($attachment->getPath());
66
        $this->assertEmpty($attachment->getName());
67
        $this->assertEmpty($attachment->getURL());
68
        $this->assertEmpty($attachment->getExtension());
69
        $this->assertNull($attachment->getElement());
70
        $this->assertEmpty($attachment->getFilename());
71
    }
72
73
    public function subClassesDataProvider(): array
74
    {
75
        return [
76
            [AttachmentTypeAttachment::class, AttachmentType::class],
77
            [CategoryAttachment::class, Category::class],
78
            [CurrencyAttachment::class, Currency::class],
79
            [DeviceAttachment::class, Device::class],
80
            [FootprintAttachment::class, Footprint::class],
81
            [GroupAttachment::class, Group::class],
82
            [ManufacturerAttachment::class, Manufacturer::class],
83
            [MeasurementUnitAttachment::class, MeasurementUnit::class],
84
            [PartAttachment::class, Part::class],
85
            [StorelocationAttachment::class, Storelocation::class],
86
            [SupplierAttachment::class, Supplier::class],
87
            [UserAttachment::class, User::class],
88
        ];
89
    }
90
91
    /**
92
     * @dataProvider subClassesDataProvider
93
     */
94
    public function testSetElement(string $attachment_class, string $allowed_class): void
95
    {
96
        /** @var Attachment $attachment */
97
        $attachment = new $attachment_class();
98
        $element = new $allowed_class();
99
100
        //This must not throw an exception
101
        $attachment->setElement($element);
102
        $this->assertEquals($element, $attachment->getElement());
103
    }
104
105
    /**
106
     * Test that all attachment subclasses like PartAttachment or similar returns an exception, when an not allowed
107
     * element is passed.
108
     *
109
     * @dataProvider subClassesDataProvider
110
     * @depends  testSetElement
111
     */
112
    public function testSetElementExceptionOnSubClasses(string $attachment_class, string $allowed_class): void
113
    {
114
        $this->expectException(\InvalidArgumentException::class);
115
116
        /** @var Attachment $attachment */
117
        $attachment = new $attachment_class();
118
        if (Device::class !== $allowed_class) {
119
            $element = new Device();
120
        } else {
121
            $element = new Category();
122
        }
123
        $attachment->setElement($element);
124
    }
125
126
    public function externalDataProvider()
127
    {
128
        return [
129
            ['', false],
130
            ['%MEDIA%/foo/bar.txt', false],
131
            ['%BASE%/foo/bar.jpg', false],
132
            ['%FOOTPRINTS%/foo/bar.jpg', false],
133
            ['%FOOTPRINTS3D%/foo/bar.jpg', false],
134
            ['%SECURE%/test.txt', false],
135
            ['%test%/foo/bar.ghp', true],
136
            ['foo%MEDIA%/foo.jpg', true],
137
            ['foo%MEDIA%/%BASE%foo.jpg', true],
138
        ];
139
    }
140
141
    /**
142
     * @dataProvider externalDataProvider
143
     */
144
    public function testIsExternal($path, $expected)
145
    {
146
        $attachment = new PartAttachment();
147
        $this->setProtectedProperty($attachment, 'path', $path);
148
        $this->assertEquals($expected, $attachment->isExternal());
149
    }
150
151
    public function extensionDataProvider()
152
    {
153
        return [
154
            ['%MEDIA%/foo/bar.txt', null, 'txt'],
155
            ['%MEDIA%/foo/bar.JPeg', null, 'jpeg'],
156
            ['%MEDIA%/foo/bar.JPeg', 'test.txt', 'txt'],
157
            ['%MEDIA%/foo/bar', null, ''],
158
            ['%MEDIA%/foo.bar', 'bar', ''],
159
            ['http://google.de', null, null],
160
            ['https://foo.bar', null, null],
161
            ['https://foo.bar/test.jpeg', null, null],
162
            ['test', null, null],
163
            ['test.txt', null, null],
164
        ];
165
    }
166
167
    /**
168
     * @dataProvider extensionDataProvider
169
     */
170
    public function testGetExtension($path, $originalFilename, $expected)
171
    {
172
        $attachment = new PartAttachment();
173
        $this->setProtectedProperty($attachment, 'path', $path);
174
        $this->setProtectedProperty($attachment, 'original_filename', $originalFilename);
175
        $this->assertEquals($expected, $attachment->getExtension());
176
    }
177
178
    public function pictureDataProvider()
179
    {
180
        return [
181
            ['%MEDIA%/foo/bar.txt', false],
182
            ['https://test.de/picture.jpeg', true],
183
            ['https://test.de', true],
184
            ['http://test.de/google.de', true],
185
            ['%MEDIA%/foo/bar.jpeg', true],
186
            ['%MEDIA%/foo/bar.webp', true],
187
            ['%MEDIA%/foo', false],
188
            ['%SECURE%/foo.txt/test', false],
189
        ];
190
    }
191
192
    /**
193
     * @dataProvider pictureDataProvider
194
     */
195
    public function testIsPicture($path, $expected)
196
    {
197
        $attachment = new PartAttachment();
198
        $this->setProtectedProperty($attachment, 'path', $path);
199
        $this->assertEquals($expected, $attachment->isPicture());
200
    }
201
202
    public function builtinDataProvider()
203
    {
204
        return [
205
            ['', false],
206
            ['%MEDIA%/foo/bar.txt', false],
207
            ['%BASE%/foo/bar.txt', false],
208
            ['/', false],
209
            ['https://google.de', false],
210
            ['%FOOTPRINTS%/foo/bar.txt', true],
211
        ];
212
    }
213
214
    /**
215
     * @dataProvider builtinDataProvider
216
     */
217
    public function testIsBuiltIn($path, $expected)
218
    {
219
        $attachment = new PartAttachment();
220
        $this->setProtectedProperty($attachment, 'path', $path);
221
        $this->assertEquals($expected, $attachment->isBuiltIn());
222
    }
223
224
    public function hostDataProvider()
225
    {
226
        return [
227
            ['%MEDIA%/foo/bar.txt', null],
228
            ['https://www.google.de/test.txt', 'www.google.de'],
229
            ['https://foo.bar/test?txt=test', 'foo.bar'],
230
        ];
231
    }
232
233
    /**
234
     * @dataProvider hostDataProvider
235
     */
236
    public function testGetHost($path, $expected)
237
    {
238
        $attachment = new PartAttachment();
239
        $this->setProtectedProperty($attachment, 'path', $path);
240
        $this->assertEquals($expected, $attachment->getHost());
241
    }
242
243
    public function filenameProvider()
244
    {
245
        return [
246
            ['%MEDIA%/foo/bar.txt', null, 'bar.txt'],
247
            ['%MEDIA%/foo/bar.JPeg', 'test.txt', 'test.txt'],
248
            ['https://www.google.de/test.txt', null, null],
249
        ];
250
    }
251
252
    /**
253
     * @dataProvider filenameProvider
254
     */
255
    public function testGetFilename($path, $original_filename, $expected)
256
    {
257
        $attachment = new PartAttachment();
258
        $this->setProtectedProperty($attachment, 'path', $path);
259
        $this->setProtectedProperty($attachment, 'original_filename', $original_filename);
260
        $this->assertEquals($expected, $attachment->getFilename());
261
    }
262
263
    public function testIsURL()
264
    {
265
        $url = '%MEDIA%/test.txt';
266
        $this->assertFalse(Attachment::isURL($url));
267
268
        $url = 'https://google.de';
269
        $this->assertFalse(Attachment::isURL($url));
270
271
        $url = 'ftp://google.de';
272
        $this->assertTrue(Attachment::isURL($url, false, false));
273
        $this->assertFalse(Attachment::isURL($url, false, true));
274
    }
275
276
    /**
277
     * Sets a protected property on a given object via reflection.
278
     *
279
     * @param object $object   - instance in which protected value is being modified
280
     * @param string $property - property on instance being modified
281
     * @param mixed  $value    - new value of the property being modified
282
     *
283
     * @return void
284
     */
285
    public function setProtectedProperty($object, $property, $value)
286
    {
287
        $reflection = new ReflectionClass($object);
288
        $reflection_property = $reflection->getProperty($property);
289
        $reflection_property->setAccessible(true);
290
        $reflection_property->setValue($object, $value);
291
    }
292
}
293