Skip to content

Commit

Permalink
Add tests (#9)
Browse files Browse the repository at this point in the history
* Add test for filesize validator
use symfony

* if size in value

* Chang to array

* Is array check
  • Loading branch information
MrEssex authored Jan 12, 2023
1 parent f940013 commit b8c9cd8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion js/validators/FileSizeValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class FileSizeValidator extends Validator

validate(value)
{
if(value.hasOwnProperty('size') && value.size > (this._maxSize * 1024 * 1024))
if('size' in value && value.size > (this._maxSize * 1024 * 1024))
{
return ValidationResponse.error(['File upload cannot be more than ' + this._maxSize + 'mb in size']);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@packaged/validate",
"version": "3.1.3",
"version": "3.1.4",
"main": "index.js",
"repository": "[email protected]:packaged/validate",
"author": "Tom Kay <[email protected]>",
Expand Down
4 changes: 2 additions & 2 deletions src/Validators/FileSizeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public static function deserialize($configuration): SerializableValidator
public function serialize(): array
{
return [
'maxSize' => $this->_maxSize,
'maxSize' => $this->getMaxSize(),
];
}

protected function _doValidate($value): Generator
{
// Validation
if(array_key_exists('size', $value) && $value['size'] < ($this->_maxSize * 1024 * 1024))
if(is_array($value) && array_key_exists('size', $value) && $value['size'] > ($this->_maxSize * 1024 * 1024))
{
yield $this->_makeError("File upload cannot be more than " . $this->_maxSize . "mb in size");
}
Expand Down
19 changes: 19 additions & 0 deletions tests/FileSizeValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Packaged\Validate\Tests;

use Packaged\Validate\Validators\FileSizeValidator;
use PHPUnit\Framework\TestCase;

class FileSizeValidatorTest extends TestCase
{
public function testNumberValidation()
{
$upload = ['size' => 1159144];
$validator = new FileSizeValidator(3);

$largeUpload = ['size' => 5123224];

$this->assertTrue($validator->isValid($upload));
$this->assertFalse($validator->isValid($largeUpload));
}
}

0 comments on commit b8c9cd8

Please sign in to comment.