2
0

SizeTest.php 7.4 KB

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