1
|
|
|
<?php |
2
|
|
|
$vendorDir = __DIR__ . '/../vendor'; |
3
|
|
|
|
4
|
|
|
if (!@include($vendorDir . '/autoload.php')) { |
5
|
|
|
die("You must set up the project dependencies, run the following commands: |
6
|
|
|
wget http://getcomposer.org/composer.phar |
7
|
|
|
php composer.phar install |
8
|
|
|
"); |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
class FwkDbTestUtil |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
private static $created = false; |
14
|
|
|
|
15
|
|
|
public static function createTestDb(\Fwk\Db\Connection $connection) |
16
|
|
|
{ |
17
|
|
|
if(self::$created == true) |
|
|
|
|
18
|
|
|
|
19
|
|
|
return; |
20
|
|
|
|
21
|
|
|
$schema = $connection->getSchema(); |
22
|
|
|
$tbl = $schema->createTable('fwkdb_test_users'); |
23
|
|
|
$tbl->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true)); |
24
|
|
|
$tbl->addColumn("username", "string", array("length" => 32)); |
25
|
|
|
$tbl->addColumn("phone_id", "integer", array("unsigned" => true, "notnull" => false)); |
26
|
|
|
$tbl->addColumn('created_at', 'datetime', array('notnull' => false)); |
27
|
|
|
$tbl->addColumn('updated_at', 'datetime', array('notnull' => false)); |
28
|
|
|
$tbl->setPrimaryKey(array("id")); |
29
|
|
|
$tbl->addUniqueIndex(array("username")); |
30
|
|
|
|
31
|
|
|
$tbl = $schema->createTable('fwkdb_test_emails'); |
32
|
|
|
$tbl->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true)); |
33
|
|
|
$tbl->addColumn("email", "string", array("length" => 255)); |
34
|
|
|
$tbl->addColumn("verified", "integer", array("length" => 1, "unsigned" => true)); |
35
|
|
|
$tbl->setPrimaryKey(array("id")); |
36
|
|
|
$tbl->addUniqueIndex(array("email")); |
37
|
|
|
|
38
|
|
|
$tbl = $schema->createTable('fwkdb_test_users_emails'); |
39
|
|
|
$tbl->addColumn("user_id", "integer", array("unsigned" => true)); |
40
|
|
|
$tbl->addColumn("email_id", "integer", array("unsigned" => true)); |
41
|
|
|
$tbl->setPrimaryKey(array("user_id", "email_id")); |
42
|
|
|
|
43
|
|
|
$tbl = $schema->createTable('fwkdb_test_users_metas'); |
44
|
|
|
$tbl->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true)); |
45
|
|
|
$tbl->addColumn("user_id", "integer", array("unsigned" => true)); |
46
|
|
|
$tbl->addColumn("name", "string", array("length" => 50)); |
47
|
|
|
$tbl->addColumn("value", "string"); |
48
|
|
|
$tbl->addIndex(array('user_id')); |
49
|
|
|
$tbl->setPrimaryKey(array("id")); |
50
|
|
|
|
51
|
|
|
$tbl = $schema->createTable('fwkdb_test_phones'); |
52
|
|
|
$tbl->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true)); |
53
|
|
|
$tbl->addColumn("number", "string", array("length" => 50)); |
54
|
|
|
$tbl->setPrimaryKey(array("id")); |
55
|
|
|
|
56
|
|
|
$connection->connect(); |
57
|
|
|
$queries = $schema->toSql($connection->getDriver()->getDatabasePlatform()); |
58
|
|
|
foreach ($queries as $query) { |
59
|
|
|
$connection->getDriver()->exec($query); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
self::$created = true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function dropTestDb(\Fwk\Db\Connection $connection) |
66
|
|
|
{ |
67
|
|
|
if(self::$created == false) |
|
|
|
|
68
|
|
|
return; |
69
|
|
|
|
70
|
|
|
$schema = $connection->getSchema(); |
71
|
|
|
$queries = $schema->toDropSql($connection->getDriver()->getDatabasePlatform()); |
72
|
|
|
foreach ($queries as $query) { |
73
|
|
|
$connection->getDriver()->exec($query); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
self::$created = false; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.