From 709c2220bbf11fa6723b7ce418d0ec40416ef558 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 19 Mar 2018 15:21:16 -0700 Subject: [PATCH 1/6] adds php cs fixer --- .php_cs.dist | 14 ++++++++++++++ .travis.yml | 6 ++++++ 2 files changed, 20 insertions(+) create mode 100644 .php_cs.dist diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..7d05e50 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,14 @@ +setRules([ + '@PSR2' => true, + 'concat_space' => ['spacing' => 'one'], + 'no_unused_imports' => true, + 'method_argument_space' => false, + ]) + ->setFinder( + PhpCsFixer\Finder::create() + ->in(__DIR__) + ) +; diff --git a/.travis.yml b/.travis.yml index f3e4e4c..fa4a58d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,10 +28,16 @@ php: env: global: - GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/credentials.json + - PATH="${HOME}/.composer/vendor/bin:${PATH}" before_install: # set Google service account credentials using environemnt variables - echo "$GOOGLE_CREDENTIALS_BASE64" | base64 --decode > "$GOOGLE_APPLICATION_CREDENTIALS" + # Install PHP CS Fixer on 5.6 + - if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.6" ]]; then composer global require friendsofphp/php-cs-fixer:^2.0; fi script: - bash test.sh + # Run the CS check on PHP 5.6 only + - if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.6" ]]; then php-cs-fixer fix --dry-run --diff .; fi + From c4ecd3dc5d1c79f2e6779a582c0083cf1c50d97b Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 19 Mar 2018 15:24:12 -0700 Subject: [PATCH 2/6] fixes calendar cs --- calendar/quickstart/quickstart.php | 94 +++++++++++++++--------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/calendar/quickstart/quickstart.php b/calendar/quickstart/quickstart.php index 4391647..ca5923f 100644 --- a/calendar/quickstart/quickstart.php +++ b/calendar/quickstart/quickstart.php @@ -20,42 +20,43 @@ * Returns an authorized API client. * @return Google_Client the authorized client object */ -function getClient() { - $client = new Google_Client(); - $client->setApplicationName('Google Calendar API PHP Quickstart'); - $client->setScopes(Google_Service_Calendar::CALENDAR_READONLY); - $client->setAuthConfig('client_secret.json'); - $client->setAccessType('offline'); +function getClient() +{ + $client = new Google_Client(); + $client->setApplicationName('Google Calendar API PHP Quickstart'); + $client->setScopes(Google_Service_Calendar::CALENDAR_READONLY); + $client->setAuthConfig('client_secret.json'); + $client->setAccessType('offline'); - // Load previously authorized credentials from a file. - $credentialsPath = expandHomeDirectory('credentials.json'); - if (file_exists($credentialsPath)) { - $accessToken = json_decode(file_get_contents($credentialsPath), true); - } else { - // Request authorization from the user. - $authUrl = $client->createAuthUrl(); - printf("Open the following link in your browser:\n%s\n", $authUrl); - print 'Enter verification code: '; - $authCode = trim(fgets(STDIN)); + // Load previously authorized credentials from a file. + $credentialsPath = expandHomeDirectory('credentials.json'); + if (file_exists($credentialsPath)) { + $accessToken = json_decode(file_get_contents($credentialsPath), true); + } else { + // Request authorization from the user. + $authUrl = $client->createAuthUrl(); + printf("Open the following link in your browser:\n%s\n", $authUrl); + print 'Enter verification code: '; + $authCode = trim(fgets(STDIN)); - // Exchange authorization code for an access token. - $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); + // Exchange authorization code for an access token. + $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); - // Store the credentials to disk. - if (!file_exists(dirname($credentialsPath))) { - mkdir(dirname($credentialsPath), 0700, true); + // Store the credentials to disk. + if (!file_exists(dirname($credentialsPath))) { + mkdir(dirname($credentialsPath), 0700, true); + } + file_put_contents($credentialsPath, json_encode($accessToken)); + printf("Credentials saved to %s\n", $credentialsPath); } - file_put_contents($credentialsPath, json_encode($accessToken)); - printf("Credentials saved to %s\n", $credentialsPath); - } - $client->setAccessToken($accessToken); + $client->setAccessToken($accessToken); - // Refresh the token if it's expired. - if ($client->isAccessTokenExpired()) { - $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); - file_put_contents($credentialsPath, json_encode($client->getAccessToken())); - } - return $client; + // Refresh the token if it's expired. + if ($client->isAccessTokenExpired()) { + $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); + file_put_contents($credentialsPath, json_encode($client->getAccessToken())); + } + return $client; } /** @@ -63,12 +64,13 @@ function getClient() { * @param string $path the path to expand. * @return string the expanded path. */ -function expandHomeDirectory($path) { - $homeDirectory = getenv('HOME'); - if (empty($homeDirectory)) { - $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); - } - return str_replace('~', realpath($homeDirectory), $path); +function expandHomeDirectory($path) +{ + $homeDirectory = getenv('HOME'); + if (empty($homeDirectory)) { + $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); + } + return str_replace('~', realpath($homeDirectory), $path); } // Get the API client and construct the service object. @@ -80,20 +82,20 @@ function expandHomeDirectory($path) { $optParams = array( 'maxResults' => 10, 'orderBy' => 'startTime', - 'singleEvents' => TRUE, + 'singleEvents' => true, 'timeMin' => date('c'), ); $results = $service->events->listEvents($calendarId, $optParams); if (empty($results->getItems())) { - print "No upcoming events found.\n"; + print "No upcoming events found.\n"; } else { - print "Upcoming events:\n"; - foreach ($results->getItems() as $event) { - $start = $event->start->dateTime; - if (empty($start)) { - $start = $event->start->date; + print "Upcoming events:\n"; + foreach ($results->getItems() as $event) { + $start = $event->start->dateTime; + if (empty($start)) { + $start = $event->start->date; + } + printf("%s (%s)\n", $event->getSummary(), $start); } - printf("%s (%s)\n", $event->getSummary(), $start); - } } From b1b4783da0ee8e50d97280c071b84e1bc49a4b67 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 19 Mar 2018 15:24:56 -0700 Subject: [PATCH 3/6] fixes apps-script cs --- apps-script/quickstart/quickstart.php | 76 ++++++++++++++------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/apps-script/quickstart/quickstart.php b/apps-script/quickstart/quickstart.php index db32773..0129d91 100644 --- a/apps-script/quickstart/quickstart.php +++ b/apps-script/quickstart/quickstart.php @@ -20,42 +20,43 @@ * Returns an authorized API client. * @return Google_Client the authorized client object */ -function getClient() { - $client = new Google_Client(); - $client->setApplicationName('Google Apps Script API PHP Quickstart'); - $client->setScopes("https://www.googleapis.com/auth/script.projects"); - $client->setAuthConfig('client_secret.json'); - $client->setAccessType('offline'); +function getClient() +{ + $client = new Google_Client(); + $client->setApplicationName('Google Apps Script API PHP Quickstart'); + $client->setScopes("https://www.googleapis.com/auth/script.projects"); + $client->setAuthConfig('client_secret.json'); + $client->setAccessType('offline'); - // Load previously authorized credentials from a file. - $credentialsPath = expandHomeDirectory('credentials.json'); - if (file_exists($credentialsPath)) { - $accessToken = json_decode(file_get_contents($credentialsPath), true); - } else { - // Request authorization from the user. - $authUrl = $client->createAuthUrl(); - printf("Open the following link in your browser:\n%s\n", $authUrl); - print 'Enter verification code: '; - $authCode = trim(fgets(STDIN)); + // Load previously authorized credentials from a file. + $credentialsPath = expandHomeDirectory('credentials.json'); + if (file_exists($credentialsPath)) { + $accessToken = json_decode(file_get_contents($credentialsPath), true); + } else { + // Request authorization from the user. + $authUrl = $client->createAuthUrl(); + printf("Open the following link in your browser:\n%s\n", $authUrl); + print 'Enter verification code: '; + $authCode = trim(fgets(STDIN)); - // Exchange authorization code for an access token. - $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); + // Exchange authorization code for an access token. + $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); - // Store the credentials to disk. - if(!file_exists(dirname($credentialsPath))) { - mkdir(dirname($credentialsPath), 0700, true); + // Store the credentials to disk. + if (!file_exists(dirname($credentialsPath))) { + mkdir(dirname($credentialsPath), 0700, true); + } + file_put_contents($credentialsPath, json_encode($accessToken)); + printf("Credentials saved to %s\n", $credentialsPath); } - file_put_contents($credentialsPath, json_encode($accessToken)); - printf("Credentials saved to %s\n", $credentialsPath); - } - $client->setAccessToken($accessToken); + $client->setAccessToken($accessToken); - // Refresh the token if it's expired. - if ($client->isAccessTokenExpired()) { - $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); - file_put_contents($credentialsPath, json_encode($client->getAccessToken())); - } - return $client; + // Refresh the token if it's expired. + if ($client->isAccessTokenExpired()) { + $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); + file_put_contents($credentialsPath, json_encode($client->getAccessToken())); + } + return $client; } /** @@ -63,12 +64,13 @@ function getClient() { * @param string $path the path to expand. * @return string the expanded path. */ -function expandHomeDirectory($path) { - $homeDirectory = getenv('HOME'); - if (empty($homeDirectory)) { - $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); - } - return str_replace('~', realpath($homeDirectory), $path); +function expandHomeDirectory($path) +{ + $homeDirectory = getenv('HOME'); + if (empty($homeDirectory)) { + $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); + } + return str_replace('~', realpath($homeDirectory), $path); } /** From 145cf7ad74f126ab2cd748ef158d7c0d88d5fd43 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 19 Mar 2018 15:26:18 -0700 Subject: [PATCH 4/6] fixes drive cs --- drive/quickstart/quickstart.php | 86 ++++++++++++++------------- drive/snippets/src/DriveSnippets.php | 21 ++++++- drive/snippets/tests/BaseTestCase.php | 1 - 3 files changed, 62 insertions(+), 46 deletions(-) diff --git a/drive/quickstart/quickstart.php b/drive/quickstart/quickstart.php index 676cf07..23dc9b3 100644 --- a/drive/quickstart/quickstart.php +++ b/drive/quickstart/quickstart.php @@ -20,42 +20,43 @@ * Returns an authorized API client. * @return Google_Client the authorized client object */ -function getClient() { - $client = new Google_Client(); - $client->setApplicationName('Google Drive API PHP Quickstart'); - $client->setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY); - $client->setAuthConfig('client_secret.json'); - $client->setAccessType('offline'); +function getClient() +{ + $client = new Google_Client(); + $client->setApplicationName('Google Drive API PHP Quickstart'); + $client->setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY); + $client->setAuthConfig('client_secret.json'); + $client->setAccessType('offline'); - // Load previously authorized credentials from a file. - $credentialsPath = expandHomeDirectory('credentials.json'); - if (file_exists($credentialsPath)) { - $accessToken = json_decode(file_get_contents($credentialsPath), true); - } else { - // Request authorization from the user. - $authUrl = $client->createAuthUrl(); - printf("Open the following link in your browser:\n%s\n", $authUrl); - print 'Enter verification code: '; - $authCode = trim(fgets(STDIN)); + // Load previously authorized credentials from a file. + $credentialsPath = expandHomeDirectory('credentials.json'); + if (file_exists($credentialsPath)) { + $accessToken = json_decode(file_get_contents($credentialsPath), true); + } else { + // Request authorization from the user. + $authUrl = $client->createAuthUrl(); + printf("Open the following link in your browser:\n%s\n", $authUrl); + print 'Enter verification code: '; + $authCode = trim(fgets(STDIN)); - // Exchange authorization code for an access token. - $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); + // Exchange authorization code for an access token. + $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); - // Store the credentials to disk. - if(!file_exists(dirname($credentialsPath))) { - mkdir(dirname($credentialsPath), 0700, true); + // Store the credentials to disk. + if (!file_exists(dirname($credentialsPath))) { + mkdir(dirname($credentialsPath), 0700, true); + } + file_put_contents($credentialsPath, json_encode($accessToken)); + printf("Credentials saved to %s\n", $credentialsPath); } - file_put_contents($credentialsPath, json_encode($accessToken)); - printf("Credentials saved to %s\n", $credentialsPath); - } - $client->setAccessToken($accessToken); + $client->setAccessToken($accessToken); - // Refresh the token if it's expired. - if ($client->isAccessTokenExpired()) { - $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); - file_put_contents($credentialsPath, json_encode($client->getAccessToken())); - } - return $client; + // Refresh the token if it's expired. + if ($client->isAccessTokenExpired()) { + $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); + file_put_contents($credentialsPath, json_encode($client->getAccessToken())); + } + return $client; } /** @@ -63,12 +64,13 @@ function getClient() { * @param string $path the path to expand. * @return string the expanded path. */ -function expandHomeDirectory($path) { - $homeDirectory = getenv('HOME'); - if (empty($homeDirectory)) { - $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); - } - return str_replace('~', realpath($homeDirectory), $path); +function expandHomeDirectory($path) +{ + $homeDirectory = getenv('HOME'); + if (empty($homeDirectory)) { + $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); + } + return str_replace('~', realpath($homeDirectory), $path); } // Get the API client and construct the service object. @@ -83,10 +85,10 @@ function expandHomeDirectory($path) { $results = $service->files->listFiles($optParams); if (count($results->getFiles()) == 0) { - print "No files found.\n"; + print "No files found.\n"; } else { - print "Files:\n"; - foreach ($results->getFiles() as $file) { - printf("%s (%s)\n", $file->getName(), $file->getId()); - } + print "Files:\n"; + foreach ($results->getFiles() as $file) { + printf("%s (%s)\n", $file->getName(), $file->getId()); + } } diff --git a/drive/snippets/src/DriveSnippets.php b/drive/snippets/src/DriveSnippets.php index ba028b3..dc6ec95 100644 --- a/drive/snippets/src/DriveSnippets.php +++ b/drive/snippets/src/DriveSnippets.php @@ -1,7 +1,22 @@ service = $service; @@ -165,7 +180,7 @@ public function moveFileToFolder($realFileId, $realFolderId) return $file->parents; } - function searchFiles() + public function searchFiles() { $driveService = $this->service; $files = array(); @@ -191,7 +206,7 @@ function searchFiles() return $files; } - function shareFile($realFileId, $realUser, $realDomain) + public function shareFile($realFileId, $realUser, $realDomain) { $driveService = $this->service; $ids = array(); diff --git a/drive/snippets/tests/BaseTestCase.php b/drive/snippets/tests/BaseTestCase.php index 9905913..015af60 100644 --- a/drive/snippets/tests/BaseTestCase.php +++ b/drive/snippets/tests/BaseTestCase.php @@ -15,7 +15,6 @@ * limitations under the License. */ use Monolog\Logger; -use Monolog\Handler\StreamHandler; class BaseTestCase extends PHPUnit_Framework_TestCase { From d4bd5428148741ea46d637b90215348eb662fbe9 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 19 Mar 2018 15:42:13 -0700 Subject: [PATCH 5/6] fixes sheets cs --- sheets/quickstart/quickstart.php | 88 +-- sheets/snippets/src/SpreadsheetSnippets.php | 571 +++++++++--------- sheets/snippets/tests/BaseTestCase.php | 143 ++--- .../tests/SpreadsheetSnippetsTest.php | 210 ++++--- 4 files changed, 513 insertions(+), 499 deletions(-) diff --git a/sheets/quickstart/quickstart.php b/sheets/quickstart/quickstart.php index fd7f7a1..3ec26d6 100644 --- a/sheets/quickstart/quickstart.php +++ b/sheets/quickstart/quickstart.php @@ -20,42 +20,43 @@ * Returns an authorized API client. * @return Google_Client the authorized client object */ -function getClient() { - $client = new Google_Client(); - $client->setApplicationName('Google Sheets API PHP Quickstart'); - $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY); - $client->setAuthConfig('client_secret.json'); - $client->setAccessType('offline'); +function getClient() +{ + $client = new Google_Client(); + $client->setApplicationName('Google Sheets API PHP Quickstart'); + $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY); + $client->setAuthConfig('client_secret.json'); + $client->setAccessType('offline'); - // Load previously authorized credentials from a file. - $credentialsPath = expandHomeDirectory('credentials.json'); - if (file_exists($credentialsPath)) { - $accessToken = json_decode(file_get_contents($credentialsPath), true); - } else { - // Request authorization from the user. - $authUrl = $client->createAuthUrl(); - printf("Open the following link in your browser:\n%s\n", $authUrl); - print 'Enter verification code: '; - $authCode = trim(fgets(STDIN)); + // Load previously authorized credentials from a file. + $credentialsPath = expandHomeDirectory('credentials.json'); + if (file_exists($credentialsPath)) { + $accessToken = json_decode(file_get_contents($credentialsPath), true); + } else { + // Request authorization from the user. + $authUrl = $client->createAuthUrl(); + printf("Open the following link in your browser:\n%s\n", $authUrl); + print 'Enter verification code: '; + $authCode = trim(fgets(STDIN)); - // Exchange authorization code for an access token. - $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); + // Exchange authorization code for an access token. + $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); - // Store the credentials to disk. - if(!file_exists(dirname($credentialsPath))) { - mkdir(dirname($credentialsPath), 0700, true); + // Store the credentials to disk. + if (!file_exists(dirname($credentialsPath))) { + mkdir(dirname($credentialsPath), 0700, true); + } + file_put_contents($credentialsPath, json_encode($accessToken)); + printf("Credentials saved to %s\n", $credentialsPath); } - file_put_contents($credentialsPath, json_encode($accessToken)); - printf("Credentials saved to %s\n", $credentialsPath); - } - $client->setAccessToken($accessToken); + $client->setAccessToken($accessToken); - // Refresh the token if it's expired. - if ($client->isAccessTokenExpired()) { - $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); - file_put_contents($credentialsPath, json_encode($client->getAccessToken())); - } - return $client; + // Refresh the token if it's expired. + if ($client->isAccessTokenExpired()) { + $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); + file_put_contents($credentialsPath, json_encode($client->getAccessToken())); + } + return $client; } /** @@ -63,12 +64,13 @@ function getClient() { * @param string $path the path to expand. * @return string the expanded path. */ -function expandHomeDirectory($path) { - $homeDirectory = getenv('HOME'); - if (empty($homeDirectory)) { - $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); - } - return str_replace('~', realpath($homeDirectory), $path); +function expandHomeDirectory($path) +{ + $homeDirectory = getenv('HOME'); + if (empty($homeDirectory)) { + $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); + } + return str_replace('~', realpath($homeDirectory), $path); } // Get the API client and construct the service object. @@ -83,11 +85,11 @@ function expandHomeDirectory($path) { $values = $response->getValues(); if (empty($values)) { - print "No data found.\n"; + print "No data found.\n"; } else { - print "Name, Major:\n"; - foreach ($values as $row) { - // Print columns A and E, which correspond to indices 0 and 4. - printf("%s, %s\n", $row[0], $row[4]); - } + print "Name, Major:\n"; + foreach ($values as $row) { + // Print columns A and E, which correspond to indices 0 and 4. + printf("%s, %s\n", $row[0], $row[4]); + } } diff --git a/sheets/snippets/src/SpreadsheetSnippets.php b/sheets/snippets/src/SpreadsheetSnippets.php index 7bd0529..0280160 100644 --- a/sheets/snippets/src/SpreadsheetSnippets.php +++ b/sheets/snippets/src/SpreadsheetSnippets.php @@ -14,306 +14,313 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -class SpreadsheetSnippets { - public function __construct($service) { - $this->service = $service; - } +class SpreadsheetSnippets +{ + public function __construct($service) + { + $this->service = $service; + } - public function create($title) { - $service = $this->service; - // [START create] - $spreadsheet = new Google_Service_Sheets_Spreadsheet([ - 'properties' => [ - 'title' => $title - ] - ]); - $spreadsheet = $service->spreadsheets->create($spreadsheet, [ - 'fields' => 'spreadsheetId' - ]); - printf("Spreadsheet ID: %s\n", $spreadsheet->spreadsheetId); - // [START end] - return $spreadsheet->spreadsheetId; - } + public function create($title) + { + $service = $this->service; + // [START create] + $spreadsheet = new Google_Service_Sheets_Spreadsheet([ + 'properties' => [ + 'title' => $title + ] + ]); + $spreadsheet = $service->spreadsheets->create($spreadsheet, [ + 'fields' => 'spreadsheetId' + ]); + printf("Spreadsheet ID: %s\n", $spreadsheet->spreadsheetId); + // [START end] + return $spreadsheet->spreadsheetId; + } - public function batchUpdate($spreadsheetId, $title, $find, $replacement) { - $service = $this->service; - // [START batchUpdate] - $requests = [ - // Change the spreadsheet's title. - new Google_Service_Sheets_Request([ - 'updateSpreadsheetProperties' => [ - 'properties' => [ - 'title' => $title - ], - 'fields' => 'title' - ] - ]), - // Find and replace text. - new Google_Service_Sheets_Request([ - 'findReplace' => [ - 'find' => $find, - 'replacement' => $replacement, - 'allSheets' => TRUE - ] - ]) - ]; - // Add additional requests (operations) ... + public function batchUpdate($spreadsheetId, $title, $find, $replacement) + { + $service = $this->service; + // [START batchUpdate] + $requests = [ + // Change the spreadsheet's title. + new Google_Service_Sheets_Request([ + 'updateSpreadsheetProperties' => [ + 'properties' => [ + 'title' => $title + ], + 'fields' => 'title' + ] + ]), + // Find and replace text. + new Google_Service_Sheets_Request([ + 'findReplace' => [ + 'find' => $find, + 'replacement' => $replacement, + 'allSheets' => true + ] + ]) + ]; - $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ - 'requests' => $requests - ]); + // Add additional requests (operations) ... + $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ + 'requests' => $requests + ]); - $response = $service->spreadsheets->batchUpdate($spreadsheetId, - $batchUpdateRequest); - $findReplaceResponse = $response->getReplies()[1]->getFindReplace(); - printf("%s replacements made.\n", + $response = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest); + $findReplaceResponse = $response->getReplies()[1]->getFindReplace(); + printf("%s replacements made.\n", $findReplaceResponse->getOccurrencesChanged()); - // [END batchUpdate] - return $response; - } + // [END batchUpdate] + return $response; + } - public function getValues($spreadsheetId, $range) { - $service = $this->service; - // [START getValues] - $result = $service->spreadsheets_values->get($spreadsheetId, $range); - $numRows = $result->getValues() != null ? count($result->getValues()) : 0; - printf("%d rows retrieved.", $numRows); - // [END getValues] - return $result; - } + public function getValues($spreadsheetId, $range) + { + $service = $this->service; + // [START getValues] + $result = $service->spreadsheets_values->get($spreadsheetId, $range); + $numRows = $result->getValues() != null ? count($result->getValues()) : 0; + printf("%d rows retrieved.", $numRows); + // [END getValues] + return $result; + } - public function batchGetValues($spreadsheetId, $_ranges) { - $service = $this->service; - // [START batchGetValues]. - $ranges = [ - // Range names ... - ]; - // [START_EXCLUDE silent] - $ranges = $_ranges; - // [END_EXCLUDE] - $params = array( - 'ranges' => $ranges - ); - $result = $service->spreadsheets_values->batchGet($spreadsheetId, $params); - printf("%d ranges retrieved.", count($result->getValueRanges())); - // [END batchGetValues] - return $result; - } + public function batchGetValues($spreadsheetId, $_ranges) + { + $service = $this->service; + // [START batchGetValues]. + $ranges = [ + // Range names ... + ]; + // [START_EXCLUDE silent] + $ranges = $_ranges; + // [END_EXCLUDE] + $params = array( + 'ranges' => $ranges + ); + $result = $service->spreadsheets_values->batchGet($spreadsheetId, $params); + printf("%d ranges retrieved.", count($result->getValueRanges())); + // [END batchGetValues] + return $result; + } - public function updateValues($spreadsheetId, $range, $valueInputOption, - $_values) { - $service = $this->service; - // [START updateValues] - $values = [ - [ - // Cell values ... - ], - // Additional rows ... - ]; - // [START_EXCLUDE silent] - $values = $_values; - // [END_EXCLUDE] - $body = new Google_Service_Sheets_ValueRange([ - 'values' => $values - ]); - $params = [ - 'valueInputOption' => $valueInputOption - ]; - $result = $service->spreadsheets_values->update($spreadsheetId, $range, + public function updateValues($spreadsheetId, $range, $valueInputOption, + $_values) + { + $service = $this->service; + // [START updateValues] + $values = [ + [ + // Cell values ... + ], + // Additional rows ... + ]; + // [START_EXCLUDE silent] + $values = $_values; + // [END_EXCLUDE] + $body = new Google_Service_Sheets_ValueRange([ + 'values' => $values + ]); + $params = [ + 'valueInputOption' => $valueInputOption + ]; + $result = $service->spreadsheets_values->update($spreadsheetId, $range, $body, $params); - printf("%d cells updated.", $result->getUpdatedCells()); - // [END updateValues] - return $result; - } + printf("%d cells updated.", $result->getUpdatedCells()); + // [END updateValues] + return $result; + } - public function batchUpdateValues($spreadsheetId, $range, $valueInputOption, - $_values) { - $service = $this->service; - // [START batchUpdateValues] - $values = [ - [ - // Cell values ... - ], - // Additional rows ... - ]; - // [START_EXCLUDE silent] - $values = $_values; - // [END_EXCLUDE] - $data = []; - $data[] = new Google_Service_Sheets_ValueRange([ - 'range' => $range, - 'values' => $values - ]); - // Additional ranges to update ... - $body = new Google_Service_Sheets_BatchUpdateValuesRequest([ - 'valueInputOption' => $valueInputOption, - 'data' => $data - ]); - $result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $body); - printf("%d cells updated.", $result->getTotalUpdatedCells()); - // [END batchUpdateValues] - return $result; - } + public function batchUpdateValues($spreadsheetId, $range, $valueInputOption, + $_values) + { + $service = $this->service; + // [START batchUpdateValues] + $values = [ + [ + // Cell values ... + ], + // Additional rows ... + ]; + // [START_EXCLUDE silent] + $values = $_values; + // [END_EXCLUDE] + $data = []; + $data[] = new Google_Service_Sheets_ValueRange([ + 'range' => $range, + 'values' => $values + ]); + // Additional ranges to update ... + $body = new Google_Service_Sheets_BatchUpdateValuesRequest([ + 'valueInputOption' => $valueInputOption, + 'data' => $data + ]); + $result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $body); + printf("%d cells updated.", $result->getTotalUpdatedCells()); + // [END batchUpdateValues] + return $result; + } - public function appendValues($spreadsheetId, $range, $valueInputOption, - $_values) { - $service = $this->service; - // [START appendValues] - $values = [ - [ - // Cell values ... - ], - // Additional rows ... - ]; - // [START_EXCLUDE silent] - $values = $_values; - // [END_EXCLUDE] - $body = new Google_Service_Sheets_ValueRange([ - 'values' => $values - ]); - $params = [ - 'valueInputOption' => $valueInputOption - ]; - $result = $service->spreadsheets_values->append($spreadsheetId, $range, - $body, $params); - printf("%d cells appended.", $result->getUpdates()->getUpdatedCells()); - // [END appendValues] - return $result; - } + public function appendValues($spreadsheetId, $range, $valueInputOption, + $_values) + { + $service = $this->service; + // [START appendValues] + $values = [ + [ + // Cell values ... + ], + // Additional rows ... + ]; + // [START_EXCLUDE silent] + $values = $_values; + // [END_EXCLUDE] + $body = new Google_Service_Sheets_ValueRange([ + 'values' => $values + ]); + $params = [ + 'valueInputOption' => $valueInputOption + ]; + $result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params); + printf("%d cells appended.", $result->getUpdates()->getUpdatedCells()); + // [END appendValues] + return $result; + } - public function pivotTables($spreadsheetId) { - $service = $this->service; - $requests = [ - new Google_Service_Sheets_Request([ - 'addSheet' => [ - 'properties' => [ - 'title' => 'Sheet 1' - ] - ] - ]), - new Google_Service_Sheets_Request([ - 'addSheet' => [ - 'properties' => [ - 'title' => 'Sheet 2' - ] - ] - ]) - ]; - // Create two sheets for our pivot table - $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ - 'requests' => $requests - ]); - $batchUpdateResponse = $service->spreadsheets->batchUpdate( - $spreadsheetId, $batchUpdateRequest); - $sourceSheetId = $batchUpdateResponse->replies[0]->addSheet->properties->sheetId; - $targetSheetId = $batchUpdateResponse->replies[1]->addSheet->properties->sheetId; - // [START pivotTables] - $requests = [ - 'updateCells' => [ - 'rows' => [ - 'values' => [ - [ - 'pivotTable' => [ - 'source' => [ - 'sheetId' => $sourceSheetId, - 'startRowIndex' => 0, - 'startColumnIndex' => 0, - 'endRowIndex' => 101, - 'endColumnIndex' => 8 - ], + public function pivotTables($spreadsheetId) + { + $service = $this->service; + $requests = [ + new Google_Service_Sheets_Request([ + 'addSheet' => [ + 'properties' => [ + 'title' => 'Sheet 1' + ] + ] + ]), + new Google_Service_Sheets_Request([ + 'addSheet' => [ + 'properties' => [ + 'title' => 'Sheet 2' + ] + ] + ]) + ]; + // Create two sheets for our pivot table + $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ + 'requests' => $requests + ]); + $batchUpdateResponse = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest); + $sourceSheetId = $batchUpdateResponse->replies[0]->addSheet->properties->sheetId; + $targetSheetId = $batchUpdateResponse->replies[1]->addSheet->properties->sheetId; + // [START pivotTables] + $requests = [ + 'updateCells' => [ 'rows' => [ - [ - 'sourceColumnOffset' => 6, - 'showTotals' => True, - 'sortOrder' => 'ASCENDING', - ], + 'values' => [ + [ + 'pivotTable' => [ + 'source' => [ + 'sheetId' => $sourceSheetId, + 'startRowIndex' => 0, + 'startColumnIndex' => 0, + 'endRowIndex' => 101, + 'endColumnIndex' => 8 + ], + 'rows' => [ + [ + 'sourceColumnOffset' => 6, + 'showTotals' => true, + 'sortOrder' => 'ASCENDING', + ], + ], + 'columns' => [ + [ + 'sourceColumnOffset' => 3, + 'sortOrder' => 'ASCENDING', + 'showTotals' => true, + ] + ], + 'values' => [ + [ + 'summarizeFunction' => 'COUNTA', + 'sourceColumnOffset' => 3 + ] + ], + 'valueLayout' => 'HORIZONTAL' + ] + ] + ] ], - 'columns' => [ - [ - 'sourceColumnOffset' => 3, - 'sortOrder' => 'ASCENDING', - 'showTotals' => True, - ] + 'start' => [ + 'sheetId' => $targetSheetId, + 'rowIndex' => 0, + 'columnIndex' => 0 ], - 'values' => [ - [ - 'summarizeFunction' => 'COUNTA', - 'sourceColumnOffset' => 3 - ] - ], - 'valueLayout' => 'HORIZONTAL' - ] + 'fields' => 'pivotTable' ] - ] - ], - 'start' => [ - 'sheetId' => $targetSheetId, - 'rowIndex' => 0, - 'columnIndex' => 0 - ], - 'fields' => 'pivotTable' - ] - ]; - return $batchUpdateResponse; - // [END pivotTables] - } + ]; + return $batchUpdateResponse; + // [END pivotTables] + } - public function conditionalFormatting($spreadsheetId) { - $service = $this->service; - // [START conditionalFormatting] - $myRange = [ - 'sheetId' => 0, - 'startRowIndex' => 1, - 'endRowIndex' => 11, - 'startColumnIndex' => 0, - 'endColumnIndex' => 4, - ]; + public function conditionalFormatting($spreadsheetId) + { + $service = $this->service; + // [START conditionalFormatting] + $myRange = [ + 'sheetId' => 0, + 'startRowIndex' => 1, + 'endRowIndex' => 11, + 'startColumnIndex' => 0, + 'endColumnIndex' => 4, + ]; - $requests = [ - new Google_Service_Sheets_Request([ - 'addConditionalFormatRule' => [ - 'rule' => [ - 'ranges' => [ $myRange ], - 'booleanRule' => [ - 'condition' => [ - 'type' => 'CUSTOM_FORMULA', - 'values' => [ [ 'userEnteredValue' => '=GT($D2,median($D$2:$D$11))' ] ] - ], - 'format' => [ - 'textFormat' => [ 'foregroundColor' => [ 'red' => 0.8 ] ] - ] - ] - ], - 'index' => 0 - ] - ]), - new Google_Service_Sheets_Request([ - 'addConditionalFormatRule' => [ - 'rule' => [ - 'ranges' => [ $myRange ], - 'booleanRule' => [ - 'condition' => [ - 'type' => 'CUSTOM_FORMULA', - 'values' => [ [ 'userEnteredValue' => '=LT($D2,median($D$2:$D$11))' ] ] - ], - 'format' => [ - 'backgroundColor' => [ 'red' => 1, 'green' => 0.4, 'blue' => 0.4 ] - ] - ] - ], - 'index' => 0 - ] - ]) - ]; + $requests = [ + new Google_Service_Sheets_Request([ + 'addConditionalFormatRule' => [ + 'rule' => [ + 'ranges' => [ $myRange ], + 'booleanRule' => [ + 'condition' => [ + 'type' => 'CUSTOM_FORMULA', + 'values' => [ [ 'userEnteredValue' => '=GT($D2,median($D$2:$D$11))' ] ] + ], + 'format' => [ + 'textFormat' => [ 'foregroundColor' => [ 'red' => 0.8 ] ] + ] + ] + ], + 'index' => 0 + ] + ]), + new Google_Service_Sheets_Request([ + 'addConditionalFormatRule' => [ + 'rule' => [ + 'ranges' => [ $myRange ], + 'booleanRule' => [ + 'condition' => [ + 'type' => 'CUSTOM_FORMULA', + 'values' => [ [ 'userEnteredValue' => '=LT($D2,median($D$2:$D$11))' ] ] + ], + 'format' => [ + 'backgroundColor' => [ 'red' => 1, 'green' => 0.4, 'blue' => 0.4 ] + ] + ] + ], + 'index' => 0 + ] + ]) + ]; - $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ - 'requests' => $requests - ]); - $response = $service->spreadsheets->batchUpdate($spreadsheetId, - $batchUpdateRequest); - printf("%d cells updated.", count($response->getReplies())); - return $response; - // [END conditionalFormatting] - } + $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ + 'requests' => $requests + ]); + $response = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest); + printf("%d cells updated.", count($response->getReplies())); + return $response; + // [END conditionalFormatting] + } } diff --git a/sheets/snippets/tests/BaseTestCase.php b/sheets/snippets/tests/BaseTestCase.php index 2993260..288aa5a 100644 --- a/sheets/snippets/tests/BaseTestCase.php +++ b/sheets/snippets/tests/BaseTestCase.php @@ -15,82 +15,89 @@ * limitations under the License. */ use Monolog\Logger; -use Monolog\Handler\StreamHandler; -class BaseTestCase extends PHPUnit_Framework_TestCase { - const APPLICATION_NAME = 'Google Sheets API Snippet Tests'; +class BaseTestCase extends PHPUnit_Framework_TestCase +{ + const APPLICATION_NAME = 'Google Sheets API Snippet Tests'; - protected static $service; - protected static $driveService; - protected $filesToDelete; + protected static $service; + protected static $driveService; + protected $filesToDelete; - public static function setUpBeforeClass() { - $client = self::createClient(); - BaseTestCase::$service = new Google_Service_Sheets($client); - BaseTestCase::$driveService = new Google_Service_Drive($client); - } + public static function setUpBeforeClass() + { + $client = self::createClient(); + BaseTestCase::$service = new Google_Service_Sheets($client); + BaseTestCase::$driveService = new Google_Service_Drive($client); + } - protected function setUp() { - $this->filesToDelete = []; - // Hide STDOUT output generated by snippets. - ob_start(); - } + protected function setUp() + { + $this->filesToDelete = []; + // Hide STDOUT output generated by snippets. + ob_start(); + } - protected function tearDown() { - foreach ($this->filesToDelete as $fileId) { - self::$driveService->files->delete($fileId); + protected function tearDown() + { + foreach ($this->filesToDelete as $fileId) { + self::$driveService->files->delete($fileId); + } + // Restore STDOUT. + ob_end_clean(); } - // Restore STDOUT. - ob_end_clean(); - } - protected static function createClient() { - // create a log channel - $log = new Logger('debug'); - $client = new Google_Client(); - $client->setApplicationName(self::APPLICATION_NAME); - $client->useApplicationDefaultCredentials(); - $client->addScope('https://www.googleapis.com/auth/drive'); - $client->setLogger($log); - return $client; - } + protected static function createClient() + { + // create a log channel + $log = new Logger('debug'); + $client = new Google_Client(); + $client->setApplicationName(self::APPLICATION_NAME); + $client->useApplicationDefaultCredentials(); + $client->addScope('https://www.googleapis.com/auth/drive'); + $client->setLogger($log); + return $client; + } - protected function deleteFileOnCleanup($id) { - array_push($this->filesToDelete, $id); - } + protected function deleteFileOnCleanup($id) + { + array_push($this->filesToDelete, $id); + } - protected function createTestSpreadsheet() { - $spreadsheet = new Google_Service_Sheets_Spreadsheet([ - 'properties' => [ - 'title' => 'Test Spreadsheet' - ] - ]); - $spreadsheet = self::$service->spreadsheets->create($spreadsheet); - $this->deleteFileOnCleanup($spreadsheet->spreadsheetId); - return $spreadsheet->spreadsheetId; - } + protected function createTestSpreadsheet() + { + $spreadsheet = new Google_Service_Sheets_Spreadsheet([ + 'properties' => [ + 'title' => 'Test Spreadsheet' + ] + ]); + $spreadsheet = self::$service->spreadsheets->create($spreadsheet); + $this->deleteFileOnCleanup($spreadsheet->spreadsheetId); + return $spreadsheet->spreadsheetId; + } - protected function populateValues($spreadsheetId) { - $requests = [ - 'repeatCell' => [ - 'range' => [ - 'sheetId' => 0, - 'startRowIndex' => 0, - 'endRowIndex' => 10, - 'startColumnIndex' => 0, - 'endColumnIndex' => 10 - ], - 'cell' => [ - 'userEnteredValue' => [ - 'stringValue' => 'Hello' - ] - ], - 'fields' => 'userEnteredValue' - ] - ]; - $body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ - 'requests' => $requests - ]); - self::$service->spreadsheets->batchUpdate($spreadsheetId, $body); - } + protected function populateValues($spreadsheetId) + { + $requests = [ + 'repeatCell' => [ + 'range' => [ + 'sheetId' => 0, + 'startRowIndex' => 0, + 'endRowIndex' => 10, + 'startColumnIndex' => 0, + 'endColumnIndex' => 10 + ], + 'cell' => [ + 'userEnteredValue' => [ + 'stringValue' => 'Hello' + ] + ], + 'fields' => 'userEnteredValue' + ] + ]; + $body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ + 'requests' => $requests + ]); + self::$service->spreadsheets->batchUpdate($spreadsheetId, $body); + } } diff --git a/sheets/snippets/tests/SpreadsheetSnippetsTest.php b/sheets/snippets/tests/SpreadsheetSnippetsTest.php index f33b126..f344989 100644 --- a/sheets/snippets/tests/SpreadsheetSnippetsTest.php +++ b/sheets/snippets/tests/SpreadsheetSnippetsTest.php @@ -17,119 +17,117 @@ require_once 'src/SpreadsheetSnippets.php'; require_once 'BaseTestCase.php'; -class SpreadsheetSnippetsTest extends BaseTestCase { - protected function setUp() { - parent::setUp(); - $this->snippets = new SpreadsheetSnippets(parent::$service); - } +class SpreadsheetSnippetsTest extends BaseTestCase +{ + protected function setUp() + { + parent::setUp(); + $this->snippets = new SpreadsheetSnippets(parent::$service); + } - public function testCreate() { - $id = $this->snippets->create('Title'); - $this->assertNotNull($id, 'ID not returned.'); - $this->deleteFileOnCleanup($id); - } + public function testCreate() + { + $id = $this->snippets->create('Title'); + $this->assertNotNull($id, 'ID not returned.'); + $this->deleteFileOnCleanup($id); + } - public function testBatchUpdate() { - $id = $this->createTestSpreadsheet(); - $this->populateValues($id); - $response = $this->snippets->batchUpdate($id, 'New Title', 'Hello', - 'Goodbye'); - $this->assertNotNull($response, 'Responses is null'); - $replies = $response->getReplies(); - $this->assertNotNull($replies, 'Replies is null'); - $this->assertEquals(2, count($replies), 'Missing replies'); - $findReplaceResponse = $replies[1]->getFindReplace(); - $this->assertNotNull($findReplaceResponse, 'Find/replace response is null'); - $this->assertEquals($findReplaceResponse->getValuesChanged(), 100, - 'Wrong number of replacements'); - } + public function testBatchUpdate() + { + $id = $this->createTestSpreadsheet(); + $this->populateValues($id); + $response = $this->snippets->batchUpdate($id, 'New Title', 'Hello', 'Goodbye'); + $this->assertNotNull($response, 'Responses is null'); + $replies = $response->getReplies(); + $this->assertNotNull($replies, 'Replies is null'); + $this->assertEquals(2, count($replies), 'Missing replies'); + $findReplaceResponse = $replies[1]->getFindReplace(); + $this->assertNotNull($findReplaceResponse, 'Find/replace response is null'); + $this->assertEquals($findReplaceResponse->getValuesChanged(), 100, 'Wrong number of replacements'); + } - public function testGetValues() { - $id = $this->createTestSpreadsheet(); - $this->populateValues($id); - $result = $this->snippets->getValues($id, 'A1:C2'); - $this->assertNotNull($result, 'No response returned.'); - $values = $result->getValues(); - $this->assertNotNull($values, 'No values returned.'); - $this->assertEquals(2, count($values), 'Wrong number of rows.'); - $this->assertEquals(3, count($values[0]), 'Wrong number of columns.'); - } + public function testGetValues() + { + $id = $this->createTestSpreadsheet(); + $this->populateValues($id); + $result = $this->snippets->getValues($id, 'A1:C2'); + $this->assertNotNull($result, 'No response returned.'); + $values = $result->getValues(); + $this->assertNotNull($values, 'No values returned.'); + $this->assertEquals(2, count($values), 'Wrong number of rows.'); + $this->assertEquals(3, count($values[0]), 'Wrong number of columns.'); + } - public function testBatchGetValues() { - $id = $this->createTestSpreadsheet(); - $this->populateValues($id); - $result = $this->snippets->batchGetValues($id, array('A1:A3', 'B1:C1')); - $this->assertNotNull($result, 'No response returned.'); - $valueRanges = $result->getValueRanges(); - $this->assertNotNull($valueRanges, 'No value ranges returned.'); - $this->assertEquals(2, count($valueRanges), 'Wrong number of value ranges.'); - $values = $valueRanges[0]->getValues(); - $this->assertNotNull($values, 'No values returned.'); - $this->assertEquals(3, count($values), 'Wrong number of rows.'); - } + public function testBatchGetValues() + { + $id = $this->createTestSpreadsheet(); + $this->populateValues($id); + $result = $this->snippets->batchGetValues($id, array('A1:A3', 'B1:C1')); + $this->assertNotNull($result, 'No response returned.'); + $valueRanges = $result->getValueRanges(); + $this->assertNotNull($valueRanges, 'No value ranges returned.'); + $this->assertEquals(2, count($valueRanges), 'Wrong number of value ranges.'); + $values = $valueRanges[0]->getValues(); + $this->assertNotNull($values, 'No values returned.'); + $this->assertEquals(3, count($values), 'Wrong number of rows.'); + } - public function testUpdateValues() { - $id = $this->createTestSpreadsheet(); - $result = $this->snippets->updateValues($id, 'A1:B2', 'USER_ENTERED', array( - array('A', 'B'), - array('C', 'D') - )); - $this->assertNotNull($result, 'No result returned.'); - $this->assertEquals(2, $result->getUpdatedRows(), - 'Wrong number of rows updated.'); - $this->assertEquals(2, $result->getUpdatedColumns(), - 'Wrong number of columns updated.'); - $this->assertEquals(4, $result->getUpdatedCells(), - 'Wrong number of cells updated.'); - } + public function testUpdateValues() + { + $id = $this->createTestSpreadsheet(); + $result = $this->snippets->updateValues($id, 'A1:B2', 'USER_ENTERED', array( + array('A', 'B'), + array('C', 'D') + )); + $this->assertNotNull($result, 'No result returned.'); + $this->assertEquals(2, $result->getUpdatedRows(), 'Wrong number of rows updated.'); + $this->assertEquals(2, $result->getUpdatedColumns(), 'Wrong number of columns updated.'); + $this->assertEquals(4, $result->getUpdatedCells(), 'Wrong number of cells updated.'); + } - public function testBatchUpdateValues() { - $id = $this->createTestSpreadsheet(); - $result = $this->snippets->batchUpdateValues($id, 'A1:B2', 'USER_ENTERED', array( - array('A', 'B'), - array('C', 'D') - )); - $this->assertNotNull($result, 'No result returned.'); - $this->assertEquals(1, count($result->getResponses()), - 'Wrong number of ranges updated.'); - $this->assertEquals(2, $result->getTotalUpdatedRows(), - 'Wrong number of rows updated.'); - $this->assertEquals(2, $result->getTotalUpdatedColumns(), - 'Wrong number of columns updated.'); - $this->assertEquals(4, $result->getTotalUpdatedCells(), - 'Wrong number of cells updated.'); - } + public function testBatchUpdateValues() + { + $id = $this->createTestSpreadsheet(); + $result = $this->snippets->batchUpdateValues($id, 'A1:B2', 'USER_ENTERED', array( + array('A', 'B'), + array('C', 'D') + )); + $this->assertNotNull($result, 'No result returned.'); + $this->assertEquals(1, count($result->getResponses()), 'Wrong number of ranges updated.'); + $this->assertEquals(2, $result->getTotalUpdatedRows(), 'Wrong number of rows updated.'); + $this->assertEquals(2, $result->getTotalUpdatedColumns(), 'Wrong number of columns updated.'); + $this->assertEquals(4, $result->getTotalUpdatedCells(), 'Wrong number of cells updated.'); + } - public function testAppendValues() { - $id = $this->createTestSpreadsheet(); - $this->populateValues($id); - $result = $this->snippets->appendValues($id, 'Sheet1', 'USER_ENTERED', array( - array('A', 'B'), - array('C', 'D') - )); - $this->assertNotNull($result, 'No result returned.'); - $this->assertEquals('Sheet1!A1:J10', $result->getTableRange(), - 'Wrong table range appended to.'); - $updates = $result->getUpdates(); - $this->assertEquals(2, $updates->getUpdatedRows(), - 'Wrong number of rows appended.'); - $this->assertEquals(2, $updates->getUpdatedColumns(), - 'Wrong number of columns appended.'); - $this->assertEquals(4, $updates->getUpdatedCells(), - 'Wrong number of cells appended.'); - } + public function testAppendValues() + { + $id = $this->createTestSpreadsheet(); + $this->populateValues($id); + $result = $this->snippets->appendValues($id, 'Sheet1', 'USER_ENTERED', array( + array('A', 'B'), + array('C', 'D') + )); + $this->assertNotNull($result, 'No result returned.'); + $this->assertEquals('Sheet1!A1:J10', $result->getTableRange(), 'Wrong table range appended to.'); + $updates = $result->getUpdates(); + $this->assertEquals(2, $updates->getUpdatedRows(), 'Wrong number of rows appended.'); + $this->assertEquals(2, $updates->getUpdatedColumns(), 'Wrong number of columns appended.'); + $this->assertEquals(4, $updates->getUpdatedCells(), 'Wrong number of cells appended.'); + } - public function testPivotTables() { - $id = $this->createTestSpreadsheet(); - $this->populateValues($id); - $response = $this->snippets->pivotTables($id); - $this->assertNotNull($response); - } + public function testPivotTables() + { + $id = $this->createTestSpreadsheet(); + $this->populateValues($id); + $response = $this->snippets->pivotTables($id); + $this->assertNotNull($response); + } - public function testConditionalFormatting() { - $id = $this->createTestSpreadsheet(); - $this->populateValues($id); - $response = $this->snippets->conditionalFormatting($id); - $this->assertEquals(2, count($response->getReplies())); - } + public function testConditionalFormatting() + { + $id = $this->createTestSpreadsheet(); + $this->populateValues($id); + $response = $this->snippets->conditionalFormatting($id); + $this->assertEquals(2, count($response->getReplies())); + } } From 7e21aea963dfccf25b4b65385ddf75162fd10f79 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 19 Mar 2018 15:42:31 -0700 Subject: [PATCH 6/6] fixes slides cs --- slides/quickstart/quickstart.php | 80 +++++++++++++------------- slides/snippets/tests/BaseTestCase.php | 1 - 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/slides/quickstart/quickstart.php b/slides/quickstart/quickstart.php index e168e17..c2e343b 100644 --- a/slides/quickstart/quickstart.php +++ b/slides/quickstart/quickstart.php @@ -19,42 +19,43 @@ * Returns an authorized API client. * @return Google_Client the authorized client object */ -function getClient() { - $client = new Google_Client(); - $client->setApplicationName('Google Slides API PHP Quickstart'); - $client->setScopes(Google_Service_Slides::PRESENTATIONS_READONLY); - $client->setAuthConfig('client_secret.json'); - $client->setAccessType('offline'); +function getClient() +{ + $client = new Google_Client(); + $client->setApplicationName('Google Slides API PHP Quickstart'); + $client->setScopes(Google_Service_Slides::PRESENTATIONS_READONLY); + $client->setAuthConfig('client_secret.json'); + $client->setAccessType('offline'); - // Load previously authorized credentials from a file. - $credentialsPath = expandHomeDirectory('credentials.json'); - if (file_exists($credentialsPath)) { - $accessToken = json_decode(file_get_contents($credentialsPath), true); - } else { - // Request authorization from the user. - $authUrl = $client->createAuthUrl(); - printf("Open the following link in your browser:\n%s\n", $authUrl); - print 'Enter verification code: '; - $authCode = trim(fgets(STDIN)); + // Load previously authorized credentials from a file. + $credentialsPath = expandHomeDirectory('credentials.json'); + if (file_exists($credentialsPath)) { + $accessToken = json_decode(file_get_contents($credentialsPath), true); + } else { + // Request authorization from the user. + $authUrl = $client->createAuthUrl(); + printf("Open the following link in your browser:\n%s\n", $authUrl); + print 'Enter verification code: '; + $authCode = trim(fgets(STDIN)); - // Exchange authorization code for an access token. - $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); + // Exchange authorization code for an access token. + $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); - // Store the credentials to disk. - if (!file_exists(dirname($credentialsPath))) { - mkdir(dirname($credentialsPath), 0700, true); + // Store the credentials to disk. + if (!file_exists(dirname($credentialsPath))) { + mkdir(dirname($credentialsPath), 0700, true); + } + file_put_contents($credentialsPath, json_encode($accessToken)); + printf("Credentials saved to %s\n", $credentialsPath); } - file_put_contents($credentialsPath, json_encode($accessToken)); - printf("Credentials saved to %s\n", $credentialsPath); - } - $client->setAccessToken($accessToken); + $client->setAccessToken($accessToken); - // Refresh the token if it's expired. - if ($client->isAccessTokenExpired()) { - $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); - file_put_contents($credentialsPath, json_encode($client->getAccessToken())); - } - return $client; + // Refresh the token if it's expired. + if ($client->isAccessTokenExpired()) { + $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); + file_put_contents($credentialsPath, json_encode($client->getAccessToken())); + } + return $client; } /** @@ -62,12 +63,13 @@ function getClient() { * @param string $path the path to expand. * @return string the expanded path. */ -function expandHomeDirectory($path) { - $homeDirectory = getenv('HOME'); - if (empty($homeDirectory)) { - $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); - } - return str_replace('~', realpath($homeDirectory), $path); +function expandHomeDirectory($path) +{ + $homeDirectory = getenv('HOME'); + if (empty($homeDirectory)) { + $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); + } + return str_replace('~', realpath($homeDirectory), $path); } // Get the API client and construct the service object. @@ -82,7 +84,7 @@ function expandHomeDirectory($path) { printf("The presentation contains %s slides:\n", count($slides)); foreach ($slides as $i => $slide) { - // Print columns A and E, which correspond to indices 0 and 4. - printf("- Slide #%s contains %s elements.\n", $i + 1, + // Print columns A and E, which correspond to indices 0 and 4. + printf("- Slide #%s contains %s elements.\n", $i + 1, count($slide->getPageElements())); } diff --git a/slides/snippets/tests/BaseTestCase.php b/slides/snippets/tests/BaseTestCase.php index 2f5c73d..af0526d 100644 --- a/slides/snippets/tests/BaseTestCase.php +++ b/slides/snippets/tests/BaseTestCase.php @@ -15,7 +15,6 @@ * limitations under the License. */ use Monolog\Logger; -use Monolog\Handler\StreamHandler; class BaseTestCase extends PHPUnit_Framework_TestCase {