44
55require_once __DIR__ . '/TestHelper.php ' ;
66require_once RELATIVE_PATH . 'framework/View.php ' ;
7+ require_once RELATIVE_PATH . 'framework/exceptions/MVCException.php ' ;
8+ require_once RELATIVE_PATH . 'framework/exceptions/NotInitializedViewException.php ' ;
9+ require_once RELATIVE_PATH . 'framework/exceptions/TemplateNotFoundException.php ' ;
10+ require_once RELATIVE_PATH . 'framework/exceptions/VariableNotFoundException.php ' ;
711
812class TestableView extends \framework \View
913{
1014 public function __construct ($ content )
1115 {
12- $ this ->tpl = $ content ;
13- $ this ->blocks = [];
14- $ this ->currentBlock = '' ;
16+ parent ::__construct ();
17+ $ this ->replaceTpl ($ content );
1518 }
19+
1620}
1721
1822final class ViewTest extends TestCase
1923{
2024 public function testSetVarReplacesPlaceholderInTemplate (): void
2125 {
26+ $ template = 'Hello {NAME} ' ;
2227 $ view = new TestableView ('Hello {NAME} ' );
2328 $ view ->setVar ('NAME ' , 'World ' );
2429
@@ -27,28 +32,39 @@ public function testSetVarReplacesPlaceholderInTemplate(): void
2732
2833 public function testHideRemovesSpecificBlock (): void
2934 {
30- $ template = '<!-- BEGIN content -->Visible<!-- END content --> ' ;
35+ $ template = '<!-- BEGIN content --> Visible <!-- END content -->More Content ' ;
3136 $ view = new TestableView ($ template );
32-
3337 $ view ->hide ('content ' );
34-
3538 $ this ->assertStringNotContainsString ('Visible ' , $ view ->parse ());
3639 }
3740
38- public function testSetVarTemplatePathInjectsSiteUrl (): void
39- {
40- $ view = new TestableView ('Path: {TEMPLATE_PATH} ' );
41-
42- $ view ->setVarTemplatePath ();
43-
44- $ this ->assertSame ('Path: ' . SITEURL . '/ ' , $ view ->parse ());
45- }
46-
4741 public function testReplaceTplOverridesContent (): void
4842 {
4943 $ view = new TestableView ('Original ' );
5044 $ view ->replaceTpl ('New Content ' );
5145
5246 $ this ->assertSame ('New Content ' , $ view ->parse ());
5347 }
48+
49+ public function testOpenBlockThenParseThenCloseThenShow (): void
50+ {
51+
52+ $ users = array (
53+ array ('UserName ' => 'Mark ' , 'UserEmail ' => 'mark@email.com ' ),
54+ array ('UserName ' => 'Elen ' , 'UserEmail ' => 'elen@email.com ' ),
55+ array ('UserName ' => 'John ' , 'UserEmail ' => 'john@email.com ' )
56+ );
57+ $ template = '<!-- BEGIN Users -->[{UserName} {UserEmail}]<!-- END Users --> ' ;
58+ $ expected = '<!-- BEGIN Users -->[Mark mark@email.com][Elen elen@email.com][John john@email.com]<!-- END Users --> ' ;
59+ $ view = new TestableView ($ template );
60+ $ view ->openBlock ("Users " );
61+ foreach ($ users as $ user ) {
62+ $ view ->setVar ("UserName " , $ user ["UserName " ]);
63+ $ view ->setVar ("UserEmail " , $ user ["UserEmail " ]);
64+ $ view ->parseCurrentBlock ();
65+ }
66+ $ view ->setBlock ();
67+ $ this ->assertSame ($ expected , $ view ->parse ());
68+ }
69+
5470}
0 commit comments