Completed
Push — master ( 5ff4ab...0cf65a )
by Alexander
16s queued 10s
created

AssetAppendTimeStampTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 62
c 2
b 0
f 0
dl 0
loc 140
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerFileDataProvider() 0 95 1
A testRegisterFileAppendTimestamp() 0 27 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Yiisoft\Assets\Tests;
5
6
/**
7
 * AssetAppendTimeStamp.
8
 */
9
final class AssetAppendTimeStampTest extends TestCase
10
{
11
    /**
12
     * @return array
13
     */
14
    public function registerFileDataProvider(): array
15
    {
16
        return [
17
            // Custom alias repeats in the asset URL
18
            [
19
                'css', '@web/assetSources/repeat/css/stub.css', false,
20
                '/repeat/assetSources/repeat/css/stub.css',
21
                '/repeat',
22
            ],
23
            [
24
                'js', '@web/assetSources/repeat/js/jquery.js', false,
25
                '/repeat/assetSources/repeat/js/jquery.js',
26
                '/repeat',
27
            ],
28
            // JS files registration
29
            [
30
                'js', '@web/assetSources/js/missing-file.js', true,
31
                '/baseUrl/assetSources/js/missing-file.js'
32
            ],
33
            [
34
                'js', '@web/assetSources/js/jquery.js', false,
35
                '/baseUrl/assetSources/js/jquery.js',
36
            ],
37
            [
38
                'js', 'http://example.com/assetSources/js/jquery.js', false,
39
                'http://example.com/assetSources/js/jquery.js',
40
            ],
41
            [
42
                'js', '//example.com/assetSources/js/jquery.js', false,
43
                '//example.com/assetSources/js/jquery.js',
44
            ],
45
            [
46
                'js', 'assetSources/js/jquery.js', false,
47
                'assetSources/js/jquery.js',
48
            ],
49
            [
50
                'js', '/assetSources/js/jquery.js', false,
51
                '/assetSources/js/jquery.js',
52
            ],
53
            // CSS file registration
54
            [
55
                'css', '@web/assetSources/css/missing-file.css', true,
56
                '/baseUrl/assetSources/css/missing-file.css',
57
            ],
58
            [
59
                'css', '@web/assetSources/css/stub.css', false,
60
                '/baseUrl/assetSources/css/stub.css',
61
            ],
62
            [
63
                'css', 'http://example.com/assetSources/css/stub.css', false,
64
                'http://example.com/assetSources/css/stub.css',
65
            ],
66
            [
67
                'css', '//example.com/assetSources/css/stub.css', false,
68
                '//example.com/assetSources/css/stub.css',
69
            ],
70
            [
71
                'css', 'assetSources/css/stub.css', false,
72
                'assetSources/css/stub.css',
73
            ],
74
            [
75
                'css', '/assetSources/css/stub.css', false,
76
                '/assetSources/css/stub.css',
77
            ],
78
            // Custom `@web` aliases
79
            [
80
                'js', '@web/assetSources/js/missing-file1.js', true,
81
                '/backend/assetSources/js/missing-file1.js',
82
                '/backend',
83
            ],
84
            [
85
                'js', 'http://full-url.example.com/backend/assetSources/js/missing-file.js', true,
86
                'http://full-url.example.com/backend/assetSources/js/missing-file.js',
87
                '/backend',
88
            ],
89
            [
90
                'css', '//backend/backend/assetSources/js/missing-file.js', true,
91
                '//backend/backend/assetSources/js/missing-file.js',
92
                '/backend',
93
            ],
94
            [
95
                'css', '@web/assetSources/css/stub.css', false,
96
                '/en/blog/backend/assetSources/css/stub.css',
97
                '/en/blog/backend',
98
            ],
99
            // UTF-8 chars
100
            [
101
                'css', '@web/assetSources/css/stub.css', false,
102
                '/рус/сайт/assetSources/css/stub.css',
103
                '/рус/сайт',
104
            ],
105
            [
106
                'js', '@web/assetSources/js/jquery.js', false,
107
                '/汉语/漢語/assetSources/js/jquery.js',
108
                '/汉语/漢語',
109
            ],
110
        ];
111
    }
112
113
    /**
114
     * @dataProvider registerFileDataProvider
115
     *
116
     * @param string $type either `js` or `css`
117
     * @param string $path
118
     * @param bool $appendTimestamp
119
     * @param string $expected
120
     * @param string|null $webAlias
121
     */
122
    public function testRegisterFileAppendTimestamp(
123
        string $type,
124
        string $path,
125
        bool $appendTimestamp,
126
        string $expected,
127
        ?string $webAlias = null
128
    ): void {
129
        $originalAlias = $this->aliases->get('@web');
130
131
        if ($webAlias === null) {
132
            $webAlias = $originalAlias;
133
        }
134
135
        $this->aliases->set('@web', $webAlias);
0 ignored issues
show
Bug introduced by
It seems like $webAlias can also be of type boolean; however, parameter $path of Yiisoft\Aliases\Aliases::set() 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

135
        $this->aliases->set('@web', /** @scrutinizer ignore-type */ $webAlias);
Loading history...
136
137
        $path = $this->aliases->get($path);
138
139
        $this->assetManager->setAppendTimestamp($appendTimestamp);
140
141
        $method = 'register' . ucfirst($type) . 'File';
142
143
        $this->assetManager->$method($path, [], null);
144
145
        $this->assertStringContainsString(
146
            $expected,
147
            $type === 'css' ? $this->assetManager->getCssFiles()[$expected]['url']
148
                : $this->assetManager->getJsFiles()[$expected]['url']
149
        );
150
    }
151
}
152