1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use Smr\Container\DiContainer; |
6
|
|
|
use Smr\Template; |
7
|
|
|
use SmrTest\TestUtils; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers Smr\Template |
11
|
|
|
*/ |
12
|
|
|
class TemplateTest extends \PHPUnit\Framework\TestCase { |
13
|
|
|
|
14
|
|
|
protected function setUp(): void { |
15
|
|
|
// Start each test with a fresh container (and Template instance). |
16
|
|
|
// This ensures the independence of each test. |
17
|
|
|
DiContainer::initializeContainer(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function test_assign_unassign() { |
21
|
|
|
$template = Template::getInstance(); |
22
|
|
|
$template->assign('foo', 'bar'); |
23
|
|
|
$this->assertTrue($template->hasTemplateVar('foo')); |
24
|
|
|
$template->unassign('foo'); |
25
|
|
|
$this->assertFalse($template->hasTemplateVar('foo')); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function test_assign_same_variable_twice_throws() { |
29
|
|
|
$template = Template::getInstance(); |
30
|
|
|
$template->assign('foo', 'bar'); |
31
|
|
|
$this->expectException(\Exception::class); |
32
|
|
|
$this->expectExceptionMessage('Cannot re-assign template variable \'foo\'!'); |
33
|
|
|
try { |
34
|
|
|
$template->assign('foo', 'barbar'); |
35
|
|
|
} catch (\Exception $err) { |
36
|
|
|
$template->unassign('foo'); // avoid destructor warning |
37
|
|
|
throw $err; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @dataProvider checkDisableAJAX_provider |
43
|
|
|
*/ |
44
|
|
|
public function test_checkDisableAJAX(string $html, bool $expected) { |
45
|
|
|
$template = Template::getInstance(); |
46
|
|
|
$method = TestUtils::getPrivateMethod($template, 'checkDisableAJAX'); |
47
|
|
|
$this->assertSame($expected, $method->invoke($template, $html)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function checkDisableAJAX_provider() : array { |
51
|
|
|
return [ |
52
|
|
|
// Special input types that do not disable ajax |
53
|
|
|
['<input type="submit">', false], |
54
|
|
|
['<input type="hidden">', false], |
55
|
|
|
['<input type="image">', false], |
56
|
|
|
// All other input types *do* disable ajax |
57
|
|
|
['<input type="checkbox">', true], |
58
|
|
|
['<input type="number">', true], |
59
|
|
|
// Random HTML not related to inputs does not disable ajax |
60
|
|
|
['bla', false], |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @dataProvider convertHtmlToAjaxXml_provider |
66
|
|
|
*/ |
67
|
|
|
public function test_convertHtmlToAjaxXml(string $html, string $expected) { |
68
|
|
|
$template = Template::getInstance(); |
69
|
|
|
$method = TestUtils::getPrivateMethod($template, 'convertHtmlToAjaxXml'); |
70
|
|
|
$this->assertSame($expected, $method->invoke($template, $html, true)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function convertHtmlToAjaxXml_provider() : array { |
74
|
|
|
return [ |
75
|
|
|
// Span with an id |
76
|
|
|
['<span id="foo">Test</span>', '<foo>Test</foo>'], |
77
|
|
|
// Non-span with the ajax class |
78
|
|
|
['<div id="bar" class="ajax">Hello</div>', '<bar>Hello</bar>'], |
79
|
|
|
// Non-span *without* the ajax class |
80
|
|
|
['<div id="bar">Goodbye</div>', ''], |
81
|
|
|
// Middle panel with content that doesn't disable ajax |
82
|
|
|
['<div id="middle_panel">Foo</div>', '<middle_panel>Foo</middle_panel>'], |
83
|
|
|
['<div id="middle_panel"><input type="submit"></div>', '<middle_panel><input type="submit"></middle_panel>'], |
84
|
|
|
// Middle panel with ajax disabled by a specific input type |
85
|
|
|
['<div id="middle_panel"><form id="foo"><input type="checkbox"></form></div>', ''], |
86
|
|
|
// Empty string |
87
|
|
|
['', ''], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|