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
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.