Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Run test suite against ParseStream #509

Merged
merged 3 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
- run: npm ci
- run: npm start
- run: npm run lint
- run: npm run test-stream
- run: npm run test:coverage
- run: npm run document-check
- run: npm run document
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "parse-php-sdk",
"scripts": {
"test": "./vendor/bin/phpunit",
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --stderr --coverage-clover=coverage.xml",
"test-stream:coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --stderr --bootstrap=./tests/bootstrap-stream.php --coverage-clover=coverage.xml",
"test-stream": "./vendor/bin/phpunit --bootstrap=./tests/bootstrap-stream.php",
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover=coverage.xml",
"test-stream:coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=./tests/bootstrap-stream.php --coverage-clover=coverage.xml",
"lint": "./vendor/bin/phpcs --standard=./phpcs.xml.dist ./src/Parse ./tests/Parse",
"lint:fix": "./vendor/bin/phpcbf --standard=./phpcs.xml.dist ./src/Parse ./tests/Parse",
"prestart": "MONGODB_VERSION=4.0.4 MONGODB_TOPOLOGY=replicaset MONGODB_STORAGE_ENGINE=wiredTiger mongodb-runner start",
Expand Down
19 changes: 13 additions & 6 deletions src/Parse/HttpClients/ParseStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,16 @@ public function get($url)
{
try {
// get our response
$response = file_get_contents($url, false, $this->stream);
$response = $this->getFileContents($url, false, $this->stream);
mtrezza marked this conversation as resolved.
Show resolved Hide resolved
$this->errorMessage = null;
$this->errorCode = null;
} catch (\Exception $e) {
// set our error message/code and return false
$this->errorMessage = $e->getMessage();
$this->errorCode = $e->getCode();
$this->responseHeaders = null;
return false;
}

// set response headers
$this->responseHeaders = $http_response_header;

return $response;
}

Expand All @@ -98,12 +95,22 @@ public function getErrorMessage()
}

/**
* Gest the current error code
* Get the current error code
*
* @return int
*/
public function getErrorCode()
{
return $this->errorCode;
}

/**
* Wrapper for file_get_contents, used for testing
*/
public function getFileContents($filename, $use_include_path, $context)
{
$result = file_get_contents($filename, $use_include_path, $context);
$this->responseHeaders = $http_response_header;
return $result;
}
}
2 changes: 1 addition & 1 deletion src/Parse/ParseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ public static function _request(
$response = $httpClient->send($url, $method, $data);

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

if (strpos($contentType, 'text/html') !== false) {
throw new ParseException('Bad Request', -1);
Expand Down
12 changes: 10 additions & 2 deletions tests/Parse/ParseClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ public function testStreamConnectionTimeout()
*/
public function testNoCurlExceptions()
{
global $USE_CLIENT_STREAM;
if (isset($USE_CLIENT_STREAM)) {
$this->markTestSkipped('Skipping curl exception test');
}
Helper::setUpWithoutCURLExceptions();

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

ParseClient::setServerURL('http://___uh___oh___.com', 'parse');
$health = ParseClient::getServerHealth();
$this->assertTrue(isset($health['error']));
$this->assertTrue(isset($health['error_message']));

global $USE_CLIENT_STREAM;
if (!isset($USE_CLIENT_STREAM)) {
$this->assertTrue(isset($health['error']));
$this->assertTrue(isset($health['error_message']));
}
}
}
13 changes: 9 additions & 4 deletions tests/Parse/ParseFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ public function testParseFileDownloadBadURL()
if (!isset($USE_CLIENT_STREAM)) {
// curl exception expectation
$this->expectException('\Parse\ParseException', '', 6);
} else {
// stream exception expectation
$this->expectException('\Parse\ParseException', '', 2);
}

$file = ParseFile::_createFromServer('file.txt', 'http://404.example.com');
$file->getData();
$data = $file->getData();

if (isset($USE_CLIENT_STREAM)) {
$this->assertEquals('', $data);
}
}

/**
Expand Down Expand Up @@ -198,6 +199,10 @@ public function testUnsavedFileOnObjectSave()

public function testFileDelete()
{
global $USE_CLIENT_STREAM;
if (isset($USE_CLIENT_STREAM)) {
$this->markTestSkipped('Skipping curl delete file test');
}
$data = 'c-c-c-combo breaker';
$name = 'php.txt';
$file = ParseFile::createFromData($data, $name);
Expand Down
21 changes: 21 additions & 0 deletions tests/Parse/ParseStreamHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Parse\Test;

use Parse\HttpClients\ParseStreamHttpClient;
use Parse\HttpClients\ParseStream;
use Parse\ParseException;

use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -41,4 +43,23 @@ public function testInvalidUrl()
$client = new ParseStreamHttpClient();
$client->send($url);
}

/**
* @group test-stream-context-error
*/
public function testStreamContextError()
{
$client = $this->getMockBuilder(ParseStream::class)
->onlyMethods(['getFileContents'])
->getMock();

$client->expects($this->once())
->method('getFileContents')
->willThrowException(new ParseException('Cannot retrieve data.', 1));

$client->get('https://example.org');

$this->assertEquals('Cannot retrieve data.', $client->getErrorMessage());
$this->assertEquals('1', $client->getErrorCode());
}
}