Skip to content

Commit b7e0d23

Browse files
committed
Added update command and create-fieldsini command.
1 parent ae4b21e commit b7e0d23

10 files changed

+866
-56
lines changed

scaffold_template/bin/create-delegate.sh

100644100755
File mode changed.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
3+
TABLES_DIR=$SCRIPTPATH/../app/tables
4+
[ -d "$TABLES_DIR" ] || mkdir "$TABLES_DIR"
5+
6+
status=`bash $SCRIPTPATH/mysql.server.sh status`
7+
if [[ $status == *"ERROR!"* ]]; then
8+
$SCRIPTPATH/mysql.server.sh start || (echo "Failed to start mysql" && exit 1)
9+
function finish() {
10+
$SCRIPTPATH/mysql.server stop
11+
}
12+
trap finish EXIT
13+
fi
14+
bash $SCRIPTPATH/php.sh $SCRIPTPATH/inc/create-fieldsini.php "$@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
chdir(dirname(__FILE__).'/../../app');
3+
require_once 'xataface/public-api.php';
4+
df_init(dirname(__FILE__).'/../../app/index.php', 'xataface');
5+
import(XFROOT.'Dataface/Table.php');
6+
7+
if (count($argv) < 2) {
8+
fwrite(STDERR, "create-fieldsini requires at least 1 arg: The table name\n");
9+
exit(1);
10+
}
11+
$tableName = $argv[1];
12+
if (!Dataface_Table::tableExists($tableName)) {
13+
fwrite(STDERR, "The table $tableName does not exist.\n");
14+
exit(1);
15+
}
16+
if (!file_exists('tables')) {
17+
mkdir('tables');
18+
}
19+
$tableDirectory = 'tables/'.basename($tableName);
20+
if (!file_exists($tableDirectory)) {
21+
mkdir($tableDirectory);
22+
}
23+
$iniFile = $tableDirectory.'/fields.ini';
24+
$iniPhpFile = $tableDirectory.'/fields.ini.php';
25+
if (file_exists($iniFile)) {
26+
fwrite(STDERR, "The fields.ini file for table $tableName already exists.\nFound $iniFile\n");
27+
exit(1);
28+
}
29+
if (file_exists($iniPhpFile)) {
30+
fwrite(STDERR, "The fields.ini file for table $tableName already exists.\nFound $iniPhpFile\n");
31+
exit(1);
32+
}
33+
34+
$table = Dataface_Table::loadTable($tableName);
35+
$fieldNames = array_keys($table->fields());
36+
$content = ";<?php exit;\n";
37+
foreach ($fieldNames as $fieldName) {
38+
$content .= "[".$fieldName."]\n\n";
39+
}
40+
41+
file_put_contents($iniPhpFile, $content);
42+
echo "Created $iniPhpFile\n";

scaffold_template/bin/mysqldump.sh

100644100755
+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#!/bin/sh
22
export PATH=/Applications/XAMPP/xamppfiles/bin:$PATH
33
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
4+
status=`bash $SCRIPTPATH/mysql.server.sh status`
5+
if [[ $status == *"ERROR!"* ]]; then
6+
$SCRIPTPATH/mysql.server.sh start || (echo "Failed to start mysql" && exit 1)
7+
function finish() {
8+
$SCRIPTPATH/mysql.server stop
9+
}
10+
trap finish EXIT
11+
fi
412
scaffolddir="$SCRIPTPATH/..";
513
ACMD="$1"
614
mysqldump --socket="$scaffolddir"/tmp/mysql.sock -u `php $SCRIPTPATH/print_config_var.php _database.user` `php $SCRIPTPATH/print_config_var.php _database.name`

scaffold_template/bin/print_config_var.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
}
4444
}
4545
}
46-
$conf = array_merge_recursive($tmp, $conf);
46+
$conf = array_replace_recursive($tmp, $conf);
4747
}
4848
}
4949

@@ -55,6 +55,19 @@
5555
$key = $argv[1];
5656
if (strpos($key, '.') !== false) {
5757
list($key1, $key2) = explode('.', $key);
58+
if (is_array($key1)) {
59+
throw new Exception("Key1 is array");
60+
}
61+
if (is_array($key2)) {
62+
throw new Exception('Key 2 is array');
63+
}
64+
if (is_array($conf[$key1][$key2])) {
65+
ob_start();
66+
print_r($conf[$key1][$key2]);
67+
$arr = ob_get_contents();
68+
ob_end_clean();
69+
throw new Exception('conf[key1][key2] is array '.$arr);
70+
}
5871
echo @$conf[$key1][$key2];
5972

6073
} else {

tests/tests/CLIServerTest.php

+53
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class CLIServerTest extends PHPUnit_TestCase {
77
private $xfServers;
88
private $server;
99
private $configFilePath = '.test-servers-config.ini';
10+
private $prefsPath = '.test-servers-prefs.json';
1011
private $xatafacePath;
1112
private $serverRunning;
1213

@@ -36,6 +37,11 @@ function setUp() {
3637
if (file_exists($this->configFilePath)) {
3738
unlink($this->configFilePath);
3839
}
40+
$prefsPath = $this->prefsPath;
41+
if (file_exists($prefsPath)) {
42+
unlink($prefsPath);
43+
}
44+
$this->xfServers->setPreferencesPath($prefsPath);
3945

4046
$httpdConf = 'server-httpd.conf';
4147
if (file_exists($httpdConf)) {
@@ -56,9 +62,32 @@ function setUp() {
5662
'mysqlRootPassword' => '',
5763
'configPath' => $httpdConf
5864
));
65+
5966
$this->serverRunning = $this->server->isRunning();
6067

6168

69+
$configFileContents =
70+
<<<END
71+
[default]
72+
startCommand=sudo apachectl start
73+
stopCommand=sudo apachectl stop
74+
restartCommand=sudo apachectl restart
75+
statusCommand=sudo apachectl status
76+
mysqlCommand=mysql
77+
mysqlRootUser=root
78+
mysqlRootPassword=
79+
configPath={$httpdConf}
80+
81+
END;
82+
file_put_contents($this->configFilePath, $configFileContents);
83+
84+
}
85+
86+
function testPrefsPath() {
87+
$this->xfServers->setPreferencesPath(null);
88+
$this->assertEquals($_SERVER['HOME'] . DIRECTORY_SEPARATOR . '.xataface' . DIRECTORY_SEPARATOR . 'server-prefs.json', $this->xfServers->preferencesPath());
89+
$this->xfServers->setPreferencesPath($this->prefsPath);
90+
$this->assertEquals($this->prefsPath, $this->xfServers->preferencesPath());
6291
}
6392

6493
function testIsRunning() {
@@ -152,8 +181,32 @@ function testLoadVirtualHosts() {
152181
$this->assertEquals($expectedRaw, $h1->getRaw());
153182
$this->assertEquals(0, $h1->getStartLine());
154183
$this->assertEquals(6, $h1->getEndLine());
184+
$this->assertEquals(
185+
<<<END
186+
<VirtualHost *:80>
187+
DocumentRoot "/www/example2"
188+
ServerName www.example.org
189+
#XATAFACE#
190+
# Other directives here
191+
</VirtualHost>
192+
END,
193+
$h1->toString()
194+
);
195+
196+
155197
}
156198

199+
function testSetServer() {
200+
$this->assertEquals(null, $this->xfServers->getCurrentServer());
201+
$this->xfServers->setCurrentServer('notexists');
202+
$this->assertEquals(null, $this->xfServers->getCurrentServer());
203+
$this->xfServers->setCurrentServer(null);
204+
$this->assertEquals(null, $this->xfServers->getCurrentServer());
205+
$this->xfServers->setCurrentServer('default');
206+
$this->assertEquals('default', $this->xfServers->getCurrentServer());
207+
$server = $this->xfServers->getServerByName($this->xfServers->getCurrentServer());
208+
$this->assertEquals('default', $server->getName());
209+
}
157210

158211
function tearDown() {
159212
if ($this->serverRunning and !$this->server->isRunning()) {

tests/tests/XFProjectTest.php

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
require_once 'PHPUnit.php';
3+
4+
class XFProjectTest extends PHPUnit_TestCase {
5+
6+
private $project;
7+
8+
function XFProjectTest($name = 'XFProjectTest') {
9+
$this->PHPUnit_TestCase($name);
10+
11+
}
12+
13+
public function __construct($name = 'XFProjectTest') {
14+
$this->XFProjectTest($name);
15+
}
16+
17+
18+
19+
function setUp() {
20+
$xatafacePath = getenv('XATAFACE', true);
21+
$this->xatafacePath = $xatafacePath;
22+
if (!$xatafacePath) {
23+
throw new Exception("Xataface path not set. Please set XATAFACE environment varable to point to xataface directory");
24+
}
25+
require_once $xatafacePath . DIRECTORY_SEPARATOR . 'tools' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'XFProject.class.php';
26+
require_once $xatafacePath . DIRECTORY_SEPARATOR . 'tools' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'XFServers.class.php';
27+
$baseDir = dirname(dirname(__FILE__));
28+
if (!file_exists($baseDir . DIRECTORY_SEPARATOR . 'www')) {
29+
throw new Exception("XFProjectTest cannot be run in this context. This file must be in root of Xataface app");
30+
}
31+
if (!file_exists($baseDir . DIRECTORY_SEPARATOR . 'bin')) {
32+
throw new Exception("XFProjectTest cannot be run in this context. This file must be in root of Xataface app");
33+
}
34+
if (!file_exists($baseDir . DIRECTORY_SEPARATOR . 'etc')) {
35+
throw new Exception("XFProjectTest cannot be run in this context. This file must be in root of Xataface app");
36+
}
37+
if (!file_exists($baseDir . DIRECTORY_SEPARATOR . 'tmp')) {
38+
throw new Exception("XFProjectTest cannot be run in this context. This file must be in root of Xataface app");
39+
}
40+
$this->project = new XFProject($baseDir);
41+
42+
43+
}
44+
45+
function test_get_config_var() {
46+
$this->assertEquals('testapp', $this->project->get_config_var('_database.name'));
47+
48+
49+
}
50+
51+
function test_mysqldump() {
52+
$file = 'dump.sql';
53+
if (file_exists($file)) {
54+
unlink($file);
55+
}
56+
$this->project->mysqldump($file);
57+
$this->assertTrue(file_exists($file));
58+
$contents = file_get_contents($file);
59+
$this->assertTrue(strpos($contents, 'DROP TABLE IF EXISTS `test`') !== false, 'Dump file missing DROP TABLE call');
60+
}
61+
62+
function test_installOnServer() {
63+
$xamppPath = '/Applications/XAMPP';
64+
if (!file_exists($xamppPath)) {
65+
echo "Skipping test_installOnServer because XAMPP could not be found\n";
66+
return;
67+
}
68+
$dbName = $this->project->get_config_var('_database.name');
69+
if ($dbName !== 'testapp') {
70+
echo "Skipping test_installOnServer because the current app is not named testapp\n";
71+
return;
72+
}
73+
$dbUser = $this->project->get_config_var('_database.user');
74+
if ($dbUser !== 'testapp') {
75+
echo "Skipping test_installOnServer because the current app user is not testapp\n";
76+
return;
77+
}
78+
79+
$httpdConf = 'server-httpd.conf';
80+
if (file_exists($httpdConf)) {
81+
unlink($httpdConf);
82+
}
83+
touch($httpdConf);
84+
$servers = new XFServers();
85+
86+
$server = new XFServer($servers, 'default', array(
87+
'startCommand' => 'sudo apachectl start',
88+
'stopCommand' => 'sudo apachectl stop',
89+
'restartCommand' => 'sudo apachectl restart',
90+
'statusCommand' => 'sudo apachectl status',
91+
'mysqlCommand' => 'mysql',
92+
'mysqlRootUser' => 'root',
93+
'mysqlRootPassword' => '',
94+
'configPath' => $httpdConf
95+
));
96+
if ($server->databaseUserExists($dbUser)) {
97+
$server->dropDatabaseUser($dbUser);
98+
}
99+
100+
$server->executeSQLQuery("DROP DATABASE `testapp`");
101+
102+
$this->project->installOnServer($server);
103+
104+
105+
106+
107+
108+
109+
}
110+
111+
112+
113+
function tearDown() {
114+
115+
}
116+
117+
118+
119+
}

0 commit comments

Comments
 (0)