|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Test\View\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Halfpastfour\HeadMinifier\View\Helper\HeadScript; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class HeadScriptTest |
|
10
|
|
|
* |
|
11
|
|
|
* @package Test\View\Helper |
|
12
|
|
|
*/ |
|
13
|
|
|
class HeadScriptTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var HeadScript |
|
17
|
|
|
*/ |
|
18
|
|
|
private $helper; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $config = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setUp() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->config = [ |
|
31
|
|
|
'enabled' => true, |
|
32
|
|
|
'directories' => [ |
|
33
|
|
|
'public' => realpath(__DIR__ . '/../../data/public'), |
|
34
|
|
|
'cache' => realpath(__DIR__ . '/../../data/cache'), |
|
35
|
|
|
], |
|
36
|
|
|
]; |
|
37
|
|
|
$this->helper = new HeadScript($this->config, ''); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Perform a test without any files. There shouldn't be anything in the result. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testNoFiles() |
|
44
|
|
|
{ |
|
45
|
|
|
$helper = clone $this->helper; |
|
46
|
|
|
$result = $helper->toString(); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals('string', gettype($result)); |
|
49
|
|
|
$this->assertEmpty($result); |
|
50
|
|
|
$this->assertEmpty(glob($this->config['directories']['cache'] . '/*.min.js')); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Test with a file. There should be exactly one element generated in the result. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testSingleFile() |
|
57
|
|
|
{ |
|
58
|
|
|
$helper = clone $this->helper; |
|
59
|
|
|
$helper->appendFile('/js/test1.js'); |
|
60
|
|
|
|
|
61
|
|
|
$result = $helper->toString(); |
|
62
|
|
|
$domDocument = new \DOMDocument(); |
|
63
|
|
|
$domDocument->loadHTML($result); |
|
64
|
|
|
/** @var \DOMNodeList $elements */ |
|
65
|
|
|
$elements = $domDocument->getElementsByTagName('script'); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals(1, $elements->length); |
|
68
|
|
|
$this->assertNotEmpty(glob($this->config['directories']['cache'] . '/*.min.js')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Test with a few files. There should be exactly one element generated in the result. |
|
73
|
|
|
* @depends testSingleFile |
|
74
|
|
|
*/ |
|
75
|
|
|
public function testMultipleFiles() |
|
76
|
|
|
{ |
|
77
|
|
|
$helper = clone $this->helper; |
|
78
|
|
|
$helper->appendFile('/js/test1.js') |
|
79
|
|
|
->appendFile('/js/test2.js'); |
|
80
|
|
|
|
|
81
|
|
|
$result = $helper->toString(); |
|
82
|
|
|
$domDocument = new \DOMDocument(); |
|
83
|
|
|
$domDocument->loadHTML($result); |
|
84
|
|
|
/** @var \DOMNodeList $elements */ |
|
85
|
|
|
$elements = $domDocument->getElementsByTagName('script'); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals(1, $elements->length); |
|
88
|
|
|
$this->assertNotEmpty(glob($this->config['directories']['cache'] . '/*.min.js')); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Test with a few files. The files should appear in the correct order in the cached file. |
|
93
|
|
|
* @depends testMultipleFiles |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testFilesOrder() |
|
96
|
|
|
{ |
|
97
|
|
|
$helper = clone $this->helper; |
|
98
|
|
|
$helper->appendFile('/js/test1.js') |
|
99
|
|
|
->prependFile('/js/test2.js'); |
|
100
|
|
|
|
|
101
|
|
|
$result = $helper->toString(); |
|
102
|
|
|
$domDocument = new \DOMDocument(); |
|
103
|
|
|
$domDocument->loadHTML($result); |
|
104
|
|
|
|
|
105
|
|
|
/** @var \DOMNode $element */ |
|
106
|
|
|
$element = $domDocument->getElementsByTagName('script')->item(0); |
|
107
|
|
|
$minifiedFile = $element->attributes->getNamedItem('src')->nodeValue; |
|
108
|
|
|
$this->assertNotEmpty($minifiedFile); |
|
109
|
|
|
$this->assertFileExists($minifiedFile); |
|
110
|
|
|
|
|
111
|
|
|
$contents = file_get_contents($minifiedFile); |
|
112
|
|
|
$firstFilePlaceholder = 'firstFile()'; |
|
113
|
|
|
$secondFilePlaceholder = 'secondFile()'; |
|
114
|
|
|
$this->assertLessThan(strpos($contents, $firstFilePlaceholder), strpos($contents, $secondFilePlaceholder)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Delete generated cache files. |
|
119
|
|
|
*/ |
|
120
|
|
|
public function tearDown() |
|
121
|
|
|
{ |
|
122
|
|
|
foreach (glob($this->config['directories']['cache'] . '/*.min.js') as $file) { |
|
123
|
|
|
unlink($file); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|