Skip to content

Commit eedefb9

Browse files
authored
ci: Run test suite against ParseStream (#509)
1 parent d46107e commit eedefb9

File tree

7 files changed

+58
-15
lines changed

7 files changed

+58
-15
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ jobs:
5252
- run: npm ci
5353
- run: npm start
5454
- run: npm run lint
55+
- run: npm run test-stream
5556
- run: npm run test:coverage
5657
- run: npm run document-check
5758
- run: npm run document

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
"name": "parse-php-sdk",
33
"scripts": {
44
"test": "./vendor/bin/phpunit",
5-
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --stderr --coverage-clover=coverage.xml",
6-
"test-stream:coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --stderr --bootstrap=./tests/bootstrap-stream.php --coverage-clover=coverage.xml",
5+
"test-stream": "./vendor/bin/phpunit --bootstrap=./tests/bootstrap-stream.php",
6+
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover=coverage.xml",
7+
"test-stream:coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=./tests/bootstrap-stream.php --coverage-clover=coverage.xml",
78
"lint": "./vendor/bin/phpcs --standard=./phpcs.xml.dist ./src/Parse ./tests/Parse",
89
"lint:fix": "./vendor/bin/phpcbf --standard=./phpcs.xml.dist ./src/Parse ./tests/Parse",
910
"prestart": "MONGODB_VERSION=4.0.4 MONGODB_TOPOLOGY=replicaset MONGODB_STORAGE_ENGINE=wiredTiger mongodb-runner start",

src/Parse/HttpClients/ParseStream.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,16 @@ public function get($url)
6161
{
6262
try {
6363
// get our response
64-
$response = file_get_contents($url, false, $this->stream);
64+
$response = $this->getFileContents($url, false, $this->stream);
6565
$this->errorMessage = null;
6666
$this->errorCode = null;
6767
} catch (\Exception $e) {
6868
// set our error message/code and return false
6969
$this->errorMessage = $e->getMessage();
7070
$this->errorCode = $e->getCode();
71+
$this->responseHeaders = null;
7172
return false;
7273
}
73-
74-
// set response headers
75-
$this->responseHeaders = $http_response_header;
76-
7774
return $response;
7875
}
7976

@@ -98,12 +95,22 @@ public function getErrorMessage()
9895
}
9996

10097
/**
101-
* Gest the current error code
98+
* Get the current error code
10299
*
103100
* @return int
104101
*/
105102
public function getErrorCode()
106103
{
107104
return $this->errorCode;
108105
}
106+
107+
/**
108+
* Wrapper for file_get_contents, used for testing
109+
*/
110+
public function getFileContents($filename, $use_include_path, $context)
111+
{
112+
$result = file_get_contents($filename, $use_include_path, $context);
113+
$this->responseHeaders = $http_response_header;
114+
return $result;
115+
}
109116
}

src/Parse/ParseClient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ public static function _request(
554554
$response = $httpClient->send($url, $method, $data);
555555

556556
// check content type of our response
557-
$contentType = $httpClient->getResponseContentType();
557+
$contentType = $httpClient->getResponseContentType() || '';
558558

559559
if (strpos($contentType, 'text/html') !== false) {
560560
throw new ParseException('Bad Request', -1);

tests/Parse/ParseClientTest.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ public function testStreamConnectionTimeout()
321321
*/
322322
public function testNoCurlExceptions()
323323
{
324+
global $USE_CLIENT_STREAM;
325+
if (isset($USE_CLIENT_STREAM)) {
326+
$this->markTestSkipped('Skipping curl exception test');
327+
}
324328
Helper::setUpWithoutCURLExceptions();
325329

326330
ParseClient::setServerURL('http://404.example.com', 'parse');
@@ -656,7 +660,11 @@ public function testCheckBadServer()
656660

657661
ParseClient::setServerURL('http://___uh___oh___.com', 'parse');
658662
$health = ParseClient::getServerHealth();
659-
$this->assertTrue(isset($health['error']));
660-
$this->assertTrue(isset($health['error_message']));
663+
664+
global $USE_CLIENT_STREAM;
665+
if (!isset($USE_CLIENT_STREAM)) {
666+
$this->assertTrue(isset($health['error']));
667+
$this->assertTrue(isset($health['error_message']));
668+
}
661669
}
662670
}

tests/Parse/ParseFileTest.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,14 @@ public function testParseFileDownloadBadURL()
9999
if (!isset($USE_CLIENT_STREAM)) {
100100
// curl exception expectation
101101
$this->expectException('\Parse\ParseException', '', 6);
102-
} else {
103-
// stream exception expectation
104-
$this->expectException('\Parse\ParseException', '', 2);
105102
}
106103

107104
$file = ParseFile::_createFromServer('file.txt', 'http://404.example.com');
108-
$file->getData();
105+
$data = $file->getData();
106+
107+
if (isset($USE_CLIENT_STREAM)) {
108+
$this->assertEquals('', $data);
109+
}
109110
}
110111

111112
/**
@@ -198,6 +199,10 @@ public function testUnsavedFileOnObjectSave()
198199

199200
public function testFileDelete()
200201
{
202+
global $USE_CLIENT_STREAM;
203+
if (isset($USE_CLIENT_STREAM)) {
204+
$this->markTestSkipped('Skipping curl delete file test');
205+
}
201206
$data = 'c-c-c-combo breaker';
202207
$name = 'php.txt';
203208
$file = ParseFile::createFromData($data, $name);

tests/Parse/ParseStreamHttpClientTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Parse\Test;
77

88
use Parse\HttpClients\ParseStreamHttpClient;
9+
use Parse\HttpClients\ParseStream;
10+
use Parse\ParseException;
911

1012
use PHPUnit\Framework\TestCase;
1113

@@ -41,4 +43,23 @@ public function testInvalidUrl()
4143
$client = new ParseStreamHttpClient();
4244
$client->send($url);
4345
}
46+
47+
/**
48+
* @group test-stream-context-error
49+
*/
50+
public function testStreamContextError()
51+
{
52+
$client = $this->getMockBuilder(ParseStream::class)
53+
->onlyMethods(['getFileContents'])
54+
->getMock();
55+
56+
$client->expects($this->once())
57+
->method('getFileContents')
58+
->willThrowException(new ParseException('Cannot retrieve data.', 1));
59+
60+
$client->get('https://example.org');
61+
62+
$this->assertEquals('Cannot retrieve data.', $client->getErrorMessage());
63+
$this->assertEquals('1', $client->getErrorCode());
64+
}
4465
}

0 commit comments

Comments
 (0)