Passed
Push — master ( 8423de...61dfc6 )
by Dmitrii
03:05
created

Recaptcha3ValidatorTest::testEmptyCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Karser\Recaptcha3Bundle\Tests\Validator\Constraints;
4
5
use Karser\Recaptcha3Bundle\Services\IpResolverInterface;
6
use Karser\Recaptcha3Bundle\Tests\fixtures\RecaptchaMock;
7
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;
8
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3Validator;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
11
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
12
13
class Recaptcha3ValidatorTest extends ConstraintValidatorTestCase
14
{
15
    /** @var IpResolverInterface|MockObject */
16
    private $resolver;
17
    /** @var RecaptchaMock */
18
    private $recaptcha;
19
20
    public function setUp(): void
21
    {
22
        $this->resolver = $this->getMockBuilder(IpResolverInterface::class)->getMock();
23
        parent::setUp();
24
    }
25
26
    protected function createValidator()
27
    {
28
        $this->recaptcha = new RecaptchaMock();
29
        return new Recaptcha3Validator($this->recaptcha, $enabled = true, $this->resolver);
30
    }
31
32
    public function testValidIfNotEnabled()
33
    {
34
        $validator = new Recaptcha3Validator($this->recaptcha, $enabled = false, $this->resolver);
35
        $this->recaptcha->nextSuccess = false;
36
37
        $validator->validate('test', new Recaptcha3());
38
        $this->assertNoViolation();
39
    }
40
41
    public function testExpectsStringCompatibleType()
42
    {
43
        $this->expectException(UnexpectedTypeException::class);
44
        $this->validator->validate(new \stdClass(), new Recaptcha3());
45
    }
46
47
    public function testValidCase()
48
    {
49
        $this->recaptcha->nextSuccess = true;
50
        $this->validator->validate('test', new Recaptcha3());
51
        $this->assertNoViolation();
52
    }
53
54
    /**
55
     * @dataProvider invalidTokensProvider
56
     */
57
    public function testInvalidCase($testToken)
58
    {
59
        $this->recaptcha->nextSuccess = false;
60
        $this->recaptcha->nextErrorCodes = ['test1', 'test2'];
61
        $this->validator->validate($testToken, new Recaptcha3(['message' => 'myMessage']));
62
63
        $this->buildViolation('myMessage')
64
            ->setParameter('{{ value }}', '"'.$testToken.'"')
65
            ->setParameter('{{ errorCodes }}', '"test1; test2"')
66
            ->setCode(Recaptcha3::INVALID_FORMAT_ERROR)
67
            ->assertRaised();
68
    }
69
70
    public function invalidTokensProvider()
71
    {
72
        return [
73
            ['invalid-token'],
74
        ];
75
    }
76
77
    /**
78
     * @dataProvider emptyTokensProvider
79
     */
80
    public function testEmptyCase($testToken)
81
    {
82
        $this->recaptcha->nextSuccess = false;
83
        $this->validator->validate($testToken, new Recaptcha3(['messageMissingValue' => 'messageMissingValue']));
84
85
        $this->buildViolation('messageMissingValue')
86
            ->setParameter('{{ value }}', '"'.$testToken.'"')
87
            ->setParameter('{{ errorCodes }}', '""')
88
            ->setCode(Recaptcha3::INVALID_FORMAT_ERROR)
89
            ->assertRaised();
90
    }
91
92
    public function emptyTokensProvider()
93
    {
94
        return [
95
            [''],
96
            [null],
97
        ];
98
    }
99
}
100