ReCaptchaHelpersV2Test   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 73
c 3
b 0
f 1
dl 0
loc 165
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testHtmlFormSnippetCalledByFacade() 0 7 1
A testTagAttributes() 0 22 1
A testHtmlScriptTagJsApiCalledByFacade() 0 8 1
A testExpectReCaptchaInstanceOfReCaptchaBuilderV2() 0 4 1
A getEnvironmentSetUp() 0 12 1
A testHtmlFormSnippet() 0 7 1
A testHtmlFormSnippetKeepsDefaultConfigValuesUnlessOtherwiseStated() 0 10 1
A testCleanAttributesReturnsOnlyAllowedAttributes() 0 18 1
A testHtmlFormSnippetOverridesDefaultAttributes() 0 15 1
A testExpectExceptionWhenGetFormIdFunctionIsCalled() 0 4 1
1
<?php
2
3
/**
4
 * Copyright (c) 2017 - present
5
 * LaravelGoogleRecaptcha - ReCaptchaHelpersV2Test.php
6
 * author: Roberto Belotti - [email protected]
7
 * web : robertobelotti.com, github.com/biscolab
8
 * Initial version created on: 8/8/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\Facades\ReCaptcha;
15
use Biscolab\ReCaptcha\ReCaptchaBuilderV2;
16
17
class ReCaptchaHelpersV2Test extends TestCase
18
{
19
20
    /**
21
     * @test
22
     */
23
    public function testHtmlScriptTagJsApiCalledByFacade()
24
    {
25
26
        ReCaptcha::shouldReceive('htmlScriptTagJsApi')
27
            ->once()
28
            ->with(["key" => "val"]);
29
30
        htmlScriptTagJsApi(["key" => "val"]);
31
    }
32
33
    /**
34
     * @test
35
     */
36
    public function testHtmlFormSnippetCalledByFacade()
37
    {
38
39
        ReCaptcha::shouldReceive('htmlFormSnippet')
40
            ->once();
41
42
        htmlFormSnippet();
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function testTagAttributes()
49
    {
50
51
        $recaptcha = \recaptcha();
52
53
        $tag_attributes = $recaptcha->getTagAttributes();
0 ignored issues
show
Bug introduced by
The method getTagAttributes() does not exist on Biscolab\ReCaptcha\ReCaptchaBuilder. It seems like you code against a sub-type of Biscolab\ReCaptcha\ReCaptchaBuilder such as Biscolab\ReCaptcha\ReCaptchaBuilderV2. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        /** @scrutinizer ignore-call */ 
54
        $tag_attributes = $recaptcha->getTagAttributes();
Loading history...
54
55
        $this->assertArrayHasKey('sitekey', $recaptcha->getTagAttributes());
56
        $this->assertArrayHasKey('theme', $recaptcha->getTagAttributes());
57
        $this->assertArrayHasKey('size', $tag_attributes);
58
        $this->assertArrayHasKey('tabindex', $tag_attributes);
59
        $this->assertArrayHasKey('callback', $tag_attributes);
60
        $this->assertArrayHasKey('expired-callback', $tag_attributes);
61
        $this->assertArrayHasKey('error-callback', $tag_attributes);
62
63
        $this->assertEquals($tag_attributes['sitekey'], 'api_site_key');
64
        $this->assertEquals($tag_attributes['theme'], 'dark');
65
        $this->assertEquals($tag_attributes['size'], 'compact');
66
        $this->assertEquals($tag_attributes['tabindex'], '2');
67
        $this->assertEquals($tag_attributes['callback'], 'callbackFunction');
68
        $this->assertEquals($tag_attributes['expired-callback'], 'expiredCallbackFunction');
69
        $this->assertEquals($tag_attributes['error-callback'], 'errorCallbackFunction');
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function testExpectReCaptchaInstanceOfReCaptchaBuilderV2()
76
    {
77
78
        $this->assertInstanceOf(ReCaptchaBuilderV2::class, \recaptcha());
79
    }
80
81
    /**
82
     * @expectedException     \Error
83
     */
84
    public function testExpectExceptionWhenGetFormIdFunctionIsCalled()
85
    {
86
        $this->expectException('\Error');
87
        getFormId();
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function testHtmlFormSnippet()
94
    {
95
96
        $html_snippet = \recaptcha()->htmlFormSnippet();
0 ignored issues
show
Bug introduced by
The method htmlFormSnippet() does not exist on Biscolab\ReCaptcha\ReCaptchaBuilder. It seems like you code against a sub-type of Biscolab\ReCaptcha\ReCaptchaBuilder such as Biscolab\ReCaptcha\ReCaptchaBuilderV2. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        $html_snippet = \recaptcha()->/** @scrutinizer ignore-call */ htmlFormSnippet();
Loading history...
97
        $this->assertEquals(
98
            '<div class="g-recaptcha" data-callback="callbackFunction" data-error-callback="errorCallbackFunction" data-expired-callback="expiredCallbackFunction" data-sitekey="api_site_key" data-size="compact" data-tabindex="2" data-theme="dark" id="recaptcha-element"></div>',
99
            $html_snippet
100
        );
101
    }
102
103
    /**
104
     * @test
105
     */
106
    public function testHtmlFormSnippetOverridesDefaultAttributes()
107
    {
108
109
        $html_snippet = \recaptcha()->htmlFormSnippet([
110
            "theme" => "light",
111
            "size" => "normal",
112
            "tabindex" => "3",
113
            "callback" => "callbackFunctionNew",
114
            "expired-callback" => "expiredCallbackFunctionNew",
115
            "error-callback" => "errorCallbackFunctionNew",
116
            "not-allowed-attribute" => "error",
117
        ]);
118
        $this->assertEquals(
119
            '<div class="g-recaptcha" data-callback="callbackFunctionNew" data-error-callback="errorCallbackFunctionNew" data-expired-callback="expiredCallbackFunctionNew" data-sitekey="api_site_key" data-size="normal" data-tabindex="3" data-theme="light" id="recaptcha-element"></div>',
120
            $html_snippet
121
        );
122
    }
123
124
    /**
125
     * @test
126
     */
127
    public function testCleanAttributesReturnsOnlyAllowedAttributes()
128
    {
129
        $attributes = ReCaptchaBuilderV2::cleanAttributes([
130
            "theme" => "theme",
131
            "size" => "size",
132
            "tabindex" => "tabindex",
133
            "callback" => "callback",
134
            "expired-callback" => "expired-callback",
135
            "error-callback" => "error-callback",
136
            "not-allowed-attribute" => "error",
137
        ]);
138
        $this->assertArrayHasKey("theme", $attributes);
139
        $this->assertArrayHasKey("size", $attributes);
140
        $this->assertArrayHasKey("tabindex", $attributes);
141
        $this->assertArrayHasKey("callback", $attributes);
142
        $this->assertArrayHasKey("expired-callback", $attributes);
143
        $this->assertArrayHasKey("error-callback", $attributes);
144
        $this->assertArrayNotHasKey("not-allowed-attribute", $attributes);
145
    }
146
147
    /**
148
     * @test
149
     */
150
    public function testHtmlFormSnippetKeepsDefaultConfigValuesUnlessOtherwiseStated()
151
    {
152
        $html_snippet = \recaptcha()->htmlFormSnippet([
153
            'callback'         => 'callbackFunction',
154
            'expired-callback' => 'expiredCallbackFunction',
155
            'error-callback'   => 'errorCallbackFunction',
156
        ]);
157
        $this->assertEquals(
158
            '<div class="g-recaptcha" data-callback="callbackFunction" data-error-callback="errorCallbackFunction" data-expired-callback="expiredCallbackFunction" data-sitekey="api_site_key" data-size="compact" data-tabindex="2" data-theme="dark" id="recaptcha-element"></div>',
159
            $html_snippet
160
        );
161
    }
162
163
    /**
164
     * Define environment setup.
165
     *
166
     * @param  \Illuminate\Foundation\Application $app
167
     *
168
     * @return void
169
     */
170
    protected function getEnvironmentSetUp($app)
171
    {
172
173
        $app['config']->set('recaptcha.api_site_key', 'api_site_key');
174
        $app['config']->set('recaptcha.version', 'v2');
175
        $app['config']->set('recaptcha.tag_attributes', [
176
            'theme'            => 'dark',
177
            'size'             => 'compact',
178
            'tabindex'         => '2',
179
            'callback'         => 'callbackFunction',
180
            'expired-callback' => 'expiredCallbackFunction',
181
            'error-callback'   => 'errorCallbackFunction',
182
        ]);
183
    }
184
}
185