ReCaptchaV3Test::testcCatchCallbackFunction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) 2017 - present
5
 * LaravelGoogleRecaptcha - ReCaptchaV3Test.php
6
 * author: Roberto Belotti - [email protected]
7
 * web : robertobelotti.com, github.com/biscolab
8
 * Initial version created on: 22/1/2019
9
 * MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
10
 */
11
12
namespace Biscolab\ReCaptcha\Tests;
13
14
use Biscolab\ReCaptcha\Controllers\ReCaptchaController;
15
use Biscolab\ReCaptcha\Facades\ReCaptcha;
16
use Biscolab\ReCaptcha\ReCaptchaBuilderV3;
17
use Illuminate\Support\Facades\App;
18
19
/**
20
 * Class ReCaptchaV3Test
21
 * @package Biscolab\ReCaptcha\Tests
22
 */
23
class ReCaptchaV3Test extends TestCase
24
{
25
26
    protected $recaptcha_v3 = null;
27
28
    /**
29
     * @test
30
     */
31
    public function testGetApiVersion()
32
    {
33
34
        $this->assertEquals($this->recaptcha_v3->getVersion(), 'v3');
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function testAction()
41
    {
42
43
        $r = $this->recaptcha_v3->htmlScriptTagJsApi(['action' => 'someAction']);
44
        $this->assertMatchesRegularExpression('/someAction/', $r);
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function testFetchCallbackFunction()
51
    {
52
53
        $r = $this->recaptcha_v3->htmlScriptTagJsApi(['callback_then' => 'functionCallbackThen']);
54
        $this->assertMatchesRegularExpression('/functionCallbackThen\(response\)/', $r);
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function testcCatchCallbackFunction()
61
    {
62
63
        $r = $this->recaptcha_v3->htmlScriptTagJsApi(['callback_catch' => 'functionCallbackCatch']);
64
        $this->assertMatchesRegularExpression('/functionCallbackCatch\(err\)/', $r);
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function testCustomValidationFunction()
71
    {
72
73
        $r = $this->recaptcha_v3->htmlScriptTagJsApi(['custom_validation' => 'functionCustomValidation']);
74
        $this->assertMatchesRegularExpression('/functionCustomValidation\(token\)/', $r);
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function testValidateController()
81
    {
82
83
        $controller = App::make(ReCaptchaController::class);
84
        $return = $controller->validateV3();
85
86
        $this->assertArrayHasKey("success", $return);
87
        $this->assertArrayHasKey("error-codes", $return);
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function testCurlTimeoutIsSet()
94
    {
95
96
        $this->assertEquals($this->recaptcha_v3->getCurlTimeout(), 3);
97
    }
98
99
    /**
100
     * @test
101
     */
102
    public function testHtmlScriptTagJsApiCalledByFacade()
103
    {
104
105
        ReCaptcha::shouldReceive('htmlScriptTagJsApi')
106
            ->once()
107
            ->with([]);
108
109
        htmlScriptTagJsApi([]);
110
    }
111
112
    /**
113
     * Define environment setup.
114
     *
115
     * @param  \Illuminate\Foundation\Application $app
116
     *
117
     * @return void
118
     */
119
    protected function getEnvironmentSetUp($app)
120
    {
121
122
        $app['config']->set('recaptcha.version', 'v3');
123
        $app['config']->set('recaptcha.curl_timeout', 3);
124
    }
125
126
    /**
127
     * Setup the test environment.
128
     */
129
    protected function setUp(): void
130
    {
131
132
        parent::setUp(); // TODO: Change the autogenerated stub
133
134
        $this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key');
135
    }
136
}
137