/tests/Zend/Form/Decorator/FileTest.php

https://github.com/leerbag/zf2 · PHP · 178 lines · 112 code · 22 blank · 44 comment · 2 complexity · c50a225d21ad418bb3205069985afc7b 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. namespace ZendTest\Form\Decorator;
  22. use Zend\Form\Decorator\File as FileDecorator,
  23. Zend\Form\Decorator\AbstractDecorator,
  24. Zend\Form\Element\File as FileElement,
  25. Zend\Form\Element,
  26. Zend\View\PhpRenderer as View;
  27. /**
  28. * Test class for Zend_Form_Decorator_Errors
  29. *
  30. * @category Zend
  31. * @package Zend_Form
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Form
  36. */
  37. class FileTest extends \PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Sets up the fixture, for example, open a network connection.
  41. * This method is called before a test is executed.
  42. *
  43. * @return void
  44. */
  45. public function setUp()
  46. {
  47. $this->decorator = new FileDecorator();
  48. }
  49. /**
  50. * This test is obsolete, as a view is always lazy-loaded
  51. *
  52. * @group disable
  53. */
  54. public function testRenderReturnsInitialContentIfNoViewPresentInElement()
  55. {
  56. $element = new FileElement('foo');
  57. $this->decorator->setElement($element);
  58. $content = 'test content';
  59. $this->assertSame($content, $this->decorator->render($content));
  60. }
  61. public function getView()
  62. {
  63. $view = new View();
  64. return $view;
  65. }
  66. public function setupSingleElement()
  67. {
  68. $element = new FileElement('foo');
  69. $element->addValidator('Count', 1)
  70. ->setView($this->getView());
  71. $this->element = $element;
  72. $this->decorator->setElement($element);
  73. }
  74. public function setupMultiElement()
  75. {
  76. $element = new FileElement('foo');
  77. $element->addValidator('Count', 1)
  78. ->setMultiFile(2)
  79. ->setView($this->getView());
  80. $this->element = $element;
  81. $this->decorator->setElement($element);
  82. }
  83. public function testRenderSingleFiles()
  84. {
  85. $this->setupSingleElement();
  86. $test = $this->decorator->render(null);
  87. $this->assertRegexp('#foo#s', $test);
  88. }
  89. public function testRenderMultiFiles()
  90. {
  91. $this->setupMultiElement();
  92. $test = $this->decorator->render(null);
  93. $this->assertRegexp('#foo\[\]#s', $test);
  94. }
  95. public function setupElementWithMaxFileSize()
  96. {
  97. $max = $this->_convertIniToInteger(trim(ini_get('upload_max_filesize')));
  98. $element = new FileElement('foo');
  99. $element->addValidator('Count', 1)
  100. ->setView($this->getView())
  101. ->setMaxFileSize($max);
  102. $this->element = $element;
  103. $this->decorator->setElement($element);
  104. }
  105. public function testRenderMaxFileSize()
  106. {
  107. $max = $this->_convertIniToInteger(trim(ini_get('upload_max_filesize')));
  108. $this->setupElementWithMaxFileSize();
  109. $test = $this->decorator->render(null);
  110. $this->assertRegexp('#MAX_FILE_SIZE#s', $test);
  111. $this->assertRegexp('#' . $max . '#s', $test);
  112. }
  113. public function testPlacementInitiallyAppends()
  114. {
  115. $this->assertEquals(AbstractDecorator::APPEND, $this->decorator->getPlacement());
  116. }
  117. /**
  118. * Test is obsolete as view is now lazy-loaded
  119. * @group disable
  120. */
  121. public function testRenderReturnsOriginalContentWhenNoViewPresentInElement()
  122. {
  123. $element = new Element('foo');
  124. $this->decorator->setElement($element);
  125. $content = 'test content';
  126. $this->assertSame($content, $this->decorator->render($content));
  127. }
  128. public function testCanPrependFileToContent()
  129. {
  130. $element = new FileElement('foo');
  131. $element->setValue('foobar')
  132. ->setView($this->getView());
  133. $this->decorator->setElement($element)
  134. ->setOption('placement', 'prepend');
  135. $file = $this->decorator->render('content');
  136. $this->assertRegexp('#<input[^>]*>.*?(content)#s', $file, $file);
  137. }
  138. private function _convertIniToInteger($setting)
  139. {
  140. if (!is_numeric($setting)) {
  141. $type = strtoupper(substr($setting, -1));
  142. $setting = (integer) substr($setting, 0, -1);
  143. switch ($type) {
  144. case 'M' :
  145. $setting *= 1024;
  146. break;
  147. case 'G' :
  148. $setting *= 1024 * 1024;
  149. break;
  150. default :
  151. break;
  152. }
  153. }
  154. return (integer) $setting;
  155. }
  156. }