blob: b6d4b78935d977db89febe757f98fd4459bc997c [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 {
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
dchill424f42be52012-10-21 21:31:19 -040033 $loader = new CI_Loader();
Taufan Aditya55f622f2012-04-05 19:01:34 +070034 $loader->dbforge();
35 $forge = CI_TestCase::instance()->ci_instance_var('dbforge');
36
Dan Bernardicd24652d2015-01-11 22:01:23 -060037 self::$db = $db;
38 self::$forge = $forge;
39 self::$driver = $driver;
Taufan Aditya55f622f2012-04-05 19:01:34 +070040 }
41
Dan Bernardicd24652d2015-01-11 22:01:23 -060042 return self::$db;
Taufan Aditya55f622f2012-04-05 19:01:34 +070043 }
Andrey Andreevf243ce12012-06-09 23:34:21 +030044
Taufan Aditya5b421662012-04-05 01:24:50 +070045 /**
46 * Create the dummy tables
47 *
48 * @return void
49 */
Taufan Aditya55f622f2012-04-05 19:01:34 +070050 public static function create_tables()
Taufan Aditya5b421662012-04-05 01:24:50 +070051 {
Taufan Aditya17f799e2012-05-03 15:15:40 +070052 // User Table
Dan Bernardicd24652d2015-01-11 22:01:23 -060053 self::$forge->add_field(array(
Taufan Aditya17f799e2012-05-03 15:15:40 +070054 'id' => array(
55 'type' => 'INTEGER',
Andrey Andreevf243ce12012-06-09 23:34:21 +030056 'constraint' => 3
Taufan Aditya17f799e2012-05-03 15:15:40 +070057 ),
58 'name' => array(
59 'type' => 'VARCHAR',
Andrey Andreevf243ce12012-06-09 23:34:21 +030060 'constraint' => 40
Taufan Aditya17f799e2012-05-03 15:15:40 +070061 ),
62 'email' => array(
63 'type' => 'VARCHAR',
Andrey Andreevf243ce12012-06-09 23:34:21 +030064 'constraint' => 100
Taufan Aditya17f799e2012-05-03 15:15:40 +070065 ),
Taufan Aditya1afd4792012-05-04 20:34:30 +070066 'country' => array(
67 'type' => 'VARCHAR',
Andrey Andreevf243ce12012-06-09 23:34:21 +030068 'constraint' => 40
69 )
Taufan Aditya17f799e2012-05-03 15:15:40 +070070 ));
Dan Bernardicd24652d2015-01-11 22:01:23 -060071 self::$forge->add_key('id', TRUE);
72 self::$forge->create_table('user', TRUE);
Taufan Aditya17f799e2012-05-03 15:15:40 +070073
Taufan Aditya5b421662012-04-05 01:24:50 +070074 // Job Table
Dan Bernardicd24652d2015-01-11 22:01:23 -060075 self::$forge->add_field(array(
Taufan Aditya5b421662012-04-05 01:24:50 +070076 'id' => array(
Taufan Aditya1bfc0d92012-04-05 02:25:25 +070077 'type' => 'INTEGER',
Andrey Andreevf243ce12012-06-09 23:34:21 +030078 'constraint' => 3
Taufan Aditya5b421662012-04-05 01:24:50 +070079 ),
80 'name' => array(
81 'type' => 'VARCHAR',
Andrey Andreevf243ce12012-06-09 23:34:21 +030082 'constraint' => 40
Taufan Aditya5b421662012-04-05 01:24:50 +070083 ),
84 'description' => array(
Andrey Andreevf243ce12012-06-09 23:34:21 +030085 'type' => 'TEXT'
86 )
Taufan Aditya5b421662012-04-05 01:24:50 +070087 ));
Dan Bernardicd24652d2015-01-11 22:01:23 -060088 self::$forge->add_key('id', TRUE);
89 self::$forge->create_table('job', TRUE);
Taufan Aditya2d574452012-05-25 04:03:56 +070090
91 // Misc Table
Dan Bernardicd24652d2015-01-11 22:01:23 -060092 self::$forge->add_field(array(
Taufan Aditya2d574452012-05-25 04:03:56 +070093 'id' => array(
94 'type' => 'INTEGER',
Andrey Andreevf243ce12012-06-09 23:34:21 +030095 'constraint' => 3
Taufan Aditya2d574452012-05-25 04:03:56 +070096 ),
97 'key' => array(
98 'type' => 'VARCHAR',
Andrey Andreevf243ce12012-06-09 23:34:21 +030099 'constraint' => 40
Taufan Aditya2d574452012-05-25 04:03:56 +0700100 ),
101 'value' => array(
Andrey Andreevf243ce12012-06-09 23:34:21 +0300102 'type' => 'TEXT'
103 )
Taufan Aditya2d574452012-05-25 04:03:56 +0700104 ));
Dan Bernardicd24652d2015-01-11 22:01:23 -0600105 self::$forge->add_key('id', TRUE);
106 self::$forge->create_table('misc', TRUE);
Taufan Aditya5b421662012-04-05 01:24:50 +0700107 }
108
109 /**
110 * Create the dummy datas
111 *
112 * @return void
113 */
Taufan Aditya55f622f2012-04-05 19:01:34 +0700114 public static function create_data()
Taufan Aditya5b421662012-04-05 01:24:50 +0700115 {
116 // Job Data
117 $data = array(
Taufan Aditya17f799e2012-05-03 15:15:40 +0700118 'user' => array(
Taufan Aditya1afd4792012-05-04 20:34:30 +0700119 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 Andreevf243ce12012-06-09 23:34:21 +0300122 array('id' => 4, 'name' => 'Chris Martin', 'email' => 'chris@world.com', 'country' => 'UK')
Taufan Adityad6b41bb2012-05-04 15:05:22 +0700123 ),
Taufan Aditya5b421662012-04-05 01:24:50 +0700124 'job' => array(
Andrey Andreevf243ce12012-06-09 23:34:21 +0300125 array('id' => 1, 'name' => 'Developer', 'description' => 'Awesome job, but sometimes makes you bored'),
Taufan Aditya5b421662012-04-05 01:24:50 +0700126 array('id' => 2, 'name' => 'Politician', 'description' => 'This is not really a job'),
Andrey Andreevf243ce12012-06-09 23:34:21 +0300127 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 Aditya5b421662012-04-05 01:24:50 +0700129 ),
Taufan Aditya2d574452012-05-25 04:03:56 +0700130 'misc' => array(
Andrey Andreevf243ce12012-06-09 23:34:21 +0300131 array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'),
Andrey Andreev96a4ca62012-10-23 01:11:48 +0300132 array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'),
Andrey Andreev348bd1e2012-10-23 01:24:11 +0300133 array('id' => 3, 'key' => 'spaces and tabs', 'value' => ' One two three tab')
Andrey Andreevf243ce12012-06-09 23:34:21 +0300134 )
Taufan Aditya5b421662012-04-05 01:24:50 +0700135 );
136
Andrey Andreevf243ce12012-06-09 23:34:21 +0300137 foreach ($data as $table => $dummy_data)
Taufan Aditya5b421662012-04-05 01:24:50 +0700138 {
Dan Bernardicd24652d2015-01-11 22:01:23 -0600139 self::$db->truncate($table);
Taufan Aditya024098d2012-04-05 01:51:57 +0700140
141 foreach ($dummy_data as $single_dummy_data)
Taufan Aditya5b421662012-04-05 01:24:50 +0700142 {
Dan Bernardicd24652d2015-01-11 22:01:23 -0600143 self::$db->insert($table, $single_dummy_data);
Taufan Aditya5b421662012-04-05 01:24:50 +0700144 }
145 }
146 }
Andrey Andreevf243ce12012-06-09 23:34:21 +0300147
Dan Bernardicd24652d2015-01-11 22:01:23 -0600148}