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

AssetManagerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetConverter() 0 5 1
A testGetPublishedPath() 0 15 1
A testGetPublishedUrlWrong() 0 7 1
A testAssetManagerSetBundles() 0 34 1
A testGetPublishedPathWrong() 0 7 1
A testAssetManagerSetAssetMap() 0 23 1
A testGetPublishedUrl() 0 16 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Yiisoft\Assets\Tests;
5
6
use Yiisoft\Assets\AssetConverterInterface;
7
use Yiisoft\Assets\Tests\stubs\JqueryAsset;
8
use Yiisoft\Assets\Tests\stubs\SourceAsset;
9
10
final class AssetManagerTest extends TestCase
11
{
12
    public function testGetConverter(): void
13
    {
14
        $this->assertInstanceOf(
15
            AssetConverterInterface::class,
16
            $this->assetManager->getConverter()
17
        );
18
    }
19
20
    public function testGetPublishedPath(): void
21
    {
22
        $bundle = new SourceAsset();
23
24
        $sourcePath = $this->aliases->get($bundle->sourcePath);
25
26
        $path = $sourcePath . @filemtime($sourcePath);
27
        $path = sprintf('%x', crc32($path . '|' . $this->assetManager->getLinkAssets()));
28
29
        $this->assertEmpty($this->assetManager->getAssetBundles());
30
        $this->assetManager->register([SourceAsset::class]);
31
32
        $this->assertEquals(
33
            $this->assetManager->getPublishedPath($bundle->sourcePath),
34
            $this->aliases->get("@public/assets/$path")
35
        );
36
    }
37
38
    public function testGetPublishedPathWrong(): void
39
    {
40
        $this->assertEmpty($this->assetManager->getAssetBundles());
41
42
        $this->assetManager->register([SourceAsset::class]);
43
44
        $this->assertNull($this->assetManager->getPublishedPath('/wrong'));
45
    }
46
47
    public function testGetPublishedUrl(): void
48
    {
49
        $bundle = new SourceAsset();
50
51
        $sourcePath = $this->aliases->get($bundle->sourcePath);
52
53
        $path = $sourcePath . @filemtime($sourcePath);
54
        $path = sprintf('%x', crc32($path . '|' . $this->assetManager->getLinkAssets()));
55
56
        $this->assertEmpty($this->assetManager->getAssetBundles());
57
58
        $this->assetManager->register([SourceAsset::class]);
59
60
        $this->assertEquals(
61
            $this->assetManager->getPublishedUrl($bundle->sourcePath),
62
            "/baseUrl/$path"
63
        );
64
    }
65
66
    public function testGetPublishedUrlWrong(): void
67
    {
68
        $this->assertEmpty($this->assetManager->getAssetBundles());
69
70
        $this->assetManager->register([SourceAsset::class]);
71
72
        $this->assertNull($this->assetManager->getPublishedUrl('/wrong'));
73
    }
74
75
    public function testAssetManagerSetBundles(): void
76
    {
77
        $urlJs = 'https://code.jquery.com/jquery-3.4.1.js';
78
79
        $this->assetManager->setBundles(
80
            [
81
                JqueryAsset::class => [
82
                    'sourcePath' => null, //no publish asset bundle
83
                    'js' => [
84
                        [
85
                            $urlJs,
86
                            'integrity' => 'sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=',
87
                            'crossorigin' => 'anonymous'
88
                        ]
89
                    ]
90
                ]
91
            ]
92
        );
93
94
        $this->assertEmpty($this->assetManager->getAssetBundles());
95
96
        $this->assetManager->register([JqueryAsset::class]);
97
98
        $this->assertStringContainsString(
99
            $urlJs,
100
            $this->assetManager->getJsFiles()[$urlJs]['url']
101
        );
102
        $this->assertEquals(
103
            [
104
                'integrity' => 'sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=',
105
                'crossorigin' => 'anonymous',
106
                'position' => 3
107
            ],
108
            $this->assetManager->getJsFiles()[$urlJs]['attributes']
109
        );
110
    }
111
112
    public function testAssetManagerSetAssetMap(): void
113
    {
114
        $urlJs = '//testme.css';
115
116
        $this->assetManager->setAssetMap(
117
            [
118
                'jquery.js' => $urlJs,
119
            ]
120
        );
121
122
        $this->assertEmpty($this->assetManager->getAssetBundles());
123
124
        $this->assetManager->register([JqueryAsset::class]);
125
126
        $this->assertStringContainsString(
127
            $urlJs,
128
            $this->assetManager->getJsFiles()[$urlJs]['url']
129
        );
130
        $this->assertEquals(
131
            [
132
                'position' => 3
133
            ],
134
            $this->assetManager->getJsFiles()[$urlJs]['attributes']
135
        );
136
    }
137
}
138