PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/tests/Zend/View/Helper/Placeholder/ContainerTest.php

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