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\Attachments; |
||||
24 | |||||
25 | use App\Services\Formatters\AmountFormatter; |
||||
26 | use App\Services\Attachments\AttachmentPathResolver; |
||||
27 | use const DIRECTORY_SEPARATOR; |
||||
28 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
||||
29 | |||||
30 | class AttachmentPathResolverTest extends WebTestCase |
||||
31 | { |
||||
32 | protected $media_path; |
||||
33 | protected $footprint_path; |
||||
34 | protected $projectDir_orig; |
||||
35 | protected $projectDir; |
||||
36 | /** |
||||
37 | * @var AmountFormatter |
||||
38 | */ |
||||
39 | protected $service; |
||||
40 | |||||
41 | public function setUp(): void |
||||
42 | { |
||||
43 | parent::setUp(); |
||||
44 | |||||
45 | //Get an service instance. |
||||
46 | self::bootKernel(); |
||||
47 | |||||
48 | $this->projectDir_orig = realpath(self::$kernel->getProjectDir()); |
||||
49 | $this->projectDir = str_replace('\\', '/', $this->projectDir_orig); |
||||
50 | $this->media_path = $this->projectDir.'/public/media'; |
||||
51 | $this->footprint_path = $this->projectDir.'/public/img/footprints'; |
||||
52 | |||||
53 | $this->service = self::getContainer()->get(AttachmentPathResolver::class); |
||||
54 | } |
||||
55 | |||||
56 | public function testParameterToAbsolutePath(): void |
||||
57 | { |
||||
58 | //If null is passed, null must be returned |
||||
59 | $this->assertNull($this->service->parameterToAbsolutePath(null)); |
||||
0 ignored issues
–
show
|
|||||
60 | |||||
61 | //Absolute path should be returned like they are (we use projectDir here, because we know that this dir exists) |
||||
62 | $this->assertSame($this->projectDir_orig, $this->service->parameterToAbsolutePath($this->projectDir)); |
||||
63 | |||||
64 | //Relative pathes should be resolved |
||||
65 | $expected = str_replace('\\', '/', $this->projectDir_orig.DIRECTORY_SEPARATOR.'src'); |
||||
66 | $this->assertSame($expected, $this->service->parameterToAbsolutePath('src')); |
||||
67 | $this->assertSame($expected, $this->service->parameterToAbsolutePath('./src')); |
||||
68 | |||||
69 | //Invalid pathes should return null |
||||
70 | $this->assertNull($this->service->parameterToAbsolutePath('/this/path/does/not/exist')); |
||||
71 | $this->assertNull($this->service->parameterToAbsolutePath('/./this/one/too')); |
||||
72 | } |
||||
73 | |||||
74 | public function placeholderDataProvider(): array |
||||
75 | { |
||||
76 | //We need to do initialization (again), as dataprovider is called before setUp() |
||||
77 | self::bootKernel(); |
||||
78 | $this->projectDir_orig = realpath(self::$kernel->getProjectDir()); |
||||
79 | $this->projectDir = str_replace('\\', '/', $this->projectDir_orig); |
||||
80 | $this->media_path = $this->projectDir.'/public/media'; |
||||
81 | $this->footprint_path = $this->projectDir.'/public/img/footprints'; |
||||
82 | |||||
83 | return [ |
||||
84 | ['%FOOTPRINTS%/test/test.jpg', $this->footprint_path.'/test/test.jpg'], |
||||
85 | ['%FOOTPRINTS%/test/', $this->footprint_path.'/test/'], |
||||
86 | ['%MEDIA%/test', $this->media_path.'/test'], |
||||
87 | ['%MEDIA%', $this->media_path], |
||||
88 | ['%FOOTPRINTS%', $this->footprint_path], |
||||
89 | //Footprints 3D are disabled |
||||
90 | ['%FOOTPRINTS_3D%', null], |
||||
91 | //Check that invalid pathes return null |
||||
92 | ['/no/placeholder', null], |
||||
93 | ['%INVALID_PLACEHOLDER%', null], |
||||
94 | ['%FOOTPRINTS/test/', null], //Malformed placeholder |
||||
95 | ['/wrong/%FOOTRPINTS%/', null], //Placeholder not at beginning |
||||
96 | ['%FOOTPRINTS%/%MEDIA%', null], //No more than one placholder |
||||
97 | ['%FOOTPRINTS%/%FOOTPRINTS%', null], |
||||
98 | ['%FOOTPRINTS%/../../etc/passwd', null], |
||||
99 | ['%FOOTPRINTS%/0\..\test', null], |
||||
100 | ]; |
||||
101 | } |
||||
102 | |||||
103 | public function realPathDataProvider(): array |
||||
104 | { |
||||
105 | //We need to do initialization (again), as dataprovider is called before setUp() |
||||
106 | self::bootKernel(); |
||||
107 | $this->projectDir_orig = realpath(self::$kernel->getProjectDir()); |
||||
108 | $this->projectDir = str_replace('\\', '/', $this->projectDir_orig); |
||||
109 | $this->media_path = $this->projectDir.'/public/media'; |
||||
110 | $this->footprint_path = $this->projectDir.'/public/img/footprints'; |
||||
111 | |||||
112 | return [ |
||||
113 | [$this->media_path.'/test/img.jpg', '%MEDIA%/test/img.jpg'], |
||||
114 | [$this->media_path.'/test/img.jpg', '%BASE%/data/media/test/img.jpg', true], |
||||
115 | [$this->footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg'], |
||||
116 | [$this->footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg', true], |
||||
117 | //Every kind of absolute path, that is not based with our placeholder dirs must be invald |
||||
118 | ['/etc/passwd', null], |
||||
119 | ['C:\\not\\existing.txt', null], |
||||
120 | //More then one placeholder is not allowed |
||||
121 | [$this->footprint_path.'/test/'.$this->footprint_path, null], |
||||
122 | //Path must begin with path |
||||
123 | ['/not/root'.$this->footprint_path, null], |
||||
124 | ]; |
||||
125 | } |
||||
126 | |||||
127 | /** |
||||
128 | * @dataProvider placeholderDataProvider |
||||
129 | */ |
||||
130 | public function testPlaceholderToRealPath($param, $expected): void |
||||
131 | { |
||||
132 | $this->assertSame($expected, $this->service->placeholderToRealPath($param)); |
||||
0 ignored issues
–
show
The method
placeholderToRealPath() 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
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. ![]() |
|||||
133 | } |
||||
134 | |||||
135 | /** |
||||
136 | * @dataProvider realPathDataProvider |
||||
137 | */ |
||||
138 | public function testRealPathToPlaceholder($param, $expected, $old_method = false): void |
||||
139 | { |
||||
140 | $this->assertSame($expected, $this->service->realPathToPlaceholder($param, $old_method)); |
||||
0 ignored issues
–
show
The method
realPathToPlaceholder() 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
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. ![]() |
|||||
141 | } |
||||
142 | |||||
143 | public function germanFootprintPathdDataProvider() |
||||
144 | { |
||||
145 | self::bootKernel(); |
||||
146 | $this->projectDir_orig = realpath(self::$kernel->getProjectDir()); |
||||
147 | $this->projectDir = str_replace('\\', '/', $this->projectDir_orig); |
||||
148 | $this->footprint_path = $this->projectDir.'/public/img/footprints'; |
||||
149 | |||||
150 | yield [$this->footprint_path. '/Active/Diodes/THT/DIODE_P600.png', '%FOOTPRINTS%/Aktiv/Dioden/Bedrahtet/DIODE_P600.png']; |
||||
151 | yield [$this->footprint_path . '/Passive/Resistors/THT/Carbon/RESISTOR-CARBON_0207.png', '%FOOTPRINTS%/Passiv/Widerstaende/Bedrahtet/Kohleschicht/WIDERSTAND-KOHLE_0207.png']; |
||||
152 | yield [$this->footprint_path . '/Optics/LEDs/THT/LED-GREEN_3MM.png', '%FOOTPRINTS%/Optik/LEDs/Bedrahtet/LED-GRUEN_3MM.png']; |
||||
153 | yield [$this->footprint_path . '/Passive/Capacitors/TrimmerCapacitors/TRIMMER_CAPACITOR-RED_TZ03F.png', '%FOOTPRINTS%/Passiv/Kondensatoren/Trimmkondensatoren/TRIMMKONDENSATOR-ROT_TZ03F.png']; |
||||
154 | yield [$this->footprint_path . '/Active/ICs/TO/IC_TO126.png', '%FOOTPRINTS%/Aktiv/ICs/TO/IC_TO126.png']; |
||||
155 | yield [$this->footprint_path . '/Electromechanics/Switches_Buttons/RotarySwitches/ROTARY_SWITCH_DIP10.png', '%FOOTPRINTS%/Elektromechanik/Schalter_Taster/Drehschalter/DREHSCHALTER_DIP10.png']; |
||||
156 | yield [$this->footprint_path . '/Electromechanics/Connectors/DINConnectors/SOCKET_DIN_MAB_4.png', '%FOOTPRINTS%/Elektromechanik/Verbinder/Rundsteckverbinder/BUCHSE_DIN_MAB_4.png']; |
||||
157 | |||||
158 | //Leave english pathes untouched |
||||
159 | yield [$this->footprint_path . '/Passive/Capacitors/CAPACITOR_CTS_A_15MM.png', '%FOOTPRINTS%/Passive/Capacitors/CAPACITOR_CTS_A_15MM.png']; |
||||
160 | } |
||||
161 | |||||
162 | /** |
||||
163 | * @dataProvider germanFootprintPathdDataProvider |
||||
164 | * @return void |
||||
165 | */ |
||||
166 | public function testConversionOfGermanFootprintPaths(string $expected, string $input): void |
||||
167 | { |
||||
168 | $this->assertSame($expected, $this->service->placeholderToRealPath($input)); |
||||
169 | } |
||||
170 | } |
||||
171 |
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.