blob: 9ebd6e85fd28fb636124b564c5ea16c1fdad0a32 [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 }
44
Taufan Aditya5b421662012-04-05 01:24:50 +070045
46 /**
47 * Create the dummy tables
48 *
49 * @return void
50 */
Taufan Aditya55f622f2012-04-05 19:01:34 +070051 public static function create_tables()
Taufan Aditya5b421662012-04-05 01:24:50 +070052 {
Taufan Aditya17f799e2012-05-03 15:15:40 +070053 // User Table
54 static::$forge->add_field(array(
55 'id' => array(
56 'type' => 'INTEGER',
57 'constraint' => 3,
58 ),
59 'name' => array(
60 'type' => 'VARCHAR',
61 'constraint' => 40,
62 ),
63 'email' => array(
64 'type' => 'VARCHAR',
65 'constraint' => 100,
66 ),
67 ));
68 static::$forge->add_key('id', TRUE);
69 static::$forge->create_table('user', (strpos(static::$driver, 'pgsql') === FALSE));
70
Taufan Aditya5b421662012-04-05 01:24:50 +070071 // Job Table
Taufan Aditya55f622f2012-04-05 19:01:34 +070072 static::$forge->add_field(array(
Taufan Aditya5b421662012-04-05 01:24:50 +070073 'id' => array(
Taufan Aditya1bfc0d92012-04-05 02:25:25 +070074 'type' => 'INTEGER',
Taufan Aditya5b421662012-04-05 01:24:50 +070075 'constraint' => 3,
76 ),
77 'name' => array(
78 'type' => 'VARCHAR',
79 'constraint' => 40,
80 ),
81 'description' => array(
82 'type' => 'TEXT',
Taufan Aditya5b421662012-04-05 01:24:50 +070083 ),
84 ));
Taufan Aditya55f622f2012-04-05 19:01:34 +070085 static::$forge->add_key('id', TRUE);
86 static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE));
Taufan Aditya5b421662012-04-05 01:24:50 +070087 }
88
89 /**
90 * Create the dummy datas
91 *
92 * @return void
93 */
Taufan Aditya55f622f2012-04-05 19:01:34 +070094 public static function create_data()
Taufan Aditya5b421662012-04-05 01:24:50 +070095 {
96 // Job Data
97 $data = array(
Taufan Aditya17f799e2012-05-03 15:15:40 +070098 'user' => array(
99 array('id' => 1, 'name' => 'Derek Jones', 'email' => 'derek@world.com'),
100 array('id' => 2, 'name' => 'Ahmadinejad', 'email' => 'ahmadinejad@world.com'),
101 array('id' => 3, 'name' => 'Richard A Causey', 'email' => 'richard@world.com'),
102 array('id' => 4, 'name' => 'Chris Martin', 'email' => 'chris@world.com'),
Taufan Adityad6b41bb2012-05-04 15:05:22 +0700103 ),
Taufan Aditya5b421662012-04-05 01:24:50 +0700104 'job' => array(
105 array('id' => 1, 'name' => 'Developer', 'description' => 'Awesome job, but sometimes makes you bored'),
106 array('id' => 2, 'name' => 'Politician', 'description' => 'This is not really a job'),
107 array('id' => 3, 'name' => 'Accountant', 'description' => 'Boring job, but you will get free snack at lunch'),
108 array('id' => 4, 'name' => 'Musician', 'description' => 'Only Coldplay can actually called Musician'),
109 ),
110 );
111
112 foreach ($data as $table => $dummy_data)
113 {
Taufan Aditya55f622f2012-04-05 19:01:34 +0700114 static::$db->truncate($table);
Taufan Aditya024098d2012-04-05 01:51:57 +0700115
116 foreach ($dummy_data as $single_dummy_data)
Taufan Aditya5b421662012-04-05 01:24:50 +0700117 {
Taufan Aditya55f622f2012-04-05 19:01:34 +0700118 static::$db->insert($table, $single_dummy_data);
Taufan Aditya5b421662012-04-05 01:24:50 +0700119 }
120 }
121 }
122}