blob: 18e1ddd4dd1bdfdd8ae2ab0ad6952978d37a5cd9 [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 {
25 if (empty(static::$db) && empty(static::$forge))
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
33 $loader = new Mock_Core_Loader();
34 $loader->dbforge();
35 $forge = CI_TestCase::instance()->ci_instance_var('dbforge');
36
37 static::$db = $db;
38 static::$forge = $forge;
39 static::$driver = $driver;
40 }
41
42 return static::$db;
43 }
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
53 static::$forge->add_field(array(
54 '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 ));
71 static::$forge->add_key('id', TRUE);
72 static::$forge->create_table('user', (strpos(static::$driver, 'pgsql') === FALSE));
73
Taufan Aditya5b421662012-04-05 01:24:50 +070074 // Job Table
Taufan Aditya55f622f2012-04-05 19:01:34 +070075 static::$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 ));
Taufan Aditya55f622f2012-04-05 19:01:34 +070088 static::$forge->add_key('id', TRUE);
89 static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE));
Taufan Aditya2d574452012-05-25 04:03:56 +070090
91 // Misc Table
92 static::$forge->add_field(array(
93 '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 ));
105 static::$forge->add_key('id', TRUE);
106 static::$forge->create_table('misc', (strpos(static::$driver, 'pgsql') === FALSE));
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'),
132 array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%')
133 )
Taufan Aditya5b421662012-04-05 01:24:50 +0700134 );
135
Andrey Andreevf243ce12012-06-09 23:34:21 +0300136 foreach ($data as $table => $dummy_data)
Taufan Aditya5b421662012-04-05 01:24:50 +0700137 {
Taufan Aditya55f622f2012-04-05 19:01:34 +0700138 static::$db->truncate($table);
Taufan Aditya024098d2012-04-05 01:51:57 +0700139
140 foreach ($dummy_data as $single_dummy_data)
Taufan Aditya5b421662012-04-05 01:24:50 +0700141 {
Andrey Andreevf243ce12012-06-09 23:34:21 +0300142 static::$db->insert($table, $single_dummy_data);
Taufan Aditya5b421662012-04-05 01:24:50 +0700143 }
144 }
145 }
Andrey Andreevf243ce12012-06-09 23:34:21 +0300146
Taufan Aditya5b421662012-04-05 01:24:50 +0700147}