Passed
Push — main ( 5fac80...79d5b0 )
by Peter
03:22
created

testCreateAssetJsViewFunctionSimple()   A

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