summaryrefslogtreecommitdiff
path: root/tests/Unit/Http/Validation/ValidatesRequestTest.php
blob: 8011bd03e4906b520d4bcb648ff193c2a5e6dd1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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']);
    }
}