forked from clifgriffin/shopp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchemaTests.php
executable file
·47 lines (36 loc) · 1.17 KB
/
SchemaTests.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/**
* Validate Shopp Schema file
*/
class SchemaTests extends ShoppTestCase {
function test_shopp_schema () {
$this->AssertTrue(file_exists(SHOPP_DBSCHEMA));
// grab the schema
ob_start();
include(SHOPP_DBSCHEMA);
$schema = ob_get_contents();
ob_end_clean();
// Strip SQL comments
$schema = preg_replace('/--\s?(.*?)\n/',"\n",$schema);
// get a list of tables from the schema
$this->AssertTrue((bool) preg_match_all("|CREATE TABLE ([^ ]*)|", $schema, $matches));
$this->AssertTrue(is_array($matches) && isset($matches[1]) && is_array($matches[1]));
$tables = $matches[1];
// replace table names with testing table names
$statements = explode(';', $schema);
$statements = preg_replace('/('.implode('|', $tables).')[a-z]{0}/', 'schematest_$1', $statements);
// test run through schema
$checks = true;
foreach ( $statements as $i => $statement ) {
$statement = trim(preg_replace('/\s+/', ' ', $statement));
if ( $statement ) {
$checks = $checks && sDB::query($statement);
}
}
// cleanup
foreach ( $tables as $table ) {
$checks = $checks && sDB::query("DROP TABLE schematest_$table");
}
$this->AssertTrue($checks);
}
}