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

/tests/Zend/Form/Decorator/ViewScriptTest.php

https://github.com/Exercise/zf2
PHP | 159 lines | 104 code | 19 blank | 36 comment | 0 complexity | 5f4ae9264a115de58aa6969fdd416734 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_Form
  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. namespace ZendTest\Form\Decorator;
  23. use Zend\Form\Decorator\ViewScript as ViewScriptDecorator,
  24. Zend\Form\Element\Text as TextElement,
  25. Zend\View\View;
  26. /**
  27. * Test class for Zend_Form_Decorator_ViewScript
  28. *
  29. * @category Zend
  30. * @package Zend_Form
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Form
  35. */
  36. class ViewScriptTest extends \PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * Sets up the fixture, for example, open a network connection.
  40. * This method is called before a test is executed.
  41. *
  42. * @return void
  43. */
  44. public function setUp()
  45. {
  46. $this->decorator = new ViewScriptDecorator();
  47. }
  48. public function getView()
  49. {
  50. $view = new View();
  51. $view->addScriptPath(__DIR__ . '/../TestAsset/views/');
  52. return $view;
  53. }
  54. public function getElement()
  55. {
  56. $element = new TextElement('foo');
  57. $element->setView($this->getView());
  58. $this->decorator->setElement($element);
  59. return $element;
  60. }
  61. public function testRenderRaisesExceptionIfNoViewScriptRegistered()
  62. {
  63. $this->setExpectedException('Zend\Form\Exception', 'script');
  64. $this->getElement();
  65. $this->decorator->render('');
  66. }
  67. public function testViewScriptNullByDefault()
  68. {
  69. $this->assertNull($this->decorator->getViewScript());
  70. }
  71. public function testCanSetViewScript()
  72. {
  73. $this->testViewScriptNullByDefault();
  74. $this->decorator->setViewScript('decorator.phtml');
  75. $this->assertEquals('decorator.phtml', $this->decorator->getViewScript());
  76. }
  77. public function testCanSetViewScriptViaOption()
  78. {
  79. $this->testViewScriptNullByDefault();
  80. $this->decorator->setOption('viewScript', 'decorator.phtml');
  81. $this->assertEquals('decorator.phtml', $this->decorator->getViewScript());
  82. }
  83. public function testCanSetViewScriptViaElementAttribute()
  84. {
  85. $this->testViewScriptNullByDefault();
  86. $this->getElement()->setAttrib('viewScript', 'decorator.phtml');
  87. $this->assertEquals('decorator.phtml', $this->decorator->getViewScript());
  88. }
  89. public function testRenderingRendersViewScript()
  90. {
  91. $this->testCanSetViewScriptViaElementAttribute();
  92. $test = $this->decorator->render('');
  93. $this->assertContains('This is content from the view script', $test);
  94. }
  95. public function testOptionsArePassedToPartialAsVariables()
  96. {
  97. $this->decorator->setOptions(array(
  98. 'foo' => 'Foo Value',
  99. 'bar' => 'Bar Value',
  100. 'baz' => 'Baz Value',
  101. 'bat' => 'Bat Value',
  102. 'viewScript' => 'decorator.phtml',
  103. ));
  104. $this->getElement();
  105. $test = $this->decorator->render('');
  106. foreach ($this->decorator->getOptions() as $key => $value) {
  107. $this->assertContains("$key: $value", $test);
  108. }
  109. }
  110. public function testCanReplaceContentBySpecifyingFalsePlacement()
  111. {
  112. $this->decorator->setViewScript('replacingDecorator.phtml')
  113. ->setOption('placement', false)
  114. ->setElement($this->getElement());
  115. $test = $this->decorator->render('content to decorate');
  116. $this->assertNotContains('content to decorate', $test, $test);
  117. $this->assertContains('This is content from the view script', $test);
  118. }
  119. public function testContentCanBeRenderedWithinViewScript()
  120. {
  121. $this->decorator->setViewScript('contentWrappingDecorator.phtml')
  122. ->setOption('placement', false)
  123. ->setElement($this->getElement());
  124. $test = $this->decorator->render('content to decorate');
  125. $this->assertContains('content to decorate', $test, $test);
  126. $this->assertContains('This text prefixes the content', $test);
  127. $this->assertContains('This text appends the content', $test);
  128. }
  129. public function testDecoratorCanControlPlacementFromWithinViewScript()
  130. {
  131. $this->decorator->setViewScript('decoratorCausesReplacement.phtml')
  132. ->setElement($this->getElement());
  133. $test = $this->decorator->render('content to decorate');
  134. $this->assertContains('content to decorate', $test, $test);
  135. $count = substr_count($test, 'content to decorate');
  136. $this->assertEquals(1, $count);
  137. $this->assertContains('This text prefixes the content', $test);
  138. $this->assertContains('This text appends the content', $test);
  139. }
  140. }