summaryrefslogtreecommitdiffstats
path: root/tests/mocks/database/schema/skeleton.php
blob: 9ebd6e85fd28fb636124b564c5ea16c1fdad0a32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

class Mock_Database_Schema_Skeleton {

	/**
	 * @var object Database Holder
	 */
	public static $db;

	/**
	 * @var object Forge Holder
	 */
	public static $forge;

	/**
	 * @var object Driver Holder
	 */
	public static $driver;

	/**
	 * Initialize both database and forge components
	 */
	public static function init($driver)
	{
		if (empty(static::$db) && empty(static::$forge))
		{
			$config = Mock_Database_DB::config($driver);
			$connection = new Mock_Database_DB($config);
			$db = Mock_Database_DB::DB($connection->set_dsn($driver), TRUE);

			CI_TestCase::instance()->ci_instance_var('db', $db);

			$loader = new Mock_Core_Loader();
			$loader->dbforge();
			$forge = CI_TestCase::instance()->ci_instance_var('dbforge');

			static::$db = $db;
			static::$forge = $forge;
			static::$driver = $driver;
		}

		return static::$db;
	}
	
	
	/**
	 * Create the dummy tables
	 *
	 * @return void
	 */
	public static function create_tables()
	{
		// User Table
		static::$forge->add_field(array(
			'id' => array(
				'type' => 'INTEGER',
				'constraint' => 3,
			),
			'name' => array(
				'type' => 'VARCHAR',
				'constraint' => 40,
			),
			'email' => array(
				'type' => 'VARCHAR',
				'constraint' => 100,
			),
		));
		static::$forge->add_key('id', TRUE);
		static::$forge->create_table('user', (strpos(static::$driver, 'pgsql') === FALSE));

		// Job Table
		static::$forge->add_field(array(
			'id' => array(
				'type' => 'INTEGER',
				'constraint' => 3,
			),
			'name' => array(
				'type' => 'VARCHAR',
				'constraint' => 40,
			),
			'description' => array(
				'type' => 'TEXT',
			),
		));
		static::$forge->add_key('id', TRUE);
		static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE));
	}

	/**
	 * Create the dummy datas
	 *
	 * @return void
	 */
	public static function create_data()
	{
		// Job Data
		$data = array(
			'user' => array(
				array('id' => 1, 'name' => 'Derek Jones', 'email' => 'derek@world.com'),
				array('id' => 2, 'name' => 'Ahmadinejad', 'email' => 'ahmadinejad@world.com'),
				array('id' => 3, 'name' => 'Richard A Causey', 'email' => 'richard@world.com'),
				array('id' => 4, 'name' => 'Chris Martin', 'email' => 'chris@world.com'),
			),
			'job' => array(
				array('id' => 1, 'name' => 'Developer', 'description' => 'Awesome job, but sometimes makes you bored'), 
				array('id' => 2, 'name' => 'Politician', 'description' => 'This is not really a job'),
    			array('id' => 3, 'name' => 'Accountant', 'description' => 'Boring job, but you will get free snack at lunch'),
			    array('id' => 4, 'name' => 'Musician', 'description' => 'Only Coldplay can actually called Musician'),
			),
		);

		foreach ($data as $table => $dummy_data) 
		{
			static::$db->truncate($table);

			foreach ($dummy_data as $single_dummy_data)
			{
				static::$db->insert($table, $single_dummy_data); 
			}
		}
	}
}