blob: 888236ff36d9f7c47fca4a4559cf5df744d3fe27 [file] [log] [blame]
Taufan Aditya5b421662012-04-05 01:24:50 +07001<?php
2
3class Mock_Database_Schema_Skeleton {
Taufan Aditya55f622f2012-04-05 19:01:34 +07004
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 Bernardicd24652d2015-01-11 22:01:23 -060025 if (empty(self::$db) && empty(self::$forge))
Taufan Aditya55f622f2012-04-05 19:01:34 +070026 {
Andrey Andreeve7068ed2015-01-20 11:32:08 +020027 // E_DEPRECATED notices thrown by mysql_connect(), mysql_pconnect()
28 // on PHP 5.5+ cause the tests to fail
29 if ($driver === 'mysql' && version_compare(PHP_VERSION, '5.5', '>='))
30 {
31 error_reporting(E_ALL & ~E_DEPRECATED);
32 }
33
Taufan Aditya55f622f2012-04-05 19:01:34 +070034 $config = Mock_Database_DB::config($driver);
35 $connection = new Mock_Database_DB($config);
36 $db = Mock_Database_DB::DB($connection->set_dsn($driver), TRUE);
37
38 CI_TestCase::instance()->ci_instance_var('db', $db);
39
dchill424f42be52012-10-21 21:31:19 -040040 $loader = new CI_Loader();
Taufan Aditya55f622f2012-04-05 19:01:34 +070041 $loader->dbforge();
42 $forge = CI_TestCase::instance()->ci_instance_var('dbforge');
43
Dan Bernardicd24652d2015-01-11 22:01:23 -060044 self::$db = $db;
45 self::$forge = $forge;
46 self::$driver = $driver;
Taufan Aditya55f622f2012-04-05 19:01:34 +070047 }
48
Dan Bernardicd24652d2015-01-11 22:01:23 -060049 return self::$db;
Taufan Aditya55f622f2012-04-05 19:01:34 +070050 }
Andrey Andreevf243ce12012-06-09 23:34:21 +030051
Taufan Aditya5b421662012-04-05 01:24:50 +070052 /**
53 * Create the dummy tables
54 *
55 * @return void
56 */
Taufan Aditya55f622f2012-04-05 19:01:34 +070057 public static function create_tables()
Taufan Aditya5b421662012-04-05 01:24:50 +070058 {
Taufan Aditya17f799e2012-05-03 15:15:40 +070059 // User Table
Dan Bernardicd24652d2015-01-11 22:01:23 -060060 self::$forge->add_field(array(
Taufan Aditya17f799e2012-05-03 15:15:40 +070061 'id' => array(
62 'type' => 'INTEGER',
Andrey Andreevf243ce12012-06-09 23:34:21 +030063 'constraint' => 3
Taufan Aditya17f799e2012-05-03 15:15:40 +070064 ),
65 'name' => array(
66 'type' => 'VARCHAR',
Andrey Andreevf243ce12012-06-09 23:34:21 +030067 'constraint' => 40
Taufan Aditya17f799e2012-05-03 15:15:40 +070068 ),
69 'email' => array(
70 'type' => 'VARCHAR',
Andrey Andreevf243ce12012-06-09 23:34:21 +030071 'constraint' => 100
Taufan Aditya17f799e2012-05-03 15:15:40 +070072 ),
Taufan Aditya1afd4792012-05-04 20:34:30 +070073 'country' => array(
74 'type' => 'VARCHAR',
Andrey Andreevf243ce12012-06-09 23:34:21 +030075 'constraint' => 40
76 )
Taufan Aditya17f799e2012-05-03 15:15:40 +070077 ));
Dan Bernardicd24652d2015-01-11 22:01:23 -060078 self::$forge->add_key('id', TRUE);
79 self::$forge->create_table('user', TRUE);
Taufan Aditya17f799e2012-05-03 15:15:40 +070080
Taufan Aditya5b421662012-04-05 01:24:50 +070081 // Job Table
Dan Bernardicd24652d2015-01-11 22:01:23 -060082 self::$forge->add_field(array(
Taufan Aditya5b421662012-04-05 01:24:50 +070083 'id' => array(
Taufan Aditya1bfc0d92012-04-05 02:25:25 +070084 'type' => 'INTEGER',
Andrey Andreevf243ce12012-06-09 23:34:21 +030085 'constraint' => 3
Taufan Aditya5b421662012-04-05 01:24:50 +070086 ),
87 'name' => array(
88 'type' => 'VARCHAR',
Andrey Andreevf243ce12012-06-09 23:34:21 +030089 'constraint' => 40
Taufan Aditya5b421662012-04-05 01:24:50 +070090 ),
91 'description' => array(
Andrey Andreevf243ce12012-06-09 23:34:21 +030092 'type' => 'TEXT'
93 )
Taufan Aditya5b421662012-04-05 01:24:50 +070094 ));
Dan Bernardicd24652d2015-01-11 22:01:23 -060095 self::$forge->add_key('id', TRUE);
96 self::$forge->create_table('job', TRUE);
Taufan Aditya2d574452012-05-25 04:03:56 +070097
98 // Misc Table
Dan Bernardicd24652d2015-01-11 22:01:23 -060099 self::$forge->add_field(array(
Taufan Aditya2d574452012-05-25 04:03:56 +0700100 'id' => array(
101 'type' => 'INTEGER',
Andrey Andreevf243ce12012-06-09 23:34:21 +0300102 'constraint' => 3
Taufan Aditya2d574452012-05-25 04:03:56 +0700103 ),
104 'key' => array(
105 'type' => 'VARCHAR',
Andrey Andreevf243ce12012-06-09 23:34:21 +0300106 'constraint' => 40
Taufan Aditya2d574452012-05-25 04:03:56 +0700107 ),
108 'value' => array(
Andrey Andreevf243ce12012-06-09 23:34:21 +0300109 'type' => 'TEXT'
110 )
Taufan Aditya2d574452012-05-25 04:03:56 +0700111 ));
Dan Bernardicd24652d2015-01-11 22:01:23 -0600112 self::$forge->add_key('id', TRUE);
113 self::$forge->create_table('misc', TRUE);
Taufan Aditya5b421662012-04-05 01:24:50 +0700114 }
115
116 /**
117 * Create the dummy datas
118 *
119 * @return void
120 */
Taufan Aditya55f622f2012-04-05 19:01:34 +0700121 public static function create_data()
Taufan Aditya5b421662012-04-05 01:24:50 +0700122 {
123 // Job Data
124 $data = array(
Taufan Aditya17f799e2012-05-03 15:15:40 +0700125 'user' => array(
Taufan Aditya1afd4792012-05-04 20:34:30 +0700126 array('id' => 1, 'name' => 'Derek Jones', 'email' => 'derek@world.com', 'country' => 'US'),
127 array('id' => 2, 'name' => 'Ahmadinejad', 'email' => 'ahmadinejad@world.com', 'country' => 'Iran'),
128 array('id' => 3, 'name' => 'Richard A Causey', 'email' => 'richard@world.com', 'country' => 'US'),
Andrey Andreevf243ce12012-06-09 23:34:21 +0300129 array('id' => 4, 'name' => 'Chris Martin', 'email' => 'chris@world.com', 'country' => 'UK')
Taufan Adityad6b41bb2012-05-04 15:05:22 +0700130 ),
Taufan Aditya5b421662012-04-05 01:24:50 +0700131 'job' => array(
Andrey Andreevf243ce12012-06-09 23:34:21 +0300132 array('id' => 1, 'name' => 'Developer', 'description' => 'Awesome job, but sometimes makes you bored'),
Taufan Aditya5b421662012-04-05 01:24:50 +0700133 array('id' => 2, 'name' => 'Politician', 'description' => 'This is not really a job'),
Andrey Andreev10fb7d12015-08-03 10:05:29 +0300134 array('id' => 3, 'name' => 'Accountant', 'description' => 'Boring job, but you will get free snack at lunch'),
Andrey Andreevf243ce12012-06-09 23:34:21 +0300135 array('id' => 4, 'name' => 'Musician', 'description' => 'Only Coldplay can actually called Musician')
Taufan Aditya5b421662012-04-05 01:24:50 +0700136 ),
Taufan Aditya2d574452012-05-25 04:03:56 +0700137 'misc' => array(
Andrey Andreevf243ce12012-06-09 23:34:21 +0300138 array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'),
Andrey Andreev96a4ca62012-10-23 01:11:48 +0300139 array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'),
Andrey Andreev348bd1e2012-10-23 01:24:11 +0300140 array('id' => 3, 'key' => 'spaces and tabs', 'value' => ' One two three tab')
Andrey Andreevf243ce12012-06-09 23:34:21 +0300141 )
Taufan Aditya5b421662012-04-05 01:24:50 +0700142 );
143
Andrey Andreevf243ce12012-06-09 23:34:21 +0300144 foreach ($data as $table => $dummy_data)
Taufan Aditya5b421662012-04-05 01:24:50 +0700145 {
Dan Bernardicd24652d2015-01-11 22:01:23 -0600146 self::$db->truncate($table);
Taufan Aditya024098d2012-04-05 01:51:57 +0700147
148 foreach ($dummy_data as $single_dummy_data)
Taufan Aditya5b421662012-04-05 01:24:50 +0700149 {
Dan Bernardicd24652d2015-01-11 22:01:23 -0600150 self::$db->insert($table, $single_dummy_data);
Taufan Aditya5b421662012-04-05 01:24:50 +0700151 }
152 }
153 }
Andrey Andreevf243ce12012-06-09 23:34:21 +0300154
Dan Bernardicd24652d2015-01-11 22:01:23 -0600155}