PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

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

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