Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Laravel 6 compatibility
  • Loading branch information
DarkGhostHunter authored Oct 20, 2019
2 parents 3ab2573 + 0ab2828 commit 90eceb4
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 24 deletions.
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
language: php

php:
- 7.1
- 7.2
- 7.3

env:
global:
- CC_TEST_REPORTER_ID=3cb0149c1ae23214062407321730aa13a558d9e10aa7011e01282b5a46e7f100

before_script:
install:
- travis_retry composer self-update
- travis_retry composer require php-coveralls/php-coveralls:2.1.* --dev
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- travis_retry curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter

before_script:
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build

script:
- ./vendor/bin/phpunit

after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

Expand Down
17 changes: 8 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@
"php": "^7.1.3",
"ext-json" : "*",
"google/recaptcha": "^1.2",
"illuminate/config": "5.8.*",
"illuminate/container": "5.8.*",
"illuminate/http": "5.8.*",
"illuminate/routing": "5.8.*",
"illuminate/support": "5.8.*",
"illuminate/validation": "5.8.*",
"illuminate/view": "5.8.*"
"illuminate/config": "6.*",
"illuminate/container": "6.*",
"illuminate/http": "6.*",
"illuminate/routing": "6.*",
"illuminate/support": "6.*",
"illuminate/validation": "6.*",
"illuminate/view": "6.*"
},
"require-dev": {
"orchestra/testbench": "3.8.*",
"phpunit/phpunit": "^7.0"
"orchestra/testbench": "4.*"
},
"autoload": {
"files": [
Expand Down
3 changes: 1 addition & 2 deletions src/Exceptions/CaptchavelException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

interface CaptchavelException
{

}
}
9 changes: 7 additions & 2 deletions src/Exceptions/FailedRecaptchaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

class FailedRecaptchaException extends Exception implements CaptchavelException
{
public function __construct(array $errorCodes)
/**
* Creates a new FailedRecaptchaException instance.
*
* @param array $errorCodes
*/
public function __construct(array $errorCodes = [])
{
$this->message = 'The Google reCAPTCHA library returned the following errors:' .
implode("\n- ", $errorCodes);
implode("\n- ", $errorCodes);

parent::__construct();
}
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/InvalidCaptchavelMiddlewareMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@

class InvalidCaptchavelMiddlewareMethod extends Exception implements CaptchavelException
{
/**
* The exception message
*
* @var string
*/
protected $message = 'Captchavel does not work in GET routes.';
}
12 changes: 12 additions & 0 deletions src/Exceptions/InvalidRecaptchaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@

class InvalidRecaptchaException extends Exception implements CaptchavelException
{
/**
* The exception message
*
* @var string
*/
protected $message = 'The reCAPTCHA token is empty';

/**
* Creates a new InvalidRecaptchaException instance.
*
* @param null $token
* @param int $code
* @param \Throwable|null $previous
*/
public function __construct($token = null, $code = 0, Throwable $previous = null)
{
if ($token !== null) {
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/RecaptchaNotResolvedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@

class RecaptchaNotResolvedException extends Exception implements CaptchavelException
{
/**
* The exception message
*
* @var string
*/
protected $message = 'The reCAPTCHA has not been verified in this Request.';
}
9 changes: 5 additions & 4 deletions src/Http/Middleware/TransparentRecaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TransparentRecaptcha extends CheckRecaptcha
/**
* Return if the Request has a valid reCAPTCHA token
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
* @return bool
* @throws \Throwable
*/
Expand All @@ -35,13 +35,14 @@ protected function hasValidRequest(Request $request)
*/
protected function resolve(Request $request, float $threshold)
{
return app('recaptcha')->setResponse(
new Response(true,
return $this->reCaptcha->setResponse(
new Response(
true,
[],
null,
now()->toIso8601ZuluString(),
null,
$request->query->has('is_robot') || $request->input('is_robot') === true ? 0 : 1,
(int)$request->query->has('is_robot'),
$this->sanitizeAction($request->getRequestUri()))
);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Middleware/CheckRecaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DarkGhostHunter\Captchavel\Exceptions\InvalidCaptchavelMiddlewareMethod;
use DarkGhostHunter\Captchavel\Exceptions\InvalidRecaptchaException;
use DarkGhostHunter\Captchavel\Http\Middleware\CheckRecaptcha;
use DarkGhostHunter\Captchavel\ReCaptcha;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase;
Expand Down Expand Up @@ -47,6 +48,33 @@ protected function getEnvironmentSetUp($app)
$app->make('router')->post('test-post', function () { return 'true'; })->middleware('recaptcha');
}

public function testSameRecaptchaInstance()
{
$mockRequester = \Mockery::mock(RequestMethod::class);
$mockRequester->shouldReceive('submit')->andReturn(json_encode([
'success' => true,
'score' => 0.8,
'action' => '/testpost',
'challenge_ts' => Carbon::now()->toIso8601ZuluString(),
]));

$this->app->when(ReCaptchaFactory::class)
->needs(RequestMethod::class)
->give(function () use ($mockRequester) {
return $mockRequester;
});

$this->post('test-post', [
'_recaptcha' => Str::random(356)
])->assertOk();

$this->assertEquals(app(ReCaptcha::class), app('recaptcha'));
$this->assertEquals(app(ReCaptcha::class), recaptcha());
$this->assertNotEquals(app(ReCaptcha::class), new ReCaptcha());
$this->assertNotEquals(app('recaptcha'), new ReCaptcha());
$this->assertNotEquals(recaptcha(), new ReCaptcha());
}

public function testRequestWithCaptchaValidates()
{
$mockRequester = \Mockery::mock(RequestMethod::class);
Expand Down
1 change: 0 additions & 1 deletion tests/RecaptchaResponseHolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,4 @@ public function testExceptionOnSinceCheck()

(new ReCaptcha())->since();
}

}

0 comments on commit 90eceb4

Please sign in to comment.