Passed
Push — master ( b01395...c75806 )
by Roberto
05:23 queued 13s
created

testRecaptchaRuleNameHelperReturnsReCaptchaBuilderDefaultRuleName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2017 - present
4
 * LaravelGoogleRecaptcha - ReCaptchaTest.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 12/9/2018
8
 * MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\ReCaptcha\Tests;
12
13
use Biscolab\ReCaptcha\Facades\ReCaptcha;
14
use Biscolab\ReCaptcha\ReCaptchaBuilder;
15
use Biscolab\ReCaptcha\ReCaptchaBuilderInvisible;
16
use Biscolab\ReCaptcha\ReCaptchaBuilderV2;
17
use Biscolab\ReCaptcha\ReCaptchaBuilderV3;
18
19
/**
20
 * Class ReCaptchaTest
21
 * @package Biscolab\ReCaptcha\Tests
22
 */
23
class ReCaptchaTest extends TestCase
24
{
25
26
	/**
27
	 * @var ReCaptchaBuilderInvisible
28
	 */
29
	protected $recaptcha_invisible = null;
30
31
	/**
32
	 * @var ReCaptchaBuilderV2
33
	 */
34
	protected $recaptcha_v2 = null;
35
36
	/**
37
	 * @var ReCaptchaBuilderV3
38
	 */
39
	protected $recaptcha_v3 = null;
40
41
	/**
42
	 * @tests
43
	 */
44
	public function testHtmlScriptTagJsApiGetHtmlScriptTag()
45
	{
46
47
		$r = ReCaptcha::htmlScriptTagJsApi();
0 ignored issues
show
Bug introduced by Roberto
The method htmlScriptTagJsApi() does not exist on Biscolab\ReCaptcha\Facades\ReCaptcha. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

47
		/** @scrutinizer ignore-call */ 
48
  $r = ReCaptcha::htmlScriptTagJsApi();
Loading history...
48
		$this->assertEquals('<script src="https://www.google.com/recaptcha/api.js" async defer></script>', $r);
49
	}
50
51
	/**
52
	 * @test
53
	 */
54
	public function testReCaptchaInvisibleHtmlFormButtonDefault()
55
	{
56
57
		$recaptcha = $this->recaptcha_invisible;
58
		$html_button = $recaptcha->htmlFormButton();
59
		$this->assertEquals('<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Submit</button>',
60
			$html_button);
61
	}
62
63
	/**
64
	 * @test
65
	 */
66
	public function testReCaptchaInvisibleHtmlFormButtonCustom()
67
	{
68
69
		$recaptcha = $this->recaptcha_invisible;
70
		$html_button = $recaptcha->htmlFormButton('Custom Text');
71
		$this->assertEquals('<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Custom Text</button>',
72
			$html_button);
73
	}
74
75
	/**
76
	 * @test
77
	 */
78
	public function testReCaptchaV2HtmlFormSnippet()
79
	{
80
81
		$recaptcha = $this->recaptcha_v2;
82
		$html_snippet = $recaptcha->htmlFormSnippet();
83
		$this->assertEquals('<div class="g-recaptcha" data-sitekey="api_site_key" data-theme="light" data-size="normal" id="recaptcha-element"></div>', $html_snippet);
84
	}
85
86
	/**
87
	 * @test
88
	 * @expectedException     \Error
89
	 */
90
	public function testReCaptchaInvisibleHtmlFormSnippetShouldThrowError()
91
	{
92
93
		$this->recaptcha_invisible->htmlFormSnippet();
0 ignored issues
show
Bug introduced by Roberto
The method htmlFormSnippet() does not exist on Biscolab\ReCaptcha\ReCaptchaBuilderInvisible. ( Ignorable by Annotation )

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

93
		$this->recaptcha_invisible->/** @scrutinizer ignore-call */ 
94
                              htmlFormSnippet();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
	}
95
96
	/**
97
	 * @test
98
	 */
99
	public function testSkipByIpAndReturnArrayReturnsDefaultArray()
100
	{
101
102
		$mock = $this->getMockBuilder(ReCaptchaBuilder::class)
103
			->setConstructorArgs([
104
				"api_site_key",
105
				"api_secret_key"
106
			])
107
			->setMethods([
108
				'returnArray'
109
			])
110
			->getMock();
111
112
		$mock->method('returnArray')
0 ignored issues
show
Bug introduced by Roberto
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

112
		$mock->/** @scrutinizer ignore-call */ 
113
         method('returnArray')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
			->willReturn(true);
114
115
		$this->setSkipByIp($this->recaptcha_v3, true);
116
117
		$validate = $this->recaptcha_v3->validate("");
118
119
		$this->assertEquals([
120
			"skip_by_ip" => true,
121
			"score"      => 0.9,
122
			"success"    => true
123
		], $validate);
124
	}
125
126
	/**
127
	 * @test
128
	 */
129
	public function testSlipByIpReturnsValidResponse()
130
	{
131
132
		$this->setSkipByIp($this->recaptcha_invisible, true);
133
		$validate = $this->recaptcha_invisible->validate("");
134
135
		$this->assertTrue($validate);
136
	}
137
138
	/**
139
	 * @test
140
	 */
141
	public function testDefaultCurlTimeout()
142
	{
143
144
		$this->assertEquals($this->recaptcha_invisible->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT);
145
		$this->assertEquals($this->recaptcha_v2->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT);
146
		$this->assertEquals($this->recaptcha_v3->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT);
147
	}
148
149
	/**
150
	 * @test
151
	 * @expectedException     \Error
152
	 */
153
	public function testReCaptchaV2htmlFormButtonShouldThrowError()
154
	{
155
156
		$this->recaptcha_v2->htmlFormButton();
0 ignored issues
show
Bug introduced by Roberto
The method htmlFormButton() does not exist on 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

156
		$this->recaptcha_v2->/** @scrutinizer ignore-call */ 
157
                       htmlFormButton();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
	}
158
159
    /**
160
     * @test
161
     */
162
	public function testRecaptchaFieldNameHelperReturnsReCaptchaBuilderDefaultFieldName() {
163
        $this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME, recaptchaFieldName());
164
    }
165
166
    /**
167
     * @test
168
     */
169
	public function testRecaptchaRuleNameHelperReturnsReCaptchaBuilderDefaultRuleName() {
170
        $this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_RULE_NAME, recaptchaRuleName());
171
    }
172
173
	protected function setSkipByIp(ReCaptchaBuilder $builder, bool $value)
174
	{
175
176
		$reflection = new \ReflectionClass($builder);
177
		$reflection_property = $reflection->getProperty('skip_by_ip');
178
		$reflection_property->setAccessible(true);
179
		$reflection_property->setValue($builder, $value);
180
	}
181
182
	/**
183
	 * @inheritdoc
184
	 */
185
	protected function setUp(): void
186
	{
187
188
		parent::setUp(); // TODO: Change the autogenerated stub
189
190
		$this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key');
191
		$this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
192
		$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key');
193
194
	}
195
}