1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Test\View\Helper; |
4
|
|
|
|
5
|
|
|
use Halfpastfour\HeadMinifier\View\Helper\HeadLink; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class HeadLinkTest |
10
|
|
|
* |
11
|
|
|
* @package Test\View\Helper |
12
|
|
|
*/ |
13
|
|
|
class HeadLinkTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var HeadLink |
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 HeadLink($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.css')); |
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->appendStylesheet('/css/test1.css'); |
60
|
|
|
|
61
|
|
|
$result = $helper->toString(); |
62
|
|
|
$domDocument = new \DOMDocument(); |
63
|
|
|
$domDocument->loadHTML($result); |
64
|
|
|
/** @var \DOMNodeList $elements */ |
65
|
|
|
$elements = $domDocument->getElementsByTagName('link'); |
66
|
|
|
|
67
|
|
|
$this->assertEquals(1, $elements->length); |
68
|
|
|
$this->assertNotEmpty(glob($this->config['directories']['cache'] . '/*.min.css')); |
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->appendStylesheet('/css/test1.css') |
79
|
|
|
->appendStylesheet('/css/test2.css'); |
80
|
|
|
|
81
|
|
|
$result = $helper->toString(); |
82
|
|
|
$domDocument = new \DOMDocument(); |
83
|
|
|
$domDocument->loadHTML($result); |
84
|
|
|
/** @var \DOMNodeList $elements */ |
85
|
|
|
$elements = $domDocument->getElementsByTagName('link'); |
86
|
|
|
|
87
|
|
|
$this->assertEquals(1, $elements->length); |
88
|
|
|
$this->assertNotEmpty(glob($this->config['directories']['cache'] . '/*.min.css')); |
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->appendStylesheet('/css/test1.css') |
99
|
|
|
->prependStylesheet('/css/test2.css'); |
100
|
|
|
|
101
|
|
|
$result = $helper->toString(); |
102
|
|
|
$domDocument = new \DOMDocument(); |
103
|
|
|
$domDocument->loadHTML($result); |
104
|
|
|
/** @var \DOMNode $element */ |
105
|
|
|
$element = $domDocument->getElementsByTagName('link')->item(0); |
106
|
|
|
$minifiedFile = $element->attributes->getNamedItem('href')->nodeValue; |
107
|
|
|
$this->assertNotEmpty($minifiedFile); |
108
|
|
|
$this->assertFileExists($minifiedFile); |
109
|
|
|
|
110
|
|
|
$contents = file_get_contents($minifiedFile); |
111
|
|
|
$firstFilePlaceholder = '.file1'; |
112
|
|
|
$secondFilePlaceholder = '.file2'; |
113
|
|
|
$this->assertLessThan(strpos($contents, $firstFilePlaceholder), strpos($contents, $secondFilePlaceholder)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Delete generated cache files. |
118
|
|
|
*/ |
119
|
|
|
public function tearDown() |
120
|
|
|
{ |
121
|
|
|
foreach (glob($this->config['directories']['cache'] . '/*.min.css') as $file) { |
122
|
|
|
unlink($file); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|