From 18253fdc0fcfe8618fe2b80f06a20fb9415e8eac Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Thu, 6 Jul 2017 12:24:27 -0700 Subject: [PATCH 1/2] Removed default api and added appropriate checking --- src/Parse/ParseClient.php | 56 ++++++++++++++++++++++++++++++--- tests/Parse/ParseClientTest.php | 38 ++++++++++++++++++++-- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/src/Parse/ParseClient.php b/src/Parse/ParseClient.php index 174dbca3..c3a9f610 100755 --- a/src/Parse/ParseClient.php +++ b/src/Parse/ParseClient.php @@ -24,14 +24,14 @@ final class ParseClient * * @var string */ - private static $serverURL = 'https://api.parse.com/'; + private static $serverURL = null; /** * The mount path for the current parse server * * @var string */ - private static $mountPath = "1/"; + private static $mountPath = null; /** * The application id. @@ -193,6 +193,25 @@ public static function setServerURL($serverURL, $mountPath) } } + /** + * Clears the existing server url. + * Used primarily for testing purposes. + */ + public static function _clearServerURL() + { + self::$serverURL = null; + + } + + /** + * Clears the existing mount path. + * Used primarily for testing purposes. + */ + public static function _clearMountPath() + { + self::$mountPath = null; + } + /** * Sets the Http client to use for requests * @@ -408,6 +427,9 @@ public static function _request( $httpClient->setCAFile(self::$caFile); } + // verify the server url and mount path have been set + self::assertServerInitialized(); + if ($appRequest) { // ** 'app' requests are not available in open source parse-server self::assertAppInitialized(); @@ -570,6 +592,32 @@ public static function _unsetStorage() self::$storage = null; } + /** + * Asserts that the server and mount path have been initialized + * + * @throws Exception + */ + private static function assertServerInitialized() + { + if (self::$serverURL === null) { + throw new Exception( + 'Missing a valid server url. '. + 'You must call ParseClient::setServerURL(\'https://your.parse-server.com\', \'/parse\') '. + ' before making any requests.' + ); + + } + + if (self::$mountPath === null) { + throw new Exception( + 'Missing a valid mount path. '. + 'You must call ParseClient::setServerURL(\'https://your.parse-server.com\', \'/parse\') '. + ' before making any requests.' + ); + + } + } + /** * Asserts that the sdk has been initialized with a valid application id * @@ -579,7 +627,7 @@ private static function assertParseInitialized() { if (self::$applicationId === null) { throw new Exception( - 'You must call Parse::initialize() before making any requests.' + 'You must call ParseClient::initialize() before making any requests.' ); } } @@ -593,7 +641,7 @@ private static function assertAppInitialized() { if (self::$accountKey === null || empty(self::$accountKey)) { throw new Exception( - 'You must call Parse::initialize(..., $accountKey) before making any app requests. '. + 'You must call ParseClient::initialize(..., $accountKey) before making any app requests. '. 'Your account key must not be null or empty.' ); } diff --git a/tests/Parse/ParseClientTest.php b/tests/Parse/ParseClientTest.php index bea4149f..01bbc0c9 100644 --- a/tests/Parse/ParseClientTest.php +++ b/tests/Parse/ParseClientTest.php @@ -47,7 +47,7 @@ public function testParseNotInitialized() { $this->setExpectedException( '\Exception', - 'You must call Parse::initialize() before making any requests.' + 'You must call ParseClient::initialize() before making any requests.' ); ParseClient::initialize( @@ -69,7 +69,7 @@ public function testAppNotNotInitialized() { $this->setExpectedException( '\Exception', - 'You must call Parse::initialize(..., $accountKey) before making any app requests. '. + 'You must call ParseClient::initialize(..., $accountKey) before making any app requests. '. 'Your account key must not be null or empty.' ); @@ -519,6 +519,40 @@ public function testStreamCAFile() ); } + /** + * @group api-not-set + */ + public function testURLNotSet() + { + $this->setExpectedException( + '\Exception', + 'Missing a valid server url. '. + 'You must call ParseClient::setServerURL(\'https://your.parse-server.com\', \'/parse\') '. + ' before making any requests.' + ); + + ParseClient::_clearServerURL(); + (new ParseObject('TestingClass'))->save(); + + } + + /** + * @group api-not-set + */ + public function testMountPathNotSet() + { + $this->setExpectedException( + '\Exception', + 'Missing a valid mount path. '. + 'You must call ParseClient::setServerURL(\'https://your.parse-server.com\', \'/parse\') '. + ' before making any requests.' + ); + + ParseClient::_clearMountPath(); + (new ParseObject('TestingClass'))->save(); + + } + /** * @group bad-api-response */ From 2dd40dfaf7722a93551ef3892793e3c92fd20f01 Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Thu, 6 Jul 2017 12:34:10 -0700 Subject: [PATCH 2/2] lint --- src/Parse/ParseClient.php | 3 --- tests/Parse/ParseClientTest.php | 2 -- 2 files changed, 5 deletions(-) diff --git a/src/Parse/ParseClient.php b/src/Parse/ParseClient.php index c3a9f610..a9d1a837 100755 --- a/src/Parse/ParseClient.php +++ b/src/Parse/ParseClient.php @@ -200,7 +200,6 @@ public static function setServerURL($serverURL, $mountPath) public static function _clearServerURL() { self::$serverURL = null; - } /** @@ -605,7 +604,6 @@ private static function assertServerInitialized() 'You must call ParseClient::setServerURL(\'https://your.parse-server.com\', \'/parse\') '. ' before making any requests.' ); - } if (self::$mountPath === null) { @@ -614,7 +612,6 @@ private static function assertServerInitialized() 'You must call ParseClient::setServerURL(\'https://your.parse-server.com\', \'/parse\') '. ' before making any requests.' ); - } } diff --git a/tests/Parse/ParseClientTest.php b/tests/Parse/ParseClientTest.php index 01bbc0c9..8f838051 100644 --- a/tests/Parse/ParseClientTest.php +++ b/tests/Parse/ParseClientTest.php @@ -533,7 +533,6 @@ public function testURLNotSet() ParseClient::_clearServerURL(); (new ParseObject('TestingClass'))->save(); - } /** @@ -550,7 +549,6 @@ public function testMountPathNotSet() ParseClient::_clearMountPath(); (new ParseObject('TestingClass'))->save(); - } /**