testCreateAssetCssViewFunctionEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Bootstrappers\Assets;
6
7
use AbterPhp\Framework\Assets\AssetManager;
8
use AbterPhp\Framework\Assets\CacheManager\ICacheManager;
9
use AbterPhp\Framework\Assets\UrlFixer;
10
use AbterPhp\Framework\Config\Routes;
11
use AbterPhp\Framework\Constant\Env;
12
use AbterPhp\Framework\Environments\Environment;
13
use AbterPhp\Framework\Filesystem\FileFinder;
14
use Opulence\Ioc\Container;
15
use Opulence\Views\Compilers\Fortune\ITranspiler;
16
use PHPUnit\Framework\MockObject\MockObject;
17
use PHPUnit\Framework\TestCase;
18
19
class AssetManagerBootstrapperTest extends TestCase
20
{
21
    private const DIR_MEDIA = '/media';
22
23
    /** @var AssetManagerBootstrapper - System Under Test */
24
    protected AssetManagerBootstrapper $sut;
25
26
    /** @var ITranspiler|MockObject */
27
    protected $transpilerMock;
28
29
    public function setUp(): void
30
    {
31
        Environment::setVar(Env::DIR_MEDIA, static::DIR_MEDIA);
32
        Environment::setVar(Env::CACHE_BASE_PATH, '/cache');
33
        Environment::setVar(Env::ENV_NAME, 'foo');
34
35
        $this->sut = new AssetManagerBootstrapper();
36
37
        $this->transpilerMock = $this->getMockBuilder(ITranspiler::class)->getMock();
38
    }
39
40
    public function testRegisterBindingsBindsAnAssetManager(): void
41
    {
42
        if (!is_dir(static::DIR_MEDIA) || !is_writable(static::DIR_MEDIA)) {
43
            $this->markTestSkipped();
44
        }
45
46
        $fileFinderMock   = $this->createMock(FileFinder::class);
47
        $cacheManagerMock = $this->createMock(ICacheManager::class);
48
        $urlFixerMock     = $this->getMockBuilder(UrlFixer::class)->disableOriginalConstructor()->getMock();
49
        $routesMock       = $this->createMock(Routes::class);
50
51
        $container = new Container();
52
        $container->bindInstance(FileFinder::class, $fileFinderMock);
53
        $container->bindInstance(ICacheManager::class, $cacheManagerMock);
54
        $container->bindInstance(UrlFixer::class, $urlFixerMock);
55
        $container->bindInstance(ITranspiler::class, $this->transpilerMock);
56
        $container->bindInstance(Routes::class, $routesMock);
57
58
        $this->transpilerMock->expects($this->exactly(3))->method('registerViewFunction');
59
60
        $this->sut->registerBindings($container);
61
62
        $actual = $container->resolve(AssetManager::class);
63
        $this->assertInstanceOf(AssetManager::class, $actual);
64
    }
65
66
    public function testCreateAssetJsViewFunctionSimple(): void
67
    {
68
        $keyStub     = 'foo';
69
        $webPathStub = 'baz';
70
71
        $assetManagerMock = $this->getMockBuilder(AssetManager::class)->disableOriginalConstructor()->getMock();
72
        $assetManagerMock->expects($this->once())->method('ensureJsWebPath')->with($keyStub)->willReturn($webPathStub);
73
74
        $actual = $this->sut->createAssetJsViewFunction($assetManagerMock)([$keyStub]);
75
        $this->assertSame("<script src=\"$webPathStub\"></script>\n", $actual);
76
    }
77
78
    public function testCreateAssetJsViewFunctionEmpty(): void
79
    {
80
        $keyStub     = 'foo';
81
        $webPathStub = '';
82
83
        $assetManagerMock = $this->getMockBuilder(AssetManager::class)->disableOriginalConstructor()->getMock();
84
        $assetManagerMock->expects($this->once())->method('ensureJsWebPath')->with($keyStub)->willReturn($webPathStub);
85
86
        $actual = $this->sut->createAssetJsViewFunction($assetManagerMock)([$keyStub]);
87
        $this->assertSame('', $actual);
88
    }
89
90
    public function testCreateAssetJsViewFunctionCustomType(): void
91
    {
92
        $keyStub     = 'foo';
93
        $typeStub    = 'bar';
94
        $webPathStub = 'baz';
95
96
        $assetManagerMock = $this->getMockBuilder(AssetManager::class)->disableOriginalConstructor()->getMock();
97
        $assetManagerMock->expects($this->once())->method('ensureJsWebPath')->with($keyStub)->willReturn($webPathStub);
98
99
        $actual = $this->sut->createAssetJsViewFunction($assetManagerMock)([$keyStub], $typeStub);
100
        $this->assertSame("<script type=\"$typeStub\" src=\"$webPathStub\"></script>\n", $actual);
101
    }
102
103
    public function testCreateAssetJsViewFunctionMultiple(): void
104
    {
105
        $keyStub1     = 'foo';
106
        $keyStub2     = 'bar';
107
        $webPathStub1 = 'baz';
108
        $webPathStub2 = 'quix';
109
110
        $assetManagerMock = $this->getMockBuilder(AssetManager::class)->disableOriginalConstructor()->getMock();
111
        $assetManagerMock
112
            ->expects($this->exactly(2))
113
            ->method('ensureJsWebPath')
114
            ->withConsecutive([$keyStub1], [$keyStub2])
115
            ->willReturnOnConsecutiveCalls($webPathStub1, $webPathStub2);
116
117
        $actual = $this->sut->createAssetJsViewFunction($assetManagerMock)([$keyStub1, $keyStub2]);
118
        $this->assertStringContainsString("<script src=\"$webPathStub1\"></script>", $actual);
119
        $this->assertStringContainsString("<script src=\"$webPathStub2\"></script>", $actual);
120
    }
121
122
    public function testCreateAssetCssViewFunctionSimple(): void
123
    {
124
        $keyStub     = 'foo';
125
        $webPathStub = 'baz';
126
127
        $assetManagerMock = $this->getMockBuilder(AssetManager::class)->disableOriginalConstructor()->getMock();
128
        $assetManagerMock->expects($this->once())->method('ensureCssWebPath')->with($keyStub)->willReturn($webPathStub);
129
130
        $actual = $this->sut->createAssetCssViewFunction($assetManagerMock)([$keyStub]);
131
        $this->assertSame("<link href=\"$webPathStub\" rel=\"stylesheet\">\n", $actual);
132
    }
133
134
    public function testCreateAssetCssViewFunctionEmpty(): void
135
    {
136
        $keyStub     = 'foo';
137
        $webPathStub = '';
138
139
        $assetManagerMock = $this->getMockBuilder(AssetManager::class)->disableOriginalConstructor()->getMock();
140
        $assetManagerMock->expects($this->once())->method('ensureCssWebPath')->with($keyStub)->willReturn($webPathStub);
141
142
        $actual = $this->sut->createAssetCssViewFunction($assetManagerMock)([$keyStub]);
143
        $this->assertSame('', $actual);
144
    }
145
146
    public function testCreateAssetCssViewFunctionMultiple(): void
147
    {
148
        $keyStub1     = 'foo';
149
        $keyStub2     = 'bar';
150
        $webPathStub1 = 'baz';
151
        $webPathStub2 = 'quix';
152
153
        $assetManagerMock = $this->getMockBuilder(AssetManager::class)->disableOriginalConstructor()->getMock();
154
        $assetManagerMock
155
            ->expects($this->exactly(2))
156
            ->method('ensureCssWebPath')
157
            ->withConsecutive([$keyStub1], [$keyStub2])
158
            ->willReturnOnConsecutiveCalls($webPathStub1, $webPathStub2);
159
160
        $actual = $this->sut->createAssetCssViewFunction($assetManagerMock)([$keyStub1, $keyStub2]);
161
        $this->assertStringContainsString("<link href=\"$webPathStub1\" rel=\"stylesheet\">", $actual);
162
        $this->assertStringContainsString("<link href=\"$webPathStub2\" rel=\"stylesheet\">", $actual);
163
    }
164
165
    public function testCreateAssetImgViewFunction(): void
166
    {
167
        $keyStub     = 'foo';
168
        $altStub     = 'bar';
169
        $webPathStub = 'baz';
170
171
        $assetManagerMock = $this->getMockBuilder(AssetManager::class)->disableOriginalConstructor()->getMock();
172
        $assetManagerMock->expects($this->once())->method('ensureImgWebPath')->with($keyStub)->willReturn($webPathStub);
173
174
        $actual = $this->sut->createAssetImgViewFunction($assetManagerMock)($keyStub, $altStub);
175
        $this->assertSame("<img src=\"$webPathStub\" alt=\"$altStub\">\n", $actual);
176
    }
177
}
178