PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/classes/fTemplating/fTemplatingTest.php

https://bitbucket.org/dsqmoore/flourish
PHP | 518 lines | 473 code | 44 blank | 1 comment | 0 complexity | f76306303d1c0367fc5974caa50653a1 MD5 | raw file
  1. <?php
  2. require_once('./support/init.php');
  3. class fTemplatingTest extends PHPUnit_Framework_TestCase
  4. {
  5. public function setUp()
  6. {
  7. mkdir('./output/minification_cache/');
  8. mkdir('./output/php_cache/');
  9. }
  10. public function testSet()
  11. {
  12. $tmpl = new fTemplating();
  13. $tmpl->set('foo', FALSE);
  14. $this->assertEquals(FALSE, $tmpl->get('foo'));
  15. }
  16. public function testSetArraySyntax()
  17. {
  18. $tmpl = new fTemplating();
  19. $tmpl->set('foo[bar]', FALSE);
  20. $this->assertEquals(array('bar' => FALSE), $tmpl->get('foo'));
  21. }
  22. public function testSetMestedArraySyntax()
  23. {
  24. $tmpl = new fTemplating();
  25. $tmpl->set('foo[bar][baz]', TRUE);
  26. $this->assertEquals(
  27. array('bar' => array('baz' => TRUE)),
  28. $tmpl->get('foo')
  29. );
  30. }
  31. public function testSetArray()
  32. {
  33. $tmpl = new fTemplating();
  34. $tmpl->set('foo', FALSE);
  35. $tmpl->set(array(
  36. 'foo' => TRUE,
  37. 'bar' => '2'
  38. ));
  39. $this->assertEquals(TRUE, $tmpl->get('foo'));
  40. $this->assertEquals('2', $tmpl->get('bar'));
  41. }
  42. public function testAdd()
  43. {
  44. $tmpl = new fTemplating();
  45. $tmpl->add('foo', TRUE);
  46. $this->assertEquals(
  47. array(TRUE),
  48. $tmpl->get('foo')
  49. );
  50. $tmpl->add('foo', FALSE);
  51. $this->assertEquals(
  52. array(TRUE, FALSE),
  53. $tmpl->get('foo')
  54. );
  55. }
  56. public function testAddBeginning()
  57. {
  58. $tmpl = new fTemplating();
  59. $tmpl->add('foo', TRUE);
  60. $this->assertEquals(
  61. array(TRUE),
  62. $tmpl->get('foo')
  63. );
  64. $tmpl->add('foo', FALSE, TRUE);
  65. $this->assertEquals(
  66. array(FALSE, TRUE),
  67. $tmpl->get('foo')
  68. );
  69. }
  70. public function testAddArraySyntax()
  71. {
  72. $tmpl = new fTemplating();
  73. $tmpl->add('foo[bar]', TRUE);
  74. $this->assertEquals(
  75. array('bar' => array(TRUE)),
  76. $tmpl->get('foo')
  77. );
  78. $tmpl->add('foo', FALSE);
  79. $this->assertEquals(
  80. array('bar' => array(TRUE), FALSE),
  81. $tmpl->get('foo')
  82. );
  83. }
  84. public function testAddNestedArraySyntax()
  85. {
  86. $tmpl = new fTemplating();
  87. $tmpl->add('foo[bar][baz]', TRUE);
  88. $this->assertEquals(
  89. array('bar' => array('baz' => array(TRUE))),
  90. $tmpl->get('foo')
  91. );
  92. $tmpl->add('foo', FALSE);
  93. $this->assertEquals(
  94. array('bar' => array('baz' => array(TRUE)), FALSE),
  95. $tmpl->get('foo')
  96. );
  97. }
  98. public function testRemove()
  99. {
  100. $tmpl = new fTemplating();
  101. $tmpl->add('foo', TRUE);
  102. $this->assertEquals(
  103. array(TRUE),
  104. $tmpl->get('foo')
  105. );
  106. $tmpl->remove('foo');
  107. $this->assertEquals(
  108. array(),
  109. $tmpl->get('foo')
  110. );
  111. }
  112. public function testRemoveBeginning()
  113. {
  114. $tmpl = new fTemplating();
  115. $tmpl->add('foo', TRUE);
  116. $this->assertEquals(
  117. array(TRUE),
  118. $tmpl->get('foo')
  119. );
  120. $tmpl->add('foo', FALSE);
  121. $this->assertEquals(
  122. array(TRUE, FALSE),
  123. $tmpl->get('foo')
  124. );
  125. $tmpl->remove('foo', TRUE);
  126. $this->assertEquals(
  127. array(FALSE),
  128. $tmpl->get('foo')
  129. );
  130. }
  131. public function testRemoveArraySyntax()
  132. {
  133. $tmpl = new fTemplating();
  134. $tmpl->add('foo[bar]', TRUE);
  135. $this->assertEquals(
  136. array('bar' => array(TRUE)),
  137. $tmpl->get('foo')
  138. );
  139. $tmpl->remove('foo[bar]');
  140. $this->assertEquals(
  141. array('bar' => array()),
  142. $tmpl->get('foo')
  143. );
  144. }
  145. public function testRemoveNestedArraySyntax()
  146. {
  147. $tmpl = new fTemplating();
  148. $tmpl->add('foo[bar][baz]', TRUE);
  149. $this->assertEquals(
  150. array('bar' => array('baz' => array(TRUE))),
  151. $tmpl->get('foo')
  152. );
  153. $tmpl->add('foo', FALSE);
  154. $this->assertEquals(
  155. array('bar' => array('baz' => array(TRUE)), FALSE),
  156. $tmpl->get('foo')
  157. );
  158. $tmpl->remove('foo[bar][baz]');
  159. $this->assertEquals(
  160. array('bar' => array('baz' => array()), FALSE),
  161. $tmpl->get('foo')
  162. );
  163. }
  164. public function testGet()
  165. {
  166. $tmpl = new fTemplating();
  167. $tmpl->set('foo', TRUE);
  168. $tmpl->set('bar', '2');
  169. $this->assertEquals(
  170. TRUE,
  171. $tmpl->get('foo')
  172. );
  173. }
  174. public function testGetArraySyntax()
  175. {
  176. $tmpl = new fTemplating();
  177. $tmpl->set('foo', array('bar' => TRUE));
  178. $tmpl->set('bar', '2');
  179. $this->assertEquals(
  180. TRUE,
  181. $tmpl->get('foo[bar]')
  182. );
  183. }
  184. public function testGetNestedArraySyntax()
  185. {
  186. $tmpl = new fTemplating();
  187. $tmpl->set('foo', array('bar' => array('baz' => TRUE)));
  188. $tmpl->set('bar', '2');
  189. $this->assertEquals(
  190. TRUE,
  191. $tmpl->get('foo[bar][baz]')
  192. );
  193. }
  194. public function testGetDefault()
  195. {
  196. $tmpl = new fTemplating();
  197. $tmpl->set('foo', TRUE);
  198. $tmpl->set('bar', '2');
  199. $this->assertEquals(
  200. '3',
  201. $tmpl->get('baz', '3')
  202. );
  203. }
  204. public function testGetArray()
  205. {
  206. $tmpl = new fTemplating();
  207. $tmpl->set('foo', TRUE);
  208. $tmpl->set('bar', '2');
  209. $this->assertEquals(
  210. array(
  211. 'foo' => TRUE,
  212. 'bar' => '2'
  213. ),
  214. $tmpl->get(array('foo', 'bar'))
  215. );
  216. }
  217. public function testGetArrayDefaults()
  218. {
  219. $tmpl = new fTemplating();
  220. $tmpl->set('foo', TRUE);
  221. $tmpl->set('bar', '2');
  222. $this->assertEquals(
  223. array(
  224. 'foo' => TRUE,
  225. 'bar' => '2',
  226. 'baz' => '3'
  227. ),
  228. $tmpl->get(array(
  229. 'foo' => NULL,
  230. 'bar' => '1',
  231. 'baz' => '3'
  232. ))
  233. );
  234. }
  235. public function testDelete()
  236. {
  237. $tmpl = new fTemplating();
  238. $tmpl->set('foo', 1);
  239. $tmpl->set('bar', 2);
  240. $tmpl->delete('foo');
  241. $this->assertEquals(
  242. array(
  243. 'foo' => NULL,
  244. 'bar' => 2
  245. ),
  246. $tmpl->get(array('foo', 'bar'))
  247. );
  248. }
  249. public function testDeleteDefault()
  250. {
  251. $tmpl = new fTemplating();
  252. $tmpl->set('foo', 1);
  253. $tmpl->set('bar', 2);
  254. $this->assertEquals(3, $tmpl->delete('baz', 3));
  255. }
  256. public function testDeleteMultiple()
  257. {
  258. $tmpl = new fTemplating();
  259. $tmpl->set('foo', 1);
  260. $tmpl->set('bar', 2);
  261. $this->assertEquals(
  262. array('foo' => 1, 'bar' => 2),
  263. $tmpl->delete(array('foo', 'bar'))
  264. );
  265. $this->assertEquals(
  266. array(
  267. 'foo' => NULL,
  268. 'bar' => NULL
  269. ),
  270. $tmpl->get(array('foo', 'bar'))
  271. );
  272. }
  273. public function testDeleteMultipleDefault()
  274. {
  275. $tmpl = new fTemplating();
  276. $tmpl->set('foo', 1);
  277. $tmpl->set('bar', 2);
  278. $this->assertEquals(
  279. array('foo' => 1, 'bar' => 2, 'baz' => 4),
  280. $tmpl->delete(array('foo' => 2, 'bar' => 3, 'baz' => 4))
  281. );
  282. $this->assertEquals(
  283. array(
  284. 'foo' => NULL,
  285. 'bar' => NULL
  286. ),
  287. $tmpl->get(array('foo', 'bar'))
  288. );
  289. }
  290. public function testDeleteArraySyntax()
  291. {
  292. $tmpl = new fTemplating();
  293. $tmpl->set('foo', array('bar' => TRUE, 'baz' => FALSE));
  294. $tmpl->delete('foo[bar]');
  295. $this->assertEquals(
  296. array(
  297. 'baz' => FALSE
  298. ),
  299. $tmpl->get('foo')
  300. );
  301. }
  302. public function testDeleteNestedArraySyntax()
  303. {
  304. $tmpl = new fTemplating();
  305. $tmpl->set('foo', array('bar' => array('qux' => TRUE), 'baz' => FALSE));
  306. $tmpl->delete('foo[bar][qux]');
  307. $this->assertEquals(
  308. array(
  309. 'bar' => array(),
  310. 'baz' => FALSE
  311. ),
  312. $tmpl->get('foo')
  313. );
  314. }
  315. public function testFilter()
  316. {
  317. $tmpl = new fTemplating();
  318. $tmpl->set('foo', array(1, 2, 3));
  319. $tmpl->filter('foo', 1);
  320. $this->assertEquals(
  321. array(
  322. 2,
  323. 3
  324. ),
  325. $tmpl->get('foo')
  326. );
  327. }
  328. public function testFilterMultiple()
  329. {
  330. $tmpl = new fTemplating();
  331. $tmpl->set('foo', array(1, 2, 1, 3, 1));
  332. $tmpl->filter('foo', 1);
  333. $this->assertEquals(
  334. array(
  335. 2,
  336. 3
  337. ),
  338. $tmpl->get('foo')
  339. );
  340. }
  341. public function testFilterFuzzy()
  342. {
  343. $tmpl = new fTemplating();
  344. $tmpl->set('foo', array(0, 1, 2, 3));
  345. $tmpl->filter('foo', '');
  346. $this->assertEquals(
  347. array(
  348. 1,
  349. 2,
  350. 3
  351. ),
  352. $tmpl->get('foo')
  353. );
  354. }
  355. public function testFilterNonExistent()
  356. {
  357. $tmpl = new fTemplating();
  358. $tmpl->set('foo', array(0, 1, 2, 3));
  359. $tmpl->filter('bar', '');
  360. }
  361. public function testFilterNonArray()
  362. {
  363. $this->setExpectedException('fProgrammerException');
  364. $tmpl = new fTemplating();
  365. $tmpl->set('foo', 1);
  366. $tmpl->filter('foo', 1);
  367. }
  368. public function testPlaceSubTemplate()
  369. {
  370. $tmpl = new fTemplating();
  371. $tmpl2 = new fTemplating($_SERVER['DOCUMENT_ROOT'], './resources/php/main.php');
  372. $tmpl->set('foo', $tmpl2);
  373. ob_start();
  374. $tmpl->place('foo');
  375. $output = ob_get_clean();
  376. $this->assertEquals('file path: ' . $_SERVER['DOCUMENT_ROOT'] . str_replace('/', DIRECTORY_SEPARATOR, '/resources/php/main.php'), $output);
  377. }
  378. public function testCssMinification()
  379. {
  380. $tmpl = new fTemplating();
  381. $tmpl->enableMinification('development', './output/minification_cache/', $_SERVER['DOCUMENT_ROOT']);
  382. $tmpl->add('css', '/resources/css/foo.css');
  383. $tmpl->add('css', '/resources/css/bar.css');
  384. ob_start();
  385. $tmpl->place('css');
  386. $output = ob_get_clean();
  387. preg_match('#/(\w+\.css)#', $output, $match);
  388. $file = $match[1];
  389. $this->assertEquals(file_get_contents('./resources/css/foo-min.css') . "\n" . file_get_contents('./resources/css/bar-min.css'), file_get_contents('./output/minification_cache/' . $file));
  390. }
  391. public function testCssMinificationAlternate()
  392. {
  393. $tmpl = new fTemplating();
  394. $tmpl->enableMinification('development', './output/minification_cache/', $_SERVER['DOCUMENT_ROOT']);
  395. $tmpl->add('css', '/resources/css/baz.css');
  396. ob_start();
  397. $tmpl->place('css');
  398. $output = ob_get_clean();
  399. preg_match('#/(\w+\.css)#', $output, $match);
  400. $file = $match[1];
  401. $this->assertEquals(file_get_contents('./resources/css/baz-min.css'), file_get_contents('./output/minification_cache/' . $file));
  402. }
  403. public function testCssMinificationDifferentMedia()
  404. {
  405. $tmpl = new fTemplating();
  406. $tmpl->enableMinification('development', './output/minification_cache/', $_SERVER['DOCUMENT_ROOT']);
  407. $tmpl->add('css', '/resources/css/foo.css');
  408. $tmpl->add('css', array('path' => '/resources/css/bar.css', 'media' => 'print'));
  409. ob_start();
  410. $tmpl->place('css');
  411. $output = ob_get_clean();
  412. preg_match_all('#/(\w+\.css)#', $output, $matches, PREG_SET_ORDER);
  413. $this->assertEquals(file_get_contents('./resources/css/foo-min.css'), file_get_contents('./output/minification_cache/' . $matches[0][1]));
  414. $this->assertEquals(file_get_contents('./resources/css/bar-min.css'), file_get_contents('./output/minification_cache/' . $matches[1][1]));
  415. }
  416. public function testJsMinification()
  417. {
  418. $tmpl = new fTemplating();
  419. $tmpl->enableMinification('development', './output/minification_cache/', $_SERVER['DOCUMENT_ROOT']);
  420. $tmpl->add('js', '/resources/js/swfobject.js');
  421. ob_start();
  422. $tmpl->place('js');
  423. $output = ob_get_clean();
  424. preg_match('#/(\w+\.js)#', $output, $match);
  425. $file = $match[1];
  426. $this->assertEquals(file_get_contents('./resources/js/swfobject-min.js'), file_get_contents('./output/minification_cache/' . $file));
  427. }
  428. public function testJsMinificationAlternate()
  429. {
  430. $tmpl = new fTemplating();
  431. $tmpl->enableMinification('development', './output/minification_cache/', $_SERVER['DOCUMENT_ROOT']);
  432. $tmpl->add('js', '/resources/js/baz.js');
  433. ob_start();
  434. $tmpl->place('js');
  435. $output = ob_get_clean();
  436. preg_match('#/(\w+\.js)#', $output, $match);
  437. $file = $match[1];
  438. $this->assertEquals(file_get_contents('./resources/js/baz-min.js'), file_get_contents('./output/minification_cache/' . $file));
  439. }
  440. public function testJsMinificationMultiple()
  441. {
  442. $tmpl = new fTemplating();
  443. $tmpl->enableMinification('development', './output/minification_cache/', $_SERVER['DOCUMENT_ROOT']);
  444. $tmpl->add('js', '/resources/js/foo.js');
  445. $tmpl->add('js', '/resources/js/bar.js');
  446. ob_start();
  447. $tmpl->place('js');
  448. $output = ob_get_clean();
  449. preg_match('#/(\w+\.js)#', $output, $match);
  450. $this->assertEquals(file_get_contents('./resources/js/foo-min.js') . "\n" . file_get_contents('./resources/js/bar-min.js'), file_get_contents('./output/minification_cache/' . $match[1]));
  451. }
  452. public function testFixShortTags()
  453. {
  454. // This is a gross cli wrapper script since we have to test for exit
  455. $code = "require_once './support/init.php'; \$tmpl = new fTemplating(); \$tmpl->enablePHPShortTags('development', './output/php_cache/'); \$tmpl->set('view', './resources/php/short_tags.php'); \$tmpl->place('view');";
  456. $this->assertEquals('<html><body class="foo">hi! how are you<? echo $foo ?><?= echo $bar ?><?= $baz ?><?
  457. echo $qux</body></html>', shell_exec('php -d short_open_tag=0 -r ' . escapeshellarg($code)));
  458. }
  459. public function tearDown()
  460. {
  461. $cache_dir = './output/minification_cache/';
  462. $files = array_diff(scandir($cache_dir), array('.', '..'));
  463. foreach ($files as $file) {
  464. unlink($cache_dir . $file);
  465. }
  466. rmdir($cache_dir);
  467. $php_cache_dir = './output/php_cache/';
  468. $files = array_diff(scandir($php_cache_dir), array('.', '..'));
  469. foreach ($files as $file) {
  470. unlink($php_cache_dir . $file);
  471. }
  472. rmdir($php_cache_dir);
  473. }
  474. }