PageRenderTime 4ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/development/PHPunit/CompileForeachTests.php

https://bitbucket.org/hallgrennetworks/smarty
PHP | 200 lines | 178 code | 4 blank | 18 comment | 0 complexity | deb2cda237b685a7f01a0c136b7a771a MD5 | raw file
  1<?php
  2/**
  3* Smarty PHPunit tests compilation of {foreach} tag
  4* 
  5* @package PHPunit
  6* @author Uwe Tews 
  7*/
  8
  9/**
 10* class for {foreach} tag tests
 11*/
 12class CompileForeachTests extends PHPUnit_Framework_TestCase {
 13    public function setUp()
 14    {
 15        $this->smarty = SmartyTests::$smarty;
 16        SmartyTests::init();
 17    } 
 18
 19    public static function isRunnable()
 20    {
 21        return true;
 22    } 
 23
 24    /**
 25    * test {foreach} tag
 26    */
 27    public function testForeach()
 28    {
 29        $tpl = $this->smarty->createTemplate('eval:{assign var=foo value=[0,1,2,3,4,5,6,7,8,9]}{foreach item=x from=$foo}{$x}{/foreach}');
 30        $this->assertEquals("0123456789", $this->smarty->fetch($tpl));
 31    } 
 32    public function testForeachBreak()
 33    {
 34        $tpl = $this->smarty->createTemplate('eval:{assign var=foo value=[0,1,2,3,4,5,6,7,8,9]}{foreach item=x from=$foo}{if $x == 2}{break}{/if}{$x}{/foreach}');
 35        $this->assertEquals("01", $this->smarty->fetch($tpl));
 36    } 
 37    public function testForeachContinue()
 38    {
 39        $tpl = $this->smarty->createTemplate('eval:{assign var=foo value=[0,1,2,3,4,5,6,7,8,9]}{foreach item=x from=$foo}{if $x == 2}{continue}{/if}{$x}{/foreach}');
 40        $this->assertEquals("013456789", $this->smarty->fetch($tpl));
 41    } 
 42    public function testForeachNotElse()
 43    {
 44        $tpl = $this->smarty->createTemplate('eval:{assign var=foo value=[0,1,2,3,4,5,6,7,8,9]}{foreach item=x from=$foo}{$x}{foreachelse}else{/foreach}');
 45        $this->assertEquals("0123456789", $this->smarty->fetch($tpl));
 46    } 
 47    public function testForeachElse()
 48    {
 49 		$this->smarty->error_reporting  = error_reporting() & ~(E_NOTICE|E_USER_NOTICE);
 50        $tpl = $this->smarty->createTemplate('eval:{foreach item=x from=$foo}{$x}{foreachelse}else{/foreach}');
 51        $this->assertEquals("else", $this->smarty->fetch($tpl));
 52    } 
 53    public function testForeachKey()
 54    {
 55        $tpl = $this->smarty->createTemplate('eval:{foreach item=x key=y from=[9,8,7,6,5,4,3,2,1,0]}{$y}{$x}{foreachelse}else{/foreach}');
 56        $this->assertEquals("09182736455463728190", $this->smarty->fetch($tpl));
 57    } 
 58    public function testForeachKeyProperty()
 59    {
 60        $tpl = $this->smarty->createTemplate('eval:{foreach item=x from=[9,8,7,6,5,4,3,2,1,0]}{$x@key}{$x}{foreachelse}else{/foreach}');
 61        $this->assertEquals("09182736455463728190", $this->smarty->fetch($tpl));
 62    } 
 63    public function testForeachTotal()
 64    {
 65        $tpl = $this->smarty->createTemplate('eval:{foreach item=x name=foo from=[0,1,2,3,4,5,6,7,8,9]}{$x}{foreachelse}else{/foreach}total{$smarty.foreach.foo.total}');
 66        $this->assertEquals("0123456789total10", $this->smarty->fetch($tpl));
 67    } 
 68    public function testForeachTotalProperty()
 69    {
 70        $tpl = $this->smarty->createTemplate('eval:{foreach item=x from=[0,1,2,3,4,5,6,7,8,9]}{$x}{foreachelse}else{/foreach}total{$x@total}');
 71        $this->assertEquals("0123456789total10", $this->smarty->fetch($tpl));
 72    } 
 73    public function testForeachIndexIteration()
 74    {
 75        $tpl = $this->smarty->createTemplate('eval:{foreach item=x name=foo from=[0,1,2,3,4,5,6,7,8,9]}{$smarty.foreach.foo.index}{$smarty.foreach.foo.iteration}{foreachelse}else{/foreach}');
 76        $this->assertEquals("011223344556677889910", $this->smarty->fetch($tpl));
 77    } 
 78    public function testForeachIndexIterationProperty()
 79    {
 80        $tpl = $this->smarty->createTemplate('eval:{foreach item=x from=[0,1,2,3,4,5,6,7,8,9]}{$x@index}{$x@iteration}{foreachelse}else{/foreach}');
 81        $this->assertEquals("011223344556677889910", $this->smarty->fetch($tpl));
 82    } 
 83    public function testForeachFirstLast()
 84    {
 85        $tpl = $this->smarty->createTemplate('eval:{foreach item=x name=foo from=[0,1,2,3,4,5,6,7,8,9]}{if $smarty.foreach.foo.first}first{/if}{if $smarty.foreach.foo.last}last{/if}{$x}{foreachelse}else{/foreach}');
 86        $this->assertEquals("first012345678last9", $this->smarty->fetch($tpl));
 87    } 
 88    public function testForeachFirstLastProperty()
 89    {
 90        $tpl = $this->smarty->createTemplate('eval:{foreach item=x name=foo from=[0,1,2,3,4,5,6,7,8,9]}{if $x@first}first{/if}{if $x@last}last{/if}{$x}{foreachelse}else{/foreach}');
 91        $this->assertEquals("first012345678last9", $this->smarty->fetch($tpl));
 92    } 
 93    public function testForeachShowTrue()
 94    {
 95        $tpl = $this->smarty->createTemplate('eval:{foreach item=x name=foo from=[0,1]}{$x}{foreachelse}else{/foreach}{if $smarty.foreach.foo.show}show{else}noshow{/if}');
 96        $this->assertEquals("01show", $this->smarty->fetch($tpl));
 97    }
 98    public function testForeachShowTrueProperty()
 99    {
100        $tpl = $this->smarty->createTemplate('eval:{foreach item=x name=foo from=[0,1]}{$x}{foreachelse}else{/foreach}{if $x@show}show{else}noshow{/if}');
101        $this->assertEquals("01show", $this->smarty->fetch($tpl));
102    } 
103    public function testForeachShowFalse()
104    {
105        $tpl = $this->smarty->createTemplate('eval:{foreach item=x name=foo from=[]}{$x}{foreachelse}else{/foreach}{if $smarty.foreach.foo.show}show{else} noshow{/if}');
106        $this->assertEquals("else noshow", $this->smarty->fetch($tpl));
107    }
108    public function testForeachShowFalseProperty()
109    {
110        $tpl = $this->smarty->createTemplate('eval:{foreach item=x name=foo from=[]}{$x}{foreachelse}else{/foreach}{if $x@show}show{else} noshow{/if}');
111        $this->assertEquals("else noshow", $this->smarty->fetch($tpl));
112    } 
113    public function testForeachShorttags()
114    {
115        $tpl = $this->smarty->createTemplate('eval:{foreach [9,8,7,6,5,4,3,2,1,0] x y foo}{$y}{$x}{foreachelse}else{/foreach}total{$smarty.foreach.foo.total}');
116        $this->assertEquals("09182736455463728190total10", $this->smarty->fetch($tpl));
117    } 
118    /**
119    * test {foreach $foo as $x} tag
120    */
121    public function testNewForeach()
122    {
123        $tpl = $this->smarty->createTemplate('eval:{assign var=foo value=[0,1,2,3,4,5,6,7,8,9]}{foreach $foo as $x}{$x}{/foreach}');
124        $this->assertEquals("0123456789", $this->smarty->fetch($tpl));
125    } 
126    public function testNewForeachNotElse()
127    {
128        $tpl = $this->smarty->createTemplate('eval:{assign var=foo value=[0,1,2,3,4,5,6,7,8,9]}{foreach $foo as $x}{$x}{foreachelse}else{/foreach}');
129        $this->assertEquals("0123456789", $this->smarty->fetch($tpl));
130    } 
131    public function testNewForeachElse()
132    {
133 		$this->smarty->error_reporting  = error_reporting() & ~(E_NOTICE|E_USER_NOTICE);
134        $tpl = $this->smarty->createTemplate('eval:{foreach $foo as $x}{$x}{foreachelse}else{/foreach}');
135        $this->assertEquals("else", $this->smarty->fetch($tpl));
136    } 
137    public function testNewForeachKey()
138    {
139        $tpl = $this->smarty->createTemplate('eval:{assign var=foo value=[9,8,7,6,5,4,3,2,1,0]}{foreach $foo as $y=>$x}{$y}{$x}{foreachelse}else{/foreach}');
140        $this->assertEquals("09182736455463728190", $this->smarty->fetch($tpl));
141    } 
142    public function testNewForeachKeyProperty()
143    {
144        $tpl = $this->smarty->createTemplate('eval:{assign var=foo value=[9,8,7,6,5,4,3,2,1,0]}{foreach $foo as $x}{$x@key}{$x}{foreachelse}else{/foreach}');
145        $this->assertEquals("09182736455463728190", $this->smarty->fetch($tpl));
146    } 
147    /*
148    *  test foreach and nocache
149    */
150    public function testForeachNocacheVar1()
151    {
152		$this->smarty->caching = true;
153        $tpl = $this->smarty->createTemplate('string:{foreach $foo as $x}{$x} {/foreach}');
154    	$tpl->assign('foo',array(1,2),true);
155        $this->assertFalse($this->smarty->isCached($tpl));
156        $this->assertEquals("1 2 ", $this->smarty->fetch($tpl));
157    } 
158    public function testForeachNocacheVar2()
159    {
160		$this->smarty->caching = true;
161        $tpl = $this->smarty->createTemplate('string:{foreach $foo as $x}{$x} {/foreach}');
162    	$tpl->assign('foo',array(9,8),true);
163        $this->assertTrue($this->smarty->isCached($tpl));
164        $this->assertEquals("9 8 ", $this->smarty->fetch($tpl));
165    } 
166    public function testForeachNocacheTag1()
167    {
168		$this->smarty->caching = true;
169        $tpl = $this->smarty->createTemplate('string:{foreach $foo as $x nocache}{$x} {/foreach}');
170    	$tpl->assign('foo',array(1,2));
171        $this->assertFalse($this->smarty->isCached($tpl));
172        $this->assertEquals("1 2 ", $this->smarty->fetch($tpl));
173    } 
174    public function testForeachNocacheTag2()
175    {
176		$this->smarty->caching = true;
177        $tpl = $this->smarty->createTemplate('string:{foreach $foo as $x nocache}{$x} {/foreach}');
178    	$tpl->assign('foo',array(9,8));
179        $this->assertTrue($this->smarty->isCached($tpl));
180        $this->assertEquals("9 8 ", $this->smarty->fetch($tpl));
181    } 
182    public function testForeachCache1()
183    {
184		$this->smarty->caching = true;
185        $tpl = $this->smarty->createTemplate('string:{foreach $foo as $x name=bar}{$x} {/foreach}');
186    	$tpl->assign('foo',array(1,2));
187        $this->assertFalse($this->smarty->isCached($tpl));
188        $this->assertEquals("1 2 ", $this->smarty->fetch($tpl));
189    } 
190    public function testForeachCache2()
191    {
192		$this->smarty->caching = true;
193        $tpl = $this->smarty->createTemplate('string:{foreach $foo as $x name=bar}{$x} {/foreach}');
194    	$tpl->assign('foo',array(9,8));
195        $this->assertTrue($this->smarty->isCached($tpl));
196        $this->assertEquals("1 2 ", $this->smarty->fetch($tpl));
197    } 
198} 
199
200?>