PageRenderTime 27ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

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

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