/tests/ZendTest/Validator/File/Crc32Test.php

https://github.com/cgmartin/zf2 · PHP · 188 lines · 105 code · 21 blank · 62 comment · 3 complexity · efb71acd96045312071f0ef33c71c767 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Validator\File;
  10. use Zend\Validator\File;
  11. /**
  12. * @group Zend_Validator
  13. */
  14. class Crc32Test extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @return array
  18. */
  19. public function basicBehaviorDataProvider()
  20. {
  21. $testFile = __DIR__ . '/_files/picture.jpg';
  22. $pictureTests = array(
  23. // Options, isValid Param, Expected value, Expected message
  24. array('3f8d07e2', $testFile, true, ''),
  25. array('9f8d07e2', $testFile, false, 'fileCrc32DoesNotMatch'),
  26. array(array('9f8d07e2', '3f8d07e2'), $testFile, true, ''),
  27. array(array('9f8d07e2', '7f8d07e2'), $testFile, false, 'fileCrc32DoesNotMatch'),
  28. );
  29. $testFile = __DIR__ . '/_files/nofile.mo';
  30. $noFileTests = array(
  31. // Options, isValid Param, Expected value, message
  32. array('3f8d07e2', $testFile, false, 'fileCrc32NotFound'),
  33. );
  34. $testFile = __DIR__ . '/_files/testsize.mo';
  35. $sizeFileTests = array(
  36. // Options, isValid Param, Expected value, message
  37. array('ffeb8d5d', $testFile, true, ''),
  38. array('9f8d07e2', $testFile, false, 'fileCrc32DoesNotMatch'),
  39. );
  40. // Dupe data in File Upload format
  41. $testData = array_merge($pictureTests, $noFileTests, $sizeFileTests);
  42. foreach ($testData as $data) {
  43. $fileUpload = array(
  44. 'tmp_name' => $data[1], 'name' => basename($data[1]),
  45. 'size' => 200, 'error' => 0, 'type' => 'text'
  46. );
  47. $testData[] = array($data[0], $fileUpload, $data[2], $data[3]);
  48. }
  49. return $testData;
  50. }
  51. /**
  52. * Ensures that the validator follows expected behavior
  53. *
  54. * @dataProvider basicBehaviorDataProvider
  55. * @return void
  56. */
  57. public function testBasic($options, $isValidParam, $expected, $messageKey)
  58. {
  59. $validator = new File\Crc32($options);
  60. $this->assertEquals($expected, $validator->isValid($isValidParam));
  61. if (!$expected) {
  62. $this->assertTrue(array_key_exists($messageKey, $validator->getMessages()));
  63. }
  64. }
  65. /**
  66. * Ensures that the validator follows expected behavior for legacy Zend\Transfer API
  67. *
  68. * @dataProvider basicBehaviorDataProvider
  69. * @return void
  70. */
  71. public function testLegacy($options, $isValidParam, $expected, $messageKey)
  72. {
  73. if (is_array($isValidParam)) {
  74. $validator = new File\Crc32($options);
  75. $this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
  76. if (!$expected) {
  77. $this->assertTrue(array_key_exists($messageKey, $validator->getMessages()));
  78. }
  79. }
  80. }
  81. /**
  82. * Ensures that getCrc32() returns expected value
  83. *
  84. * @return void
  85. */
  86. public function testgetCrc32()
  87. {
  88. $validator = new File\Crc32('12345');
  89. $this->assertEquals(array('12345' => 'crc32'), $validator->getCrc32());
  90. $validator = new File\Crc32(array('12345', '12333', '12344'));
  91. $this->assertEquals(array('12345' => 'crc32', '12333' => 'crc32', '12344' => 'crc32'), $validator->getCrc32());
  92. }
  93. /**
  94. * Ensures that getHash() returns expected value
  95. *
  96. * @return void
  97. */
  98. public function testgetHash()
  99. {
  100. $validator = new File\Crc32('12345');
  101. $this->assertEquals(array('12345' => 'crc32'), $validator->getHash());
  102. $validator = new File\Crc32(array('12345', '12333', '12344'));
  103. $this->assertEquals(array('12345' => 'crc32', '12333' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  104. }
  105. /**
  106. * Ensures that setCrc32() returns expected value
  107. *
  108. * @return void
  109. */
  110. public function testSetCrc32()
  111. {
  112. $validator = new File\Crc32('12345');
  113. $validator->setCrc32('12333');
  114. $this->assertEquals(array('12333' => 'crc32'), $validator->getCrc32());
  115. $validator->setCrc32(array('12321', '12121'));
  116. $this->assertEquals(array('12321' => 'crc32', '12121' => 'crc32'), $validator->getCrc32());
  117. }
  118. /**
  119. * Ensures that setHash() returns expected value
  120. *
  121. * @return void
  122. */
  123. public function testSetHash()
  124. {
  125. $validator = new File\Crc32('12345');
  126. $validator->setHash('12333');
  127. $this->assertEquals(array('12333' => 'crc32'), $validator->getCrc32());
  128. $validator->setHash(array('12321', '12121'));
  129. $this->assertEquals(array('12321' => 'crc32', '12121' => 'crc32'), $validator->getCrc32());
  130. }
  131. /**
  132. * Ensures that addCrc32() returns expected value
  133. *
  134. * @return void
  135. */
  136. public function testAddCrc32()
  137. {
  138. $validator = new File\Crc32('12345');
  139. $validator->addCrc32('12344');
  140. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32'), $validator->getCrc32());
  141. $validator->addCrc32(array('12321', '12121'));
  142. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32', '12321' => 'crc32', '12121' => 'crc32'), $validator->getCrc32());
  143. }
  144. /**
  145. * Ensures that addHash() returns expected value
  146. *
  147. * @return void
  148. */
  149. public function testAddHash()
  150. {
  151. $validator = new File\Crc32('12345');
  152. $validator->addHash('12344');
  153. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32'), $validator->getCrc32());
  154. $validator->addHash(array('12321', '12121'));
  155. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32', '12321' => 'crc32', '12121' => 'crc32'), $validator->getCrc32());
  156. }
  157. /**
  158. * @group ZF-11258
  159. */
  160. public function testZF11258()
  161. {
  162. $validator = new File\Crc32('3f8d07e2');
  163. $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo'));
  164. $this->assertTrue(array_key_exists('fileCrc32NotFound', $validator->getMessages()));
  165. $this->assertContains("does not exist", current($validator->getMessages()));
  166. }
  167. }