|
1
|
|
|
<?php |
|
2
|
|
|
namespace FwlibTest\Html\Generator\Element; |
|
3
|
|
|
|
|
4
|
|
|
use Fwlib\Html\Generator\Element\AbstractJsAlert; |
|
5
|
|
|
use Fwlib\Html\Generator\ElementMode; |
|
6
|
|
|
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase; |
|
7
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @copyright Copyright 2014-2015 Fwolf |
|
11
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
|
12
|
|
|
*/ |
|
13
|
|
|
class AbstractJsAlertTest extends PHPUnitTestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param string[] $methods |
|
17
|
|
|
* @return MockObject|AbstractJsAlert |
|
18
|
|
|
*/ |
|
19
|
|
View Code Duplication |
protected function buildMock(array $methods = null) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
if (is_null($methods)) { |
|
22
|
|
|
$methods = []; |
|
23
|
|
|
} |
|
24
|
|
|
$methods = array_merge($methods, ['getJsPath']); |
|
25
|
|
|
|
|
26
|
|
|
$mock = $this->getMock( |
|
27
|
|
|
AbstractJsAlert::class, |
|
28
|
|
|
$methods |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
$mock->expects($this->any()) |
|
32
|
|
|
->method('getJsPath') |
|
33
|
|
|
->willReturn('/path/to/js'); |
|
34
|
|
|
|
|
35
|
|
|
return $mock; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
public function testGetOutputForShowMode() |
|
40
|
|
|
{ |
|
41
|
|
|
$element = $this->buildMock(); |
|
42
|
|
|
|
|
43
|
|
|
$element->setConfigs([ |
|
|
|
|
|
|
44
|
|
|
'messages' => [], |
|
45
|
|
|
]) |
|
46
|
|
|
->setTitle('dummy'); |
|
47
|
|
|
$output = $element->getOutput(ElementMode::SHOW); |
|
|
|
|
|
|
48
|
|
|
$this->assertEmpty($output); |
|
49
|
|
|
|
|
50
|
|
|
// First output will load js |
|
51
|
|
|
$element->setConfig('messages', ['foo', 'bar']); |
|
|
|
|
|
|
52
|
|
|
$output = $element->getOutput(ElementMode::SHOW); |
|
53
|
|
|
$expectedOutput = <<<TAG |
|
54
|
|
|
<script type='text/javascript' src='/path/to/js'></script> |
|
55
|
|
|
<script type='text/javascript'> |
|
56
|
|
|
<!-- |
|
57
|
|
|
(function () { |
|
58
|
|
|
JsAlert( |
|
59
|
|
|
['foo', 'bar'], |
|
60
|
|
|
'dummy', |
|
61
|
|
|
'', |
|
62
|
|
|
true, |
|
63
|
|
|
true |
|
64
|
|
|
); |
|
65
|
|
|
}) (); |
|
66
|
|
|
--> |
|
67
|
|
|
</script> |
|
68
|
|
|
TAG; |
|
69
|
|
|
$this->assertEquals($expectedOutput, $output); |
|
70
|
|
|
|
|
71
|
|
|
// Second output will not load js |
|
72
|
|
|
$output = $element->getOutput(ElementMode::SHOW); |
|
73
|
|
|
$this->assertRegExp( |
|
74
|
|
|
"/^(?!<script type='text\\/javascript' src=$).*/", |
|
75
|
|
|
$output |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
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.