From 9fb3e639ad07437c93c5a488c432ce8f6cad9cd4 Mon Sep 17 00:00:00 2001 From: peter279k Date: Mon, 18 Jun 2018 23:57:18 +0800 Subject: [PATCH] Test enhancement --- .travis.yml | 13 ++++++ composer.json | 8 +++- phpunit.xml | 5 ++ tests/Check/AfterTest.php | 4 +- tests/Check/AlphaTest.php | 4 +- tests/Check/AlphanumTest.php | 4 +- tests/Check/{Before.php => BeforeTest.php} | 8 ++-- tests/Check/BooleanTest.php | 4 +- tests/Check/DateTest.php | 4 +- tests/Check/EmailTest.php | 4 +- tests/Check/InTest.php | 4 +- tests/Check/IntegerTest.php | 4 +- tests/Check/IpTest.php | 4 +- tests/Check/IsarrayTest.php | 4 +- tests/Check/JsonTest.php | 4 +- tests/Check/LengthTest.php | 4 +- tests/Check/NumericTest.php | 4 +- tests/Check/RegexTest.php | 4 +- tests/Check/RequiredTest.php | 4 +- tests/CheckSetTest.php | 19 +++++++- tests/CheckTest.php | 54 +++++++++++++++++++++- tests/RuleTest.php | 31 ++++++++++++- 22 files changed, 175 insertions(+), 23 deletions(-) create mode 100644 .travis.yml rename tests/Check/{Before.php => BeforeTest.php} (65%) diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8227a58 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: php + +php: + - 7.0 + - 7.1 + - 7.2 + - 5.6 + +before_script: + - composer install --no-interaction + +script: + - php vendor/bin/phpunit diff --git a/composer.json b/composer.json index 4982a15..adf165f 100644 --- a/composer.json +++ b/composer.json @@ -10,13 +10,19 @@ } ], "require": { + "php": ">=5.6" }, "require-dev": { - "phpunit/phpunit": "4.1.4" + "phpunit/phpunit": "^4.8 || ^5.7 || ^6.5" }, "autoload": { "psr-4": { "Psecio\\Validation\\": "src/" } + }, + "autoload-dev": { + "psr-4": { + "Psecio\\Validation\\": "tests/" + } } } diff --git a/phpunit.xml b/phpunit.xml index e50f05e..02fc9ac 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -12,4 +12,9 @@ ./tests + + + src + + diff --git a/tests/Check/AfterTest.php b/tests/Check/AfterTest.php index b4141e0..8606ee3 100644 --- a/tests/Check/AfterTest.php +++ b/tests/Check/AfterTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class AfterTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class AfterTest extends TestCase { protected $after; diff --git a/tests/Check/AlphaTest.php b/tests/Check/AlphaTest.php index 48a6e70..b7e587b 100644 --- a/tests/Check/AlphaTest.php +++ b/tests/Check/AlphaTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class AlphaTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class AlphaTest extends TestCase { protected $alpha; diff --git a/tests/Check/AlphanumTest.php b/tests/Check/AlphanumTest.php index b88201b..c9c2180 100644 --- a/tests/Check/AlphanumTest.php +++ b/tests/Check/AlphanumTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class AlphanumTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class AlphanumTest extends TestCase { protected $alphanum; diff --git a/tests/Check/Before.php b/tests/Check/BeforeTest.php similarity index 65% rename from tests/Check/Before.php rename to tests/Check/BeforeTest.php index 357a13c..5e8139f 100644 --- a/tests/Check/Before.php +++ b/tests/Check/BeforeTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class BeforeTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class BeforeTest extends TestCase { protected $before; @@ -20,8 +22,8 @@ public function testCheckValidAfterDate() $start = '+2 days'; $end = 'yesterday'; - $this->after->setAdditional(['start' => $start]); - $this->assertTrue($this->after->execute($end)); + $this->before->setAdditional(['start' => $start]); + $this->assertTrue($this->before->execute($end)); } } diff --git a/tests/Check/BooleanTest.php b/tests/Check/BooleanTest.php index 90cee38..777bd87 100644 --- a/tests/Check/BooleanTest.php +++ b/tests/Check/BooleanTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class BooleanTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class BooleanTest extends TestCase { protected $boolean; diff --git a/tests/Check/DateTest.php b/tests/Check/DateTest.php index 21554f5..39a623b 100644 --- a/tests/Check/DateTest.php +++ b/tests/Check/DateTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class DateTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class DateTest extends TestCase { protected $date; diff --git a/tests/Check/EmailTest.php b/tests/Check/EmailTest.php index 7633eb9..aceb7c2 100644 --- a/tests/Check/EmailTest.php +++ b/tests/Check/EmailTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class EmailTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class EmailTest extends TestCase { protected $email; diff --git a/tests/Check/InTest.php b/tests/Check/InTest.php index dddce5d..c406ece 100644 --- a/tests/Check/InTest.php +++ b/tests/Check/InTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class InTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class InTest extends TestCase { protected $in; diff --git a/tests/Check/IntegerTest.php b/tests/Check/IntegerTest.php index 73c138b..44702dd 100644 --- a/tests/Check/IntegerTest.php +++ b/tests/Check/IntegerTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class IntegerTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class IntegerTest extends TestCase { protected $int; diff --git a/tests/Check/IpTest.php b/tests/Check/IpTest.php index 85e00ae..bbe6ef5 100644 --- a/tests/Check/IpTest.php +++ b/tests/Check/IpTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class IpTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class IpTest extends TestCase { protected $ip; diff --git a/tests/Check/IsarrayTest.php b/tests/Check/IsarrayTest.php index 922775f..cb964e3 100644 --- a/tests/Check/IsarrayTest.php +++ b/tests/Check/IsarrayTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class IsarrayTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class IsarrayTest extends TestCase { protected $array; diff --git a/tests/Check/JsonTest.php b/tests/Check/JsonTest.php index a5ec59a..5c7d7fb 100644 --- a/tests/Check/JsonTest.php +++ b/tests/Check/JsonTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class JsonTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class JsonTest extends TestCase { protected $json; diff --git a/tests/Check/LengthTest.php b/tests/Check/LengthTest.php index 7b40e18..602b3ef 100644 --- a/tests/Check/LengthTest.php +++ b/tests/Check/LengthTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class LengthTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class LengthTest extends TestCase { protected $length; diff --git a/tests/Check/NumericTest.php b/tests/Check/NumericTest.php index 701e170..10d81ba 100644 --- a/tests/Check/NumericTest.php +++ b/tests/Check/NumericTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class NumericTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class NumericTest extends TestCase { protected $num; diff --git a/tests/Check/RegexTest.php b/tests/Check/RegexTest.php index 01f3b3f..fa600a1 100644 --- a/tests/Check/RegexTest.php +++ b/tests/Check/RegexTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class RegexTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class RegexTest extends TestCase { protected $regex; diff --git a/tests/Check/RequiredTest.php b/tests/Check/RequiredTest.php index 554e5dd..c32ed9e 100644 --- a/tests/Check/RequiredTest.php +++ b/tests/Check/RequiredTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation\Check; -class RequiredTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class RequiredTest extends TestCase { protected $req; diff --git a/tests/CheckSetTest.php b/tests/CheckSetTest.php index fd7d77f..96479aa 100644 --- a/tests/CheckSetTest.php +++ b/tests/CheckSetTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation; -class CheckSetTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class CheckSetTest extends TestCase { /** * Test the looping of the set via a foreach @@ -39,4 +41,19 @@ public function testArrayAccessValid() unset($set[2]); $this->assertFalse(isset($set[2])); } + + /** + * Test that the check set can remove the specific index + */ + public function testRemove() + { + $checks = [ + new \Psecio\Validation\Check\Integer('test1'), + new \Psecio\Validation\Check\Numeric('test2') + ]; + $set = new CheckSet($checks); + $set->remove(0); + + $this->assertFalse(isset($set[0])); + } } diff --git a/tests/CheckTest.php b/tests/CheckTest.php index aa80cb6..8f6a2ec 100644 --- a/tests/CheckTest.php +++ b/tests/CheckTest.php @@ -2,7 +2,9 @@ namespace Psecio\Validation; -class CheckTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class CheckTest extends TestCase { /** * Test setting the additional info on construct @@ -53,4 +55,54 @@ public function testGetAdditionalByNameInvalid() $check = $this->getMockForAbstractClass('\Psecio\Validation\Check', [$addl]); $this->assertNull($check->get('wrongkey')); } + + /** + * Test that can get the input + */ + public function testGetInput() + { + $addl = [ + 'foo' => 'bar' + ]; + $check = $this->getMockForAbstractClass('\Psecio\Validation\Check', [$addl]); + $check->setInput('input_value'); + $this->assertEquals('input_value', $check->getInput()); + } + + /** + * Test that can get the key + */ + public function testGetKey() + { + $addl = [ + 'foo' => 'bar' + ]; + $check = $this->getMockForAbstractClass('\Psecio\Validation\Check', [$addl]); + $check->setKey('key'); + $this->assertEquals('key', $check->getKey()); + } + + /** + * Test that can get the type + */ + public function testGetType() + { + $addl = [ + 'foo' => 'bar' + ]; + $check = $this->getMockForAbstractClass('\Psecio\Validation\Check', [$addl]); + $this->assertStringStartsWith('mock_', $check->getType()); + } + + /** + * Test that can get the message if name is null + */ + public function testGetMessage() + { + $addl = [ + 'foo' => 'bar' + ]; + $check = $this->getMockForAbstractClass('\Psecio\Validation\Check', [$addl]); + $this->assertEquals('', $check->getMessage()); + } } diff --git a/tests/RuleTest.php b/tests/RuleTest.php index e6a49ec..a6ae2d0 100644 --- a/tests/RuleTest.php +++ b/tests/RuleTest.php @@ -1,9 +1,10 @@ assertEquals(1, count($result)); $this->assertInstanceOf('\Psecio\Validation\Check\Isarray', $result[0]); } + + public function testGetMessages() + { + $checks = 'array'; + $result = $this->rule->parse($checks); + + $this->assertEquals([], $this->rule->getMessages()); + } + + public function testGetMessage() + { + $checks = 'array'; + $result = $this->rule->parse($checks); + $this->rule->setMessages(['message']); + + $this->assertEquals('message', $this->rule->getMessage(0)); + $this->assertNull($this->rule->getMessage(1)); + } + + public function testGetFailures() + { + $checks = 'array'; + $result = $this->rule->parse($checks); + + $this->assertEquals([], $this->rule->getFailures(true)); + } }