Completed
Push — master ( d47933...c392d1 )
by smiley
04:38
created

HTML5ModuleTest   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 221
Duplicated Lines 16.29 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 11
Bugs 0 Features 6
Metric Value
wmc 25
c 11
b 0
f 6
lcom 1
cbo 4
dl 36
loc 221
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 *
4
 * @filesource   HTML5ModuleTestCritical.php
5
 * @created      11.02.2016
6
 * @package      chillerlan\BBCodeTest\normal
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2015 Smiley
9
 * @license      MIT
10
 */
11
12
namespace chillerlan\BBCodeTest\normal\Modules;
13
14
use chillerlan\bbcode\BBTemp;
15
use chillerlan\bbcode\Modules\Html5\Simpletext;
16
use chillerlan\bbcode\Modules\Html5\Singletags;
17
use chillerlan\bbcode\Parser;
18
use chillerlan\bbcode\ParserOptions;
19
use Dotenv\Dotenv;
20
21
/**
22
 * Class HTML5ModuleTestCritical
23
 */
24
class HTML5ModuleTest extends \PHPUnit_Framework_TestCase{
25
26
	/**
27
	 * @todo TRAVIS REMINDER!
28
	 */
29
	const DOTENV = '.env_example';
30
31
	/**
32
	 * @var \chillerlan\bbcode\Parser
33
	 */
34
	protected $parser;
35
36
	protected function setUp(){
37
		(new Dotenv(__DIR__.'/../../', self::DOTENV))->load(); // nasty
38
39
		$options = new ParserOptions;
40
		$options->google_api_key = getenv('GOOGLE_API');
41
		$options->vimeo_access_token = getenv('VIMEO_TOKEN');
42
		$options->ca_info        = __DIR__.'/../../test-cacert.pem';
43
		$options->allow_all = true;
44
		$this->parser = new Parser($options);
45
	}
46
47
	public function testEmptyTags(){
48
		$singletags = $this->parser->getSingle();
49
		$exceptions = ['td','th'];
50
51
		foreach(array_keys($this->parser->getTagmap()) as $tag){
52
			if(!in_array($tag, $singletags) && !in_array($tag, $exceptions)){
53
				$parsed = $this->parser->parse('['.$tag.'][/'.$tag.']');
54
				$this->assertEquals('', $parsed);
55
			}
56
		}
57
	}
58
59
	public function containerDataProvider(){
60
		return [
61
			['[div]a div[/div]', '<div class="bb-container left">a div</div>'],
62
			['[div align=right]an aligned div[/div]', '<div class="bb-container right">an aligned div</div>'],
63
			['[p]a paragraph[/p]', '<p class="bb-container left">a paragraph</p>'],
64
			['[p align=right]an aligned paragraph[/p]', '<p class="bb-container right">an aligned paragraph</p>'],
65
			['[left]left[/left]', '<p class="bb-container left">left</p>'],
66
			['[left align=right]WAT[/left]', '<p class="bb-container left">WAT</p>'],
67
			['[right]right[/right]', '<p class="bb-container right">right</p>'],
68
			['[center]center[/center]', '<p class="bb-container center">center</p>'],
69
		];
70
	}
71
72
	/**
73
	 * @dataProvider containerDataProvider
74
	 */
75
	public function testContainerModule($bbcode, $expected){
76
		$parsed = $this->parser->parse($bbcode);
77
		$this->assertEquals($expected, $parsed);
78
	}
79
80
	public function expanderDataProvider(){
81
		return [
82
			['[expander]expander[/expander]', '<div data-id="abcdef12" title="Expander" class="expander-header expander">Expander</div><div id="abcdef12" class="expander-body" style="display:block">expander</div>'],
83
			['[expander hide=1]expander[/expander]', '<div data-id="abcdef12" title="Expander" class="expander-header expander">Expander</div><div id="abcdef12" class="expander-body" style="display:none">expander</div>'],
84
			['[quote]quote[/quote]', '<div data-id="abcdef12" title="Quote" class="quote-header expander">Quote</div><div id="abcdef12" class="quote-body" style="display:block">quote</div>'],
85
			['[quote name=\'some person\' url=http://www.example.com hide=1]quote[/quote]', '<div data-id="abcdef12" title="Quote: some person, source: http://www.example.com" class="quote-header expander">Quote: some person <small>[<a href="http://www.example.com">source</a>]<small></div><div id="abcdef12" class="quote-body" style="display:none">quote</div>'],
86
			['[spoiler]spoiler[/spoiler]', '<div data-id="abcdef12" title="Spoiler" class="spoiler-header expander">Spoiler</div><div id="abcdef12" class="spoiler-body" style="display:none">spoiler</div>'],
87
			['[spoiler hide=0]spoiler[/spoiler]', '<div data-id="abcdef12" title="Spoiler" class="spoiler-header expander">Spoiler</div><div id="abcdef12" class="spoiler-body" style="display:none">spoiler</div>'],
88
			['[trigger]trigger warning[/trigger]', '<div data-id="abcdef12" title="Trigger warning! The following content may be harmful to sensitive audience!" class="trigger-header expander">Trigger warning! The following content may be harmful to sensitive audience!</div><div id="abcdef12" class="trigger-body" style="display:none">trigger warning</div>'],
89
			['[trigger hide=0]trigger warning[/trigger]', '<div data-id="abcdef12" title="Trigger warning! The following content may be harmful to sensitive audience!" class="trigger-header expander">Trigger warning! The following content may be harmful to sensitive audience!</div><div id="abcdef12" class="trigger-body" style="display:none">trigger warning</div>'],
90
		];
91
	}
92
93
	/**
94
	 * @dataProvider expanderDataProvider
95
	 */
96
	public function testExpanderModule($bbcode, $expected){
97
		$parsed = $this->parser->parse($bbcode);
98
		$parsed = preg_replace('/\"([a-f\d]{8})\"/i', '"abcdef12"', $parsed);
99
		$this->assertEquals($expected, $parsed);
100
	}
101
102
	public function linkDataProvider(){
103
		return [
104
			['[url]https://github.com/chillerlan/bbcode[/url]', '<a href="https://github.com/chillerlan/bbcode" target="_blank" class="blank">https://github.com/chillerlan/bbcode</a>'],
105
			['[url=https://github.com/chillerlan/bbcode]chillerlan/bbcode[/url]', '<a href="https://github.com/chillerlan/bbcode" target="_blank" class="blank">chillerlan/bbcode</a>'],
106
			['[url=https://github.com/chillerlan/bbcode title=\'some stuff\' class=some-css-class]chillerlan/bbcode[/url]', '<a href="https://github.com/chillerlan/bbcode" target="_blank" title="some stuff" class="some-css-class blank">chillerlan/bbcode</a>'],
107
		];
108
	}
109
110
	/**
111
	 * @dataProvider linkDataProvider
112
	 */
113
	public function testLinkModule($bbcode, $expected){
114
		$parsed = $this->parser->parse($bbcode);
115
		$this->assertEquals($expected, $parsed);
116
	}
117
118
	public function testListModule(){
119
		$list_inner_bbcode = '[*]blah[*]blubb[*]foo[*]bar';
120
		$list_inner_html = '<li>blah</li><li>blubb</li><li>foo</li><li>bar</li>';
121
122
		foreach(['0' => 'decimal-leading-zero', '1' => 'decimal', 'a' => 'lower-alpha',
123
				'A' => 'upper-alpha', 'i' => 'lower-roman', 'I' => 'upper-roman'] as $type => $css){
124
			// type only
125
			$parsed = $this->parser->parse('[list type='.$type.']'.$list_inner_bbcode.'[/list]');
126
			$this->assertEquals('<ol class="bb-list '.$css.'">'.$list_inner_html.'</ol>', $parsed);
127
			// reversed
128
			$parsed = $this->parser->parse('[list type='.$type.' reversed=1]'.$list_inner_bbcode.'[/list]');
129
			$this->assertEquals('<ol reversed="true" class="bb-list '.$css.'">'.$list_inner_html.'</ol>', $parsed);
130
			// start
131
			$parsed = $this->parser->parse('[list=42 type='.$type.']'.$list_inner_bbcode.'[/list]');
132
			$this->assertEquals('<ol start="42" class="bb-list '.$css.'">'.$list_inner_html.'</ol>', $parsed);
133
			// all
134
			$parsed = $this->parser->parse('[list=42 type='.$type.' reversed=1 class=foobar title=WAT]'.$list_inner_bbcode.'[/list]');
135
			$this->assertEquals('<ol start="42" reversed="true" title="WAT" class="foobar bb-list '.$css.'">'.$list_inner_html.'</ol>', $parsed);
136
		}
137
138
		foreach(['c' => 'circle', 'd' => 'disc', 's' => 'square'] as $type => $css){
139
			$expected = '<ul class="bb-list '.$css.'">'.$list_inner_html.'</ul>';
140
			// type only
141
			$parsed = $this->parser->parse('[list type='.$type.']'.$list_inner_bbcode.'[/list]');
142
			$this->assertEquals($expected, $parsed);
143
144
			// should not happen...
145
			$parsed = $this->parser->parse('[list=42 reversed=1 type='.$type.']'.$list_inner_bbcode.'[/list]');
146
			$this->assertEquals($expected, $parsed);
147
		}
148
	}
149
150
	public function testSimpletextModule(){
151
		foreach(array_keys($this->parser->getTagmap(), Simpletext::class) as $tag){
152
			$parsed = $this->parser->parse('['.$tag.' class=foobar]WAT[/'.$tag.']');
153
			$this->assertEquals('<'.$tag.' class="foobar">WAT</'.$tag.'>', $parsed);
154
		}
155
	}
156
157
	public function testSingletagModule(){
158
		foreach(array_keys($this->parser->getTagmap(), Singletags::class) as $tag){
159
			$parsed = $this->parser->parse('['.$tag.']');
160
			$expected = $tag === 'clear' ? '<br class="bb-clear both" />' : '<'.$tag.' />';
161
			$this->assertEquals($expected, $parsed);
162
		}
163
	}
164
165
	public function styledTextDataProvider(){
166
		return [
167
			['[color=#424242]color[/color]', '<span class="bb-text color" style="color:#424242">color</span>'],
168
			['[font=Helvetica]font[/font]', '<span class="bb-text font" style="font-family:Helvetica">font</span>'],
169
			['[size=42px]size[/size]', '<span class="bb-text size" style="font-size:42px">size</span>'],
170
			['[tt]typewriter[/tt]', '<span class="bb-text typewriter">typewriter</span>'],
171
			['[i]italic[/i]', '<span class="bb-text italic">italic</span>'],
172
			['[b]bold[/b]', '<span class="bb-text bold">bold</span>'],
173
			['[s]strikethrough[/s]', '<span class="bb-text linethrough">strikethrough</span>'],
174
			['[u]underline[/u]', '<span class="bb-text underline">underline</span>'],
175
		];
176
	}
177
178
	/**
179
	 * @dataProvider styledTextDataProvider
180
	 */
181
	public function testStyledTextModule($bbcode, $expected){
182
		$parsed = $this->parser->parse($bbcode);
183
		$this->assertEquals($expected, $parsed);
184
	}
185
186
	public function videoURLDataProvider(){
187
		return [
188
			['https://vimeo.com/136964218', '<iframe src="https://player.vimeo.com/video/136964218" allowfullscreen></iframe>'],
189
			['https://www.youtube.com/watch?v=6r1-HTiwGiY', '<iframe src="https://www.youtube.com/embed/6r1-HTiwGiY" allowfullscreen></iframe>'],
190
			['http://youtu.be/6r1-HTiwGiY', '<iframe src="https://www.youtube.com/embed/6r1-HTiwGiY" allowfullscreen></iframe>'],
191
			['http://www.moddb.com/media/embed/72159', '<iframe src="http://www.moddb.com/media/iframe/72159" allowfullscreen></iframe>'],
192
			['http://dai.ly/x3sjscz', '<iframe src="http://www.dailymotion.com/embed/video/x3sjscz" allowfullscreen></iframe>'],
193
			['http://www.dailymotion.com/video/x3sjscz_the-bmw-m2-is-here-but-does-it-live-up-to-its-legendary-badge-let-s-try-it-out_tech', '<iframe src="http://www.dailymotion.com/embed/video/x3sjscz" allowfullscreen></iframe>'],
194
		];
195
	}
196
197
	/**
198
	 * @dataProvider videoURLDataProvider
199
	 */
200
/*	public function testVideoModuleURLMatch($url, $expected){
201
		// this test will fail on travis due to missing credentials
202
		$parsed = $this->parser->parse('[video]'.$url.'[/video]');
203
		$this->assertEquals('<div class="bb-video">'.$expected.'</div>', $parsed);
204
	}*/
205
206
	public function videoBBCodeDataProvider(){
207
		return [
208
			['[video]http://youtu.be/6r1-HTiwGiY[/video]', '<div class="bb-video"><iframe src="https://www.youtube.com/embed/6r1-HTiwGiY" allowfullscreen></iframe></div>'],
209
			['[youtube]http://youtu.be/6r1-HTiwGiY[/youtube]', '<div class="bb-video"><iframe src="https://www.youtube.com/embed/6r1-HTiwGiY" allowfullscreen></iframe></div>'],
210
			['[youtube]6r1-HTiwGiY[/youtube]', '<div class="bb-video"><iframe src="https://www.youtube.com/embed/6r1-HTiwGiY" allowfullscreen></iframe></div>'],
211
			['[youtube flash=1]6r1-HTiwGiY[/youtube]', '<div class="bb-video"><object type="application/x-shockwave-flash" data="https://www.youtube.com/v/6r1-HTiwGiY"><param name="allowfullscreen" value="true"><param name="wmode" value="opaque" /><param name="movie" value="https://www.youtube.com/v/6r1-HTiwGiY" /></object></div>'],
212
			['[youtube wide=1]6r1-HTiwGiY[/youtube]', '<div class="bb-video wide"><iframe src="https://www.youtube.com/embed/6r1-HTiwGiY" allowfullscreen></iframe></div>'],
213
			['[youtube flash=1 wide=1]6r1-HTiwGiY[/youtube]', '<div class="bb-video wide"><object type="application/x-shockwave-flash" data="https://www.youtube.com/embed/6r1-HTiwGiY"><param name="allowfullscreen" value="true"><param name="wmode" value="opaque" /><param name="movie" value="https://www.youtube.com/embed/6r1-HTiwGiY" /></object></div>'],
214
			['[video]http://some.video.url/whatever[/video]', '<video src="http://some.video.url/whatever" class="bb-video" preload="auto" controls="true"></video>'],
215
		];
216
	}
217
218
	/**
219
	 * @dataProvider videoBBCodeDataProvider
220
	 */
221
/*	public function testVideoModuleBBCode($bbcode, $expected){
222
		// this test will fail on travis due to missing credentials
223
		$parsed = $this->parser->parse($bbcode);
224
		$this->assertEquals($expected, $parsed);
225
226
	}*/
227
228
	public function tableDataProvider(){
229
		return [
230
			['[table width=300px class=mybbtyble][tr][td]foobar[/td][/tr][/table]', '<table class="mybbtyble bb-table" style="width:300px"><tr><td>foobar</td></tr></table>'],
231
#			['[table][/table]', ''],
232
		];
233
	}
234
235
	/**
236
	 * @dataProvider tableDataProvider
237
	 */
238
	public function testTableModule($bbcode, $expected){
239
		$parsed = $this->parser->parse($bbcode);
240
		$this->assertEquals($expected, $parsed);
241
	}
242
243
244
}
245