SizeTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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-2010 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Validate
  41. */
  42. class Zend_Validate_File_SizeTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @return void
  48. */
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_Validate_File_SizeTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  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' => 10000), true),
  63. array(array('min' => 0, 'max' => '10 MB'), true),
  64. array(array('min' => '4B', 'max' => '10 MB'), true),
  65. array(array('min' => 0, 'max' => '10MB'), true),
  66. array(array('min' => 0, 'max' => '10 MB'), true),
  67. array(794, true),
  68. array(array('min' => 794), true),
  69. array(array('min' => 0, 'max' => 500), false),
  70. array(500, false),
  71. );
  72. foreach ($valuesExpected as $element) {
  73. $options = array_shift($element);
  74. $value = array_shift($element);
  75. $validator = new Zend_Validate_File_Size($options);
  76. $this->assertEquals(
  77. $value,
  78. $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo'),
  79. "Tested " . var_export($value, 1) . " against options " . var_export($options, 1)
  80. );
  81. }
  82. }
  83. /**
  84. * Ensures that getMin() returns expected value
  85. *
  86. * @return void
  87. */
  88. public function testGetMin()
  89. {
  90. $validator = new Zend_Validate_File_Size(array('min' => 1, 'max' => 100));
  91. $this->assertEquals('1B', $validator->getMin());
  92. try {
  93. $validator = new Zend_Validate_File_Size(array('min' => 100, 'max' => 1));
  94. $this->fail("Missing exception");
  95. } catch (Zend_Validate_Exception $e) {
  96. $this->assertContains("greater than or equal", $e->getMessage());
  97. }
  98. $validator = new Zend_Validate_File_Size(array('min' => 1, 'max' => 100, 'bytestring' => false));
  99. $this->assertEquals(1, $validator->getMin());
  100. }
  101. /**
  102. * Ensures that setMin() returns expected value
  103. *
  104. * @return void
  105. */
  106. public function testSetMin()
  107. {
  108. $validator = new Zend_Validate_File_Size(array('min' => 1000, 'max' => 10000));
  109. $validator->setMin(100);
  110. $this->assertEquals('100B', $validator->getMin());
  111. try {
  112. $validator->setMin(20000);
  113. $this->fail("Missing exception");
  114. } catch (Zend_Validate_Exception $e) {
  115. $this->assertContains("less than or equal", $e->getMessage());
  116. }
  117. $validator = new Zend_Validate_File_Size(array('min' => 1000, 'max' => 10000, 'bytestring' => false));
  118. $validator->setMin(100);
  119. $this->assertEquals(100, $validator->getMin());
  120. }
  121. /**
  122. * Ensures that getMax() returns expected value
  123. *
  124. * @return void
  125. */
  126. public function testGetMax()
  127. {
  128. $validator = new Zend_Validate_File_Size(array('min' => 1, 'max' => 100, 'bytestring' => false));
  129. $this->assertEquals(100, $validator->getMax());
  130. try {
  131. $validator = new Zend_Validate_File_Size(array('min' => 100, 'max' => 1));
  132. $this->fail("Missing exception");
  133. } catch (Zend_Validate_Exception $e) {
  134. $this->assertContains("greater than or equal", $e->getMessage());
  135. }
  136. $validator = new Zend_Validate_File_Size(array('min' => 1, 'max' => 100000));
  137. $this->assertEquals('97.66kB', $validator->getMax());
  138. $validator = new Zend_Validate_File_Size(2000);
  139. $this->assertEquals('1.95kB', $validator->getMax());
  140. }
  141. /**
  142. * Ensures that setMax() returns expected value
  143. *
  144. * @return void
  145. */
  146. public function testSetMax()
  147. {
  148. $validator = new Zend_Validate_File_Size(array('max' => 0, 'bytestring' => true));
  149. $this->assertEquals('0B', $validator->getMax());
  150. $validator->setMax(1000000);
  151. $this->assertEquals('976.56kB', $validator->getMax());
  152. $validator->setMin(100);
  153. $this->assertEquals('976.56kB', $validator->getMax());
  154. $validator->setMax('100 AB');
  155. $this->assertEquals('100B', $validator->getMax());
  156. $validator->setMax('100 kB');
  157. $this->assertEquals('100kB', $validator->getMax());
  158. $validator->setMax('100 MB');
  159. $this->assertEquals('100MB', $validator->getMax());
  160. $validator->setMax('1 GB');
  161. $this->assertEquals('1GB', $validator->getMax());
  162. $validator->setMax('0.001 TB');
  163. $this->assertEquals('1.02GB', $validator->getMax());
  164. $validator->setMax('0.000001 PB');
  165. $this->assertEquals('1.05GB', $validator->getMax());
  166. $validator->setMax('0.000000001 EB');
  167. $this->assertEquals('1.07GB', $validator->getMax());
  168. $validator->setMax('0.000000000001 ZB');
  169. $this->assertEquals('1.1GB', $validator->getMax());
  170. $validator->setMax('0.000000000000001 YB');
  171. $this->assertEquals('1.13GB', $validator->getMax());
  172. }
  173. }
  174. // Call Zend_Validate_File_SizeTest::main() if this source file is executed directly.
  175. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_SizeTest::main") {
  176. Zend_Validate_File_SizeTest::main();
  177. }