PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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