Skip to content

Commit 8d271d9

Browse files
author
Takashi Matsuo
committed
Added LocalTestTrait.
1 parent 5a2b2b0 commit 8d271d9

File tree

5 files changed

+131
-53
lines changed

5 files changed

+131
-53
lines changed

appengine/standard/modules/tests/LocalTest.php

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,11 @@
1616
*/
1717
namespace Google\Cloud\Test;
1818

19-
use GuzzleHttp\Client;
20-
use Google\Cloud\TestUtils\GaeApp;
19+
use Google\Cloud\TestUtils\LocalTestTrait;
2120

2221
class LocalTest extends \PHPUnit_Framework_TestCase
2322
{
24-
private static $gaeApp;
25-
26-
private $client;
27-
28-
const PROJECT_ENV = 'GOOGLE_PROJECT_ID';
29-
const VERSION_ENV = 'GOOGLE_VERSION_ID';
30-
31-
private static function getVersion()
32-
{
33-
return "modules-" . getenv(self::VERSION_ENV);
34-
}
35-
36-
private static function getTargetDir()
37-
{
38-
return realpath(__DIR__ . '/../');
39-
}
40-
41-
public static function setUpBeforeClass()
42-
{
43-
self::markTestIncomplete(
44-
'dev_appserver.py is not working for us now');
45-
$project_id = getenv(self::PROJECT_ENV);
46-
$e2e_test_version = getenv(self::VERSION_ENV);
47-
if ($project_id === false) {
48-
self::fail('Please set ' . self::PROJECT_ENV . ' env var.');
49-
}
50-
if ($e2e_test_version === false) {
51-
self::fail('Please set ' . self::VERSION_ENV . ' env var.');
52-
}
53-
self::$gaeApp = new GaeApp(
54-
$project_id,
55-
$e2e_test_version,
56-
self::getTargetDir());
57-
if (self::$gaeApp->run() === false) {
58-
self::fail('dev_appserver failed.');
59-
}
60-
}
61-
62-
public static function tearDownAfterClass()
63-
{
64-
self::$gaeApp->stop();
65-
}
66-
67-
public function setUp()
68-
{
69-
$url = self::$gaeApp->getLocalBaseUrl();
70-
$this->client = new Client(['base_uri' => $url]);
71-
}
23+
use LocalTestTrait;
7224

7325
public function testIndex()
7426
{

appengine/standard/modules/tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
require __DIR__ . '/../vendor/autoload.php';
1818
require_once __DIR__ . '/../../../../testing/E2EDeploymentTrait.php';
1919
require_once __DIR__ . '/../../../../testing/GaeApp.php';
20+
require_once __DIR__ . '/../../../../testing/LocalTestTrait.php';

testing/E2EDeploymentTrait.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ trait E2EDeploymentTrait
3333
public static $projectEnv = 'GOOGLE_PROJECT_ID';
3434
public static $versionEnv = 'GOOGLE_VERSION_ID';
3535

36-
public static function setUpBeforeClass()
36+
/**
37+
* @beforeClass
38+
*/
39+
public static function deployApp()
3740
{
3841
$project_id = getenv(self::$projectEnv);
3942
$e2e_test_version = getenv(self::$versionEnv);
@@ -52,7 +55,10 @@ public static function setUpBeforeClass()
5255
}
5356
}
5457

55-
public static function tearDownAfterClass()
58+
/**
59+
* @afterClass
60+
*/
61+
public static function deleteApp()
5662
{
5763
self::$gaeApp->delete();
5864
}

testing/GaeApp.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace Google\Cloud\TestUtils;
1919

20+
use Symfony\Component\Process\Process;
21+
2022
/**
2123
* Class GaeApp
2224
* @package Google\Cloud\TestUtils
@@ -29,6 +31,7 @@ class GaeApp
2931
private $version;
3032
private $port;
3133
private $deployed;
34+
private $isRunning;
3235
private $process;
3336
private $dir;
3437

@@ -47,7 +50,7 @@ private function execWithRetry($cmd, $retry = self::DEFAULT_RETRY)
4750
exec($cmd, $output, $ret);
4851
if ($ret === 0) {
4952
return true;
50-
} else if ($i <= $retry) {
53+
} elseif ($i <= $retry) {
5154
$this->errorLog('Retrying the command: ' . $cmd);
5255
}
5356
}
@@ -77,6 +80,7 @@ public function __construct(
7780
}
7881
$this->version = $version;
7982
$this->deployed = false;
83+
$this->isRunning = false;
8084
$this->dir = $dir;
8185
$this->port = $port;
8286
}
@@ -117,6 +121,41 @@ public function deploy(
117121
return $ret;
118122
}
119123

124+
/**
125+
* Runs the app with dev_appserver.
126+
*
127+
* @return bool true if the app is running, otherwise false
128+
*/
129+
public function run()
130+
{
131+
$options = '--port ' . $this->port
132+
. ' --php_executable_path ' . PHP_BINARY;
133+
$cmd = 'dev_appserver.py --port ' . $this->port
134+
. ' --php_executable_path ' . PHP_BINARY
135+
. ' ' . $this->dir;
136+
$this->process = new Process($cmd);
137+
$this->process->start();
138+
sleep(3);
139+
if (! $this->process->isRunning()) {
140+
$this->errorLog('dev_appserver failed to run.');
141+
$this->errorLog($this->process->getErrorOutput());
142+
return false;
143+
}
144+
$this->isRunning = true;
145+
return true;
146+
}
147+
148+
/**
149+
* Stops the dev_appserver.
150+
*/
151+
public function stop()
152+
{
153+
if ($this->process->isRunning()) {
154+
$this->process->stop();
155+
}
156+
$this->isRunning = false;
157+
}
158+
120159
/**
121160
* Deletes the deployed app.
122161
*
@@ -135,9 +174,25 @@ public function delete(
135174
return $ret;
136175
}
137176

177+
/**
178+
* Returns the base URL of the local dev_appserver.
179+
*
180+
* @return mixed returns the base URL of the running app, or false when
181+
* the app is not running
182+
*/
183+
public function getLocalBaseUrl()
184+
{
185+
if (! $this->isRunning) {
186+
$this->errorLog('The app is not running.');
187+
return false;
188+
}
189+
return 'http://localhost:' . $this->port;
190+
}
191+
138192
/**
139193
* Returns the base URL of the deployed app.
140194
*
195+
* @param string $module optional
141196
* @return mixed returns the base URL of the deployed app, or false when
142197
* the app is not deployed.
143198
*/

testing/LocalTestTrait.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
/*
3+
* Copyright 2016 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\TestUtils;
19+
20+
use GuzzleHttp\Client;
21+
22+
/**
23+
* Class LocalTestTrait
24+
* @package Google\Cloud\TestUtils
25+
*
26+
* Use this trait to use dev_appserver for testing.
27+
*/
28+
trait LocalTestTrait
29+
{
30+
private static $gaeApp;
31+
private $client;
32+
33+
/**
34+
* @beforeClass
35+
*/
36+
public static function startServer()
37+
{
38+
self::markTestIncomplete(
39+
'dev_appserver.py is not working for us now.'
40+
. ' TODO: Make this work with specifying --php_gae_extension_path'
41+
);
42+
self::$gaeApp = new GaeApp('', '');
43+
if (self::$gaeApp->run() === false) {
44+
self::fail('dev_appserver failed');
45+
}
46+
}
47+
48+
/**
49+
* @afterClass
50+
*/
51+
public static function stopServer()
52+
{
53+
self::$gaeApp->stop();
54+
}
55+
56+
/**
57+
* @before
58+
*/
59+
public function setUpClient()
60+
{
61+
$url = self::$gaeApp->getLocalBaseUrl();
62+
$this->client = new Client(['base_uri' => $url]);
63+
}
64+
}

0 commit comments

Comments
 (0)