AsgardAssetPipelineTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
1
<?php namespace Modules\Core\Tests\Asset;
2
3
use Modules\Core\Foundation\Asset\Pipeline\AsgardAssetPipeline;
4
use Modules\Core\Tests\BaseTestCase;
5
6
class AsgardAssetPipelineTest extends BaseTestCase
7
{
8
    /**
9
     * @var \Modules\Core\Foundation\Asset\Pipeline\AsgardAssetPipeline
10
     */
11
    private $assetPipeline;
12
    /**
13
     * @var \Modules\Core\Foundation\Asset\Manager\AsgardAssetManager
14
     */
15
    private $assetManager;
16
17
    /**
18
     *
19
     */
20
    public function setUp()
21
    {
22
        parent::__construct();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__construct() instead of setUp()). Are you sure this is correct? If so, you might want to change this to $this->__construct().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
23
        $this->refreshApplication();
24
        $this->assetPipeline = new AsgardAssetPipeline($this->app['Modules\Core\Foundation\Asset\Manager\AssetManager']);
25
        $this->assetManager = $this->app['Modules\Core\Foundation\Asset\Manager\AssetManager'];
26
    }
27
28
    /** @test */
29 View Code Duplication
    public function it_should_return_empty_collection_if_no_assets()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $cssResult = $this->assetPipeline->allCss();
32
        $jsResult = $this->assetPipeline->allJs();
33
34
        $this->assertInstanceOf('Illuminate\Support\Collection', $cssResult);
35
        $this->assertEquals(0, $cssResult->count());
36
        $this->assertInstanceOf('Illuminate\Support\Collection', $jsResult);
37
        $this->assertEquals(0, $jsResult->count());
38
    }
39
40
    /** @test */
41
    public function it_should_require_add_js_asset()
42
    {
43
        $this->assetManager->addAsset('jquery', '/path/to/jquery.js');
44
45
        $this->assetPipeline->requireJs('jquery');
46
47
        $jsAssets = $this->assetPipeline->allJs();
48
49
        $this->assertEquals('/path/to/jquery.js', $jsAssets->first());
50
    }
51
52
    /** @test */
53
    public function it_should_require_a_css_asset()
54
    {
55
        $this->assetManager->addAsset('main', '/path/to/main.css');
56
57
        $this->assetPipeline->requireCss('main');
58
59
        $cssAssets = $this->assetPipeline->allCss();
60
61
        $this->assertEquals('/path/to/main.css', $cssAssets->first());
62
    }
63
64
    /** @test */
65
    public function it_should_throw_an_exception_if_js_asset_not_found()
66
    {
67
        $this->setExpectedException('Modules\Core\Foundation\Asset\AssetNotFoundException');
68
69
        $this->assetManager->addAsset('jquery', '/path/to/jquery.js');
70
71
        $this->assetPipeline->requireJs('app');
72
    }
73
74
    /** @test */
75
    public function it_should_throw_an_exception_if_css_asset_not_found()
76
    {
77
        $this->setExpectedException('Modules\Core\Foundation\Asset\AssetNotFoundException');
78
79
        $this->assetManager->addAsset('main', '/path/to/main.css');
80
81
        $this->assetPipeline->requireCss('iCheck');
82
    }
83
84
    /** @test */
85 View Code Duplication
    public function it_should_place_js_asset_after_dependency()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        $this->assetManager->addAsset('mega_slider', '/path/to/mega_slider.js');
88
        $this->assetManager->addAsset('jquery', '/path/to/jquery.js');
89
        $this->assetManager->addAsset('jquery_plugin', '/path/to/jquery_plugin.js');
90
        $this->assetManager->addAsset('jquery.iCheck', '/path/to/jquery_iCheck.js');
91
        $this->assetManager->addAsset('main', '/path/to/main.css');
92
        $this->assetManager->addAsset('iCheck', '/path/to/iCheck.css');
93
        $this->assetManager->addAsset('bootstrap', '/path/to/bootstrap.css');
94
95
        $this->assetPipeline->requireJs('jquery');
96
        $this->assetPipeline->requireJs('mega_slider');
97
        $this->assetPipeline->requireJs('jquery_plugin')->after('jquery');
98
        $this->assetPipeline->requireJs('jquery.iCheck');
99
100
        $jsAssets = $this->assetPipeline->allJs();
101
102
        $expected = [
103
            'jquery' => '/path/to/jquery.js',
104
            'jquery_plugin' => '/path/to/jquery_plugin.js',
105
            'mega_slider' => '/path/to/mega_slider.js',
106
            'jquery.iCheck' => '/path/to/jquery_iCheck.js',
107
        ];
108
        $this->assertEquals($expected, $jsAssets->toArray());
109
    }
110
111
    /** @test */
112 View Code Duplication
    public function it_should_place_css_asset_after_dependency()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        $this->assetManager->addAsset('mega_slider', '/path/to/mega_slider.js');
115
        $this->assetManager->addAsset('jquery', '/path/to/jquery.js');
116
        $this->assetManager->addAsset('jquery_plugin', '/path/to/jquery_plugin.js');
117
        $this->assetManager->addAsset('main', '/path/to/main.css');
118
        $this->assetManager->addAsset('iCheck', '/path/to/iCheck.css');
119
        $this->assetManager->addAsset('bootstrap', '/path/to/bootstrap.css');
120
        $this->assetManager->addAsset('datatables-css', '/path/to/datatables.css');
121
122
        $this->assetPipeline->requireCss('bootstrap');
123
        $this->assetPipeline->requireCss('iCheck');
124
        $this->assetPipeline->requireCss('main')->after('bootstrap');
125
        $this->assetPipeline->requireCss('datatables-css');
126
127
        $cssAssets = $this->assetPipeline->allCss();
128
129
        $expected = [
130
            'bootstrap' => '/path/to/bootstrap.css',
131
            'main' => '/path/to/main.css',
132
            'iCheck' => '/path/to/iCheck.css',
133
            'datatables-css' => '/path/to/datatables.css',
134
        ];
135
        $this->assertEquals($expected, $cssAssets->toArray());
136
    }
137
138
    /** @test */
139 View Code Duplication
    public function it_should_place_js_asset_before_dependency()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        $this->assetManager->addAsset('mega_slider', '/path/to/mega_slider.js');
142
        $this->assetManager->addAsset('jquery', '/path/to/jquery.js');
143
        $this->assetManager->addAsset('jquery_plugin', '/path/to/jquery_plugin.js');
144
        $this->assetManager->addAsset('jquery.iCheck', '/path/to/jquery_iCheck.js');
145
        $this->assetManager->addAsset('main', '/path/to/main.css');
146
        $this->assetManager->addAsset('iCheck', '/path/to/iCheck.css');
147
        $this->assetManager->addAsset('bootstrap', '/path/to/bootstrap.css');
148
149
        $this->assetPipeline->requireJs('jquery');
150
        $this->assetPipeline->requireJs('mega_slider');
151
        $this->assetPipeline->requireJs('jquery_plugin')->before('mega_slider');
152
        $this->assetPipeline->requireJs('jquery.iCheck');
153
154
        $jsAssets = $this->assetPipeline->allJs();
155
156
        $expected = [
157
            'jquery' => '/path/to/jquery.js',
158
            'mega_slider' => '/path/to/mega_slider.js',
159
            'jquery_plugin' => '/path/to/jquery_plugin.js',
160
            'jquery.iCheck' => '/path/to/jquery_iCheck.js',
161
        ];
162
        $this->assertEquals($expected, $jsAssets->toArray());
163
    }
164
165
    /** @test */
166 View Code Duplication
    public function it_should_place_css_asset_before_dependency()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168
        $this->assetManager->addAsset('mega_slider', '/path/to/mega_slider.js');
169
        $this->assetManager->addAsset('jquery', '/path/to/jquery.js');
170
        $this->assetManager->addAsset('jquery_plugin', '/path/to/jquery_plugin.js');
171
        $this->assetManager->addAsset('main', '/path/to/main.css');
172
        $this->assetManager->addAsset('iCheck', '/path/to/iCheck.css');
173
        $this->assetManager->addAsset('bootstrap', '/path/to/bootstrap.css');
174
        $this->assetManager->addAsset('datatables-css', '/path/to/datatables.css');
175
176
        $this->assetPipeline->requireCss('bootstrap');
177
        $this->assetPipeline->requireCss('iCheck');
178
        $this->assetPipeline->requireCss('main')->before('iCheck');
179
        $this->assetPipeline->requireCss('datatables-css');
180
181
        $cssAssets = $this->assetPipeline->allCss();
182
183
        $expected = [
184
            'bootstrap' => '/path/to/bootstrap.css',
185
            'main' => '/path/to/main.css',
186
            'iCheck' => '/path/to/iCheck.css',
187
            'datatables-css' => '/path/to/datatables.css',
188
        ];
189
        $this->assertEquals($expected, $cssAssets->toArray());
190
    }
191
192
    /** @test */
193
    public function it_should_require_an_array_of_assets()
194
    {
195
        $this->assetManager->addAssets([
196
            'jquery' => '/path/to/jquery.js',
197
            'plugin' => '/path/to/plugin.js',
198
            'main' => '/path/to/main.css',
199
            'icheck' => '/path/to/icheck.css',
200
        ]);
201
202
        $this->assetPipeline->requireCss([
0 ignored issues
show
Documentation introduced by
array('main', 'icheck') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
203
            'main',
204
            'icheck',
205
        ]);
206
        $this->assetPipeline->requireJs([
0 ignored issues
show
Documentation introduced by
array('jquery', 'plugin') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
207
            'jquery',
208
            'plugin',
209
        ]);
210
211
        $cssAssets = $this->assetPipeline->allCss();
212
        $jsAssets = $this->assetPipeline->allJs();
213
214
        $expectedCss = [
215
            'main' => '/path/to/main.css',
216
            'icheck' => '/path/to/icheck.css',
217
        ];
218
        $expectedJs = [
219
            'jquery' => '/path/to/jquery.js',
220
            'plugin' => '/path/to/plugin.js',
221
        ];
222
223
        $this->assertEquals($expectedCss, $cssAssets->toArray());
224
        $this->assertEquals($expectedJs, $jsAssets->toArray());
225
    }
226
}
227