PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/tests/Zend/Form/Decorator/CallbackTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 223 lines | 144 code | 30 blank | 49 comment | 5 complexity | f4d2b327201fc0e65533bd684b5f2033 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: CallbackTest.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. // Call Zend_Form_Decorator_CallbackTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Decorator_CallbackTest::main");
  25. }
  26. require_once 'Zend/Form/Decorator/Callback.php';
  27. require_once 'Zend/Form/Element.php';
  28. /**
  29. * Test class for Zend_Form_Decorator_Callback
  30. *
  31. * @category Zend
  32. * @package Zend_Form
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Form
  37. */
  38. class Zend_Form_Decorator_CallbackTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Runs the test methods of this class.
  42. *
  43. * @return void
  44. */
  45. public static function main()
  46. {
  47. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Decorator_CallbackTest");
  48. $result = PHPUnit_TextUI_TestRunner::run($suite);
  49. }
  50. /**
  51. * Sets up the fixture, for example, open a network connection.
  52. * This method is called before a test is executed.
  53. *
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. $this->decorator = new Zend_Form_Decorator_Callback();
  59. }
  60. /**
  61. * Tears down the fixture, for example, close a network connection.
  62. * This method is called after a test is executed.
  63. *
  64. * @return void
  65. */
  66. public function tearDown()
  67. {
  68. }
  69. public function testCanSetCallback()
  70. {
  71. $callback = 'Zend_Form_Decorator_CallbackTest_TestCallback';
  72. $this->decorator->setCallback($callback);
  73. $this->assertSame($callback, $this->decorator->getCallback());
  74. $callback = array('Zend_Form_Decorator_CallbackTest_TestCallbackClass', 'direct');
  75. $this->decorator->setCallback($callback);
  76. $this->assertSame($callback, $this->decorator->getCallback());
  77. }
  78. public function testCanSetCallbackViaOptions()
  79. {
  80. $callback = 'Zend_Form_Decorator_CallbackTest_TestCallback';
  81. $this->decorator->setOptions(array('callback' => $callback));
  82. $this->assertSame($callback, $this->decorator->getCallback());
  83. }
  84. public function testInvalidCallbackRaisesException()
  85. {
  86. try {
  87. $this->decorator->setCallback(true);
  88. $this->fail('Only string or array callbacks should be allowed');
  89. } catch (Zend_Form_Exception $e) {
  90. $this->assertContains('Invalid', $e->getMessage());
  91. }
  92. try {
  93. $o = new stdClass;
  94. $this->decorator->setCallback($o);
  95. $this->fail('Only string or array callbacks should be allowed');
  96. } catch (Zend_Form_Exception $e) {
  97. $this->assertContains('Invalid', $e->getMessage());
  98. }
  99. try {
  100. $this->decorator->setCallback(array('foo', 'bar', 'baz'));
  101. $this->fail('Only arrays of two elements should be allowed as callbacks');
  102. } catch (Zend_Form_Exception $e) {
  103. $this->assertContains('Invalid', $e->getMessage());
  104. }
  105. try {
  106. $this->decorator->setCallback(array('foo'));
  107. $this->fail('Only arrays of two elements should be allowed as callbacks');
  108. } catch (Zend_Form_Exception $e) {
  109. $this->assertContains('Invalid', $e->getMessage());
  110. }
  111. }
  112. public function testRenderCallsFunctionCallback()
  113. {
  114. $callback = 'Zend_Form_Decorator_CallbackTest_TestCallback';
  115. $element = new Zend_Form_Element('foobar');
  116. $element->setLabel('Label Me');
  117. $this->decorator->setOptions(array('callback' => $callback))
  118. ->setElement($element);
  119. $content = $this->decorator->render('foo bar');
  120. $this->assertContains('foo bar', $content);
  121. $this->assertContains($element->getName(), $content);
  122. $this->assertContains($element->getLabel(), $content);
  123. }
  124. public function testRenderCallsMethodCallback()
  125. {
  126. $callback = array('Zend_Form_Decorator_CallbackTest_TestCallbackClass', 'direct');
  127. $element = new Zend_Form_Element('foobar');
  128. $element->setLabel('Label Me');
  129. $this->decorator->setOptions(array('callback' => $callback))
  130. ->setElement($element);
  131. $content = $this->decorator->render('foo bar');
  132. $this->assertContains('foo bar', $content);
  133. $this->assertContains($element->getName(), $content);
  134. $this->assertContains($element->getLabel(), $content);
  135. $this->assertContains('Item ', $content);
  136. }
  137. public function testRenderCanPrepend()
  138. {
  139. $callback = 'Zend_Form_Decorator_CallbackTest_TestCallback';
  140. $element = new Zend_Form_Element('foobar');
  141. $element->setLabel('Label Me');
  142. $this->decorator->setOptions(array('callback' => $callback, 'placement' => 'prepend'))
  143. ->setElement($element);
  144. $content = $this->decorator->render('foo bar');
  145. $this->assertContains('foo bar', $content);
  146. $this->assertContains($element->getName(), $content);
  147. $this->assertContains($element->getLabel(), $content);
  148. $this->assertRegexp('/foo bar$/s', $content);
  149. }
  150. public function testRenderCanReplaceContent()
  151. {
  152. $callback = 'Zend_Form_Decorator_CallbackTest_TestCallback';
  153. $element = new Zend_Form_Element('foobar');
  154. $element->setLabel('Label Me');
  155. $this->decorator->setOptions(array('callback' => $callback, 'placement' => false))
  156. ->setElement($element);
  157. $content = $this->decorator->render('foo bar');
  158. $this->assertNotContains('foo bar', $content, $content);
  159. $this->assertContains($element->getName(), $content);
  160. $this->assertContains($element->getLabel(), $content);
  161. }
  162. }
  163. function Zend_Form_Decorator_CallbackTest_TestCallback($content, $element, array $options)
  164. {
  165. $name = $element->getName();
  166. $label = '';
  167. if (method_exists($element, 'getLabel')) {
  168. $label = $element->getLabel();
  169. }
  170. $html =<<<EOH
  171. $label: $name
  172. EOH;
  173. return $html;
  174. }
  175. class Zend_Form_Decorator_CallbackTest_TestCallbackClass
  176. {
  177. public static function direct($content, $element, array $options)
  178. {
  179. $name = $element->getName();
  180. $label = '';
  181. if (method_exists($element, 'getLabel')) {
  182. $label = $element->getLabel();
  183. }
  184. $html =<<<EOH
  185. Item "$label": $name
  186. EOH;
  187. return $html;
  188. }
  189. }
  190. // Call Zend_Form_Decorator_CallbackTest::main() if this source file is executed directly.
  191. if (PHPUnit_MAIN_METHOD == "Zend_Form_Decorator_CallbackTest::main") {
  192. Zend_Form_Decorator_CallbackTest::main();
  193. }