Completed
Push — master ( 7b9b82...f4954a )
by Roberto
07:53 queued 11s
created

testReCaptchaInvisibleHtmlScriptTagJsApiShouldThrowError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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-sitekey="api_site_key" data-callback="biscolabLaravelReCaptcha">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-sitekey="api_site_key" data-callback="biscolabLaravelReCaptcha">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"></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
	 * @expectedException     \Exception
141
	 */
142
	public function testReCaptchaInvisibleHtmlScriptTagJsApiShouldThrowError()
143
	{
144
145
		$this->recaptcha_invisible->htmlScriptTagJsApi();
146
	}
147
148
	/**
149
	 * @test
150
	 */
151
	public function testDefaultCurlTimeout()
152
	{
153
154
		$this->assertEquals($this->recaptcha_invisible->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT);
155
		$this->assertEquals($this->recaptcha_v2->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT);
156
		$this->assertEquals($this->recaptcha_v3->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT);
157
	}
158
159
	/**
160
	 * @test
161
	 * @expectedException     \Error
162
	 */
163
	public function testReCaptchaV2htmlFormButtonShouldThrowError()
164
	{
165
166
		$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

166
		$this->recaptcha_v2->/** @scrutinizer ignore-call */ 
167
                       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...
167
	}
168
169
	protected function setSkipByIp(ReCaptchaBuilder $builder, bool $value)
170
	{
171
172
		$reflection = new \ReflectionClass($builder);
173
		$reflection_property = $reflection->getProperty('skip_by_ip');
174
		$reflection_property->setAccessible(true);
175
		$reflection_property->setValue($builder, $value);
176
	}
177
178
	/**
179
	 * @inheritdoc
180
	 */
181
	protected function setUp(): void
182
	{
183
184
		parent::setUp(); // TODO: Change the autogenerated stub
185
186
		$this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key');
187
		$this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
188
		$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key');
189
190
	}
191
}