PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/View/Helper/Placeholder/ContainerTest.php

https://github.com/tanduy/zf
PHP | 455 lines | 271 code | 52 blank | 132 comment | 3 complexity | 4b2524904ddd66c0e93eced32c396052 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_View_Helper_Placeholder_Container_AbstractTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_Placeholder_ContainerTest::main");
  25. }
  26. require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/TestHelper.php';
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. require_once "PHPUnit/Framework/TestSuite.php";
  29. /** Zend_View_Helper_Placeholder_Container */
  30. require_once 'Zend/View/Helper/Placeholder/Container.php';
  31. /**
  32. * Test class for Zend_View_Helper_Placeholder_Container.
  33. *
  34. * @category Zend
  35. * @package Zend_View
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_View
  40. * @group Zend_View_Helper
  41. */
  42. class Zend_View_Helper_Placeholder_ContainerTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * @var Zend_View_Helper_Placeholder_Container
  46. */
  47. public $container;
  48. /**
  49. * Runs the test methods of this class.
  50. *
  51. * @return void
  52. */
  53. public static function main()
  54. {
  55. require_once "PHPUnit/TextUI/TestRunner.php";
  56. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_Placeholder_ContainerTest");
  57. $result = PHPUnit_TextUI_TestRunner::run($suite);
  58. }
  59. /**
  60. * Sets up the fixture, for example, open a network connection.
  61. * This method is called before a test is executed.
  62. *
  63. * @return void
  64. */
  65. public function setUp()
  66. {
  67. $this->container = new Zend_View_Helper_Placeholder_Container(array());
  68. }
  69. /**
  70. * Tears down the fixture, for example, close a network connection.
  71. * This method is called after a test is executed.
  72. *
  73. * @return void
  74. */
  75. public function tearDown()
  76. {
  77. unset($this->container);
  78. }
  79. /**
  80. * @return void
  81. */
  82. public function testSetSetsASingleValue()
  83. {
  84. $this->container['foo'] = 'bar';
  85. $this->container['bar'] = 'baz';
  86. $this->assertEquals('bar', $this->container['foo']);
  87. $this->assertEquals('baz', $this->container['bar']);
  88. $this->container->set('foo');
  89. $this->assertEquals(1, count($this->container));
  90. $this->assertEquals('foo', $this->container[0]);
  91. }
  92. /**
  93. * @return void
  94. */
  95. public function testGetValueReturnsScalarWhenOneElementRegistered()
  96. {
  97. $this->container->set('foo');
  98. $this->assertEquals('foo', $this->container->getValue());
  99. }
  100. /**
  101. * @return void
  102. */
  103. public function testGetValueReturnsArrayWhenMultipleValuesPresent()
  104. {
  105. $this->container['foo'] = 'bar';
  106. $this->container['bar'] = 'baz';
  107. $expected = array('foo' => 'bar', 'bar' => 'baz');
  108. $return = $this->container->getValue();
  109. $this->assertEquals($expected, $return);
  110. }
  111. /**
  112. * @return void
  113. */
  114. public function testPrefixAccesorsWork()
  115. {
  116. $this->assertEquals('', $this->container->getPrefix());
  117. $this->container->setPrefix('<ul><li>');
  118. $this->assertEquals('<ul><li>', $this->container->getPrefix());
  119. }
  120. /**
  121. * @return void
  122. */
  123. public function testSetPrefixImplementsFluentInterface()
  124. {
  125. $result = $this->container->setPrefix('<ul><li>');
  126. $this->assertSame($this->container, $result);
  127. }
  128. /**
  129. * @return void
  130. */
  131. public function testPostfixAccesorsWork()
  132. {
  133. $this->assertEquals('', $this->container->getPostfix());
  134. $this->container->setPostfix('</li></ul>');
  135. $this->assertEquals('</li></ul>', $this->container->getPostfix());
  136. }
  137. /**
  138. * @return void
  139. */
  140. public function testSetPostfixImplementsFluentInterface()
  141. {
  142. $result = $this->container->setPostfix('</li></ul>');
  143. $this->assertSame($this->container, $result);
  144. }
  145. /**
  146. * @return void
  147. */
  148. public function testSeparatorAccesorsWork()
  149. {
  150. $this->assertEquals('', $this->container->getSeparator());
  151. $this->container->setSeparator('</li><li>');
  152. $this->assertEquals('</li><li>', $this->container->getSeparator());
  153. }
  154. /**
  155. * @return void
  156. */
  157. public function testSetSeparatorImplementsFluentInterface()
  158. {
  159. $result = $this->container->setSeparator('</li><li>');
  160. $this->assertSame($this->container, $result);
  161. }
  162. /**
  163. * @return void
  164. */
  165. public function testIndentAccesorsWork()
  166. {
  167. $this->assertEquals('', $this->container->getIndent());
  168. $this->container->setIndent(' ');
  169. $this->assertEquals(' ', $this->container->getIndent());
  170. $this->container->setIndent(5);
  171. $this->assertEquals(' ', $this->container->getIndent());
  172. }
  173. /**
  174. * @return void
  175. */
  176. public function testSetIndentImplementsFluentInterface()
  177. {
  178. $result = $this->container->setIndent(' ');
  179. $this->assertSame($this->container, $result);
  180. }
  181. /**
  182. * @return void
  183. */
  184. public function testCapturingToPlaceholderStoresContent()
  185. {
  186. $this->container->captureStart();
  187. echo 'This is content intended for capture';
  188. $this->container->captureEnd();
  189. $value = $this->container->getValue();
  190. $this->assertContains('This is content intended for capture', $value);
  191. }
  192. /**
  193. * @return void
  194. */
  195. public function testCapturingToPlaceholderAppendsContent()
  196. {
  197. $this->container[] = 'foo';
  198. $originalCount = count($this->container);
  199. $this->container->captureStart();
  200. echo 'This is content intended for capture';
  201. $this->container->captureEnd();
  202. $this->assertEquals($originalCount + 1, count($this->container));
  203. $value = $this->container->getValue();
  204. $keys = array_keys($value);
  205. $lastIndex = array_pop($keys);
  206. $this->assertEquals('foo', $value[$lastIndex - 1]);
  207. $this->assertContains('This is content intended for capture', $value[$lastIndex]);
  208. }
  209. /**
  210. * @return void
  211. */
  212. public function testCapturingToPlaceholderUsingPrependPrependsContent()
  213. {
  214. $this->container[] = 'foo';
  215. $originalCount = count($this->container);
  216. $this->container->captureStart('PREPEND');
  217. echo 'This is content intended for capture';
  218. $this->container->captureEnd();
  219. $this->assertEquals($originalCount + 1, count($this->container));
  220. $value = $this->container->getValue();
  221. $keys = array_keys($value);
  222. $lastIndex = array_pop($keys);
  223. $this->assertEquals('foo', $value[$lastIndex]);
  224. $this->assertContains('This is content intended for capture', $value[$lastIndex - 1]);
  225. }
  226. /**
  227. * @return void
  228. */
  229. public function testCapturingToPlaceholderUsingSetOverwritesContent()
  230. {
  231. $this->container[] = 'foo';
  232. $this->container->captureStart('SET');
  233. echo 'This is content intended for capture';
  234. $this->container->captureEnd();
  235. $this->assertEquals(1, count($this->container));
  236. $value = $this->container->getValue();
  237. $this->assertContains('This is content intended for capture', $value);
  238. }
  239. /**
  240. * @return void
  241. */
  242. public function testCapturingToPlaceholderKeyUsingSetCapturesContent()
  243. {
  244. $this->container->captureStart('SET', 'key');
  245. echo 'This is content intended for capture';
  246. $this->container->captureEnd();
  247. $this->assertEquals(1, count($this->container));
  248. $this->assertTrue(isset($this->container['key']));
  249. $value = $this->container['key'];
  250. $this->assertContains('This is content intended for capture', $value);
  251. }
  252. /**
  253. * @return void
  254. */
  255. public function testCapturingToPlaceholderKeyUsingSetReplacesContentAtKey()
  256. {
  257. $this->container['key'] = 'Foobar';
  258. $this->container->captureStart('SET', 'key');
  259. echo 'This is content intended for capture';
  260. $this->container->captureEnd();
  261. $this->assertEquals(1, count($this->container));
  262. $this->assertTrue(isset($this->container['key']));
  263. $value = $this->container['key'];
  264. $this->assertContains('This is content intended for capture', $value);
  265. }
  266. /**
  267. * @return void
  268. */
  269. public function testCapturingToPlaceholderKeyUsingAppendAppendsContentAtKey()
  270. {
  271. $this->container['key'] = 'Foobar ';
  272. $this->container->captureStart('APPEND', 'key');
  273. echo 'This is content intended for capture';
  274. $this->container->captureEnd();
  275. $this->assertEquals(1, count($this->container));
  276. $this->assertTrue(isset($this->container['key']));
  277. $value = $this->container['key'];
  278. $this->assertContains('Foobar This is content intended for capture', $value);
  279. }
  280. /**
  281. * @return void
  282. */
  283. public function testNestedCapturesThrowsException()
  284. {
  285. $this->container[] = 'foo';
  286. $caught = false;
  287. try {
  288. $this->container->captureStart('SET');
  289. $this->container->captureStart('SET');
  290. $this->container->captureEnd();
  291. $this->container->captureEnd();
  292. } catch (Exception $e) {
  293. $this->container->captureEnd();
  294. $caught = true;
  295. }
  296. $this->assertTrue($caught, 'Nested captures should throw exceptions');
  297. }
  298. /**
  299. * @return void
  300. */
  301. public function testToStringWithNoModifiersAndSingleValueReturnsValue()
  302. {
  303. $this->container->set('foo');
  304. $value = $this->container->toString();
  305. $this->assertEquals($this->container->getValue(), $value);
  306. }
  307. /**
  308. * @return void
  309. */
  310. public function testToStringWithModifiersAndSingleValueReturnsFormattedValue()
  311. {
  312. $this->container->set('foo');
  313. $this->container->setPrefix('<li>')
  314. ->setPostfix('</li>');
  315. $value = $this->container->toString();
  316. $this->assertEquals('<li>foo</li>', $value);
  317. }
  318. /**
  319. * @return void
  320. */
  321. public function testToStringWithNoModifiersAndCollectionReturnsImplodedString()
  322. {
  323. $this->container[] = 'foo';
  324. $this->container[] = 'bar';
  325. $this->container[] = 'baz';
  326. $value = $this->container->toString();
  327. $this->assertEquals('foobarbaz', $value);
  328. }
  329. /**
  330. * @return void
  331. */
  332. public function testToStringWithModifiersAndCollectionReturnsFormattedString()
  333. {
  334. $this->container[] = 'foo';
  335. $this->container[] = 'bar';
  336. $this->container[] = 'baz';
  337. $this->container->setPrefix('<ul><li>')
  338. ->setSeparator('</li><li>')
  339. ->setPostfix('</li></ul>');
  340. $value = $this->container->toString();
  341. $this->assertEquals('<ul><li>foo</li><li>bar</li><li>baz</li></ul>', $value);
  342. }
  343. /**
  344. * @return void
  345. */
  346. public function testToStringWithModifiersAndCollectionReturnsFormattedStringWithIndentation()
  347. {
  348. $this->container[] = 'foo';
  349. $this->container[] = 'bar';
  350. $this->container[] = 'baz';
  351. $this->container->setPrefix('<ul><li>')
  352. ->setSeparator('</li>' . PHP_EOL . '<li>')
  353. ->setPostfix('</li></ul>')
  354. ->setIndent(' ');
  355. $value = $this->container->toString();
  356. $expectedValue = ' <ul><li>foo</li>' . PHP_EOL . ' <li>bar</li>' . PHP_EOL . ' <li>baz</li></ul>';
  357. $this->assertEquals($expectedValue, $value);
  358. }
  359. /**
  360. * @return void
  361. */
  362. public function test__toStringProxiesToToString()
  363. {
  364. $this->container[] = 'foo';
  365. $this->container[] = 'bar';
  366. $this->container[] = 'baz';
  367. $this->container->setPrefix('<ul><li>')
  368. ->setSeparator('</li><li>')
  369. ->setPostfix('</li></ul>');
  370. $value = $this->container->__toString();
  371. $this->assertEquals('<ul><li>foo</li><li>bar</li><li>baz</li></ul>', $value);
  372. }
  373. /**
  374. * @return void
  375. */
  376. public function testPrependPushesValueToTopOfContainer()
  377. {
  378. $this->container['foo'] = 'bar';
  379. $this->container->prepend('baz');
  380. $expected = array('baz', 'foo' => 'bar');
  381. $array = $this->container->getArrayCopy();
  382. $this->assertSame($expected, $array);
  383. }
  384. public function testIndentationIsHonored()
  385. {
  386. $this->container->setIndent(4)
  387. ->setPrefix("<ul>\n <li>")
  388. ->setSeparator("</li>\n <li>")
  389. ->setPostfix("</li>\n</ul>");
  390. $this->container->append('foo');
  391. $this->container->append('bar');
  392. $this->container->append('baz');
  393. $string = $this->container->toString();
  394. $lis = substr_count($string, "\n <li>");
  395. $this->assertEquals(3, $lis);
  396. $this->assertTrue((strstr($string, " <ul>\n")) ? true : false, $string);
  397. $this->assertTrue((strstr($string, "\n </ul>")) ? true : false);
  398. }
  399. }
  400. // Call Zend_View_Helper_Placeholder_ContainerTest::main() if this source file is executed directly.
  401. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_Placeholder_ContainerTest::main") {
  402. Zend_View_Helper_Placeholder_ContainerTest::main();
  403. }