Skip to content

Commit 5ad4f0c

Browse files
committed
Update unit tests
1 parent 513dd16 commit 5ad4f0c

File tree

8 files changed

+105
-34
lines changed

8 files changed

+105
-34
lines changed

phpunit.xml.dist

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
4-
colors="true"
5-
displayDetailsOnTestsThatTriggerWarnings="true"
6-
displayDetailsOnTestsThatTriggerDeprecations="true"
7-
bootstrap="src/autoload.php"
8-
cacheDirectory=".phpunit.cache"
9-
>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd" colors="true" displayDetailsOnTestsThatTriggerDeprecations="true" displayDetailsOnTestsThatTriggerErrors="true" displayDetailsOnTestsThatTriggerNotices="true" displayDetailsOnTestsThatTriggerWarnings="true" bootstrap="tests/bootstrap.php" cacheDirectory=".phpunit.cache">
103
<coverage>
11-
<include>
12-
<directory suffix=".php">src/ReCaptcha/</directory>
13-
</include>
144
<report>
155
<clover outputFile="build/logs/clover.xml"/>
166
</report>
@@ -21,4 +11,9 @@
2111
</testsuite>
2212
</testsuites>
2313
<logging/>
14+
<source>
15+
<include>
16+
<directory suffix=".php">src/ReCaptcha/</directory>
17+
</include>
18+
</source>
2419
</phpunit>

tests/ReCaptcha/ReCaptchaTest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*
@@ -35,12 +36,11 @@
3536
namespace ReCaptcha;
3637

3738
use PHPUnit\Framework\TestCase;
39+
use PHPUnit\Framework\Attributes\DataProvider;
3840

3941
class ReCaptchaTest extends TestCase
4042
{
41-
/**
42-
* @dataProvider invalidSecretProvider
43-
*/
43+
#[DataProvider('invalidSecretProvider')]
4444
public function testExceptionThrownOnInvalidSecret($invalid)
4545
{
4646
$this->expectException(\RuntimeException::class);
@@ -49,13 +49,19 @@ public function testExceptionThrownOnInvalidSecret($invalid)
4949

5050
public static function invalidSecretProvider()
5151
{
52-
return array(
53-
array(''),
54-
array(null),
55-
array(0),
56-
array(new \stdClass()),
57-
array(array()),
58-
);
52+
return [[''], [0],];
53+
}
54+
55+
#[DataProvider('invalidTypeSecretProvider')]
56+
public function testTypeExceptionThrownOnInvalidSecret($invalid)
57+
{
58+
$this->expectException(\TypeError::class);
59+
$rc = new ReCaptcha($invalid);
60+
}
61+
62+
public static function invalidTypeSecretProvider()
63+
{
64+
return [[null], [new \stdClass()], [[]], ];
5965
}
6066

6167
public function testVerifyReturnsErrorOnMissingResponse()
@@ -76,7 +82,7 @@ private function getMockRequestMethod($responseJson)
7682
->with($this->callback(function ($params) {
7783
return true;
7884
}))
79-
->will($this->returnValue($responseJson));
85+
->willReturn($responseJson);
8086
return $method;
8187
}
8288

tests/ReCaptcha/RequestMethod/CurlPostTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*

tests/ReCaptcha/RequestMethod/PostTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*

tests/ReCaptcha/RequestMethod/SocketPostTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*
@@ -55,7 +56,7 @@ public function testSubmitSuccess()
5556
->willReturn("HTTP/1.0 200 OK\n\nRESPONSEBODY");
5657
$socket->expects($this->exactly(2))
5758
->method('feof')
58-
->will($this->onConsecutiveCalls(false, true));
59+
->willReturn(false, true);
5960
$socket->expects($this->once())
6061
->method('fclose')
6162
->willReturn(true);
@@ -82,7 +83,7 @@ public function testOverrideSiteVerifyUrl()
8283
->willReturn("HTTP/1.0 200 OK\n\nRESPONSEBODY");
8384
$socket->expects($this->exactly(2))
8485
->method('feof')
85-
->will($this->onConsecutiveCalls(false, true));
86+
->willReturn(false, true);
8687
$socket->expects($this->once())
8788
->method('fclose')
8889
->willReturn(true);
@@ -107,7 +108,7 @@ public function testSubmitBadResponse()
107108
->willReturn("HTTP/1.0 500 NOPEn\\nBOBBINS");
108109
$socket->expects($this->exactly(2))
109110
->method('feof')
110-
->will($this->onConsecutiveCalls(false, true));
111+
->willReturn(false, true);
111112
$socket->expects($this->once())
112113
->method('fclose')
113114
->willReturn(true);

tests/ReCaptcha/RequestParametersTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*
@@ -35,6 +36,7 @@
3536
namespace ReCaptcha;
3637

3738
use PHPUnit\Framework\TestCase;
39+
use PHPUnit\Framework\Attributes\DataProvider;
3840

3941
class RequestParametersTest extends Testcase
4042
{
@@ -50,18 +52,14 @@ public static function provideValidData()
5052
);
5153
}
5254

53-
/**
54-
* @dataProvider provideValidData
55-
*/
55+
#[DataProvider('provideValidData')]
5656
public function testToArray($secret, $response, $remoteIp, $version, $expectedArray, $expectedQuery)
5757
{
5858
$params = new RequestParameters($secret, $response, $remoteIp, $version);
5959
$this->assertEquals($params->toArray(), $expectedArray);
6060
}
6161

62-
/**
63-
* @dataProvider provideValidData
64-
*/
62+
#[DataProvider('provideValidData')]
6563
public function testToQueryString($secret, $response, $remoteIp, $version, $expectedArray, $expectedQuery)
6664
{
6765
$params = new RequestParameters($secret, $response, $remoteIp, $version);

tests/ReCaptcha/ResponseTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This is a PHP library that handles calling reCAPTCHA.
45
*
@@ -35,12 +36,11 @@
3536
namespace ReCaptcha;
3637

3738
use PHPUnit\Framework\TestCase;
39+
use PHPUnit\Framework\Attributes\DataProvider;
3840

3941
class ResponseTest extends TestCase
4042
{
41-
/**
42-
* @dataProvider provideJson
43-
*/
43+
#[DataProvider('provideJson')]
4444
public function testFromJson($json, $success, $errorCodes, $hostname, $challengeTs, $apkPackageName, $score, $action)
4545
{
4646
$response = Response::fromJson($json);

tests/bootstrap.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/* An autoloader for ReCaptcha\Foo classes. This should be required()
4+
* by the user before attempting to instantiate any of the ReCaptcha
5+
* classes.
6+
*
7+
* BSD 3-Clause License
8+
* @copyright (c) 2019, Google Inc.
9+
* @link https://www.google.com/recaptcha
10+
* All rights reserved.
11+
*
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions are met:
14+
* 1. Redistributions of source code must retain the above copyright notice, this
15+
* list of conditions and the following disclaimer.
16+
*
17+
* 2. Redistributions in binary form must reproduce the above copyright notice,
18+
* this list of conditions and the following disclaimer in the documentation
19+
* and/or other materials provided with the distribution.
20+
*
21+
* 3. Neither the name of the copyright holder nor the names of its
22+
* contributors may be used to endorse or promote products derived from
23+
* this software without specific prior written permission.
24+
*
25+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+
*/
36+
37+
spl_autoload_register(function ($class) {
38+
if (substr($class, 0, 10) !== 'ReCaptcha\\') {
39+
/* If the class does not lie under the "ReCaptcha" namespace,
40+
* then we can exit immediately.
41+
*/
42+
return;
43+
}
44+
45+
/* All of the classes have names like "ReCaptcha\Foo", so we need
46+
* to replace the backslashes with frontslashes if we want the
47+
* name to map directly to a location in the filesystem.
48+
*/
49+
$class = str_replace('\\', '/', $class);
50+
51+
/* First, check under the current directory. It is important that
52+
* we look here first, so that we don't waste time searching for
53+
* test classes in the common case.
54+
*/
55+
$path = dirname(__FILE__).'/'.$class.'.php';
56+
if (is_readable($path)) {
57+
require_once $path;
58+
59+
return;
60+
}
61+
62+
/* If we didn't find what we're looking for already, maybe it's
63+
* a test class?
64+
*/
65+
$path = dirname(__FILE__).'/../tests/'.$class.'.php';
66+
if (is_readable($path)) {
67+
require_once $path;
68+
}
69+
});

0 commit comments

Comments
 (0)