PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PhptalTest.php

http://github.com/pornel/PHPTAL
PHP | 287 lines | 234 code | 36 blank | 17 comment | 1 complexity | 30a145140ea145f068333904583452ec MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * PHPTAL templating engine
  4. *
  5. * PHP Version 5
  6. *
  7. * @category HTML
  8. * @package PHPTAL
  9. * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
  10. * @author Kornel LesiƄski <kornel@aardvarkmedia.co.uk>
  11. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  12. * @version SVN: $Id$
  13. * @link http://phptal.org/
  14. */
  15. class PhptalTest extends PHPTAL_TestCase
  16. {
  17. function test01()
  18. {
  19. $tpl = $this->newPHPTAL('input/phptal.01.html');
  20. $tpl->setOutputMode(PHPTAL::XML);
  21. $res = $tpl->execute();
  22. $this->assertEquals('<dummy/>', $res);
  23. }
  24. function testXmlHeader()
  25. {
  26. $tpl = $this->newPHPTAL('input/phptal.02.html');
  27. $res = normalize_html($tpl->execute());
  28. $exp = normalize_html_file('output/phptal.02.html');
  29. $this->assertEquals($exp, $res);
  30. }
  31. function testExceptionNoEcho()
  32. {
  33. $tpl = $this->newPHPTAL('input/phptal.03.html');
  34. ob_start();
  35. try {
  36. $res = $tpl->execute();
  37. }
  38. catch (Exception $e)
  39. {
  40. }
  41. $c = ob_get_contents();
  42. ob_end_clean();
  43. $this->assertEquals('', $c);
  44. }
  45. function testRepositorySingle()
  46. {
  47. $tpl = $this->newPHPTAL('phptal.01.html');
  48. $tpl->setTemplateRepository('input');
  49. $tpl->setOutputMode(PHPTAL::XML);
  50. $res = $tpl->execute();
  51. $this->assertEquals('<dummy/>', $res);
  52. }
  53. function testRepositorySingleWithSlash()
  54. {
  55. $tpl = $this->newPHPTAL('phptal.01.html');
  56. $tpl->setTemplateRepository('input/');
  57. $tpl->setOutputMode(PHPTAL::XML);
  58. $res = $tpl->execute();
  59. $this->assertEquals('<dummy/>', $res);
  60. }
  61. function testRepositoryMuliple()
  62. {
  63. $tpl = $this->newPHPTAL('phptal.01.html');
  64. $tpl->setTemplateRepository(array('bar', 'input/'));
  65. $tpl->setOutputMode(PHPTAL::XML);
  66. $res = $tpl->execute();
  67. $this->assertEquals('<dummy/>', $res);
  68. }
  69. function testSetTemplate()
  70. {
  71. $tpl = $this->newPHPTAL();
  72. $tpl->setTemplateRepository(array('bar', 'input/'));
  73. $tpl->setOutputMode(PHPTAL::XML);
  74. $tpl->setTemplate('phptal.01.html');
  75. $res = $tpl->execute();
  76. $this->assertEquals('<dummy/>', $res);
  77. }
  78. function testXmlMode()
  79. {
  80. $tpl = $this->newPHPTAL('input/xml.04.xml');
  81. $tpl->setOutputMode(PHPTAL::XML);
  82. $res = $tpl->execute();
  83. $exp = file_get_contents('input/xml.04.xml');
  84. $this->assertXMLEquals($exp, $res);
  85. }
  86. function testSource()
  87. {
  88. $source = '<span tal:content="foo"/>';
  89. $tpl = $this->newPHPTAL();
  90. $tpl->foo = 'foo value';
  91. $tpl->setSource($source);
  92. $res = $tpl->execute();
  93. $this->assertEquals('<span>foo value</span>', $res);
  94. $this->assertMatchesRegularExpression('/^tpl_\d{8}_/', $tpl->getFunctionName());
  95. $this->assertStringContainsString('string', $tpl->getFunctionName());
  96. $this->assertStringNotContainsString(PHPTAL_VERSION, $tpl->getFunctionName());
  97. }
  98. /**
  99. * @todo: write it :)
  100. */
  101. function testFunctionNameChangesWhenSettingsChange()
  102. {
  103. $this->markTestIncomplete();
  104. }
  105. function testSourceWithPath()
  106. {
  107. $source = '<span tal:content="foo"/>';
  108. $tpl = $this->newPHPTAL();
  109. $tpl->foo = 'foo value';
  110. $tpl->setSource($source, $fakename = 'abc12345');
  111. $res = $tpl->execute();
  112. $this->assertEquals('<span>foo value</span>', $res);
  113. $this->assertMatchesRegularExpression('/^tpl_\d{8}_/', $tpl->getFunctionName());
  114. $this->assertStringContainsString($fakename, $tpl->getFunctionName());
  115. $this->assertStringNotContainsString(PHPTAL_VERSION, $tpl->getFunctionName());
  116. }
  117. function testStripComments()
  118. {
  119. $tpl = $this->newPHPTAL('input/phptal.04.html');
  120. $exp = normalize_html_file('output/phptal.04.html');
  121. $tpl->stripComments(true);
  122. $res = $tpl->execute();
  123. $res = normalize_html($res);
  124. $this->assertEquals($exp, $res);
  125. }
  126. function testStripCommentsReset()
  127. {
  128. $tpl = $this->newPHPTAL('input/phptal.04.html');
  129. $exp = normalize_html_file('output/phptal.04.html');
  130. $tpl->stripComments(false);
  131. $tpl->stripComments(true);
  132. $res = $tpl->execute();
  133. $res = normalize_html($res);
  134. $this->assertEquals($exp, $res);
  135. }
  136. function testStripCommentsUnset()
  137. {
  138. $tpl = $this->newPHPTAL('input/phptal.04.html');
  139. $exp = normalize_html_file('input/phptal.04.html');
  140. $tpl->stripComments(true);
  141. $tpl->stripComments(false);
  142. $res = $tpl->execute();
  143. $res = normalize_html($res);
  144. $this->assertEquals($exp, $res);
  145. }
  146. function testUnknownOutputMode()
  147. {
  148. try {
  149. $tpl = $this->newPHPTAL();
  150. $tpl->setOutputMode('unknown');
  151. $this->assertTrue(false);
  152. }
  153. catch (PHPTAL_Exception $e)
  154. {
  155. $this->assertTrue(true);
  156. }
  157. }
  158. function testZeroedContent()
  159. {
  160. $tpl = $this->newPHPTAL('input/phptal.05.html');
  161. $res = $tpl->execute();
  162. $exp = normalize_html_file('input/phptal.05.html');
  163. $this->assertEquals($exp, $res);
  164. }
  165. function testOnlineExpression()
  166. {
  167. $tpl = $this->newPHPTAL('input/phptal.06.html');
  168. $tpl->foo = '<p>hello</p>';
  169. $res = $tpl->execute();
  170. $exp = normalize_html_file('output/phptal.06.html');
  171. $this->assertEquals($exp, $res);
  172. }
  173. function testDirAsTemplate()
  174. {
  175. try {
  176. $tpl = $this->newPHPTAL(dirname(__FILE__));
  177. $tpl->execute();
  178. $this->fail("Executed directory as if it was a template file");
  179. }
  180. catch(PHPTAL_IOException $e) {
  181. // ok
  182. }
  183. catch(PHPTAL_Exception $e) {
  184. $this->fail("Thrown exception ".get_class($e)." (".$e->getMessage().") rather than PHPTAL_IOException");
  185. }
  186. }
  187. function testEncodingUppercase()
  188. {
  189. $tpl = $this->newPHPTAL();
  190. $tpl->setEncoding('utf-8');
  191. $this->assertEquals('UTF-8', $tpl->getEncoding());
  192. }
  193. function testPHPParseErrorDoesNotStopPHPTAL2()
  194. {
  195. $tpl = $this->newPHPTAL()->setSource('<x tal:content="php:\'deliberate parse\' \'error test\'"/>');
  196. $this->expectException(PHPTAL_TemplateException::class);
  197. ob_start();
  198. echo "\n".__CLASS__."::testPHPParseErrorDoesNotStopPHPTAL2 failed\n";
  199. try {
  200. @$tpl->execute(); // if test dies for no apparent reason, the reason is '@'
  201. }
  202. catch(Exception $e) {
  203. ob_end_clean();
  204. throw $e;
  205. }
  206. ob_end_clean();
  207. }
  208. function testThrowsIfNoTemplate()
  209. {
  210. $tpl = $this->newPHPTAL();
  211. $this->expectException(PHPTAL_ConfigurationException::class);
  212. $tpl->execute();
  213. }
  214. function testCreateMethod()
  215. {
  216. $obj = PHPTAL::create();
  217. $this->assertInstanceOf('PHPTAL', $obj);
  218. try {
  219. $obj->execute();
  220. $this->fail("Should not execute without template");
  221. }
  222. catch(PHPTAL_ConfigurationException $e) {
  223. $this->assertStringContainsString('No template', $e->getMessage());
  224. }
  225. }
  226. function testCreateWithFileMethod()
  227. {
  228. $obj = PHPTAL::create('input/phptal.01.html');
  229. $this->assertInstanceOf('PHPTAL', $obj);
  230. $obj->getCodePath();
  231. }
  232. function testDoctypeWithClone()
  233. {
  234. $tpl = $this->newPHPTAL();
  235. $tpl->setTemplate('input/phptal.07.html');
  236. $tpl->execute();
  237. $tpl2 = clone $tpl;
  238. $tpl2->setTemplate('input/phptal.09.html');
  239. $res = $tpl2->execute();
  240. $this->assertStringContainsString('DOCTYPE',$res);
  241. }
  242. function testDoctypeWithoutClone()
  243. {
  244. $tpl = $this->newPHPTAL();
  245. $tpl->setTemplate('input/phptal.07.html');
  246. $tpl->execute();
  247. $tpl->setTemplate('input/phptal.09.html');
  248. $res = $tpl->execute();
  249. $this->assertStringContainsString('DOCTYPE',$res);
  250. }
  251. }