PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Validator/File/FilesSizeTest.php

https://github.com/mrbanzai/zf2
PHP | 202 lines | 121 code | 21 blank | 60 comment | 1 complexity | 108036affb79f96eddbdc1274e695943 MD5 | raw file
  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_Validator_File
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace ZendTest\Validator\File;
  25. use Zend\Validator\File;
  26. use Zend\Validator;
  27. /**
  28. * @category Zend
  29. * @package Zend_Validator_File
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Validator
  34. */
  35. class FilesSizeTest extends \PHPUnit_Framework_TestCase
  36. {
  37. public function setUp()
  38. {
  39. $this->multipleOptionsDetected = false;
  40. }
  41. /**
  42. * Ensures that the validator follows expected behavior
  43. *
  44. * @return void
  45. */
  46. public function testBasic()
  47. {
  48. $valuesExpected = array(
  49. array(array('min' => 0, 'max' => 2000), true, true, false),
  50. array(array('min' => 0, 'max' => '2 MB'), true, true, true),
  51. array(array('min' => 0, 'max' => '2MB'), true, true, true),
  52. array(array('min' => 0, 'max' => '2 MB'), true, true, true),
  53. array(2000, true, true, false),
  54. array(array('min' => 0, 'max' => 500), false, false, false),
  55. array(500, false, false, false)
  56. );
  57. foreach ($valuesExpected as $element) {
  58. $validator = new File\FilesSize($element[0]);
  59. $this->assertEquals(
  60. $element[1],
  61. $validator->isValid(__DIR__ . '/_files/testsize.mo'),
  62. "Tested with " . var_export($element, 1)
  63. );
  64. $this->assertEquals(
  65. $element[2],
  66. $validator->isValid(__DIR__ . '/_files/testsize2.mo'),
  67. "Tested with " . var_export($element, 1)
  68. );
  69. $this->assertEquals(
  70. $element[3],
  71. $validator->isValid(__DIR__ . '/_files/testsize3.mo'),
  72. "Tested with " . var_export($element, 1)
  73. );
  74. }
  75. $validator = new File\FilesSize(array('min' => 0, 'max' => 200));
  76. $this->assertEquals(false, $validator->isValid(__DIR__ . '/_files/nofile.mo'));
  77. $this->assertTrue(array_key_exists('fileFilesSizeNotReadable', $validator->getMessages()));
  78. $validator = new File\FilesSize(array('min' => 0, 'max' => 500000));
  79. $this->assertEquals(true, $validator->isValid(array(
  80. __DIR__ . '/_files/testsize.mo',
  81. __DIR__ . '/_files/testsize.mo',
  82. __DIR__ . '/_files/testsize2.mo')));
  83. $this->assertEquals(true, $validator->isValid(__DIR__ . '/_files/testsize.mo'));
  84. }
  85. /**
  86. * Ensures that getMin() returns expected value
  87. *
  88. * @return void
  89. */
  90. public function testGetMin()
  91. {
  92. $validator = new File\FilesSize(array('min' => 1, 'max' => 100));
  93. $this->assertEquals('1B', $validator->getMin());
  94. $validator = new File\FilesSize(array('min' => 1, 'max' => 100));
  95. $this->assertEquals('1B', $validator->getMin());
  96. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'greater than or equal');
  97. $validator = new File\FilesSize(array('min' => 100, 'max' => 1));
  98. }
  99. /**
  100. * Ensures that setMin() returns expected value
  101. *
  102. * @return void
  103. */
  104. public function testSetMin()
  105. {
  106. $validator = new File\FilesSize(array('min' => 1000, 'max' => 10000));
  107. $validator->setMin(100);
  108. $this->assertEquals('100B', $validator->getMin());
  109. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'less than or equal');
  110. $validator->setMin(20000);
  111. }
  112. /**
  113. * Ensures that getMax() returns expected value
  114. *
  115. * @return void
  116. */
  117. public function testGetMax()
  118. {
  119. $validator = new File\FilesSize(array('min' => 1, 'max' => 100));
  120. $this->assertEquals('100B', $validator->getMax());
  121. $validator = new File\FilesSize(array('min' => 1, 'max' => 100000));
  122. $this->assertEquals('97.66kB', $validator->getMax());
  123. $validator = new File\FilesSize(2000);
  124. $validator->useByteString(false);
  125. $test = $validator->getMax();
  126. $this->assertEquals('2000', $test);
  127. $this->setExpectedException('Zend\Validator\Exception\InvalidArgumentException', 'greater than or equal');
  128. $validator = new File\FilesSize(array('min' => 100, 'max' => 1));
  129. }
  130. /**
  131. * Ensures that setMax() returns expected value
  132. *
  133. * @return void
  134. */
  135. public function testSetMax()
  136. {
  137. $validator = new File\FilesSize(array('min' => 1000, 'max' => 10000));
  138. $validator->setMax(1000000);
  139. $this->assertEquals('976.56kB', $validator->getMax());
  140. $validator->setMin(100);
  141. $this->assertEquals('976.56kB', $validator->getMax());
  142. }
  143. public function testConstructorShouldRaiseErrorWhenPassedMultipleOptions()
  144. {
  145. $handler = set_error_handler(array($this, 'errorHandler'), E_USER_NOTICE);
  146. $validator = new File\FilesSize(1000, 10000);
  147. restore_error_handler();
  148. }
  149. /**
  150. * Ensures that the validator returns size infos
  151. *
  152. * @return void
  153. */
  154. public function testFailureMessage()
  155. {
  156. $validator = new File\FilesSize(array('min' => 9999, 'max' => 10000));
  157. $this->assertFalse($validator->isValid(array(
  158. __DIR__ . '/_files/testsize.mo',
  159. __DIR__ . '/_files/testsize.mo',
  160. __DIR__ . '/_files/testsize2.mo'))
  161. );
  162. $messages = $validator->getMessages();
  163. $this->assertContains('9.76kB', current($messages));
  164. $this->assertContains('1.55kB', current($messages));
  165. $validator = new File\FilesSize(array('min' => 9999, 'max' => 10000, 'useByteString' => false));
  166. $this->assertFalse($validator->isValid(array(
  167. __DIR__ . '/_files/testsize.mo',
  168. __DIR__ . '/_files/testsize.mo',
  169. __DIR__ . '/_files/testsize2.mo'))
  170. );
  171. $messages = $validator->getMessages();
  172. $this->assertContains('9999', current($messages));
  173. $this->assertContains('1588', current($messages));
  174. }
  175. public function errorHandler($errno, $errstr)
  176. {
  177. if (strstr($errstr, 'deprecated')) {
  178. $this->multipleOptionsDetected = true;
  179. }
  180. }
  181. }