Skip to content

Commit

Permalink
Merge pull request parse-community#198 from phelipealves/master
Browse files Browse the repository at this point in the history
Add class ParseApp
  • Loading branch information
gfosco committed Jan 8, 2016
2 parents ba0400e + 7f24f70 commit 7b83c5d
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 5 deletions.
103 changes: 103 additions & 0 deletions src/Parse/ParseApp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Parse;

class ParseApp
{
public static $APP_NAME = 'appName';
public static $CLIENT_CLASS_CREATION_ENABLED = 'clientClassCreationEnabled';
public static $CLIENT_PUSH_ENABLED = 'clientPushEnabled';
public static $REQUIRE_REVOCABLE_SESSION = 'requireRevocableSessions';
public static $REVOKE_SESSION_ON_PASSWORD_CHANGE = 'revokeSessionOnPasswordChange';

/**
* To fetch the keys and settings for all of the apps that you are a collaborator on.
*
* @throws ParseException
*
* @return array Containing the keys and settings for your apps.
*/
public static function fetchApps()
{
$result = ParseClient::_request(
'GET',
'apps',
null,
null,
false,
true
);

return $result['results'];
}

/**
* To fetch the keys and settings of a single app.
*
* @param string $application_id
*
* @throws ParseException
*
* @return array Containing the keys and settings for your app.
*/
public static function fetchApp($application_id)
{
$result = ParseClient::_request(
'GET',
'apps/'.$application_id,
null,
null,
false,
true
);

return $result;
}

/**
* Create a new app, that is owned by your account. The only required field for creating an app is the app name.
*
* @param array $data
*
* @throws ParseException
*
* @return array
*/
public static function createApp(array $data)
{
$result = ParseClient::_request(
'POST',
'apps',
null,
json_encode($data),
false,
true
);

return $result;
}

/**
* You can change your app's name, as well as change your app's settings.
*
* @param string $application_id
* @param array $data
*
* @throws ParseException
*
* @return array
*/
public static function updateApp($application_id, array $data)
{
$result = ParseClient::_request(
'PUT',
'apps/'.$application_id,
null,
json_encode($data),
false,
true
);

return $result;
}
}
61 changes: 57 additions & 4 deletions src/Parse/ParseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Parse;

use Exception;
use InvalidArgumentException;
use Parse\Internal\Encodable;

/**
Expand Down Expand Up @@ -50,6 +51,13 @@ final class ParseClient
*/
private static $enableCurlExceptions;

/**
* The account key.
*
* @var string
*/
private static $accountKey;

/**
* The object for managing persistence.
*
Expand Down Expand Up @@ -92,8 +100,12 @@ final class ParseClient
* @param string $rest_key Parse REST API Key
* @param string $master_key Parse Master Key
* @param bool $enableCurlExceptions Enable or disable Parse curl exceptions
* @param null $email Parse Account Email
* @param null $password Parse Account Password
*
* @throws Exception
*/
public static function initialize($app_id, $rest_key, $master_key, $enableCurlExceptions = true)
public static function initialize($app_id, $rest_key, $master_key, $enableCurlExceptions = true, $account_key = null)
{
if (!ParseObject::hasRegisteredSubclass('_User')) {
ParseUser::registerSubclass();
Expand All @@ -112,6 +124,7 @@ public static function initialize($app_id, $rest_key, $master_key, $enableCurlEx
self::$restKey = $rest_key;
self::$masterKey = $master_key;
self::$enableCurlExceptions = $enableCurlExceptions;
self::$accountKey = $account_key;
if (!static::$storage) {
if (session_status() === PHP_SESSION_ACTIVE) {
self::setStorage(new ParseSessionStorage());
Expand Down Expand Up @@ -259,6 +272,7 @@ public static function _encodeArray($value, $allowParseObjects)
* @param null $sessionToken Session Token.
* @param null $data Data to provide with the request.
* @param bool $useMasterKey Whether to use the Master Key.
* @param bool $appRequest App request to create or modify a application
*
* @throws \Exception
*
Expand All @@ -269,13 +283,19 @@ public static function _request(
$relativeUrl,
$sessionToken = null,
$data = null,
$useMasterKey = false
$useMasterKey = false,
$appRequest = false
) {
if ($data === '[]') {
$data = '{}';
}
self::assertParseInitialized();
$headers = self::_getRequestHeaders($sessionToken, $useMasterKey);
if ($appRequest) {
self::assertAppInitialized();
$headers = self::_getAppRequestHeaders();
} else {
self::assertParseInitialized();
$headers = self::_getRequestHeaders($sessionToken, $useMasterKey);
}

$url = self::HOST_NAME.'/'.self::API_VERSION.'/'.ltrim($relativeUrl, '/');
if ($method === 'GET' && !empty($data)) {
Expand Down Expand Up @@ -374,6 +394,18 @@ private static function assertParseInitialized()
}
}

/**
* @throws Exception
*/
private static function assertAppInitialized()
{
if (self::$accountKey === null) {
throw new Exception(
'You must call Parse::initialize(..., $accountKey) before making any requests.'
);
}
}

/**
* @param $sessionToken
* @param $useMasterKey
Expand Down Expand Up @@ -405,6 +437,27 @@ public static function _getRequestHeaders($sessionToken, $useMasterKey)
return $headers;
}

/**
* @return array
*/
public static function _getAppRequestHeaders()
{
if (is_null(self::$accountKey) || empty(self::$accountKey)) {
throw new InvalidArgumentException('A account key is required and can not be null or empty');
} else {
$headers[] = 'X-Parse-Account-Key: '.self::$accountKey;
}

/*
* Set an empty Expect header to stop the 100-continue behavior for post
* data greater than 1024 bytes.
* http://pilif.github.io/2007/02/the-return-of-except-100-continue/
*/
$headers[] = 'Expect: ';

return $headers;
}

/**
* Get remote Parse API url.
*
Expand Down
4 changes: 3 additions & 1 deletion tests/Parse/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public static function setUp()
ParseClient::initialize(
'app-id-here',
'rest-api-key-here',
'master-key-here'
'master-key-here',
true,
'account-key-here'
);
}

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

namespace Parse\Test;

use Parse\ParseApp;
use PHPUnit_Framework_TestCase;

class ParseAppTest extends PHPUnit_Framework_TestCase
{
public static function setUpBeforeClass()
{
Helper::setUp();
}

public function testFetchingApps()
{
self::_createApp(self::_getNewName());
self::_createApp(self::_getNewName());

$apps = ParseApp::fetchApps();
$this->assertGreaterThanOrEqual(2, $apps);
}

public function testFetchSingleApp()
{
$app_created = self::_createApp(self::_getNewName());

$app = ParseApp::fetchApp($app_created['applicationId']);

$this->assertCount(13, $app);
}

public function testFetchNotFound()
{
$invalid_application_id = '1YkU7V110nEDUqU7ctCEbLr6xcgQgdEkePuBaw6P';

$this->setExpectedException('Parse\ParseException', 'requested resource was not found');
ParseApp::fetchApp($invalid_application_id);
}

public function testCreateApp()
{
$app_name = self::_getNewName();

$app = ParseApp::createApp([
'appName' => $app_name,
]);

$this->assertEquals($app_name, $app['appName']);
$this->assertEquals(true, $app['clientClassCreationEnabled']);
$this->assertEquals(false, $app['clientPushEnabled']);
$this->assertEquals(true, $app['requireRevocableSessions']);
$this->assertEquals(true, $app['revokeSessionOnPasswordChange']);
}

public function testNameAlreadyInAccount()
{
$app_name = self::_getNewName();

ParseApp::createApp([
'appName' => $app_name,
]);

$this->setExpectedException('Parse\ParseException', 'App name must not already be used in your account');
ParseApp::createApp([
'appName' => $app_name,
]);
}

public function testUpdateApp()
{
$app_name = self::_getNewName();
$updated_name = self::_getNewName();
$this->assertNotEquals($app_name, $updated_name);

$app = ParseApp::createApp([
'appName' => $app_name,
]);

$updated_app = ParseApp::updateApp($app['applicationId'], [
'appName' => $updated_name,
'clientClassCreationEnabled' => false,
'clientPushEnabled' => true,
'requireRevocableSessions' => false,
'revokeSessionOnPasswordChange' => false,
]);

$this->assertEquals($updated_name, $updated_app['appName']);
$this->assertNotTrue($updated_name['clientClassCreationEnabled']);
$this->assertNotFalse($updated_name['clientPushEnabled']);
$this->assertNotTrue($updated_name['requireRevocableSessions']);
$this->assertNotTrue($updated_name['revokeSessionOnPasswordChange']);
}

private static function _createApp($name)
{
return ParseApp::createApp([
'appName' => $name,
]);
}

private static function _getNewName()
{
return md5(uniqid(rand(), true));
}
}

0 comments on commit 7b83c5d

Please sign in to comment.