Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from karser/little-issues-fix
little issues fixes
- Loading branch information
Showing
10 changed files
with
277 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Karser\Recaptcha3Bundle\Services; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
final class IpResolver implements IpResolverInterface | ||
{ | ||
/** @var RequestStack */ | ||
private $requestStack; | ||
|
||
public function __construct(RequestStack $requestStack) | ||
{ | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
public function resolveIp(): ?string | ||
{ | ||
$request = $this->requestStack->getCurrentRequest(); | ||
if ($request === null) { | ||
return null; | ||
} | ||
return $request->getClientIp(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Karser\Recaptcha3Bundle\Services; | ||
|
||
interface IpResolverInterface | ||
{ | ||
public function resolveIp(): ?string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Karser\Recaptcha3Bundle\Tests\Form; | ||
|
||
use Karser\Recaptcha3Bundle\Form\Recaptcha3Type; | ||
use Symfony\Component\Form\PreloadedExtension; | ||
use Symfony\Component\Form\Test\TypeTestCase; | ||
|
||
class Recaptcha3TypeTest extends TypeTestCase | ||
{ | ||
const SITEKEY = '<sitekey>'; | ||
|
||
protected function getExtensions() | ||
{ | ||
$type = new Recaptcha3Type(self::SITEKEY, $enabled = true); | ||
|
||
return [ | ||
new PreloadedExtension([$type], []), | ||
]; | ||
} | ||
|
||
public function testDefaultOptions() | ||
{ | ||
$data = '<captcha-token>'; | ||
|
||
$form = $this->factory->create(Recaptcha3Type::class); | ||
$form->setData($data); | ||
|
||
$this->assertTrue($form->isSynchronized()); | ||
$this->assertEquals($data, $form->getData()); | ||
|
||
$view = $form->createView(); | ||
$this->assertSame(self::SITEKEY, $view->vars['site_key']); | ||
$this->assertSame('homepage', $view->vars['action_name']); | ||
$this->assertTrue($view->vars['enabled']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Karser\Recaptcha3Bundle\Tests\Services; | ||
|
||
use Karser\Recaptcha3Bundle\Services\IpResolver; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
class IpResolverTest extends TestCase | ||
{ | ||
public function testEmptyRequest() | ||
{ | ||
$stack = new RequestStack(); | ||
$stack->push(new Request()); | ||
$resolver = new IpResolver($stack); | ||
self::assertNull($resolver->resolveIp()); | ||
} | ||
|
||
public function testRequest() | ||
{ | ||
$stack = new RequestStack(); | ||
$stack->push(new Request([], [], [], [], [], ['REMOTE_ADDR' => '0.0.0.0'])); | ||
$resolver = new IpResolver($stack); | ||
self::assertSame('0.0.0.0', $resolver->resolveIp()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Karser\Recaptcha3Bundle\Tests\Validator\Constraints; | ||
|
||
use Karser\Recaptcha3Bundle\Services\IpResolverInterface; | ||
use Karser\Recaptcha3Bundle\Tests\fixtures\RecaptchaMock; | ||
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3; | ||
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3Validator; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | ||
|
||
class Recaptcha3ValidatorTest extends ConstraintValidatorTestCase | ||
{ | ||
/** @var IpResolverInterface|MockObject */ | ||
private $resolver; | ||
/** @var RecaptchaMock */ | ||
private $recaptcha; | ||
|
||
public function setUp() | ||
{ | ||
$this->resolver = $this->getMockBuilder(IpResolverInterface::class)->getMock(); | ||
parent::setUp(); | ||
} | ||
|
||
protected function createValidator() | ||
{ | ||
$this->recaptcha = new RecaptchaMock(); | ||
return new Recaptcha3Validator($this->recaptcha, $enabled = true, $this->resolver); | ||
} | ||
|
||
public function testNullIsValid() | ||
{ | ||
$this->validator->validate(null, new Recaptcha3()); | ||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testEmptyStringIsValid() | ||
{ | ||
$this->validator->validate('', new Recaptcha3()); | ||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testValidIfNotEnabled() | ||
{ | ||
$validator = new Recaptcha3Validator($this->recaptcha, $enabled = false, $this->resolver); | ||
$this->recaptcha->nextSuccess = false; | ||
|
||
$validator->validate('test', new Recaptcha3()); | ||
$this->assertNoViolation(); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException | ||
*/ | ||
public function testExpectsStringCompatibleType() | ||
{ | ||
$this->validator->validate(new \stdClass(), new Recaptcha3()); | ||
} | ||
|
||
public function testValidCase() | ||
{ | ||
$this->recaptcha->nextSuccess = true; | ||
$this->validator->validate('test', new Recaptcha3()); | ||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testInvalidCase() | ||
{ | ||
$testToken = 'test-token'; | ||
$this->recaptcha->nextSuccess = false; | ||
$this->validator->validate($testToken, new Recaptcha3(['message' => 'myMessage'])); | ||
|
||
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', '"'.$testToken.'"') | ||
->setCode(Recaptcha3::INVALID_FORMAT_ERROR) | ||
->assertRaised(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters