diff options
Diffstat (limited to 'tests/Unit/Http/Validation')
-rw-r--r-- | tests/Unit/Http/Validation/Rules/InTest.php | 19 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/Rules/NotInTest.php | 20 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/Stub/ValidatesRequestImplementation.php | 27 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/ValidatesRequestTest.php | 46 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/ValidationServiceProviderTest.php | 34 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/ValidatorTest.php | 142 |
6 files changed, 288 insertions, 0 deletions
diff --git a/tests/Unit/Http/Validation/Rules/InTest.php b/tests/Unit/Http/Validation/Rules/InTest.php new file mode 100644 index 00000000..e5688d90 --- /dev/null +++ b/tests/Unit/Http/Validation/Rules/InTest.php @@ -0,0 +1,19 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation\Rules; + +use Engelsystem\Http\Validation\Rules\In; +use Engelsystem\Test\Unit\TestCase; + +class InTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\Validation\Rules\In::__construct + */ + public function testConstruct() + { + $rule = new In('foo,bar'); + + $this->assertEquals(['foo', 'bar'], $rule->haystack); + } +} diff --git a/tests/Unit/Http/Validation/Rules/NotInTest.php b/tests/Unit/Http/Validation/Rules/NotInTest.php new file mode 100644 index 00000000..9be12336 --- /dev/null +++ b/tests/Unit/Http/Validation/Rules/NotInTest.php @@ -0,0 +1,20 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation\Rules; + +use Engelsystem\Http\Validation\Rules\NotIn; +use Engelsystem\Test\Unit\TestCase; + +class NotInTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\Validation\Rules\NotIn::validate + */ + public function testConstruct() + { + $rule = new NotIn('foo,bar'); + + $this->assertTrue($rule->validate('lorem')); + $this->assertFalse($rule->validate('foo')); + } +} diff --git a/tests/Unit/Http/Validation/Stub/ValidatesRequestImplementation.php b/tests/Unit/Http/Validation/Stub/ValidatesRequestImplementation.php new file mode 100644 index 00000000..772b1dc9 --- /dev/null +++ b/tests/Unit/Http/Validation/Stub/ValidatesRequestImplementation.php @@ -0,0 +1,27 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation\Stub; + +use Engelsystem\Controllers\BaseController; +use Engelsystem\Http\Request; + +class ValidatesRequestImplementation extends BaseController +{ + /** + * @param Request $request + * @param array $rules + * @return array + */ + public function validateData(Request $request, array $rules) + { + return $this->validate($request, $rules); + } + + /** + * @return bool + */ + public function hasValidator() + { + return !is_null($this->validator); + } +} diff --git a/tests/Unit/Http/Validation/ValidatesRequestTest.php b/tests/Unit/Http/Validation/ValidatesRequestTest.php new file mode 100644 index 00000000..8011bd03 --- /dev/null +++ b/tests/Unit/Http/Validation/ValidatesRequestTest.php @@ -0,0 +1,46 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation; + +use Engelsystem\Http\Exceptions\ValidationException; +use Engelsystem\Http\Request; +use Engelsystem\Http\Validation\Validator; +use Engelsystem\Test\Unit\Http\Validation\Stub\ValidatesRequestImplementation; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; + +class ValidatesRequestTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\Validation\ValidatesRequest::validate + * @covers \Engelsystem\Http\Validation\ValidatesRequest::setValidator + */ + public function testValidate() + { + /** @var Validator|MockObject $validator */ + $validator = $this->createMock(Validator::class); + $validator->expects($this->exactly(2)) + ->method('validate') + ->withConsecutive( + [['foo' => 'bar'], ['foo' => 'required']], + [[], ['foo' => 'required']] + ) + ->willReturnOnConsecutiveCalls( + true, + false + ); + $validator->expects($this->once()) + ->method('getData') + ->willReturn(['foo' => 'bar']); + + $implementation = new ValidatesRequestImplementation(); + $implementation->setValidator($validator); + + $return = $implementation->validateData(new Request([], ['foo' => 'bar']), ['foo' => 'required']); + + $this->assertEquals(['foo' => 'bar'], $return); + + $this->expectException(ValidationException::class); + $implementation->validateData(new Request([], []), ['foo' => 'required']); + } +} diff --git a/tests/Unit/Http/Validation/ValidationServiceProviderTest.php b/tests/Unit/Http/Validation/ValidationServiceProviderTest.php new file mode 100644 index 00000000..969f4351 --- /dev/null +++ b/tests/Unit/Http/Validation/ValidationServiceProviderTest.php @@ -0,0 +1,34 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation; + +use Engelsystem\Application; +use Engelsystem\Http\Validation\ValidationServiceProvider; +use Engelsystem\Http\Validation\Validator; +use Engelsystem\Test\Unit\Http\Validation\Stub\ValidatesRequestImplementation; +use Engelsystem\Test\Unit\ServiceProviderTest; +use stdClass; + +class ValidationServiceProviderTest extends ServiceProviderTest +{ + /** + * @covers \Engelsystem\Http\Validation\ValidationServiceProvider::register + */ + public function testRegister() + { + $app = new Application(); + + $serviceProvider = new ValidationServiceProvider($app); + $serviceProvider->register(); + + $this->assertTrue($app->has(Validator::class)); + $this->assertTrue($app->has('validator')); + + /** @var ValidatesRequestImplementation $validatesRequest */ + $validatesRequest = $app->make(ValidatesRequestImplementation::class); + $this->assertTrue($validatesRequest->hasValidator()); + + // Test afterResolving early return + $app->make(stdClass::class); + } +} diff --git a/tests/Unit/Http/Validation/ValidatorTest.php b/tests/Unit/Http/Validation/ValidatorTest.php new file mode 100644 index 00000000..450e5d4e --- /dev/null +++ b/tests/Unit/Http/Validation/ValidatorTest.php @@ -0,0 +1,142 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation; + +use Engelsystem\Http\Validation\Validator; +use InvalidArgumentException; +use PHPUnit\Framework\TestCase; + +class ValidatorTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\Validation\Validator::validate + * @covers \Engelsystem\Http\Validation\Validator::getData + * @covers \Engelsystem\Http\Validation\Validator::getErrors + */ + public function testValidate() + { + $val = new Validator(); + + $this->assertTrue($val->validate( + ['foo' => 'bar', 'lorem' => 'on', 'dolor' => 'bla'], + ['lorem' => 'accepted'] + )); + $this->assertEquals(['lorem' => 'on'], $val->getData()); + + $this->assertFalse($val->validate( + [], + ['lorem' => 'required|min:3'] + )); + $this->assertEquals( + ['lorem' => ['validation.lorem.required', 'validation.lorem.min']], + $val->getErrors() + ); + } + + /** + * @covers \Engelsystem\Http\Validation\Validator::validate + */ + public function testValidateChaining() + { + $val = new Validator(); + + $this->assertTrue($val->validate( + ['lorem' => 10], + ['lorem' => 'required|min:3|max:10'] + )); + $this->assertTrue($val->validate( + ['lorem' => 3], + ['lorem' => 'required|min:3|max:10'] + )); + + $this->assertFalse($val->validate( + ['lorem' => 2], + ['lorem' => 'required|min:3|max:10'] + )); + $this->assertFalse($val->validate( + ['lorem' => 42], + ['lorem' => 'required|min:3|max:10'] + )); + } + + /** + * @covers \Engelsystem\Http\Validation\Validator::validate + */ + public function testValidateNotImplemented() + { + $val = new Validator(); + + $this->expectException(InvalidArgumentException::class); + + $val->validate( + ['lorem' => 'bar'], + ['foo' => 'never_implemented'] + ); + } + + /** + * @covers \Engelsystem\Http\Validation\Validator::map + * @covers \Engelsystem\Http\Validation\Validator::mapBack + */ + public function testValidateMapping() + { + $val = new Validator(); + + $this->assertTrue($val->validate( + ['foo' => 'bar'], + ['foo' => 'required'] + )); + $this->assertTrue($val->validate( + ['foo' => '0'], + ['foo' => 'int'] + )); + $this->assertTrue($val->validate( + ['foo' => 'on'], + ['foo' => 'accepted'] + )); + + $this->assertFalse($val->validate( + [], + ['lorem' => 'required'] + )); + $this->assertEquals( + ['lorem' => ['validation.lorem.required']], + $val->getErrors() + ); + } + + /** + * @covers \Engelsystem\Http\Validation\Validator::validate + */ + public function testValidateNesting() + { + $val = new Validator(); + + $this->assertTrue($val->validate( + [], + ['foo' => 'not|required'] + )); + + $this->assertTrue($val->validate( + ['foo' => 'foo'], + ['foo' => 'not|int'] + )); + $this->assertFalse($val->validate( + ['foo' => 1], + ['foo' => 'not|int'] + )); + + $this->assertTrue($val->validate( + [], + ['foo' => 'optional|int'] + )); + $this->assertTrue($val->validate( + ['foo' => '33'], + ['foo' => 'optional|int'] + )); + $this->assertFalse($val->validate( + ['foo' => 'T'], + ['foo' => 'optional|int'] + )); + } +} |