PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

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