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

Getting Started: Sessions #209

Merged
merged 12 commits into from
Oct 3, 2019
1 change: 1 addition & 0 deletions sessions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions sessions/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runtime: php72
5 changes: 5 additions & 0 deletions sessions/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"google/cloud-firestore": "^1.7"
dwsupplee marked this conversation as resolved.
Show resolved Hide resolved
}
}
60 changes: 60 additions & 0 deletions sessions/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

# Copyright 2019 Google LLC
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START getting_started_sessions_all]
require_once __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Firestore\FirestoreSessionHandler;

$projectId = getenv('GOOGLE_CLOUD_PROJECT');
# [START getting_started_sessions_register_handler]
// Instantiate the Firestore Client for your project ID.
$firestore = new FirestoreClient([
'projectId' => $projectId,
]);

// Configure PHP to use the the Firebase session handler.
$handler = new FirestoreSessionHandler($firestore);
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
session_set_save_handler($handler, true);
session_save_path('sessions');
session_start();
# [END getting_started_sessions_register_handler]

# [START getting_started_sessions_front_controller]
$colors = ['red', 'blue', 'green', 'yellow', 'pink'];
/**
* This is an example of a front controller for a flat file PHP site. Using a
* Static list provides security against URL injection by default.
*/
switch (@parse_url($_SERVER['REQUEST_URI'])['path']) {
case '/':
if (!isset($_SESSION['views'])) {
$_SESSION['views'] = 0;
$_SESSION['color'] = $colors[rand(0, 4)];
}
printf(
'<body bgcolor="%s">Views: %s</body>',
$_SESSION['color'],
$_SESSION['views']++
);
break;
default:
http_response_code(404);
exit('Not Found');
}
# [END getting_started_sessions_front_controller]
# [END getting_started_sessions_all]
2 changes: 2 additions & 0 deletions sessions/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
; enable the gRPC extension
extension=grpc.so
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
28 changes: 28 additions & 0 deletions sessions/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite>
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
<directory>test</directory>
</testsuite>
</testsuites>
<filter>
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
57 changes: 57 additions & 0 deletions sessions/test/DeployTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\GettingStarted\Sessions;

use Google\Cloud\TestUtils\AppEngineDeploymentTrait;
use Google\Cloud\TestUtils\TestTrait;
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;

/**
* Class DeployTest
*/
class DeployTest extends TestCase
{
use TestTrait;
use AppEngineDeploymentTrait;

public function testIndex()
{
$resp = $this->client->get('/');
$this->assertEquals('200', $resp->getStatusCode());
$this->assertContains('Views: 0', (string) $resp->getBody());

$resp = $this->client->get('/');
$this->assertEquals('200', $resp->getStatusCode());
$this->assertContains('Views: 1', (string) $resp->getBody());
}

/**
* Set up the client.
*
* @before
*/
public function setUpClient()
{
$url = self::$gcloudWrapper->getBaseUrl();
$this->client = new Client([
'base_uri' => $url,
'cookies' => true,
]);
}
}