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\Test\ConstraintValidatorTestCase; |
11
|
|
|
|
12
|
|
|
class Recaptcha3ValidatorTest extends ConstraintValidatorTestCase |
13
|
|
|
{ |
14
|
|
|
/** @var IpResolverInterface|MockObject */ |
15
|
|
|
private $resolver; |
16
|
|
|
/** @var RecaptchaMock */ |
17
|
|
|
private $recaptcha; |
18
|
|
|
|
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->resolver = $this->getMockBuilder(IpResolverInterface::class)->getMock(); |
22
|
|
|
parent::setUp(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function createValidator() |
26
|
|
|
{ |
27
|
|
|
$this->recaptcha = new RecaptchaMock(); |
28
|
|
|
return new Recaptcha3Validator($this->recaptcha, $enabled = true, $this->resolver); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testNullIsValid() |
32
|
|
|
{ |
33
|
|
|
$this->validator->validate(null, new Recaptcha3()); |
34
|
|
|
$this->assertNoViolation(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testEmptyStringIsValid() |
38
|
|
|
{ |
39
|
|
|
$this->validator->validate('', new Recaptcha3()); |
40
|
|
|
$this->assertNoViolation(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testValidIfNotEnabled() |
44
|
|
|
{ |
45
|
|
|
$validator = new Recaptcha3Validator($this->recaptcha, $enabled = false, $this->resolver); |
46
|
|
|
$this->recaptcha->nextSuccess = false; |
47
|
|
|
|
48
|
|
|
$validator->validate('test', new Recaptcha3()); |
49
|
|
|
$this->assertNoViolation(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException |
54
|
|
|
*/ |
55
|
|
|
public function testExpectsStringCompatibleType() |
56
|
|
|
{ |
57
|
|
|
$this->validator->validate(new \stdClass(), new Recaptcha3()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testValidCase() |
61
|
|
|
{ |
62
|
|
|
$this->recaptcha->nextSuccess = true; |
63
|
|
|
$this->validator->validate('test', new Recaptcha3()); |
64
|
|
|
$this->assertNoViolation(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testInvalidCase() |
68
|
|
|
{ |
69
|
|
|
$testToken = 'test-token'; |
70
|
|
|
$this->recaptcha->nextSuccess = false; |
71
|
|
|
$this->validator->validate($testToken, new Recaptcha3(['message' => 'myMessage'])); |
72
|
|
|
|
73
|
|
|
$this->buildViolation('myMessage') |
74
|
|
|
->setParameter('{{ value }}', '"'.$testToken.'"') |
75
|
|
|
->setCode(Recaptcha3::INVALID_FORMAT_ERROR) |
76
|
|
|
->assertRaised(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|