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->validator->validate($testToken, new Recaptcha3(['message' => 'myMessage'])); |
61
|
|
|
|
62
|
|
|
$this->buildViolation('myMessage') |
63
|
|
|
->setParameter('{{ value }}', '"'.$testToken.'"') |
64
|
|
|
->setCode(Recaptcha3::INVALID_FORMAT_ERROR) |
65
|
|
|
->assertRaised(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function invalidTokensProvider() |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
['invalid-token'], |
72
|
|
|
[''], |
73
|
|
|
[null], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|