Skip to content

Commit

Permalink
Merge pull request #446 from WordPress/add/phpunit-polyfills-library
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera authored Jun 21, 2021
2 parents 0c427c8 + e94cc19 commit c637258
Show file tree
Hide file tree
Showing 20 changed files with 109 additions and 212 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ tests/coverage/*
# Ignore local overrides of the PHPCS config file.
.phpcs.xml
phpcs.xml

# Ignore PHPUnit results cache file.
.phpunit.result.cache
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
},
"require-dev": {
"requests/test-server": "dev-master",
"phpunit/phpunit": "^5.7 || ^6.5 || ^7.5",
"squizlabs/php_codesniffer": "^3.5",
"phpcompatibility/php-compatibility": "^9.0",
"wp-coding-standards/wpcs": "^2.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"php-parallel-lint/php-parallel-lint": "^1.3",
"php-parallel-lint/php-console-highlighter": "^0.5.0"
"php-parallel-lint/php-console-highlighter": "^0.5.0",
"yoast/phpunit-polyfills": "^1.0.0"
},
"type": "library",
"autoload": {
Expand Down
8 changes: 3 additions & 5 deletions tests/Auth/Basic.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class RequestsTest_Auth_Basic extends PHPUnit_Framework_TestCase {
class RequestsTest_Auth_Basic extends RequestsTest_TestCase {
public static function transportProvider() {
return array(
array('Requests_Transport_fsockopen'),
Expand Down Expand Up @@ -76,11 +76,9 @@ public function testPOSTUsingInstantiation($transport) {
$this->assertSame('test', $result->data);
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Invalid number of arguments
*/
public function testMissingPassword() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Invalid number of arguments');
new Requests_Auth_Basic(array('user'));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ChunkedEncoding.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class RequestsTest_ChunkedDecoding extends PHPUnit_Framework_TestCase {
class RequestsTest_ChunkedDecoding extends RequestsTest_TestCase {
public static function chunkedProvider() {
return array(
array(
Expand Down
10 changes: 4 additions & 6 deletions tests/Cookies.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class RequestsTest_Cookies extends PHPUnit_Framework_TestCase {
class RequestsTest_Cookies extends RequestsTest_TestCase {
public function testBasicCookie() {
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue');

Expand Down Expand Up @@ -57,11 +57,9 @@ public function testCookieJarUnsetter() {
$this->assertFalse(isset($jar['requests-testcookie']));
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Object is a dictionary, not a list
*/
public function testCookieJarAsList() {
$this->expectException(Requests_Exception::class);
$this->expectExceptionMessage('Object is a dictionary, not a list');
$cookies = new Requests_Cookie_Jar();
$cookies[] = 'requests-testcookie1=testvalue1';
}
Expand Down Expand Up @@ -111,7 +109,7 @@ protected function setCookieRequest($cookies) {
$response = Requests::get(httpbin('/cookies/set'), array(), $options);

$data = json_decode($response->body, true);
$this->assertInternalType('array', $data);
$this->assertIsArray($data);
$this->assertArrayHasKey('cookies', $data);
return $data['cookies'];
}
Expand Down
9 changes: 1 addition & 8 deletions tests/Encoding.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class RequestsTests_Encoding extends PHPUnit_Framework_TestCase {
class RequestsTests_Encoding extends RequestsTest_TestCase {
protected static function mapData($type, $data) {
$real_data = array();
foreach ($data as $value) {
Expand Down Expand Up @@ -84,11 +84,4 @@ public function testCompatibleInflate($original, $encoded) {
$decoded = Requests::compatible_gzinflate($encoded);
$this->assertSame($original, $decoded);
}

protected function bin2hex($field) {
$field = bin2hex($field);
$field = chunk_split($field, 2, "\\x");
$field = "\\x" . substr($field, 0, -2);
return $field;
}
}
50 changes: 17 additions & 33 deletions tests/IDNAEncoder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class RequestsTest_IDNAEncoder extends PHPUnit_Framework_TestCase {
class RequestsTest_IDNAEncoder extends RequestsTest_TestCase {
public static function specExamples() {
return array(
array(
Expand All @@ -22,29 +22,23 @@ public function testEncoding($data, $expected) {
$this->assertSame($expected, $result);
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Provided string is too long
*/
public function testASCIITooLong() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Provided string is too long');
$data = str_repeat('abcd', 20);
Requests_IDNAEncoder::encode($data);
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Encoded string is too long
*/
public function testEncodedTooLong() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Encoded string is too long');
$data = str_repeat("\xe4\xbb\x96", 60);
Requests_IDNAEncoder::encode($data);
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Provided string begins with ACE prefix
*/
public function testAlreadyPrefixed() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Provided string begins with ACE prefix');
Requests_IDNAEncoder::encode("xn--\xe4\xbb\x96");
}

Expand All @@ -68,43 +62,33 @@ public function testFourByteCharacter() {
$this->assertSame('xn--ww6j', $result);
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Invalid Unicode codepoint
*/
public function testFiveByteCharacter() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Invalid Unicode codepoint');
Requests_IDNAEncoder::encode("\xfb\xb6\xb6\xb6\xb6");
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Invalid Unicode codepoint
*/
public function testSixByteCharacter() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Invalid Unicode codepoint');
Requests_IDNAEncoder::encode("\xfd\xb6\xb6\xb6\xb6\xb6");
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Invalid Unicode codepoint
*/
public function testInvalidASCIICharacterWithMultibyte() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Invalid Unicode codepoint');
Requests_IDNAEncoder::encode("\0\xc2\xb6");
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Invalid Unicode codepoint
*/
public function testUnfinishedMultibyte() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Invalid Unicode codepoint');
Requests_IDNAEncoder::encode("\xc2");
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Invalid Unicode codepoint
*/
public function testPartialMultibyte() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Invalid Unicode codepoint');
Requests_IDNAEncoder::encode("\xc2\xc2\xb6");
}
}
6 changes: 2 additions & 4 deletions tests/IRI.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*
*/

class RequestsTest_IRI extends PHPUnit_Framework_TestCase
class RequestsTest_IRI extends RequestsTest_TestCase
{
public static function rfc3986_tests()
{
Expand Down Expand Up @@ -384,11 +384,9 @@ public function testWriteAliased()
$this->assertSame('test', $iri->fragment);
}

/**
* @expectedException PHPUnit_Framework_Error_Notice
*/
public function testNonexistantProperty()
{
$this->expectNotice('Undefined property: Requests_IRI::nonexistant_prop');
$iri = new Requests_IRI();
$this->assertFalse(isset($iri->nonexistant_prop));
$should_fail = $iri->nonexistant_prop;
Expand Down
7 changes: 3 additions & 4 deletions tests/Proxy/HTTP.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class RequestsTest_Proxy_HTTP extends PHPUnit_Framework_TestCase {
class RequestsTest_Proxy_HTTP extends RequestsTest_TestCase {
protected function checkProxyAvailable($type = '') {
switch ($type) {
case 'auth':
Expand Down Expand Up @@ -60,9 +60,6 @@ public function testConnectWithArray($transport) {

/**
* @dataProvider transportProvider
*
* @expectedException Requests_Exception
* @expectedExceptionMessage Invalid number of arguments
*/
public function testConnectInvalidParameters($transport) {
$this->checkProxyAvailable();
Expand All @@ -71,6 +68,8 @@ public function testConnectInvalidParameters($transport) {
'proxy' => array(REQUESTS_HTTP_PROXY, 'testuser', 'password', 'something'),
'transport' => $transport,
);
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Invalid number of arguments');
Requests::get(httpbin('/get'), array(), $options);
}

Expand Down
36 changes: 16 additions & 20 deletions tests/Requests.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

class RequestsTest_Requests extends PHPUnit_Framework_TestCase {
/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Only HTTP(S) requests are handled
*/
class RequestsTest_Requests extends RequestsTest_TestCase {

public function testInvalidProtocol() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Only HTTP(S) requests are handled');
Requests::request('ftp://128.0.0.1/');
}

Expand Down Expand Up @@ -100,9 +99,6 @@ public function testHeaderOnlyLF() {
*
* We do not support HTTP/0.9. If this is really an issue for you, file a
* new issue, and update your server/proxy to support a proper protocol.
*
* @expectedException Requests_Exception
* @expectedExceptionMessage Response could not be parsed
*/
public function testInvalidProtocolVersion() {
$transport = new RequestsTest_Mock_RawTransport();
Expand All @@ -111,14 +107,14 @@ public function testInvalidProtocolVersion() {
$options = array(
'transport' => $transport,
);

$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Response could not be parsed');
Requests::get('http://example.com/', array(), $options);
}

/**
* HTTP/0.9 also appears to use a single CRLF instead of two
*
* @expectedException Requests_Exception
* @expectedExceptionMessage Missing header/body separator
* HTTP/0.9 also appears to use a single CRLF instead of two.
*/
public function testSingleCRLFSeparator() {
$transport = new RequestsTest_Mock_RawTransport();
Expand All @@ -127,20 +123,22 @@ public function testSingleCRLFSeparator() {
$options = array(
'transport' => $transport,
);

$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Missing header/body separator');
Requests::get('http://example.com/', array(), $options);
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Response could not be parsed
*/
public function testInvalidStatus() {
$transport = new RequestsTest_Mock_RawTransport();
$transport->data = "HTTP/1.1 OK\r\nTest: value\nAnother-Test: value\r\n\r\nTest";

$options = array(
'transport' => $transport,
);

$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Response could not be parsed');
Requests::get('http://example.com/', array(), $options);
}

Expand All @@ -156,12 +154,10 @@ public function test30xWithoutLocation() {
$this->assertSame(0, $response->redirects);
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage timed out
*/
public function testTimeoutException() {
$options = array('timeout' => 0.5);
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('timed out');
Requests::get(httpbin('/delay/3'), array(), $options);
}
}
8 changes: 3 additions & 5 deletions tests/Response/Headers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class RequestsTest_Response_Headers extends PHPUnit_Framework_TestCase {
class RequestsTest_Response_Headers extends RequestsTest_TestCase {
public function testArrayAccess() {
$headers = new Requests_Response_Headers();
$headers['Content-Type'] = 'text/plain';
Expand Down Expand Up @@ -37,11 +37,9 @@ public function testIteration() {
}
}

/**
* @expectedException Requests_Exception
* @expectedExceptionMessage Object is a dictionary, not a list
*/
public function testInvalidKey() {
$this->expectException('Requests_Exception');
$this->expectExceptionMessage('Object is a dictionary, not a list');
$headers = new Requests_Response_Headers();
$headers[] = 'text/plain';
}
Expand Down
2 changes: 1 addition & 1 deletion tests/SSL.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class RequestsTest_SSL extends PHPUnit_Framework_TestCase {
class RequestsTest_SSL extends RequestsTest_TestCase {
public static function domainMatchProvider() {
return array(
array('example.com', 'example.com'),
Expand Down
2 changes: 1 addition & 1 deletion tests/Session.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class RequestsTest_Session extends PHPUnit_Framework_TestCase {
class RequestsTest_Session extends RequestsTest_TestCase {
public function testURLResolution() {
$session = new Requests_Session(httpbin('/'));

Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Yoast\PHPUnitPolyfills\TestCases\TestCase;

class RequestsTest_TestCase extends TestCase {

}
Loading

0 comments on commit c637258

Please sign in to comment.