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

AttachmentPathResolverTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 10
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\Services\Attachments;
23
24
use App\Services\AmountFormatter;
25
use App\Services\Attachments\AttachmentPathResolver;
26
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
27
28
class AttachmentPathResolverTest extends WebTestCase
29
{
30
    /**
31
     * @var AmountFormatter
32
     */
33
    protected static $service;
34
    protected static $projectDir_orig;
35
    protected static $projectDir;
36
37
    public static $media_path;
38
    public static $footprint_path;
39
40
    public function __construct($name = null, array $data = [], $dataName = '')
41
    {
42
        parent::__construct($name, $data, $dataName);
43
44
        self::bootKernel();
45
        self::$projectDir_orig = realpath(self::$kernel->getProjectDir());
46
        self::$projectDir = str_replace('\\', '/', self::$projectDir_orig);
47
        self::$media_path = self::$projectDir.'/public/media';
48
        self::$footprint_path = self::$projectDir.'/public/img/footprints';
49
    }
50
51
    public static function setUpBeforeClass()
52
    {
53
        parent::setUpBeforeClass();
54
55
        //Get an service instance.
56
        self::bootKernel();
57
        self::$service = self::$container->get(AttachmentPathResolver::class);
58
    }
59
60
    public function testParameterToAbsolutePath()
61
    {
62
        //If null is passed, null must be returned
63
        $this->assertNull(self::$service->parameterToAbsolutePath(null));
0 ignored issues
show
Bug introduced by
The method parameterToAbsolutePath() does not exist on App\Services\AmountFormatter. ( Ignorable by Annotation )

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

63
        $this->assertNull(self::$service->/** @scrutinizer ignore-call */ parameterToAbsolutePath(null));

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...
64
65
        //Absolute path should be returned like they are (we use projectDir here, because we know that this dir exists)
66
        $this->assertEquals(self::$projectDir_orig, self::$service->parameterToAbsolutePath(self::$projectDir));
67
68
        //Relative pathes should be resolved
69
        $this->assertEquals(self::$projectDir_orig.\DIRECTORY_SEPARATOR.'src', self::$service->parameterToAbsolutePath('src'));
70
        $this->assertEquals(self::$projectDir_orig.\DIRECTORY_SEPARATOR.'src', self::$service->parameterToAbsolutePath('./src'));
71
72
        //Invalid pathes should return null
73
        $this->assertNull(self::$service->parameterToAbsolutePath('/this/path/does/not/exist'));
74
        $this->assertNull(self::$service->parameterToAbsolutePath('/./this/one/too'));
75
    }
76
77
    public function placeholderDataProvider()
78
    {
79
        return [
80
            ['%FOOTPRINTS%/test/test.jpg', self::$footprint_path.'/test/test.jpg'],
81
            ['%FOOTPRINTS%/test/', self::$footprint_path.'/test/'],
82
            ['%MEDIA%/test', self::$media_path.'/test'],
83
            ['%MEDIA%', self::$media_path],
84
            ['%FOOTPRINTS%', self::$footprint_path],
85
            //Footprints 3D are disabled
86
            ['%FOOTPRINTS_3D%', null],
87
            //Check that invalid pathes return null
88
            ['/no/placeholder', null],
89
            ['%INVALID_PLACEHOLDER%', null],
90
            ['%FOOTPRINTS/test/', null], //Malformed placeholder
91
            ['/wrong/%FOOTRPINTS%/', null], //Placeholder not at beginning
92
            ['%FOOTPRINTS%/%MEDIA%', null], //No more than one placholder
93
            ['%FOOTPRINTS%/%FOOTPRINTS%', null],
94
            ['%FOOTPRINTS%/../../etc/passwd', null],
95
            ['%FOOTPRINTS%/0\..\test', null],
96
        ];
97
    }
98
99
    public function realPathDataProvider()
100
    {
101
        return [
102
            [self::$media_path.'/test/img.jpg', '%MEDIA%/test/img.jpg'],
103
            [self::$media_path.'/test/img.jpg', '%BASE%/data/media/test/img.jpg', true],
104
            [self::$footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg'],
105
            [self::$footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg', true],
106
            //Every kind of absolute path, that is not based with our placeholder dirs must be invald
107
            ['/etc/passwd', null],
108
            ['C:\\not\\existing.txt', null],
109
            //More then one placeholder is not allowed
110
            [self::$footprint_path.'/test/'.self::$footprint_path, null],
111
            //Path must begin with path
112
            ['/not/root'.self::$footprint_path, null],
113
        ];
114
    }
115
116
    /**
117
     * @dataProvider placeholderDataProvider
118
     */
119
    public function testPlaceholderToRealPath($param, $expected)
120
    {
121
        $this->assertEquals($expected, self::$service->placeholderToRealPath($param));
0 ignored issues
show
Bug introduced by
The method placeholderToRealPath() does not exist on App\Services\AmountFormatter. ( Ignorable by Annotation )

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

121
        $this->assertEquals($expected, self::$service->/** @scrutinizer ignore-call */ placeholderToRealPath($param));

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...
122
    }
123
124
    /**
125
     * @dataProvider realPathDataProvider
126
     */
127
    public function testRealPathToPlaceholder($param, $expected, $old_method = false)
128
    {
129
        $this->assertEquals($expected, self::$service->realPathToPlaceholder($param, $old_method));
0 ignored issues
show
Bug introduced by
The method realPathToPlaceholder() does not exist on App\Services\AmountFormatter. ( Ignorable by Annotation )

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

129
        $this->assertEquals($expected, self::$service->/** @scrutinizer ignore-call */ realPathToPlaceholder($param, $old_method));

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...
130
    }
131
}
132