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