1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* For the full copyright and license information, please view the LICENSE |
4
|
|
|
* file that was distributed with this source code. |
5
|
|
|
* 11/05/12 17:20 |
6
|
|
|
*/ |
7
|
|
|
namespace CssEmbed\Tests; |
8
|
|
|
|
9
|
|
|
use CssEmbed\CssEmbed; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Pierre Tachoire <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class CssEmbedTest extends \PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
public function testEmbedCss() |
17
|
|
|
{ |
18
|
|
|
$origin = __DIR__.'/rsc/test.css'; |
19
|
|
|
$expected = file_get_contents(__DIR__.'/rsc/expected.css'); |
20
|
|
|
|
21
|
|
|
$cssEmbed = new CssEmbed(); |
22
|
|
|
$tested = $cssEmbed->embedCss($origin); |
23
|
|
|
|
24
|
|
|
$this->assertEquals($expected, $tested); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function testEmbedString() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$origin = file_get_contents(__DIR__.'/rsc/test.css'); |
30
|
|
|
$expected = file_get_contents(__DIR__.'/rsc/expected.css'); |
31
|
|
|
|
32
|
|
|
$cssEmbed = new CssEmbed(); |
33
|
|
|
$cssEmbed->setRootDir(__DIR__.'/rsc'); |
34
|
|
|
$tested = $cssEmbed->embedString($origin); |
35
|
|
|
|
36
|
|
|
$this->assertEquals($expected, $tested); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function mimeTypeProvider() |
40
|
|
|
{ |
41
|
|
|
return array( |
42
|
|
|
array('application/octet-stream', 'binary.file'), |
43
|
|
|
array('image/gif', 'php.gif') |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @dataProvider mimeTypeProvider |
49
|
|
|
*/ |
50
|
|
|
public function testMimeType($expected, $file) |
51
|
|
|
{ |
52
|
|
|
$cssEmbed = new CssEmbedTestable(); |
53
|
|
|
$file = __DIR__.'/rsc/'.$file; |
54
|
|
|
$this->assertEquals($expected, $cssEmbed->mimeType($file)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testHttpEnabledEmbedCss() |
58
|
|
|
{ |
59
|
|
|
$origin = __DIR__.'/rsc/test-local-with-http.css'; |
60
|
|
|
$expected = file_get_contents(__DIR__.'/rsc/expected-local-with-http.css'); |
61
|
|
|
|
62
|
|
|
$cssEmbed = new CssEmbed(); |
63
|
|
|
$cssEmbed->enableHttp(); |
64
|
|
|
$tested = $cssEmbed->embedCss($origin); |
65
|
|
|
|
66
|
|
|
$this->assertEquals($expected, $tested); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function testHttpEnabledEmbedString() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$origin = file_get_contents(__DIR__.'/rsc/test-http.css'); |
72
|
|
|
$expected = file_get_contents(__DIR__.'/rsc/expected-http.css'); |
73
|
|
|
|
74
|
|
|
$cssEmbed = new CssEmbed(); |
75
|
|
|
$cssEmbed->enableHttp(); |
76
|
|
|
$cssEmbed->setRootDir('//httpbin.org/media/hypothetical-css-dir'); |
77
|
|
|
$tested = $cssEmbed->embedString($origin); |
78
|
|
|
$this->assertEquals($expected, $tested); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testSetHttpFlag() |
82
|
|
|
{ |
83
|
|
|
$origin = file_get_contents(__DIR__.'/rsc/test-http.css'); |
84
|
|
|
$expected = file_get_contents(__DIR__.'/rsc/expected-http-options.css'); |
85
|
|
|
|
86
|
|
|
$cssEmbed = new CssEmbed(); |
87
|
|
|
|
88
|
|
|
$cssEmbed->enableHttp(); |
89
|
|
|
$cssEmbed->setHttpFlag(CssEmbed::HTTP_DEFAULT_HTTPS); |
90
|
|
|
$cssEmbed->setHttpFlag(CssEmbed::HTTP_EMBED_SVG); |
91
|
|
|
$cssEmbed->setHttpFlag(CssEmbed::HTTP_EMBED_SCHEME); |
92
|
|
|
|
93
|
|
|
$cssEmbed->setRootDir('//httpbin.org/media/hypothetical-css-dir'); |
94
|
|
|
$tested = $cssEmbed->embedString($origin); |
95
|
|
|
|
96
|
|
|
$this->assertEquals($expected, $tested); |
97
|
|
|
|
98
|
|
|
$expected = file_get_contents(__DIR__.'/rsc/expected-http.css'); |
99
|
|
|
|
100
|
|
|
$cssEmbed->unsetHttpFlag(CssEmbed::HTTP_DEFAULT_HTTPS); |
101
|
|
|
$cssEmbed->unsetHttpFlag(CssEmbed::HTTP_EMBED_SVG); |
102
|
|
|
$cssEmbed->unsetHttpFlag(CssEmbed::HTTP_EMBED_SCHEME); |
103
|
|
|
$tested = $cssEmbed->embedString($origin); |
104
|
|
|
$this->assertEquals($expected, $tested); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function testHttpEmbedUrl() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$origin = file_get_contents(__DIR__.'/rsc/test-http-url-only.css'); |
110
|
|
|
$expected = file_get_contents(__DIR__.'/rsc/expected-http-url-only.css'); |
111
|
|
|
|
112
|
|
|
$cssEmbed = new CssEmbed(); |
113
|
|
|
$cssEmbed->enableHttp(); |
114
|
|
|
$cssEmbed->setHttpFlag(CssEmbed::HTTP_EMBED_URL_ONLY); |
115
|
|
|
$cssEmbed->setRootDir('//httpbin.org/media/hypothetical-css-dir'); |
116
|
|
|
$tested = $cssEmbed->embedString($origin); |
117
|
|
|
$this->assertEquals($expected, $tested); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
class CssEmbedTestable extends CssEmbed |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
public function mimeType($file) |
124
|
|
|
{ |
125
|
|
|
return parent::mimeType($file); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.