PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Validator/File/HashTest.php

https://github.com/Exercise/zf2
PHP | 189 lines | 103 code | 20 blank | 66 comment | 3 complexity | d7b3593df4e4ab8659543e3df8f476dd 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_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. /**
  23. * @namespace
  24. */
  25. namespace ZendTest\Validate\File;
  26. use Zend\Validator\File;
  27. // Call Zend_Validate_File_HashTest::main() if this source file is executed directly.
  28. if (!defined("PHPUnit_MAIN_METHOD")) {
  29. define("PHPUnit_MAIN_METHOD", "Zend_Validate_File_HashTest::main");
  30. }
  31. /**
  32. * Test helper
  33. */
  34. /**
  35. * @see Zend_Validate_File_Hash
  36. */
  37. /**
  38. * Hash testbed
  39. *
  40. * @category Zend
  41. * @package Zend_Validate_File
  42. * @subpackage UnitTests
  43. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. * @group Zend_Validate
  46. */
  47. class HashTest extends \PHPUnit_Framework_TestCase
  48. {
  49. /**
  50. * Runs the test methods of this class.
  51. *
  52. * @return void
  53. */
  54. public static function main()
  55. {
  56. $suite = new \PHPUnit_Framework_TestSuite("Zend_Validate_File_HashTest");
  57. $result = \PHPUnit_TextUI_TestRunner::run($suite);
  58. }
  59. /**
  60. * Ensures that the validator follows expected behavior
  61. *
  62. * @return void
  63. */
  64. public function testBasic()
  65. {
  66. $valuesExpected = array(
  67. array('3f8d07e2', true),
  68. array('9f8d07e2', false),
  69. array(array('9f8d07e2', '3f8d07e2'), true),
  70. array(array('9f8d07e2', '7f8d07e2'), false),
  71. );
  72. foreach ($valuesExpected as $element) {
  73. $validator = new File\Hash($element[0]);
  74. $this->assertEquals(
  75. $element[1],
  76. $validator->isValid(__DIR__ . '/_files/picture.jpg'),
  77. "Tested with " . var_export($element, 1)
  78. );
  79. }
  80. $valuesExpected = array(
  81. array(array('ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), true),
  82. array(array('4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), false),
  83. array(array('4d74c22109fe9f110579f77b053b8bc3', 'ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), true),
  84. array(array('1d74c22109fe9f110579f77b053b8bc3', '4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), false),
  85. );
  86. foreach ($valuesExpected as $element) {
  87. $validator = new File\Hash($element[0]);
  88. $this->assertEquals(
  89. $element[1],
  90. $validator->isValid(__DIR__ . '/_files/picture.jpg'),
  91. "Tested with " . var_export($element, 1)
  92. );
  93. }
  94. $validator = new File\Hash('3f8d07e2');
  95. $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo'));
  96. $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
  97. $files = array(
  98. 'name' => 'test1',
  99. 'type' => 'text',
  100. 'size' => 200,
  101. 'tmp_name' => 'tmp_test1',
  102. 'error' => 0
  103. );
  104. $validator = new File\Hash('3f8d07e2');
  105. $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo', $files));
  106. $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
  107. $files = array(
  108. 'name' => 'testsize.mo',
  109. 'type' => 'text',
  110. 'size' => 200,
  111. 'tmp_name' => __DIR__ . '/_files/testsize.mo',
  112. 'error' => 0
  113. );
  114. $validator = new File\Hash('3f8d07e2');
  115. $this->assertTrue($validator->isValid(__DIR__ . '/_files/picture.jpg', $files));
  116. $files = array(
  117. 'name' => 'testsize.mo',
  118. 'type' => 'text',
  119. 'size' => 200,
  120. 'tmp_name' => __DIR__ . '/_files/testsize.mo',
  121. 'error' => 0
  122. );
  123. $validator = new File\Hash('9f8d07e2');
  124. $this->assertFalse($validator->isValid(__DIR__ . '/_files/picture.jpg', $files));
  125. $this->assertTrue(array_key_exists('fileHashDoesNotMatch', $validator->getMessages()));
  126. }
  127. /**
  128. * Ensures that getHash() returns expected value
  129. *
  130. * @return void
  131. */
  132. public function testgetHash()
  133. {
  134. $validator = new File\Hash('12345');
  135. $this->assertEquals(array('12345' => 'crc32'), $validator->getHash());
  136. $validator = new File\Hash(array('12345', '12333', '12344'));
  137. $this->assertEquals(array('12345' => 'crc32', '12333' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  138. }
  139. /**
  140. * Ensures that setHash() returns expected value
  141. *
  142. * @return void
  143. */
  144. public function testSetHash()
  145. {
  146. $validator = new File\Hash('12345');
  147. $validator->setHash('12333');
  148. $this->assertEquals(array('12333' => 'crc32'), $validator->getHash());
  149. $validator->setHash(array('12321', '12121'));
  150. $this->assertEquals(array('12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
  151. }
  152. /**
  153. * Ensures that addHash() returns expected value
  154. *
  155. * @return void
  156. */
  157. public function testAddHash()
  158. {
  159. $validator = new File\Hash('12345');
  160. $validator->addHash('12344');
  161. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  162. $validator->addHash(array('12321', '12121'));
  163. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32', '12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
  164. }
  165. }
  166. // Call Zend_Validate_File_HashTest::main() if this source file is executed directly.
  167. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_HashTest::main") {
  168. \Zend_Validate_File_HashTest::main();
  169. }