Skip to content

Commit 3d09e9f

Browse files
committed
Cover validator and form with unit tests
1 parent 6e9c9b0 commit 3d09e9f

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

Tests/Form/Recaptcha3TypeTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Karser\Recaptcha3Bundle\Tests\Form;
4+
5+
use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
6+
use Symfony\Component\Form\PreloadedExtension;
7+
use Symfony\Component\Form\Test\TypeTestCase;
8+
9+
class Recaptcha3TypeTest extends TypeTestCase
10+
{
11+
const SITEKEY = '<sitekey>';
12+
13+
protected function getExtensions()
14+
{
15+
$type = new Recaptcha3Type(self::SITEKEY, $enabled = true);
16+
17+
return [
18+
new PreloadedExtension([$type], []),
19+
];
20+
}
21+
22+
public function testDefaultOptions()
23+
{
24+
$data = '<captcha-token>';
25+
26+
$form = $this->factory->create(Recaptcha3Type::class);
27+
$form->setData($data);
28+
29+
$this->assertTrue($form->isSynchronized());
30+
$this->assertEquals($data, $form->getData());
31+
32+
$view = $form->createView();
33+
$this->assertSame(self::SITEKEY, $view->vars['site_key']);
34+
$this->assertSame('homepage', $view->vars['action_name']);
35+
$this->assertTrue($view->vars['enabled']);
36+
}
37+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

Validator/Constraints/Recaptcha3.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
*/
1010
final class Recaptcha3 extends Constraint
1111
{
12+
const INVALID_FORMAT_ERROR = '7147ffdb-0af4-4f7a-bd5e-e9dcfa6d7a2d';
13+
1214
public $message = 'Your computer or network may be sending automated queries';
1315
}

Validator/Constraints/Recaptcha3Validator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public function validate($value, Constraint $constraint): void
3131
if (!$constraint instanceof Recaptcha3) {
3232
throw new UnexpectedTypeException($constraint, Recaptcha3::class);
3333
}
34+
if (null === $value || '' === $value) {
35+
return;
36+
}
37+
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
38+
throw new UnexpectedTypeException($value, 'string');
39+
}
3440

3541
if (!$this->enabled) {
3642
return;
@@ -40,7 +46,10 @@ public function validate($value, Constraint $constraint): void
4046

4147
$response = $this->recaptcha->verify($value, $ip);
4248
if (!$response->isSuccess()) {
43-
$this->context->addViolation($constraint->message);
49+
$this->context->buildViolation($constraint->message)
50+
->setParameter('{{ value }}', $this->formatValue($value))
51+
->setCode(Recaptcha3::INVALID_FORMAT_ERROR)
52+
->addViolation();
4453
}
4554
}
4655
}

0 commit comments

Comments
 (0)