FilesSizeTest.php 7.2 KB

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