Skip to content

Commit 17608b0

Browse files
author
Greg Bowler
committed
Tidy serve/test scripts for #233
1 parent 49e7550 commit 17608b0

File tree

3 files changed

+71
-61
lines changed

3 files changed

+71
-61
lines changed

bin/serve

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,35 @@ use Symfony\Component\Console\Input\InputArgument;
1515
use Symfony\Component\Console\Input\InputOption;
1616
use Symfony\Component\Console\Input\ArgvInput;
1717

18-
$cwd = getcwd();
19-
$vendorAutoLoadFile = realpath(__DIR__ . "/../vendor/autoload.php");
18+
// PHP.Gt should always be loaded as a Composer dependency. The vendor directory
19+
// will be in the project's root directory — not PHP.Gt's root directory.
20+
// We first need to find the root directory by working up the directory tree
21+
// until the vendor directory is found.
22+
$vendorDirectory = null;
23+
while(true) {
24+
$cwd = getcwd();
25+
if(is_dir("$cwd/vendor")) {
26+
$vendorDirectory = "$cwd/vendor";
27+
break;
28+
}
29+
chdir("..");
30+
31+
if(getcwd() === $cwd) {
32+
// We have reached the root directory of the filesystem without finding
33+
// the vendor directory.
34+
echo "No vendor directory found.\n"
35+
. "Have you performed a composer install?\n\n"
36+
. "See http://www.php.gt/docs/installation for more information.\n";
37+
exit(1);
38+
}
39+
}
40+
41+
$vendorAutoLoadFile = realpath("$vendorDirectory/autoload.php");
2042
if(!file_exists($vendorAutoLoadFile)) {
21-
// If this script exists within the vendor/brightflair/php.gt directory:
22-
$vendorAutoLoadFile = realpath(__DIR__ . "/../../../autoload.php");
23-
$cwd = realpath(__DIR__ . "/../../../..");
43+
echo "No Composer autoloader script found.\n"
44+
. "Have you performed a composer install?\n\n"
45+
. "See http://www.php.gt/docs/installation for more information.\n";
46+
exit(1);
2447
}
2548
require($vendorAutoLoadFile);
2649

@@ -47,25 +70,10 @@ try {
4770
if(!is_null($overrideApproot)) {
4871
$approot = "$approot/$overrideApproot";
4972
}
50-
// Change autoloaders so we are definitely using the current project's
51-
// dependencies (rather than the global PHP.Gt dependencies).
52-
$projectAutoloader = "$approot/vendor/autoload.php";
53-
if(!file_exists($projectAutoloader)) {
54-
// We must be running the bin/serve script manually...
55-
$projectAutoloader = $vendorAutoLoadFile;
56-
}
57-
58-
if(!file_exists($projectAutoloader)) {
59-
echo "\nYour project must require PHP.Gt "
60-
. "as a dependency using composer.\n\n"
61-
. "See http://www.php.gt/docs/installation for more information.";
62-
exit(1);
63-
}
64-
73+
6574
$xdebug = $input->getOption("xdebug");
6675
ini_set("xdebug.idekey", $xdebug);
67-
68-
require($projectAutoloader);
76+
6977
new Server($input);
7078
exit(0);
7179
}
@@ -76,4 +84,4 @@ catch(Exception $e) {
7684
echo strip_tags($definition->asText());
7785
echo "\n";
7886
exit(1);
79-
}
87+
}

bin/test

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,35 @@ use Symfony\Component\Console\Input\InputArgument;
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Input\ArgvInput;
1919

20-
$cwd = getcwd();
21-
$vendorAutoLoadFile = realpath(__DIR__ . "/../vendor/autoload.php");
20+
// PHP.Gt should always be loaded as a Composer dependency. The vendor directory
21+
// will be in the project's root directory — not PHP.Gt's root directory.
22+
// We first need to find the root directory by working up the directory tree
23+
// until the vendor directory is found.
24+
$vendorDirectory = null;
25+
while(true) {
26+
$cwd = getcwd();
27+
if(is_dir("$cwd/vendor")) {
28+
$vendorDirectory = "$cwd/vendor";
29+
break;
30+
}
31+
chdir("..");
32+
33+
if(getcwd() === $cwd) {
34+
// We have reached the root directory of the filesystem without finding
35+
// the vendor directory.
36+
echo "No vendor directory found.\n"
37+
. "Have you performed a composer install?\n\n"
38+
. "See http://www.php.gt/docs/installation for more information.\n";
39+
exit(1);
40+
}
41+
}
42+
43+
$vendorAutoLoadFile = realpath("$vendorDirectory/autoload.php");
2244
if(!file_exists($vendorAutoLoadFile)) {
23-
// If this script exists within the vendor/brightflair/php.gt directory:
24-
$vendorAutoLoadFile = realpath(__DIR__ . "/../../../autoload.php");
25-
$cwd = realpath(__DIR__ . "/../../..");
45+
echo "No Composer autoloader script found.\n"
46+
. "Have you performed a composer install?\n\n"
47+
. "See http://www.php.gt/docs/installation for more information.\n";
48+
exit(1);
2649
}
2750
require($vendorAutoLoadFile);
2851

@@ -46,21 +69,6 @@ try {
4669
$approot = $input->getOption("approot");
4770
$type = $input->getOption("type");
4871

49-
// Change autoloaders so we are definitely using the current project's
50-
// dependencies (rather than the global PHP.Gt dependencies).
51-
$projectAutoloader = "$approot/vendor/autoload.php";
52-
if(!file_exists($projectAutoloader)) {
53-
// We must be running the bin/serve script manually...
54-
$projectAutoloader = $vendorAutoLoadFile;
55-
}
56-
57-
if(!file_exists($projectAutoloader)) {
58-
echo "\nYour project must require PHP.Gt "
59-
. "as a dependency using composer.\n\n"
60-
. "See http://www.php.gt/docs/installation for more information.";
61-
exit(1);
62-
}
63-
require($projectAutoloader);
6472
new TestRunner($approot, $type);
6573
exit(0);
6674
}
@@ -74,4 +82,4 @@ catch(Exception $e) {
7482
echo strip_tags($definition->asText());
7583
echo "\n";
7684
exit(1);
77-
}
85+
}

src/Cli/TestRunner.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class TestRunner {
2424

2525
private $descriptorSpec = [
2626
0 => ["pipe", "r"],
27-
1 => ["pipe", "w"],
28-
2 => ["pipe", "w"],
27+
1 => ["pipe", "r"],
28+
2 => ["pipe", "r"],
2929
];
3030

3131
public function __construct($approot, $type) {
@@ -115,26 +115,20 @@ private function testUnit() {
115115
*/
116116
private function testAcceptance() {
117117
$result = 0;
118-
$baseCwd = getcwd();
119-
$gtroot = Path::get(Path::GTROOT);
120-
$root = Path::get(Path::ROOT);
121-
$testPath = Path::fixCase($root . "/test/Acceptance");
118+
$rememberCwd = getcwd();
119+
120+
$testPath = Path::fixCase(getcwd() . "/test/Acceptance");
122121

123122
if(!is_dir($testPath)) {
124123
return 0;
125124
}
126125

127-
$testWebroot = "$testPath/www";
128-
if(!is_dir($testWebroot)) {
129-
mkdir($testWebroot, 0775, true);
130-
}
131-
132-
$serverCommand = "$gtroot/bin/serve --approot=$root --port=8089";
126+
$serverCommand = "./vendor/bin/serve";
133127
$server = proc_open($serverCommand, $this->descriptorSpec, $pipes);
134128

129+
$testExec = realpath("./vendor/bin/behat");
135130
chdir($testPath);
136-
$behat = new \Behat\Behat\Console\BehatApplication(null);
137-
$behat->run();
131+
passthru($testExec, $result);
138132

139133
// Inbuilt server spawns child processes that need killing.
140134
$status = proc_get_status($server);
@@ -152,16 +146,16 @@ private function testAcceptance() {
152146
posix_kill($p, SIGKILL);
153147
}
154148

155-
if($exitCode == 0) {
149+
if($result === 0) {
156150
$this->countAcceptance++;
157151
}
158152
else {
159153
$result = self::ERRORLEVEL_ACCEPTANCE;
160154
}
161155

162156
// Reset the cwd.
163-
chdir($baseCwd);
157+
chdir($rememberCwd);
164158
return $result;
165159
}
166160

167-
}#
161+
}#

0 commit comments

Comments
 (0)