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 | { |
| 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 Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Create the dummy tables |
| 48 | * |
| 49 | * @return void |
| 50 | */ |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 51 | public static function create_tables() |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 52 | { |
Taufan Aditya | 17f799e | 2012-05-03 15:15:40 +0700 | [diff] [blame] | 53 | // 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 Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 71 | // Job Table |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 72 | static::$forge->add_field(array( |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 73 | 'id' => array( |
Taufan Aditya | 1bfc0d9 | 2012-04-05 02:25:25 +0700 | [diff] [blame] | 74 | 'type' => 'INTEGER', |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 75 | 'constraint' => 3, |
| 76 | ), |
| 77 | 'name' => array( |
| 78 | 'type' => 'VARCHAR', |
| 79 | 'constraint' => 40, |
| 80 | ), |
| 81 | 'description' => array( |
| 82 | 'type' => 'TEXT', |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 83 | ), |
| 84 | )); |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 85 | static::$forge->add_key('id', TRUE); |
| 86 | static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE)); |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Create the dummy datas |
| 91 | * |
| 92 | * @return void |
| 93 | */ |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 94 | public static function create_data() |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 95 | { |
| 96 | // Job Data |
| 97 | $data = array( |
Taufan Aditya | 17f799e | 2012-05-03 15:15:40 +0700 | [diff] [blame] | 98 | '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 Aditya | d6b41bb | 2012-05-04 15:05:22 +0700 | [diff] [blame^] | 103 | ), |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 104 | '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 Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 114 | static::$db->truncate($table); |
Taufan Aditya | 024098d | 2012-04-05 01:51:57 +0700 | [diff] [blame] | 115 | |
| 116 | foreach ($dummy_data as $single_dummy_data) |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 117 | { |
Taufan Aditya | 55f622f | 2012-04-05 19:01:34 +0700 | [diff] [blame] | 118 | static::$db->insert($table, $single_dummy_data); |
Taufan Aditya | 5b42166 | 2012-04-05 01:24:50 +0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |