FilesSizeTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_Validate_File
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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. // Call Zend_Validate_File_FilesSizeTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Validate_File_FilesSizeTest::main");
  25. }
  26. /**
  27. * @see Zend_Validate_File_FilesSize
  28. */
  29. require_once 'Zend/Validate/File/FilesSize.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Validate_File
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Validate
  37. */
  38. class Zend_Validate_File_FilesSizeTest 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_Validate_File_FilesSizeTest");
  48. $result = PHPUnit_TextUI_TestRunner::run($suite);
  49. }
  50. public function setUp()
  51. {
  52. $this->multipleOptionsDetected = false;
  53. }
  54. /**
  55. * Ensures that the validator follows expected behavior
  56. *
  57. * @return void
  58. */
  59. public function testBasic()
  60. {
  61. $valuesExpected = array(
  62. array(array('min' => 0, 'max' => 2000), true, true, false),
  63. array(array('min' => 0, 'max' => '2 MB'), true, true, true),
  64. array(array('min' => 0, 'max' => '2MB'), true, true, true),
  65. array(array('min' => 0, 'max' => '2 MB'), true, true, true),
  66. array(2000, true, true, false),
  67. array(array('min' => 0, 'max' => 500), false, false, false),
  68. array(500, false, false, false)
  69. );
  70. foreach ($valuesExpected as $element) {
  71. $validator = new Zend_Validate_File_FilesSize($element[0]);
  72. $this->assertEquals(
  73. $element[1],
  74. $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo'),
  75. "Tested with " . var_export($element, 1)
  76. );
  77. $this->assertEquals(
  78. $element[2],
  79. $validator->isValid(dirname(__FILE__) . '/_files/testsize2.mo'),
  80. "Tested with " . var_export($element, 1)
  81. );
  82. $this->assertEquals(
  83. $element[3],
  84. $validator->isValid(dirname(__FILE__) . '/_files/testsize3.mo'),
  85. "Tested with " . var_export($element, 1)
  86. );
  87. }
  88. $validator = new Zend_Validate_File_FilesSize(array('min' => 0, 'max' => 200));
  89. $this->assertEquals(false, $validator->isValid(dirname(__FILE__) . '/_files/nofile.mo'));
  90. $this->assertTrue(array_key_exists('fileFilesSizeNotReadable', $validator->getMessages()));
  91. $validator = new Zend_Validate_File_FilesSize(array('min' => 0, 'max' => 500000));
  92. $this->assertEquals(true, $validator->isValid(array(
  93. dirname(__FILE__) . '/_files/testsize.mo',
  94. dirname(__FILE__) . '/_files/testsize.mo',
  95. dirname(__FILE__) . '/_files/testsize2.mo')));
  96. $this->assertEquals(true, $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo'));
  97. }
  98. /**
  99. * Ensures that getMin() returns expected value
  100. *
  101. * @return void
  102. */
  103. public function testGetMin()
  104. {
  105. $validator = new Zend_Validate_File_FilesSize(array('min' => 1, 'max' => 100));
  106. $this->assertEquals('1B', $validator->getMin());
  107. try {
  108. $validator = new Zend_Validate_File_FilesSize(array('min' => 100, 'max' => 1));
  109. $this->fail("Missing exception");
  110. } catch (Zend_Validate_Exception $e) {
  111. $this->assertContains("greater than or equal", $e->getMessage());
  112. }
  113. $validator = new Zend_Validate_File_FilesSize(array('min' => 1, 'max' => 100));
  114. $this->assertEquals('1B', $validator->getMin());
  115. }
  116. /**
  117. * Ensures that setMin() returns expected value
  118. *
  119. * @return void
  120. */
  121. public function testSetMin()
  122. {
  123. $validator = new Zend_Validate_File_FilesSize(array('min' => 1000, 'max' => 10000));
  124. $validator->setMin(100);
  125. $this->assertEquals('100B', $validator->getMin());
  126. try {
  127. $validator->setMin(20000);
  128. $this->fail("Missing exception");
  129. } catch (Zend_Validate_Exception $e) {
  130. $this->assertContains("less than or equal", $e->getMessage());
  131. }
  132. }
  133. /**
  134. * Ensures that getMax() returns expected value
  135. *
  136. * @return void
  137. */
  138. public function testGetMax()
  139. {
  140. $validator = new Zend_Validate_File_FilesSize(array('min' => 1, 'max' => 100));
  141. $this->assertEquals('100B', $validator->getMax());
  142. try {
  143. $validator = new Zend_Validate_File_FilesSize(array('min' => 100, 'max' => 1));
  144. $this->fail("Missing exception");
  145. } catch (Zend_Validate_Exception $e) {
  146. $this->assertContains("greater than or equal", $e->getMessage());
  147. }
  148. $validator = new Zend_Validate_File_FilesSize(array('min' => 1, 'max' => 100000));
  149. $this->assertEquals('97.66kB', $validator->getMax());
  150. $validator = new Zend_Validate_File_FilesSize(2000);
  151. $validator->setUseByteString(false);
  152. $test = $validator->getMax();
  153. $this->assertEquals('2000', $test);
  154. }
  155. /**
  156. * Ensures that setMax() returns expected value
  157. *
  158. * @return void
  159. */
  160. public function testSetMax()
  161. {
  162. $validator = new Zend_Validate_File_FilesSize(array('min' => 1000, 'max' => 10000));
  163. $validator->setMax(1000000);
  164. $this->assertEquals('976.56kB', $validator->getMax());
  165. $validator->setMin(100);
  166. $this->assertEquals('976.56kB', $validator->getMax());
  167. }
  168. public function testConstructorShouldRaiseErrorWhenPassedMultipleOptions()
  169. {
  170. $handler = set_error_handler(array($this, 'errorHandler'), E_USER_NOTICE);
  171. $validator = new Zend_Validate_File_FilesSize(1000, 10000);
  172. restore_error_handler();
  173. }
  174. /**
  175. * Ensures that the validator returns size infos
  176. *
  177. * @return void
  178. */
  179. public function testFailureMessage()
  180. {
  181. $validator = new Zend_Validate_File_FilesSize(array('min' => 9999, 'max' => 10000));
  182. $this->assertFalse($validator->isValid(array(
  183. dirname(__FILE__) . '/_files/testsize.mo',
  184. dirname(__FILE__) . '/_files/testsize.mo',
  185. dirname(__FILE__) . '/_files/testsize2.mo')));
  186. $this->assertContains('9.76kB', current($validator->getMessages()));
  187. $this->assertContains('1.55kB', current($validator->getMessages()));
  188. $validator = new Zend_Validate_File_FilesSize(array('min' => 9999, 'max' => 10000, 'bytestring' => false));
  189. $this->assertFalse($validator->isValid(array(
  190. dirname(__FILE__) . '/_files/testsize.mo',
  191. dirname(__FILE__) . '/_files/testsize.mo',
  192. dirname(__FILE__) . '/_files/testsize2.mo')));
  193. $this->assertContains('9999', current($validator->getMessages()));
  194. $this->assertContains('1588', current($validator->getMessages()));
  195. }
  196. public function errorHandler($errno, $errstr)
  197. {
  198. if (strstr($errstr, 'deprecated')) {
  199. $this->multipleOptionsDetected = true;
  200. }
  201. }
  202. }
  203. // Call Zend_Validate_File_FilesSizeTest::main() if this source file is executed directly.
  204. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_FilesSizeTest::main") {
  205. Zend_Validate_File_FilesSizeTest::main();
  206. }