SizeTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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$
  21. */
  22. // Call Zend_Validate_File_SizeTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Validate_File_SizeTest::main");
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  30. /**
  31. * @see Zend_Validate_File_Size
  32. */
  33. require_once 'Zend/Validate/File/Size.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_SizeTest 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_SizeTest");
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. /**
  54. * Ensures that the validator follows expected behavior
  55. *
  56. * @return void
  57. */
  58. public function testBasic()
  59. {
  60. $valuesExpected = array(
  61. array(array('min' => 0, 'max' => 10000), true),
  62. array(array('min' => 0, 'max' => '10 MB'), true),
  63. array(array('min' => '4B', 'max' => '10 MB'), true),
  64. array(array('min' => 0, 'max' => '10MB'), true),
  65. array(array('min' => 0, 'max' => '10 MB'), true),
  66. array(794, true),
  67. array(array('min' => 794), true),
  68. array(array('min' => 0, 'max' => 500), false),
  69. array(500, false),
  70. );
  71. foreach ($valuesExpected as $element) {
  72. $options = array_shift($element);
  73. $value = array_shift($element);
  74. $validator = new Zend_Validate_File_Size($options);
  75. $this->assertEquals(
  76. $value,
  77. $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo'),
  78. "Tested " . var_export($value, 1) . " against options " . var_export($options, 1)
  79. );
  80. }
  81. }
  82. /**
  83. * Ensures that getMin() returns expected value
  84. *
  85. * @return void
  86. */
  87. public function testGetMin()
  88. {
  89. $validator = new Zend_Validate_File_Size(array('min' => 1, 'max' => 100));
  90. $this->assertEquals('1B', $validator->getMin());
  91. try {
  92. $validator = new Zend_Validate_File_Size(array('min' => 100, 'max' => 1));
  93. $this->fail("Missing exception");
  94. } catch (Zend_Validate_Exception $e) {
  95. $this->assertContains("greater than or equal", $e->getMessage());
  96. }
  97. $validator = new Zend_Validate_File_Size(array('min' => 1, 'max' => 100, 'bytestring' => false));
  98. $this->assertEquals(1, $validator->getMin());
  99. }
  100. /**
  101. * Ensures that setMin() returns expected value
  102. *
  103. * @return void
  104. */
  105. public function testSetMin()
  106. {
  107. $validator = new Zend_Validate_File_Size(array('min' => 1000, 'max' => 10000));
  108. $validator->setMin(100);
  109. $this->assertEquals('100B', $validator->getMin());
  110. try {
  111. $validator->setMin(20000);
  112. $this->fail("Missing exception");
  113. } catch (Zend_Validate_Exception $e) {
  114. $this->assertContains("less than or equal", $e->getMessage());
  115. }
  116. $validator = new Zend_Validate_File_Size(array('min' => 1000, 'max' => 10000, 'bytestring' => false));
  117. $validator->setMin(100);
  118. $this->assertEquals(100, $validator->getMin());
  119. }
  120. /**
  121. * Ensures that getMax() returns expected value
  122. *
  123. * @return void
  124. */
  125. public function testGetMax()
  126. {
  127. $validator = new Zend_Validate_File_Size(array('min' => 1, 'max' => 100, 'bytestring' => false));
  128. $this->assertEquals(100, $validator->getMax());
  129. try {
  130. $validator = new Zend_Validate_File_Size(array('min' => 100, 'max' => 1));
  131. $this->fail("Missing exception");
  132. } catch (Zend_Validate_Exception $e) {
  133. $this->assertContains("greater than or equal", $e->getMessage());
  134. }
  135. $validator = new Zend_Validate_File_Size(array('min' => 1, 'max' => 100000));
  136. $this->assertEquals('97.66kB', $validator->getMax());
  137. $validator = new Zend_Validate_File_Size(2000);
  138. $this->assertEquals('1.95kB', $validator->getMax());
  139. }
  140. /**
  141. * Ensures that setMax() returns expected value
  142. *
  143. * @return void
  144. */
  145. public function testSetMax()
  146. {
  147. $validator = new Zend_Validate_File_Size(array('max' => 0, 'bytestring' => true));
  148. $this->assertEquals('0B', $validator->getMax());
  149. $validator->setMax(1000000);
  150. $this->assertEquals('976.56kB', $validator->getMax());
  151. $validator->setMin(100);
  152. $this->assertEquals('976.56kB', $validator->getMax());
  153. $validator->setMax('100 AB');
  154. $this->assertEquals('100B', $validator->getMax());
  155. $validator->setMax('100 kB');
  156. $this->assertEquals('100kB', $validator->getMax());
  157. $validator->setMax('100 MB');
  158. $this->assertEquals('100MB', $validator->getMax());
  159. $validator->setMax('1 GB');
  160. $this->assertEquals('1GB', $validator->getMax());
  161. $validator->setMax('0.001 TB');
  162. $this->assertEquals('1.02GB', $validator->getMax());
  163. $validator->setMax('0.000001 PB');
  164. $this->assertEquals('1.05GB', $validator->getMax());
  165. $validator->setMax('0.000000001 EB');
  166. $this->assertEquals('1.07GB', $validator->getMax());
  167. $validator->setMax('0.000000000001 ZB');
  168. $this->assertEquals('1.1GB', $validator->getMax());
  169. $validator->setMax('0.000000000000001 YB');
  170. $this->assertEquals('1.13GB', $validator->getMax());
  171. }
  172. }
  173. // Call Zend_Validate_File_SizeTest::main() if this source file is executed directly.
  174. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_SizeTest::main") {
  175. Zend_Validate_File_SizeTest::main();
  176. }