summaryrefslogtreecommitdiff
path: root/tests/Unit/Http
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Http')
-rw-r--r--tests/Unit/Http/Exceptions/ValidationExceptionTest.php25
-rw-r--r--tests/Unit/Http/Validation/Stub/ValidatesRequestImplementation.php27
-rw-r--r--tests/Unit/Http/Validation/ValidatesRequestTest.php46
-rw-r--r--tests/Unit/Http/Validation/ValidatesTest.php308
-rw-r--r--tests/Unit/Http/Validation/ValidationServiceProviderTest.php34
-rw-r--r--tests/Unit/Http/Validation/ValidatorTest.php50
6 files changed, 490 insertions, 0 deletions
diff --git a/tests/Unit/Http/Exceptions/ValidationExceptionTest.php b/tests/Unit/Http/Exceptions/ValidationExceptionTest.php
new file mode 100644
index 00000000..c5a38b5a
--- /dev/null
+++ b/tests/Unit/Http/Exceptions/ValidationExceptionTest.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Http\Exceptions;
+
+use Engelsystem\Http\Exceptions\ValidationException;
+use Engelsystem\Http\Validation\Validator;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+
+class ValidationExceptionTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Http\Exceptions\ValidationException::__construct
+ * @covers \Engelsystem\Http\Exceptions\ValidationException::getValidator
+ */
+ public function testConstruct()
+ {
+ /** @var Validator|MockObject $validator */
+ $validator = $this->createMock(Validator::class);
+
+ $exception = new ValidationException($validator);
+
+ $this->assertEquals($validator, $exception->getValidator());
+ }
+}
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/ValidatesTest.php b/tests/Unit/Http/Validation/ValidatesTest.php
new file mode 100644
index 00000000..5cf0447a
--- /dev/null
+++ b/tests/Unit/Http/Validation/ValidatesTest.php
@@ -0,0 +1,308 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Http\Validation;
+
+use Engelsystem\Http\Validation\Validates;
+use InvalidArgumentException;
+use PHPUnit\Framework\TestCase;
+
+class ValidatesTest extends TestCase
+{
+ /**
+ * @return array
+ */
+ public function provideAccepted()
+ {
+ return [
+ ['true'],
+ ['1'],
+ ['y'],
+ ['yes'],
+ ['on'],
+ ['1test', false],
+ ['false', false],
+ ['no', false],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::accepted
+ * @param mixed $value
+ * @param bool $result
+ * @dataProvider provideAccepted
+ */
+ public function testAccepted($value, bool $result = true)
+ {
+ $val = new Validates;
+ $this->assertTrue($val->accepted($value) === $result);
+ }
+
+ /**
+ * @return array
+ */
+ public function provideBetween()
+ {
+ return [
+ ['42', [10, 100]],
+ [42.5, [42, 43]],
+ [42, [42, 1000]],
+ [1337, [0, 99], false],
+ [-17, [32, 45], false],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::between
+ * @param mixed $value
+ * @param array $parameters
+ * @param bool $result
+ * @dataProvider provideBetween
+ */
+ public function testBetween($value, array $parameters, bool $result = true)
+ {
+ $val = new Validates;
+ $this->assertTrue($val->between($value, $parameters) === $result);
+ }
+
+ /**
+ * @return array
+ */
+ public function provideBool()
+ {
+ return [
+ ['1'],
+ [1],
+ [true],
+ ['0'],
+ [0],
+ [false],
+ ['true', false],
+ ['false', false],
+ ['yes', false],
+ ['no', false],
+ ['bool', false],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::bool
+ * @param mixed $value
+ * @param bool $result
+ * @dataProvider provideBool
+ */
+ public function testBool($value, bool $result = true)
+ {
+ $val = new Validates;
+ $this->assertTrue($val->bool($value) === $result);
+ }
+
+ /**
+ * @return array
+ */
+ public function provideIn()
+ {
+ return [
+ ['lorem', ['lorem,ipsum,dolor']],
+ [99, ['66,77,88,99,111']],
+ [4, ['1,3,5,7'], false],
+ ['toggle', ['on,off'], false],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::in
+ * @param mixed $value
+ * @param array $parameters
+ * @param bool $result
+ * @dataProvider provideIn
+ */
+ public function testIn($value, array $parameters, bool $result = true)
+ {
+ $val = new Validates;
+ $this->assertTrue($val->in($value, $parameters) === $result);
+ }
+
+ /**
+ * @return array
+ */
+ public function provideInt()
+ {
+ return [
+ ['1337'],
+ [42],
+ ['0'],
+ [false, false],
+ ['12asd1', false],
+ ['one', false],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::int
+ * @param mixed $value
+ * @param bool $result
+ * @dataProvider provideInt
+ */
+ public function testInt($value, bool $result = true)
+ {
+ $val = new Validates;
+ $this->assertTrue($val->int($value) === $result);
+ }
+
+ /**
+ * @return array
+ */
+ public function provideMax()
+ {
+ return [
+ ['99', [100]],
+ [-42, [1024]],
+ [99, [99]],
+ [100, [10], false],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::max
+ * @param mixed $value
+ * @param array $parameters
+ * @param bool $result
+ * @dataProvider provideMax
+ */
+ public function testMax($value, array $parameters, bool $result = true)
+ {
+ $val = new Validates;
+ $this->assertTrue($val->max($value, $parameters) === $result);
+ }
+
+ /**
+ * @return array
+ */
+ public function provideMin()
+ {
+ return [
+ [32, [0]],
+ [7, [7]],
+ ['99', [10]],
+ [3, [42], false],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::min
+ * @param mixed $value
+ * @param array $parameters
+ * @param bool $result
+ * @dataProvider provideMin
+ */
+ public function testMin($value, array $parameters, bool $result = true)
+ {
+ $val = new Validates;
+ $this->assertTrue($val->min($value, $parameters) === $result);
+ }
+
+ /**
+ * @return array
+ */
+ public function provideNotIn()
+ {
+ return [
+ [77, ['50,60,70']],
+ ['test', ['coding,deployment']],
+ ['PHP', ['Java,PHP,bash'], false],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::notIn
+ * @param mixed $value
+ * @param array $parameters
+ * @param bool $result
+ * @dataProvider provideNotIn
+ */
+ public function testNotIn($value, array $parameters, bool $result = true)
+ {
+ $val = new Validates;
+ $this->assertTrue($val->notIn($value, $parameters) === $result);
+ }
+
+ /**
+ * @return array
+ */
+ public function provideNumeric()
+ {
+ return [
+ [77],
+ ['42'],
+ ['1337e0'],
+ ['123f00', false],
+ [null, false],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::numeric
+ * @param mixed $value
+ * @param bool $result
+ * @dataProvider provideNumeric
+ */
+ public function testNumeric($value, bool $result = true)
+ {
+ $val = new Validates;
+ $this->assertTrue($val->numeric($value) === $result);
+ }
+
+ /**
+ * @return array
+ */
+ public function provideRequired()
+ {
+ return [
+ ['Lorem ipsum'],
+ ['1234'],
+ [1234],
+ ['0'],
+ [0],
+ ['', false],
+ [' ', false],
+ [null, false],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::required
+ * @param mixed $value
+ * @param bool $result
+ * @dataProvider provideRequired
+ */
+ public function testRequired($value, bool $result = true)
+ {
+ $val = new Validates;
+ $this->assertTrue($val->required($value) === $result);
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::getSize
+ */
+ public function testGetSize()
+ {
+ $val = new Validates;
+ $this->assertTrue($val->max(42, [999]));
+ $this->assertTrue($val->max('99', [100]));
+ $this->assertFalse($val->max('101', [100]));
+ $this->assertTrue($val->max('lorem', [5]));
+ $this->assertFalse($val->max('Lorem Ipsum', [5]));
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Validation\Validates::validateParameterCount
+ */
+ public function testValidateParameterCount()
+ {
+ $val = new Validates;
+ $this->assertTrue($val->between(42, [1, 100]));
+
+ $this->expectException(InvalidArgumentException::class);
+ $val->between(42, [1]);
+ }
+}
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..799265ec
--- /dev/null
+++ b/tests/Unit/Http/Validation/ValidatorTest.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Http\Validation;
+
+use Engelsystem\Http\Validation\Validates;
+use Engelsystem\Http\Validation\Validator;
+use InvalidArgumentException;
+use PHPUnit\Framework\TestCase;
+
+class ValidatorTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Http\Validation\Validator::__construct
+ * @covers \Engelsystem\Http\Validation\Validator::validate
+ * @covers \Engelsystem\Http\Validation\Validator::getData
+ * @covers \Engelsystem\Http\Validation\Validator::getErrors
+ */
+ public function testValidate()
+ {
+ $val = new Validator(new Validates);
+ $this->assertTrue($val->validate(
+ ['foo' => 'bar', 'lorem' => 'on'],
+ ['foo' => 'required|not_in:lorem,ipsum,dolor', 'lorem' => 'accepted']
+ ));
+ $this->assertEquals(['foo' => 'bar', '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 testValidateNotImplemented()
+ {
+ $val = new Validator(new Validates);
+ $this->expectException(InvalidArgumentException::class);
+
+ $val->validate(
+ ['lorem' => 'bar'],
+ ['foo' => 'never_implemented']
+ );
+ }
+}