Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Mock_Database_Schema_Skeleton { |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 4 | |
| 5 | /** |
| 6 | * @var object Database Holder |
| 7 | */ |
| 8 | public static $db; |
| 9 | |
| 10 | /** |
| 11 | * @var object Forge Holder |
| 12 | */ |
| 13 | public static $forge; |
| 14 | |
| 15 | /** |
| 16 | * @var object Driver Holder |
| 17 | */ |
| 18 | public static $driver; |
| 19 | |
| 20 | /** |
| 21 | * Initialize both database and forge components |
| 22 | */ |
| 23 | public static function init($driver) |
| 24 | { |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 25 | if (empty(self::$db) && empty(self::$forge)) |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 26 | { |
| 27 | $config = Mock_Database_DB::config($driver); |
| 28 | $connection = new Mock_Database_DB($config); |
| 29 | $db = Mock_Database_DB::DB($connection->set_dsn($driver), TRUE); |
| 30 | |
| 31 | CI_TestCase::instance()->ci_instance_var('db', $db); |
| 32 | |
dchill42 | 4f42be5 | 2012-10-21 21:31:19 -0400 | [diff] [blame] | 33 | $loader = new CI_Loader(); |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 34 | $loader->dbforge(); |
| 35 | $forge = CI_TestCase::instance()->ci_instance_var('dbforge'); |
| 36 | |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 37 | self::$db = $db; |
| 38 | self::$forge = $forge; |
| 39 | self::$driver = $driver; |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 40 | } |
| 41 | |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 42 | return self::$db; |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 43 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 44 | |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 45 | /** |
| 46 | * Create the dummy tables |
| 47 | * |
| 48 | * @return void |
| 49 | */ |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 50 | public static function create_tables() |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 51 | { |
Taufan Aditya | 17f799e | 2012-05-03 15:15:40 +0700 | [diff] [blame] | 52 | // User Table |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 53 | self::$forge->add_field(array( |
Taufan Aditya | 17f799e | 2012-05-03 15:15:40 +0700 | [diff] [blame] | 54 | 'id' => array( |
| 55 | 'type' => 'INTEGER', |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 56 | 'constraint' => 3 |
Taufan Aditya | 17f799e | 2012-05-03 15:15:40 +0700 | [diff] [blame] | 57 | ), |
| 58 | 'name' => array( |
| 59 | 'type' => 'VARCHAR', |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 60 | 'constraint' => 40 |
Taufan Aditya | 17f799e | 2012-05-03 15:15:40 +0700 | [diff] [blame] | 61 | ), |
| 62 | 'email' => array( |
| 63 | 'type' => 'VARCHAR', |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 64 | 'constraint' => 100 |
Taufan Aditya | 17f799e | 2012-05-03 15:15:40 +0700 | [diff] [blame] | 65 | ), |
Taufan Aditya | 1afd479 | 2012-05-04 20:34:30 +0700 | [diff] [blame] | 66 | 'country' => array( |
| 67 | 'type' => 'VARCHAR', |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 68 | 'constraint' => 40 |
| 69 | ) |
Taufan Aditya | 17f799e | 2012-05-03 15:15:40 +0700 | [diff] [blame] | 70 | )); |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 71 | self::$forge->add_key('id', TRUE); |
| 72 | self::$forge->create_table('user', TRUE); |
Taufan Aditya | 17f799e | 2012-05-03 15:15:40 +0700 | [diff] [blame] | 73 | |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 74 | // Job Table |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 75 | self::$forge->add_field(array( |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 76 | 'id' => array( |
Taufan Aditya | 1bfc0d9 | 2012-04-05 02:25:25 +0700 | [diff] [blame] | 77 | 'type' => 'INTEGER', |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 78 | 'constraint' => 3 |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 79 | ), |
| 80 | 'name' => array( |
| 81 | 'type' => 'VARCHAR', |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 82 | 'constraint' => 40 |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 83 | ), |
| 84 | 'description' => array( |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 85 | 'type' => 'TEXT' |
| 86 | ) |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 87 | )); |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 88 | self::$forge->add_key('id', TRUE); |
| 89 | self::$forge->create_table('job', TRUE); |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 90 | |
| 91 | // Misc Table |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 92 | self::$forge->add_field(array( |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 93 | 'id' => array( |
| 94 | 'type' => 'INTEGER', |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 95 | 'constraint' => 3 |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 96 | ), |
| 97 | 'key' => array( |
| 98 | 'type' => 'VARCHAR', |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 99 | 'constraint' => 40 |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 100 | ), |
| 101 | 'value' => array( |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 102 | 'type' => 'TEXT' |
| 103 | ) |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 104 | )); |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 105 | self::$forge->add_key('id', TRUE); |
| 106 | self::$forge->create_table('misc', TRUE); |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Create the dummy datas |
| 111 | * |
| 112 | * @return void |
| 113 | */ |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 114 | public static function create_data() |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 115 | { |
| 116 | // Job Data |
| 117 | $data = array( |
Taufan Aditya | 17f799e | 2012-05-03 15:15:40 +0700 | [diff] [blame] | 118 | 'user' => array( |
Taufan Aditya | 1afd479 | 2012-05-04 20:34:30 +0700 | [diff] [blame] | 119 | array('id' => 1, 'name' => 'Derek Jones', 'email' => 'derek@world.com', 'country' => 'US'), |
| 120 | array('id' => 2, 'name' => 'Ahmadinejad', 'email' => 'ahmadinejad@world.com', 'country' => 'Iran'), |
| 121 | array('id' => 3, 'name' => 'Richard A Causey', 'email' => 'richard@world.com', 'country' => 'US'), |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 122 | array('id' => 4, 'name' => 'Chris Martin', 'email' => 'chris@world.com', 'country' => 'UK') |
Taufan Aditya | d6b41bb | 2012-05-04 15:05:22 +0700 | [diff] [blame] | 123 | ), |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 124 | 'job' => array( |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 125 | array('id' => 1, 'name' => 'Developer', 'description' => 'Awesome job, but sometimes makes you bored'), |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 126 | array('id' => 2, 'name' => 'Politician', 'description' => 'This is not really a job'), |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 127 | array('id' => 3, 'name' => 'Accountant', 'description' => 'Boring job, but you will get free snack at lunch'), |
| 128 | array('id' => 4, 'name' => 'Musician', 'description' => 'Only Coldplay can actually called Musician') |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 129 | ), |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 130 | 'misc' => array( |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 131 | array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'), |
Andrey Andreev | 96a4ca6 | 2012-10-23 01:11:48 +0300 | [diff] [blame] | 132 | array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'), |
Andrey Andreev | 348bd1e | 2012-10-23 01:24:11 +0300 | [diff] [blame] | 133 | array('id' => 3, 'key' => 'spaces and tabs', 'value' => ' One two three tab') |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 134 | ) |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 135 | ); |
| 136 | |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 137 | foreach ($data as $table => $dummy_data) |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 138 | { |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 139 | self::$db->truncate($table); |
Taufan Aditya | 024098d | 2012-04-05 01:51:57 +0700 | [diff] [blame] | 140 | |
| 141 | foreach ($dummy_data as $single_dummy_data) |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 142 | { |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 143 | self::$db->insert($table, $single_dummy_data); |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 147 | |
Dan Bernardic | d24652d | 2015-01-11 22:01:23 -0600 | [diff] [blame] | 148 | } |