diff options
Diffstat (limited to 'tests/Unit/Http')
-rw-r--r-- | tests/Unit/Http/Exceptions/HttpNotFoundTest.php | 22 | ||||
-rw-r--r-- | tests/Unit/Http/ResponseTest.php | 12 | ||||
-rw-r--r-- | tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php | 3 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/Rules/BetweenTest.php | 28 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/Rules/MaxTest.php | 26 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/Rules/MinTest.php | 26 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/Rules/StringInputLengthTest.php | 37 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/Rules/Stub/ParentClassImplementation.php | 23 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/Rules/Stub/UsesStringInputLength.php | 10 | ||||
-rw-r--r-- | tests/Unit/Http/Validation/ValidatorTest.php | 5 |
10 files changed, 189 insertions, 3 deletions
diff --git a/tests/Unit/Http/Exceptions/HttpNotFoundTest.php b/tests/Unit/Http/Exceptions/HttpNotFoundTest.php new file mode 100644 index 00000000..a39ea087 --- /dev/null +++ b/tests/Unit/Http/Exceptions/HttpNotFoundTest.php @@ -0,0 +1,22 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Exceptions; + +use Engelsystem\Http\Exceptions\HttpNotFound; +use PHPUnit\Framework\TestCase; + +class HttpNotFoundTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\Exceptions\HttpNotFound::__construct + */ + public function testConstruct() + { + $exception = new HttpNotFound(); + $this->assertEquals(404, $exception->getStatusCode()); + $this->assertEquals('', $exception->getMessage()); + + $exception = new HttpNotFound('Nothing to see here!'); + $this->assertEquals('Nothing to see here!', $exception->getMessage()); + } +} diff --git a/tests/Unit/Http/ResponseTest.php b/tests/Unit/Http/ResponseTest.php index 34f76513..b8e6e527 100644 --- a/tests/Unit/Http/ResponseTest.php +++ b/tests/Unit/Http/ResponseTest.php @@ -55,6 +55,7 @@ class ResponseTest extends TestCase /** * @covers \Engelsystem\Http\Response::withView + * @covers \Engelsystem\Http\Response::setRenderer */ public function testWithView() { @@ -73,6 +74,17 @@ class ResponseTest extends TestCase $this->assertEquals('Foo ipsum!', $newResponse->getContent()); $this->assertEquals(505, $newResponse->getStatusCode()); $this->assertArraySubset(['test' => ['er']], $newResponse->getHeaders()); + + /** @var REnderer|MockObject $renderer */ + $anotherRenderer = $this->createMock(Renderer::class); + $anotherRenderer->expects($this->once()) + ->method('render') + ->with('bar') + ->willReturn('Stuff'); + + $response->setRenderer($anotherRenderer); + $response = $response->withView('bar'); + $this->assertEquals('Stuff', $response->getContent()); } /** diff --git a/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php b/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php index 14f23c00..0325ccfe 100644 --- a/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php +++ b/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php @@ -4,7 +4,7 @@ namespace Engelsystem\Test\Unit\Http\SessionHandlers; use Engelsystem\Http\SessionHandlers\DatabaseHandler; use Engelsystem\Test\Unit\HasDatabase; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\TestCase; class DatabaseHandlerTest extends TestCase { @@ -90,6 +90,7 @@ class DatabaseHandlerTest extends TestCase */ protected function setUp(): void { + parent::setUp(); $this->initDatabase(); } } diff --git a/tests/Unit/Http/Validation/Rules/BetweenTest.php b/tests/Unit/Http/Validation/Rules/BetweenTest.php new file mode 100644 index 00000000..130d2f93 --- /dev/null +++ b/tests/Unit/Http/Validation/Rules/BetweenTest.php @@ -0,0 +1,28 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation\Rules; + +use Engelsystem\Http\Validation\Rules\Between; +use Engelsystem\Test\Unit\TestCase; + +class BetweenTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\Validation\Rules\Between + */ + public function testValidate() + { + $rule = new Between(3, 10); + $this->assertFalse($rule->validate(1)); + $this->assertFalse($rule->validate('11')); + $this->assertTrue($rule->validate(5)); + $this->assertFalse($rule->validate('AS')); + $this->assertFalse($rule->validate('TestContentThatCounts')); + $this->assertTrue($rule->validate('TESTING')); + + $rule = new Between('2042-01-01', '2042-10-10'); + $this->assertFalse($rule->validate('2000-01-01')); + $this->assertFalse($rule->validate('3000-01-01')); + $this->assertTrue($rule->validate('2042-05-11')); + } +} diff --git a/tests/Unit/Http/Validation/Rules/MaxTest.php b/tests/Unit/Http/Validation/Rules/MaxTest.php new file mode 100644 index 00000000..3f4d9516 --- /dev/null +++ b/tests/Unit/Http/Validation/Rules/MaxTest.php @@ -0,0 +1,26 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation\Rules; + +use Engelsystem\Http\Validation\Rules\Max; +use Engelsystem\Test\Unit\TestCase; + +class MaxTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\Validation\Rules\Max + */ + public function testValidate() + { + $rule = new Max(3); + $this->assertFalse($rule->validate(10)); + $this->assertFalse($rule->validate('22')); + $this->assertTrue($rule->validate(3)); + $this->assertFalse($rule->validate('TEST')); + $this->assertTrue($rule->validate('AS')); + + $rule = new Max('2042-01-01'); + $this->assertFalse($rule->validate('2100-01-01')); + $this->assertTrue($rule->validate('2000-01-01')); + } +} diff --git a/tests/Unit/Http/Validation/Rules/MinTest.php b/tests/Unit/Http/Validation/Rules/MinTest.php new file mode 100644 index 00000000..56350802 --- /dev/null +++ b/tests/Unit/Http/Validation/Rules/MinTest.php @@ -0,0 +1,26 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation\Rules; + +use Engelsystem\Http\Validation\Rules\Min; +use Engelsystem\Test\Unit\TestCase; + +class MinTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\Validation\Rules\Min + */ + public function testValidate() + { + $rule = new Min(3); + $this->assertFalse($rule->validate(1)); + $this->assertFalse($rule->validate('2')); + $this->assertTrue($rule->validate(3)); + $this->assertFalse($rule->validate('AS')); + $this->assertTrue($rule->validate('TEST')); + + $rule = new Min('2042-01-01'); + $this->assertFalse($rule->validate('2000-01-01')); + $this->assertTrue($rule->validate('2345-01-01')); + } +} diff --git a/tests/Unit/Http/Validation/Rules/StringInputLengthTest.php b/tests/Unit/Http/Validation/Rules/StringInputLengthTest.php new file mode 100644 index 00000000..5c4dc512 --- /dev/null +++ b/tests/Unit/Http/Validation/Rules/StringInputLengthTest.php @@ -0,0 +1,37 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation\Rules; + +use Engelsystem\Test\Unit\Http\Validation\Rules\Stub\UsesStringInputLength; +use Engelsystem\Test\Unit\TestCase; + +class StringInputLengthTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\Validation\Rules\StringInputLength::validate + * @covers \Engelsystem\Http\Validation\Rules\StringInputLength::isDateTime + * @dataProvider validateProvider + * @param mixed $input + * @param mixed $expectedInput + */ + public function testValidate($input, $expectedInput) + { + $rule = new UsesStringInputLength(); + $rule->validate($input); + + $this->assertEquals($expectedInput, $rule->lastInput); + } + + /** + * @return array[] + */ + public function validateProvider() + { + return [ + ['TEST', 4], + ['?', 1], + ['2042-01-01 00:00', '2042-01-01 00:00'], + ['3', '3'], + ]; + } +} diff --git a/tests/Unit/Http/Validation/Rules/Stub/ParentClassImplementation.php b/tests/Unit/Http/Validation/Rules/Stub/ParentClassImplementation.php new file mode 100644 index 00000000..1b6aaaf5 --- /dev/null +++ b/tests/Unit/Http/Validation/Rules/Stub/ParentClassImplementation.php @@ -0,0 +1,23 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation\Rules\Stub; + +class ParentClassImplementation +{ + /** @var bool */ + public $validateResult = true; + + /** @var mixed */ + public $lastInput; + + /** + * @param mixed $input + * @return bool + */ + public function validate($input): bool + { + $this->lastInput = $input; + + return $this->validateResult; + } +} diff --git a/tests/Unit/Http/Validation/Rules/Stub/UsesStringInputLength.php b/tests/Unit/Http/Validation/Rules/Stub/UsesStringInputLength.php new file mode 100644 index 00000000..3522304c --- /dev/null +++ b/tests/Unit/Http/Validation/Rules/Stub/UsesStringInputLength.php @@ -0,0 +1,10 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Validation\Rules\Stub; + +use Engelsystem\Http\Validation\Rules\StringInputLength; + +class UsesStringInputLength extends ParentClassImplementation +{ + use StringInputLength; +} diff --git a/tests/Unit/Http/Validation/ValidatorTest.php b/tests/Unit/Http/Validation/ValidatorTest.php index 450e5d4e..124673df 100644 --- a/tests/Unit/Http/Validation/ValidatorTest.php +++ b/tests/Unit/Http/Validation/ValidatorTest.php @@ -50,9 +50,10 @@ class ValidatorTest extends TestCase )); $this->assertFalse($val->validate( - ['lorem' => 2], - ['lorem' => 'required|min:3|max:10'] + ['lorem' => 'OMG'], + ['lorem' => 'required|min:4|max:10'] )); + $this->assertEquals(['lorem' => ['validation.lorem.min']], $val->getErrors()); $this->assertFalse($val->validate( ['lorem' => 42], ['lorem' => 'required|min:3|max:10'] |