diff --git a/.sensiolabs.yml b/.sensiolabs.yml new file mode 100644 index 0000000..a1223e5 --- /dev/null +++ b/.sensiolabs.yml @@ -0,0 +1,4 @@ +global_exclude_dirs: + - vendor + - example + - tests diff --git a/.travis.yml b/.travis.yml index ad965af..02c3872 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,8 +9,7 @@ install: - composer install --no-interaction script: - - phpunit --coverage-clover=coverage.clover --coverage-text + - composer test-ci after_script: - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover coverage.clover + - composer upload-code-coverage diff --git a/README.md b/README.md index 0b62781..18c68a2 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -# Validation traits - [![Travis](https://img.shields.io/travis/webcore/validation-traits.svg?maxAge=2592000)](https://travis-ci.org/webcore/validation-traits) [![SensioLabs Insight](https://img.shields.io/sensiolabs/i/6a166fab-ddf0-4d5c-b605-09f6681f8795.svg?maxAge=2592000)](https://insight.sensiolabs.com/projects/6a166fab-ddf0-4d5c-b605-09f6681f8795) [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/webcore/validation-traits.svg?maxAge=2592000)](https://scrutinizer-ci.com/g/webcore/validation-traits/?branch=master) @@ -7,19 +5,50 @@ [![Dependency Status](https://www.versioneye.com/user/projects/575827ac7757a0004a1de479/badge.svg?style=plastic)](https://www.versioneye.com/user/projects/575827ac7757a0004a1de479) [![license](https://img.shields.io/github/license/webcore/validation-traits.svg?maxAge=2592000)](https://opensource.org/licenses/MIT) +# Validation traits + +A PHP library/collection of traits aimed to simplify cration of immutable value objects with validation of input value. Basic idea was also inspired by [Laravel's Validation trait for Eloquent models](https://github.com/dwightwatson/validating) and redesign more towards [nicolopignatelli/valueobjects](https://github.com/nicolopignatelli/valueobjects) + +[Version 1.0](https://github.com/webcore/validation-traits/tree/v1.0) has less complex attitude on value objects and provide mainly validation via one trait. + +## Installation + +Via Composer + +``` +composer require webcore/validation-traits +``` + ### Example -Let's define simple token class with 3 rules: +`SingleValueObjectInterface` define getter method to retrieve value and method for comparing with another value objects implementing this interface. ```php -getValue(); // 1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz +$value = str_repeat('BeerNowThere0sATemporarySolution', 2); +$tokenFoo = new Token($value); +Assert::same("BeerNowThere0sATemporarySolutionBeerNowThere0sATemporarySolution", $tokenFoo->getValue()); + +//compare with another object of same value +$tokenBar = new Token("BeerNowThere0sATemporarySolutionBeerNowThere0sATemporarySolution"); +$areSame = $tokenBar->sameValueAs($tokenFoo); +Assert::true($areSame); + +//compare with another object of different value +$value = str_repeat('CraftBeerLovers0', 4); // +$tokenPub = new Token($value); +Assert::same("CraftBeerLovers0CraftBeerLovers0CraftBeerLovers0CraftBeerLovers0", $tokenPub->getValue()); +$areSame = $tokenPub->sameValueAs($tokenBar); +Assert::false($areSame); //invalid values -try { - $value = null; - $token = new Token($value); -} catch (InvalidArgumentException $e) { - echo $e->getMessage(); //Token must be not empty -} - -try { - $value = "InvalidTokenLength123456789"; - $token = new Token($value); -} catch (InvalidArgumentException $e) { - echo $e->getMessage(); //Token must be 64 characters long -} - -try { - $value = str_repeat('?!@#$%^&', 8);; - $token = new Token($value); -} catch (InvalidArgumentException $e) { - echo $e->getMessage(); //Token must be valid base_64 -} - +Assert::exception( + function () { + new Token(null); + }, + InvalidArgumentException::class, + "Token must be not empty" +); + +Assert::exception( + function () { + new Token("InvalidTokenLength123456789"); + }, + InvalidArgumentException::class, + "Token must be 64 characters long" +); + +Assert::exception( + function () { + $value = str_repeat('?!@#$%^&', 8); + new Token($value); + }, + InvalidArgumentException::class, + "Token must be valid base_64" +); ``` -### TODO - -- divide into single traits for each validation - ### MIT license Copyright (c) 2016, Štefan Peťovský diff --git a/composer.json b/composer.json index fff3b3b..43103ea 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "php": ">=5.6" }, "require-dev": { - "phpunit/phpunit": "^5.4.2" + "phpunit/phpunit": "^5.4.2", + "nette/tester": "^1.7" }, "autoload": { "psr-4": { @@ -22,5 +23,14 @@ }, "autoload-dev": { "psr-4": { "Webcore\\Validation\\Tests\\": "tests/" } + }, + "scripts": { + "test": "vendor/bin/phpunit --coverage-text", + "test-html": "vendor/bin/phpunit --coverage-html coverage tests", + "test-ci": "vendor/bin/phpunit --coverage-clover=coverage.clover --coverage-text", + "upload-code-coverage": [ + "wget https://scrutinizer-ci.com/ocular.phar", + "php ocular.phar code-coverage:upload --format=php-clover coverage.clover" + ] } } diff --git a/composer.lock b/composer.lock index db34bdf..ae53b75 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "78dfba6b284762cd2c53d3d59ff03125", - "content-hash": "94e04ffe35cc71da7dcfc748c5e52fdb", + "hash": "b933e8fdb3d915aab1c6887d1e7363c2", + "content-hash": "841cf17dc3a6b9d82e0f0f923759ad9e", "packages": [], "packages-dev": [ { @@ -105,135 +105,94 @@ "time": "2015-11-20 12:04:31" }, { - "name": "phpdocumentor/reflection-common", - "version": "1.0", + "name": "nette/tester", + "version": "v1.7.1", "source": { "type": "git", - "url": "https://github.com/phpDocumentor/ReflectionCommon.git", - "reference": "144c307535e82c8fdcaacbcfc1d6d8eeb896687c" + "url": "https://github.com/nette/tester.git", + "reference": "d97534578e8cf66eabe081e7d5eaa4dd527ab0c8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/144c307535e82c8fdcaacbcfc1d6d8eeb896687c", - "reference": "144c307535e82c8fdcaacbcfc1d6d8eeb896687c", + "url": "https://api.github.com/repos/nette/tester/zipball/d97534578e8cf66eabe081e7d5eaa4dd527ab0c8", + "reference": "d97534578e8cf66eabe081e7d5eaa4dd527ab0c8", "shasum": "" }, "require": { - "php": ">=5.5" - }, - "require-dev": { - "phpunit/phpunit": "^4.6" + "php": ">=5.3.0" }, + "bin": [ + "src/tester" + ], "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.7-dev" } }, "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": [ - "src" - ] - } + "classmap": [ + "src/" + ] }, "notification-url": "https://packagist.org/downloads/", "license": [ - "MIT" + "BSD-3-Clause", + "GPL-2.0", + "GPL-3.0" ], "authors": [ { - "name": "Jaap van Otterdijk", - "email": "opensource@ijaap.nl" + "name": "David Grudl", + "homepage": "https://davidgrudl.com" + }, + { + "name": "Nette Community", + "homepage": "https://nette.org/contributors" } ], - "description": "Common reflection classes used by phpdocumentor to reflect the code structure", - "homepage": "http://www.phpdoc.org", + "description": "An easy-to-use PHP unit testing framework.", + "homepage": "https://tester.nette.org", "keywords": [ - "FQSEN", - "phpDocumentor", - "phpdoc", - "reflection", - "static analysis" + "nette", + "testing", + "unit" ], - "time": "2015-12-27 11:43:31" + "time": "2016-03-19 14:34:02" }, { "name": "phpdocumentor/reflection-docblock", - "version": "3.0.2", + "version": "2.0.4", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "45ada3e3fd09789fbfbd6d65b3f0901f0030dc61" + "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/45ada3e3fd09789fbfbd6d65b3f0901f0030dc61", - "reference": "45ada3e3fd09789fbfbd6d65b3f0901f0030dc61", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/d68dbdc53dc358a816f00b300704702b2eaff7b8", + "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8", "shasum": "" }, "require": { - "php": ">=5.5", - "phpdocumentor/reflection-common": "^1.0@dev", - "phpdocumentor/type-resolver": "^0.1.5", - "webmozart/assert": "^1.0" + "php": ">=5.3.3" }, "require-dev": { - "mockery/mockery": "^0.9.4", - "phpunit/phpunit": "^4.4" - }, - "type": "library", - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": [ - "src/" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike van Riel", - "email": "me@mikevanriel.com" - } - ], - "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2016-06-06 06:44:13" - }, - { - "name": "phpdocumentor/type-resolver", - "version": "0.1.8", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "9891754231e55d42f0d16988ffb799af39f31a12" + "phpunit/phpunit": "~4.0" }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/9891754231e55d42f0d16988ffb799af39f31a12", - "reference": "9891754231e55d42f0d16988ffb799af39f31a12", - "shasum": "" - }, - "require": { - "php": ">=5.5", - "phpdocumentor/reflection-common": "^1.0" - }, - "require-dev": { - "mockery/mockery": "^0.9.4", - "phpunit/phpunit": "^5.2" + "suggest": { + "dflydev/markdown": "~1.0", + "erusev/parsedown": "~1.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "2.0.x-dev" } }, "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": [ + "psr-0": { + "phpDocumentor": [ "src/" ] } @@ -245,10 +204,10 @@ "authors": [ { "name": "Mike van Riel", - "email": "me@mikevanriel.com" + "email": "mike.vanriel@naenius.com" } ], - "time": "2016-03-28 10:02:29" + "time": "2015-02-03 12:10:50" }, { "name": "phpspec/prophecy", @@ -558,16 +517,16 @@ }, { "name": "phpunit/phpunit", - "version": "5.4.2", + "version": "5.4.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "f5726a0262e5f74f8e9cf03128798b64160c441d" + "reference": "02d5b64aa0837a038a5a4faeeefa5ef44bdcb928" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f5726a0262e5f74f8e9cf03128798b64160c441d", - "reference": "f5726a0262e5f74f8e9cf03128798b64160c441d", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/02d5b64aa0837a038a5a4faeeefa5ef44bdcb928", + "reference": "02d5b64aa0837a038a5a4faeeefa5ef44bdcb928", "shasum": "" }, "require": { @@ -594,6 +553,9 @@ "sebastian/version": "~1.0|~2.0", "symfony/yaml": "~2.1|~3.0" }, + "conflict": { + "phpdocumentor/reflection-docblock": "3.0.2" + }, "suggest": { "phpunit/php-invoker": "~1.1" }, @@ -629,7 +591,7 @@ "testing", "xunit" ], - "time": "2016-06-03 09:59:50" + "time": "2016-06-09 09:09:27" }, { "name": "phpunit/phpunit-mock-objects", @@ -1250,55 +1212,6 @@ "description": "Symfony Yaml Component", "homepage": "https://symfony.com", "time": "2016-05-26 21:46:24" - }, - { - "name": "webmozart/assert", - "version": "1.0.2", - "source": { - "type": "git", - "url": "https://github.com/webmozart/assert.git", - "reference": "30eed06dd6bc88410a4ff7f77b6d22f3ce13dbde" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/30eed06dd6bc88410a4ff7f77b6d22f3ce13dbde", - "reference": "30eed06dd6bc88410a4ff7f77b6d22f3ce13dbde", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "^4.6" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - } - }, - "autoload": { - "psr-4": { - "Webmozart\\Assert\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Bernhard Schussek", - "email": "bschussek@gmail.com" - } - ], - "description": "Assertions to validate method input/output with nice error messages.", - "keywords": [ - "assert", - "check", - "validate" - ], - "time": "2015-08-24 13:29:44" } ], "aliases": [], diff --git a/example/Token.php b/example/Token.php index b534e7d..30e5c65 100644 --- a/example/Token.php +++ b/example/Token.php @@ -1,11 +1,14 @@ getValue(); // 1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz +$value = str_repeat('BeerNowThere0sATemporarySolution', 2); +$tokenFoo = new Token($value); +Assert::same("BeerNowThere0sATemporarySolutionBeerNowThere0sATemporarySolution", $tokenFoo->getValue()); + +//compare with another object of same value +$tokenBar = new Token("BeerNowThere0sATemporarySolutionBeerNowThere0sATemporarySolution"); +$areSame = $tokenBar->sameValueAs($tokenFoo); +Assert::true($areSame); + +//compare with another object of different value +$value = str_repeat('CraftBeerLovers0', 4); // +$tokenPub = new Token($value); +Assert::same("CraftBeerLovers0CraftBeerLovers0CraftBeerLovers0CraftBeerLovers0", $tokenPub->getValue()); +$areSame = $tokenPub->sameValueAs($tokenBar); +Assert::false($areSame); //invalid values -try { - $value = null; - $token = new Token($value); -} catch (InvalidArgumentException $e) { - echo $e->getMessage(); //Token must be not empty -} - -try { - $value = "InvalidTokenLength123456789"; - $token = new Token($value); -} catch (InvalidArgumentException $e) { - echo $e->getMessage(); //Token must be 64 characters long -} - -try { - $value = str_repeat('?!@#$%^&', 8);; - $token = new Token($value); -} catch (InvalidArgumentException $e) { - echo $e->getMessage(); //Token must be valid base_64 -} +Assert::exception( + function () { + new Token(null); + }, + InvalidArgumentException::class, + "Token must be not empty" +); + +Assert::exception( + function () { + new Token("InvalidTokenLength123456789"); + }, + InvalidArgumentException::class, + "Token must be 64 characters long" +); + +Assert::exception( + function () { + $value = str_repeat('?!@#$%^&', 8); + new Token($value); + }, + InvalidArgumentException::class, + "Token must be valid base_64" +); diff --git a/src/AbstractRuleTrait.php b/src/AbstractRuleTrait.php new file mode 100644 index 0000000..8b832cb --- /dev/null +++ b/src/AbstractRuleTrait.php @@ -0,0 +1,20 @@ +throwInvalidArgumentMustBe("valid base_64"); + } + } +} diff --git a/src/Rules/BoolTrait.php b/src/Rules/BoolTrait.php new file mode 100644 index 0000000..ed0c99f --- /dev/null +++ b/src/Rules/BoolTrait.php @@ -0,0 +1,19 @@ +throwInvalidArgumentMustBe("boolean"); + } + } +} diff --git a/src/Rules/EmailTrait.php b/src/Rules/EmailTrait.php new file mode 100644 index 0000000..8845635 --- /dev/null +++ b/src/Rules/EmailTrait.php @@ -0,0 +1,26 @@ +throwInvalidArgumentMustBe("valid email"); + } + } +} diff --git a/src/Rules/FloatTrait.php b/src/Rules/FloatTrait.php new file mode 100644 index 0000000..5f6cd8b --- /dev/null +++ b/src/Rules/FloatTrait.php @@ -0,0 +1,19 @@ +throwInvalidArgumentMustBe("float"); + } + } +} diff --git a/src/Rules/HexadecimalTrait.php b/src/Rules/HexadecimalTrait.php new file mode 100644 index 0000000..587290c --- /dev/null +++ b/src/Rules/HexadecimalTrait.php @@ -0,0 +1,31 @@ + array("regexp" => '`^[0-9A-Fa-f]+$`'), + ) + ); + if ($isValid === false) { + $this->throwInvalidArgumentMustBe("valid hexadecimal"); + } + } +} diff --git a/src/Rules/IntegerTrait.php b/src/Rules/IntegerTrait.php new file mode 100644 index 0000000..6cffedb --- /dev/null +++ b/src/Rules/IntegerTrait.php @@ -0,0 +1,19 @@ +throwInvalidArgumentMustBe("integer"); + } + } +} diff --git a/src/Rules/IpAddressTrait.php b/src/Rules/IpAddressTrait.php new file mode 100644 index 0000000..9955ccc --- /dev/null +++ b/src/Rules/IpAddressTrait.php @@ -0,0 +1,20 @@ +throwInvalidArgumentMustBe("valid ip address"); + } + } +} diff --git a/src/Rules/LengthRangeTrait.php b/src/Rules/LengthRangeTrait.php new file mode 100644 index 0000000..dd1d2fa --- /dev/null +++ b/src/Rules/LengthRangeTrait.php @@ -0,0 +1,20 @@ + $end) { + $this->throwInvalidArgumentMustBe(" long in range between ".$start." and".$end); + } + } +} diff --git a/src/Rules/LengthTrait.php b/src/Rules/LengthTrait.php new file mode 100644 index 0000000..12fa7d7 --- /dev/null +++ b/src/Rules/LengthTrait.php @@ -0,0 +1,20 @@ +throwInvalidArgumentMustBe($length." characters long"); + } + } +} diff --git a/src/Rules/NotEmptyTrait.php b/src/Rules/NotEmptyTrait.php new file mode 100644 index 0000000..0b7d382 --- /dev/null +++ b/src/Rules/NotEmptyTrait.php @@ -0,0 +1,19 @@ +throwInvalidArgumentMustBe("not empty"); + } + } +} diff --git a/src/Rules/StringTrait.php b/src/Rules/StringTrait.php new file mode 100644 index 0000000..b08a91e --- /dev/null +++ b/src/Rules/StringTrait.php @@ -0,0 +1,19 @@ +throwInvalidArgumentMustBe("string"); + } + } +} diff --git a/src/Rules/UrlTrait.php b/src/Rules/UrlTrait.php new file mode 100644 index 0000000..61a12cd --- /dev/null +++ b/src/Rules/UrlTrait.php @@ -0,0 +1,20 @@ +throwInvalidArgumentMustBe("valid url"); + } + } +} diff --git a/src/SingleValueObjectInterface.php b/src/SingleValueObjectInterface.php new file mode 100644 index 0000000..90a58cc --- /dev/null +++ b/src/SingleValueObjectInterface.php @@ -0,0 +1,21 @@ +validation($value); + $this->value = $value; + } + + /** + * @return mixed + */ + public function getValue() + { + return $this->value; + } + + /** + * Compare two SingleValueObject and tells whether they can be considered equal + * + * @param SingleValueObjectInterface $object + * @return bool + */ + public function sameValueAs(SingleValueObjectInterface $object) + { + $areSame = ($this->value === $object->getValue()); + + return $areSame; + } + + /** + * @param string $string + * @throws InvalidArgumentException + */ + protected function throwInvalidArgumentMustBe($string) + { + throw new InvalidArgumentException((new ReflectionClass($this))->getShortName()." must be ".$string); + } + + /** + * @param mixed $value + * @return null + */ + abstract protected function validation($value); +} diff --git a/src/ValidationTrait.php b/src/ValidationTrait.php deleted file mode 100644 index e972e21..0000000 --- a/src/ValidationTrait.php +++ /dev/null @@ -1,124 +0,0 @@ -throwInvalidArgumentMustBe("string"); - } - } - - private function validateInteger($value) - { - if (!is_integer($value)) { - $this->throwInvalidArgumentMustBe("integer"); - } - } - - private function validateFloat($value) - { - if (!is_float($value)) { - $this->throwInvalidArgumentMustBe("float"); - } - } - - private function validateBool($value) - { - if (!is_bool($value)) { - $this->throwInvalidArgumentMustBe("boolean"); - } - } - - /** - * Allowed characters: A-Z, a-z, 0-9, +, /, = - * - * @param $value - */ - private function validateBase64($value) - { - $isNotValid = base64_decode($value, true) === false; - if ($isNotValid) { - $this->throwInvalidArgumentMustBe("valid base_64"); - } - } - - private function validateIpAddress($value) - { - $isValid = filter_var($value, FILTER_VALIDATE_IP); - if ($isValid === false) { - $this->throwInvalidArgumentMustBe("valid ip address"); - } - } - - /** - * "I found some addresses that FILTER_VALIDATE_EMAIL rejects, but RFC5321 permits" - * http://php.net/manual/en/function.filter-var.php#112492 - * - * @param $value - */ - private function validateEmail($value) - { - $isValid = filter_var($value, FILTER_VALIDATE_EMAIL); - if ($isValid === false) { - $this->throwInvalidArgumentMustBe("valid email"); - } - } - - private function validateUrl($value) - { - $isValid = filter_var($value, FILTER_VALIDATE_URL); - if ($isValid === false) { - $this->throwInvalidArgumentMustBe("valid url"); - } - } - - /** - * Allowed characters: 0-9, a-f, A-F - * - * @param $value - */ - private function validateHexadecimal($value) - { - $isValid = filter_var( - $value, - FILTER_VALIDATE_REGEXP, - array( - "options" => array("regexp" => '`^[0-9A-Fa-f]+$`') - ) - ); - if ($isValid === false) { - $this->throwInvalidArgumentMustBe("valid hexadecimal"); - } - } - - public function validateLength($value, $length) - { - $realLength = strlen($value); - if ($realLength !== $length) { - $this->throwInvalidArgumentMustBe($length." characters long"); - } - } - - private function validateNotEmpty($value) - { - if ($value === null) { - $this->throwInvalidArgumentMustBe("not empty"); - } - } - - private function throwInvalidArgumentMustBe($string) - { - throw new InvalidArgumentException((new ReflectionClass($this))->getShortName()." must be ".$string); - } -} diff --git a/src/ValidationValueBoxTrait.php b/src/ValidationValueBoxTrait.php deleted file mode 100644 index 5f73e78..0000000 --- a/src/ValidationValueBoxTrait.php +++ /dev/null @@ -1,33 +0,0 @@ -validation($value); - $this->value = $value; - } - - /** - * @return mixed - */ - public function getValue() - { - return $this->value; - } - - /** - * @param $value - * @return void - */ - abstract protected function validation($value); -} diff --git a/tests/ValidationBaseTest.php b/tests/InvokeInaccessibleMethodTrait.php similarity index 70% rename from tests/ValidationBaseTest.php rename to tests/InvokeInaccessibleMethodTrait.php index 4acb0ab..1367cfd 100644 --- a/tests/ValidationBaseTest.php +++ b/tests/InvokeInaccessibleMethodTrait.php @@ -4,19 +4,10 @@ namespace Webcore\Validation\Tests; -use PHPUnit_Framework_TestCase; use ReflectionClass; -use Webcore\Validation\ValidationTrait; -abstract class ValidationBaseTest extends PHPUnit_Framework_TestCase +trait InvokeInaccessibleMethodTrait { - protected $traitObject; - - public function setUp() - { - $this->traitObject = $this->getObjectForTrait(ValidationTrait::class); - } - /** * Call protected/private method of a class. * @@ -34,4 +25,4 @@ protected function invokeMethod(&$object, $methodName, array $parameters = array return $method->invokeArgs($object, $parameters); } -} \ No newline at end of file +} diff --git a/tests/Validation/ValidationTraitBase64Test.php b/tests/Rules/Base64TraitTest.php similarity index 78% rename from tests/Validation/ValidationTraitBase64Test.php rename to tests/Rules/Base64TraitTest.php index bf504da..3afe844 100644 --- a/tests/Validation/ValidationTraitBase64Test.php +++ b/tests/Rules/Base64TraitTest.php @@ -1,10 +1,16 @@ traitObject = $this->createMockForTrait($this->getTestTraitName()); + } + + /** + * @param string $name + * @return PHPUnit_Framework_MockObject_MockObject + */ + private function createMockForTrait($name) + { + $mockForTrait = $this->getMockForTrait($name); + $mockForTrait->expects($this->any()) + ->method('throwInvalidArgumentMustBe') + ->will($this->throwException(new InvalidArgumentException())); + + return $mockForTrait; + } + + abstract protected function getTestTraitName(); + abstract protected function getTestMethodName(); + + abstract public function provideInvalidValues(); + abstract public function provideValidValues(); +} diff --git a/tests/ValidationTraitBaseTest.php b/tests/SingleParamsTraitTest.php similarity index 72% rename from tests/ValidationTraitBaseTest.php rename to tests/SingleParamsTraitTest.php index f796fd9..cff67ee 100644 --- a/tests/ValidationTraitBaseTest.php +++ b/tests/SingleParamsTraitTest.php @@ -6,10 +6,8 @@ use InvalidArgumentException; -abstract class ValidationTraitBaseTest extends ValidationBaseTest +abstract class SingleParamsTraitTest extends RulesBaseTest { - abstract protected function getTestMethodName(); - /** * @dataProvider provideInvalidValues * @param $value @@ -28,8 +26,4 @@ public function testValidateWithValidValue($value) { $this->invokeMethod($this->traitObject, $this->getTestMethodName(), [$value]); } - - abstract public function provideInvalidValues(); - - abstract public function provideValidValues(); -} \ No newline at end of file +} diff --git a/tests/SingleValueObject/SingleValueObjectTraitTest.php b/tests/SingleValueObject/SingleValueObjectTraitTest.php new file mode 100644 index 0000000..c766bbe --- /dev/null +++ b/tests/SingleValueObject/SingleValueObjectTraitTest.php @@ -0,0 +1,97 @@ +getMockForTrait($this->getTestTraitName(), [$value]); + $mockForTrait->expects($this->any()) + ->method('validation') + ->willReturn(true); + + return $mockForTrait; + } + + /** + * @dataProvider provideSingleValueObjectValues + * @param $value + */ + public function testConstructAndGetValue($value) + { + $mockForTrait = $this->createMockForTraitWithValue($value); + $this->assertSame($value, $mockForTrait->getValue()); + } + + public function provideSingleValueObjectValues() + { + return [ + ["Lorem Ipsum"], + [true], + [false], + [1.2], + [1234], + [-100023], + [new \stdClass()], + [array()], + [null], + ]; + } + + /** + * @dataProvider provideThrowInvalidValues + * @param $value + */ + public function testThrowInvalidArgumentMustBe($value) + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessageRegExp("/must be ".$value."$/"); + $mockForTrait = $this->createMockForTraitWithValue($value); + $this->invokeMethod($mockForTrait, "throwInvalidArgumentMustBe", [$value]); + } + + public function provideThrowInvalidValues() + { + return [ + ["string"], + ["bool"], + ["hexa",], + ]; + } + + /** + * @dataProvider provideSingleValueObjectValues + * @param $value + */ + public function testSameValueAs($value) + { + $traitFoo = $this->createMockForTraitWithValue($value); + $traitBar = $this->createMock(SingleValueObjectInterface::class); + $traitBar->expects($this->any()) + ->method('getValue') + ->willReturn($value); + + $this->assertTrue($traitFoo->sameValueAs($traitBar)); + } +} diff --git a/tests/ThreeParamsTraitTest.php b/tests/ThreeParamsTraitTest.php new file mode 100644 index 0000000..c02d18b --- /dev/null +++ b/tests/ThreeParamsTraitTest.php @@ -0,0 +1,33 @@ +expectException(InvalidArgumentException::class); + $this->invokeMethod($this->traitObject, $this->getTestMethodName(), [$valueFoo, $firstConstraint, $secondConstraint]); + } + + /** + * @dataProvider provideValidValues + * @param $valueFoo + * @param $firstConstraint + * @param $secondConstraint + */ + public function testValidateWithValidValue($valueFoo, $firstConstraint, $secondConstraint) + { + $this->invokeMethod($this->traitObject, $this->getTestMethodName(), [$valueFoo, $firstConstraint, $secondConstraint]); + } +} diff --git a/tests/TwoParamsTraitTest.php b/tests/TwoParamsTraitTest.php new file mode 100644 index 0000000..9a067a3 --- /dev/null +++ b/tests/TwoParamsTraitTest.php @@ -0,0 +1,31 @@ +expectException(InvalidArgumentException::class); + $this->invokeMethod($this->traitObject, $this->getTestMethodName(), [$valueFoo, $constraint]); + } + + /** + * @dataProvider provideValidValues + * @param $valueFoo + * @param $constraint + */ + public function testValidateWithValidValue($valueFoo, $constraint) + { + $this->invokeMethod($this->traitObject, $this->getTestMethodName(), [$valueFoo, $constraint]); + } +} diff --git a/tests/Validation/ValidationTraitLengthTest.php b/tests/Validation/ValidationTraitLengthTest.php deleted file mode 100644 index 6fa83ce..0000000 --- a/tests/Validation/ValidationTraitLengthTest.php +++ /dev/null @@ -1,53 +0,0 @@ -expectException(InvalidArgumentException::class); - $this->invokeMethod($this->traitObject, $this->getTestMethodName(), [$value, $length]); - } - - /** - * @dataProvider provideValidValues - * @param $value - * @param $length - */ - public function testValidateLengthWithValidValues($value, $length) - { - $this->invokeMethod($this->traitObject, $this->getTestMethodName(), [$value, $length]); - } - - public function provideInvalidValues() - { - return [ - ["aaa", 1], - ["", -1], - ["Lorem", 0], - ["Lorem", 10] - ]; - } - - public function provideValidValues() - { - return [ - ["", 0], - ["Lorem", 5], - ["12345678901234567890", 20] - ]; - } -} \ No newline at end of file diff --git a/tests/Validation/ValidationTraitThrowExceptionTest.php b/tests/Validation/ValidationTraitThrowExceptionTest.php deleted file mode 100644 index 4eac184..0000000 --- a/tests/Validation/ValidationTraitThrowExceptionTest.php +++ /dev/null @@ -1,33 +0,0 @@ -expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp("/must be ".$value."$/"); - $this->invokeMethod($this->traitObject, $this->getTestMethodName(), [$value]); - } - - public function provideValues() - { - return [ - ["string"], - ["bool"], - ["hexa",], - ]; - } -} \ No newline at end of file diff --git a/tests/ValidationValueBox/ValidationValueTraitTest.php b/tests/ValidationValueBox/ValidationValueTraitTest.php deleted file mode 100644 index 67da37a..0000000 --- a/tests/ValidationValueBox/ValidationValueTraitTest.php +++ /dev/null @@ -1,39 +0,0 @@ -getMockForTrait(ValidationValueBoxTrait::class, [$value]); - $mockForTrait->expects($this->any()) - ->method('validation') - ->will($this->returnValue(true)); - $this->assertSame($value, $mockForTrait->getValue()); - } - - public function provideValidationValueBoxValues() - { - return [ - ["Lorem Ipsum"], - [true], - [false], - [1.2], - [1234], - [-100023], - [new \stdClass()], - [array()], - [null], - ]; - } -}