PageRenderTime 258ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Validate/File/HashTest.php

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