/tests/ZendTest/Validator/File/HashTest.php
https://github.com/zucchi/zf2 · PHP · 155 lines · 99 code · 17 blank · 39 comment · 0 complexity · 5ec74fa4288c0a0c9230881dea82b51b MD5 · raw file
- <?php
- /**
- * Zend Framework (http://framework.zend.com/)
- *
- * @link http://github.com/zendframework/zf2 for the canonical source repository
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @package Zend_Validator
- */
- namespace ZendTest\Validator\File;
- use Zend\Validator\File;
- /**
- * Hash testbed
- *
- * @category Zend
- * @package Zend_Validator_File
- * @subpackage UnitTests
- * @group Zend_Validator
- */
- class HashTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * Ensures that the validator follows expected behavior
- *
- * @return void
- */
- public function testBasic()
- {
- $valuesExpected = array(
- array('3f8d07e2', true),
- array('9f8d07e2', false),
- array(array('9f8d07e2', '3f8d07e2'), true),
- array(array('9f8d07e2', '7f8d07e2'), false),
- );
- foreach ($valuesExpected as $element) {
- $validator = new File\Hash($element[0]);
- $this->assertEquals(
- $element[1],
- $validator->isValid(__DIR__ . '/_files/picture.jpg'),
- "Tested with " . var_export($element, 1)
- );
- }
- $valuesExpected = array(
- array(array('ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), true),
- array(array('4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), false),
- array(array('4d74c22109fe9f110579f77b053b8bc3', 'ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), true),
- array(array('1d74c22109fe9f110579f77b053b8bc3', '4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), false),
- );
- foreach ($valuesExpected as $element) {
- $validator = new File\Hash($element[0]);
- $this->assertEquals(
- $element[1],
- $validator->isValid(__DIR__ . '/_files/picture.jpg'),
- "Tested with " . var_export($element, 1)
- );
- }
- $validator = new File\Hash('3f8d07e2');
- $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo'));
- $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
- $files = array(
- 'name' => 'test1',
- 'type' => 'text',
- 'size' => 200,
- 'tmp_name' => 'tmp_test1',
- 'error' => 0
- );
- $validator = new File\Hash('3f8d07e2');
- $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo', $files));
- $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
- $files = array(
- 'name' => 'testsize.mo',
- 'type' => 'text',
- 'size' => 200,
- 'tmp_name' => __DIR__ . '/_files/testsize.mo',
- 'error' => 0
- );
- $validator = new File\Hash('3f8d07e2');
- $this->assertTrue($validator->isValid(__DIR__ . '/_files/picture.jpg', $files));
- $files = array(
- 'name' => 'testsize.mo',
- 'type' => 'text',
- 'size' => 200,
- 'tmp_name' => __DIR__ . '/_files/testsize.mo',
- 'error' => 0
- );
- $validator = new File\Hash('9f8d07e2');
- $this->assertFalse($validator->isValid(__DIR__ . '/_files/picture.jpg', $files));
- $this->assertTrue(array_key_exists('fileHashDoesNotMatch', $validator->getMessages()));
- }
- /**
- * Ensures that getHash() returns expected value
- *
- * @return void
- */
- public function testgetHash()
- {
- $validator = new File\Hash('12345');
- $this->assertEquals(array('12345' => 'crc32'), $validator->getHash());
- $validator = new File\Hash(array('12345', '12333', '12344'));
- $this->assertEquals(array('12345' => 'crc32', '12333' => 'crc32', '12344' => 'crc32'), $validator->getHash());
- }
- /**
- * Ensures that setHash() returns expected value
- *
- * @return void
- */
- public function testSetHash()
- {
- $validator = new File\Hash('12345');
- $validator->setHash('12333');
- $this->assertEquals(array('12333' => 'crc32'), $validator->getHash());
- $validator->setHash(array('12321', '12121'));
- $this->assertEquals(array('12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
- }
- /**
- * Ensures that addHash() returns expected value
- *
- * @return void
- */
- public function testAddHash()
- {
- $validator = new File\Hash('12345');
- $validator->addHash('12344');
- $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32'), $validator->getHash());
- $validator->addHash(array('12321', '12121'));
- $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32', '12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
- }
- /**
- * @group ZF-11258
- */
- public function testZF11258()
- {
- $validator = new File\Hash('3f8d07e2');
- $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo'));
- $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
- $this->assertContains("'nofile.mo'", current($validator->getMessages()));
- }
- }