Skip to content

Commit

Permalink
Refactor tests to use a provider.
Browse files Browse the repository at this point in the history
Use a provider to streamline uploadedFile tests.
  • Loading branch information
markstory committed Oct 10, 2016
1 parent e646773 commit 30e118c
Showing 1 changed file with 25 additions and 54 deletions.
79 changes: 25 additions & 54 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -2510,9 +2510,10 @@ public function testUploadedFileErrorCode()
/**
* Test uploaded file validation.
*
* @dataProvider uploadedFileProvider
* @return void
*/
public function testUploadedFileMimeType()
public function testUploadedFileArray($expected, $options)
{
$file = [
'name' => 'cake.power.gif',
Expand All @@ -2521,40 +2522,7 @@ public function testUploadedFileMimeType()
'type' => 'text/plain',
'size' => 201
];
$options = [
'types' => ['text/plain']
];
$this->assertFalse(Validation::uploadedFile($file, $options), 'Incorrect mimetype.');

$options = [
'types' => ['image/gif', 'image/png']
];
$this->assertTrue(Validation::uploadedFile($file, $options));
}

/**
* Test uploaded file validation.
*
* @return void
*/
public function testUploadedFileSize()
{
$file = [
'name' => 'cake.power.gif',
'tmp_name' => TEST_APP . 'webroot/img/cake.power.gif',
'error' => UPLOAD_ERR_OK,
'type' => 'text/plain',
'size' => 201
];
$options = [
'minSize' => 500
];
$this->assertFalse(Validation::uploadedFile($file, $options), 'Too small');

$options = [
'maxSize' => 100
];
$this->assertFalse(Validation::uploadedFile($file, $options), 'Too big');
$this->assertSame($expected, Validation::uploadedFile($file, $options));
}

/**
Expand Down Expand Up @@ -2602,33 +2570,36 @@ public function testUploadedFileWithDifferentFileParametersOrder()
$this->assertTrue(Validation::uploadedFile($file, $options), 'Wrong order');
}

/**
* Provider for uploaded file tests.
*
* @return void
*/
public function uploadedFileProvider()
{
return [
'minSize fail' => [false, ['minSize' => 500]],
'minSize pass' => [true, ['minSize' => 190]],
'maxSize fail' => [false, ['maxSize' => 100]],
'maxSize pass' => [true, ['maxSize' => 202]],
'types fail' => [false, ['types' => ['text/plain']]],
'types fail - string' => [false, ['types' => '/^text.*$/']],
'types pass - string' => [true, ['types' => '/^image.*$/']],
'types pass' => [true, ['types' => ['image/gif', 'image/png']]],
];
}

/**
* Test uploadedFile with a PSR7 object.
*
* @dataProvider uploadedFileProvider
* @return void
*/
public function testUploadedFilePsr7()
public function testUploadedFilePsr7($expected, $options)
{
$image = TEST_APP . 'webroot/img/cake.power.gif';
$file = new UploadedFile($image, 1000, UPLOAD_ERR_OK, 'cake.power.gif', 'image/gif');

$options = ['minSize' => 500];
$this->assertFalse(Validation::uploadedFile($file, $options));

$options = ['minSize' => 190];
$this->assertTrue(Validation::uploadedFile($file, $options));

$options = ['maxSize' => 10];
$this->assertFalse(Validation::uploadedFile($file, $options));

$options = ['maxSize' => 1000];
$this->assertTrue(Validation::uploadedFile($file, $options));

$options = ['types' => ['image/gif', 'image/png']];
$this->assertTrue(Validation::uploadedFile($file, $options));

$options = ['types' => ['text/plain']];
$this->assertFalse(Validation::uploadedFile($file, $options));
$this->assertSame($expected, Validation::uploadedFile($file, $options));
}

/**
Expand Down

0 comments on commit 30e118c

Please sign in to comment.