├── .gitignore ├── .gitmodules ├── README.md ├── config └── autoload.php ├── libraries └── Schema.php ├── spark.info └── tests ├── .DS_Store ├── Schema_Table_Definition_test.php ├── Schema_test.php ├── all.php └── support ├── environment.php └── mocks.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "tests/support/inferno"] 2 | path = tests/support/inferno 3 | url = git@github.com:jamierumbelow/inferno.git 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATED: Schema 2 | 3 | [![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/) 4 | 5 | Schema allows you to write your migrations (and define your database tables) in pure, beautiful PHP. No more feeding the DBForge library raw SQL fragments or dumping and loading at the command line. Schema lets you design your database in your migrations with ultimate ease. 6 | 7 | ## Synopsis 8 | 9 | ```php 10 | Schema::create_table('users', function($table){ 11 | 12 | $table->auto_increment_integer('id'); 13 | 14 | $table->string('email'); 15 | $table->string('password'); 16 | 17 | $table->string('first_name'); 18 | $table->string('last_name'); 19 | 20 | $table->text('biography'); 21 | 22 | $table->boolean('enabled'); 23 | 24 | $table->timestamps(); 25 | }); 26 | ``` 27 | 28 | ## Requirements 29 | 30 | Schema requires at minimum PHP 5.1.2 (although 5.3 is recommended) and CodeIgniter 2.0.0. 31 | 32 | ## Installation 33 | 34 | Schema is easy to install using [CodeIgniter Sparks](http://getsparks.org). Simply install `schema`: 35 | 36 | ```bash 37 | $ php tools/spark install schema 38 | ``` 39 | 40 | ...and load the Spark when you want to use it: 41 | 42 | ```php 43 | $this->load->spark('schema'); 44 | ``` 45 | 46 | Alternatively, you can download the repository through GitHub, extract the **libraries/Schema.php** and move it to the **application/libraries** directory. Then load it by calling `$this->load->library`. 47 | 48 | ## Creating Tables 49 | 50 | The `Schema::create_table()` function allows you to define and create a table in one fell swoop. It takes two parameters: the table name as a string, and a closure function, or 'block', (which enables the nice schema defining DSL). 51 | 52 | This function takes a single parameter, which is an instance of `Schema_Table_Definition`. You can then call methods on this object to define the schema for your database tables. 53 | 54 | ```php 55 | Schema::create_table('users', function($table){ 56 | // Definition goes in here 57 | }); 58 | ``` 59 | 60 | If you're not running PHP5.3, you can pass through `FALSE` (boolean) and `create_table()` will return the table object directly. Finish your statements with a call to `$table->create_table()`. 61 | 62 | ```php 63 | $table = Schema::create_table('users', FALSE); 64 | 65 | $table->string('name'); 66 | $table->create_table(); 67 | ``` 68 | 69 | ## Data Types 70 | 71 | `Schema_Table_Definition` has a bunch of methods that allow you to create data types. These are all reasonably self-explanatory: 72 | 73 | * `integer($name)` - create an INT column 74 | * `tinyint($name)` - create an TINYINT column 75 | * `string($name, $constraint = 200)` - create a VARCHAR column (with optional constraint) 76 | * `text($name)` - create a TEXT column 77 | * `longtext($name)` - create a LONGTEXT column 78 | * `date($name)` - create a DATE column 79 | * `datetime($text)` - create a DATETIME column 80 | * `decimal($name, $constraint = '10,2')` - create an DECIMAL column (with optional constraint) 81 | * `boolean($name)` - MySQL doesn't have a native boolean type, so we create a TINYINT 82 | 83 | There are also two special methods to speed up your development. 84 | 85 | `auto_increment_integer($name)` allows you to specify an auto_increment INT (with required primary keys). This is most helpful for adding an id column: 86 | 87 | ```php 88 | $table->auto_increment_integer('id'); 89 | ``` 90 | 91 | `timestamps()` automatically generates two columns, `created_at` and `updated_at`, both `DATETIME` columns. 92 | 93 | ### Keys 94 | 95 | You can add keys (indexes) through two functions, `primary_key($name)`, and `key($name, $primary = FALSE)`. `primary_key()` is a small wrapper around `key()`. 96 | 97 | ## Modifying Tables 98 | 99 | Schema also contains a series of functions that allow you to modify an existing table. 100 | 101 | ### Add Columns 102 | 103 | `Schema::add_column()` lets you add a column to an existing table very simply. It takes three parameters, the table name, the column name and the column data type. 104 | 105 | ```php 106 | Schema::add_column('users', 'rank', 'integer'); 107 | ``` 108 | 109 | ### Remove Columns 110 | 111 | `Schema::remove_column()` lets you remove a column from a database table. 112 | 113 | ```php 114 | Schema::remove_column('users', 'rank'); 115 | ``` 116 | 117 | ### Rename Columns 118 | 119 | `Schema::rename_column()` lets you rename a column in a database table. 120 | 121 | ```php 122 | Schema::rename_column('users', 'rank', 'position'); 123 | ``` 124 | 125 | ### Modify Columns 126 | 127 | `Schema::modify_column()` allows you to modify the type and properties of a column. 128 | 129 | ```php 130 | Schema::modify_column('users', 'rank', 'string', array('constraint' => 250)); 131 | ``` 132 | 133 | ## Running Unit Tests 134 | 135 | **NOTE: In order to run the test suite PHP5.3 is required** 136 | 137 | Schema comes with a unit test suite to ensure that the software remains stable and refactorable. It uses the very simple and lightweight [PHP testing framework Inferno](https://github.com/jamierumbelow/inferno). Inferno is tucked away in a submodule: 138 | 139 | ```bash 140 | $ cd path_to_schema_repo/ 141 | $ git submodule init 142 | $ git submodule update 143 | ``` 144 | 145 | Then, all it takes is a call to **tests/all.php** to run the entire test suite. 146 | 147 | ```bash 148 | $ php tests/all.php 149 | ``` 150 | 151 | You can also specify particular files in the suite by calling that file directly: 152 | 153 | ```bash 154 | $ php tests/Schema_test.php 155 | ``` 156 | 157 | ## Release Notes 158 | 159 | * **0.2.0** 160 | * Added the ability to add a column after another 161 | * Added a boolean() method 162 | * **0.1.0** 163 | * Initial Release 164 | -------------------------------------------------------------------------------- /config/autoload.php: -------------------------------------------------------------------------------- 1 | 8 | * @version 0.2.0 9 | * @copyright (c)2011 Jamie Rumbelow 10 | */ 11 | 12 | $autoload = array(); 13 | $autoload['libraries'] = array('schema'); -------------------------------------------------------------------------------- /libraries/Schema.php: -------------------------------------------------------------------------------- 1 | 8 | * @version 0.2.0 9 | * @copyright (c)2011 Jamie Rumbelow 10 | */ 11 | 12 | /* -------------------------------------------------------------- 13 | * THE SCHEMA FACTORY CLASS 14 | * ------------------------------------------------------------ */ 15 | 16 | class Schema { 17 | 18 | /* -------------------------------------------------------------- 19 | * VARIABLES 20 | * ------------------------------------------------------------ */ 21 | 22 | static public $types = array( 23 | 'integer' => 'INT', 24 | 'int' => 'INT', 25 | 'bigint' => 'BIGINT', 26 | 'decimal' => 'DECIMAL', 27 | 'string' => 'VARCHAR', 28 | 'varchar' => 'VARCHAR', 29 | 'char' => 'CHAR', 30 | 'text' => 'TEXT', 31 | 'longtext' => 'LONGTEXT', 32 | 'date' => 'DATE', 33 | 'datetime' => 'DATETIME', 34 | 'boolean' => 'TINYINT', 35 | 'tinyint' => 'TINYINT' 36 | ); 37 | 38 | /* -------------------------------------------------------------- 39 | * GENERIC METHODS 40 | * ------------------------------------------------------------ */ 41 | 42 | public function __construct() { } 43 | 44 | /* -------------------------------------------------------------- 45 | * FACTORY API 46 | * ------------------------------------------------------------ */ 47 | 48 | static public function create_table($table_name, $callback) { 49 | $table_definition = new Schema_Table_Definition($table_name); 50 | 51 | if ($callback === FALSE) { 52 | return $table_definition; 53 | } else { 54 | $callback($table_definition); 55 | $table_definition->create_table(); 56 | } 57 | } 58 | 59 | static public function add_column($table, $name, $type, $options = array(), $after_column = '') { 60 | $column = array(); 61 | 62 | if (isset(self::$types[strtolower($type)])) 63 | { 64 | $column = array( 'type' => self::$types[$type] ); 65 | } 66 | elseif ($type == 'auto_increment_integer') 67 | { 68 | $column = array( 'type' => 'INT', 'unsigned' => TRUE, 'auto_increment' => TRUE ); 69 | } 70 | elseif ($type == 'timestamps') 71 | { 72 | self::add_column($table, 'created_at', 'datetime'); 73 | self::add_column($table, 'updated_at', 'datetime'); 74 | 75 | return; 76 | } 77 | 78 | $ci =& get_instance(); 79 | $ci->load->dbforge(); 80 | 81 | $ci->dbforge->add_column($table, array($name => array_merge($column, $options)), $after_column); 82 | } 83 | 84 | static public function remove_column($table, $name) { 85 | $ci =& get_instance(); 86 | $ci->load->dbforge(); 87 | 88 | $ci->dbforge->drop_column($table, $name); 89 | } 90 | 91 | static public function rename_column($table, $name, $new_name) { 92 | $ci =& get_instance(); 93 | $ci->load->dbforge(); 94 | 95 | $field_data = $ci->db->field_data($table); 96 | $types = array(); 97 | 98 | foreach ($field_data as $col) 99 | { 100 | $types[$col->name] = $col->type; 101 | } 102 | 103 | $ci->dbforge->modify_column($table, array( $name => array( 'name' => $new_name, 'type' => $types[$name] ))); 104 | } 105 | 106 | static public function modify_column($table, $name, $type, $options = array()) { 107 | $column = array( 'type' => self::$types[strtolower($type)] ); 108 | 109 | $ci =& get_instance(); 110 | $ci->load->dbforge(); 111 | 112 | $ci->dbforge->modify_column($table, array( $name => array_merge($column, $options) )); 113 | } 114 | } 115 | 116 | /* -------------------------------------------------------------- 117 | * SCHEMA TABLE DEFINITION CLASS 118 | * ------------------------------------------------------------ */ 119 | 120 | class Schema_Table_Definition { 121 | 122 | /* -------------------------------------------------------------- 123 | * VARIABLES 124 | * ------------------------------------------------------------ */ 125 | 126 | protected $name = ''; 127 | protected $definition = array( 128 | 'columns' => array(), 129 | 'keys' => array() 130 | ); 131 | 132 | /* -------------------------------------------------------------- 133 | * GENERIC METHODS 134 | * ------------------------------------------------------------ */ 135 | 136 | public function __construct($table_name = '') { 137 | $this->name = $table_name; 138 | } 139 | 140 | /* -------------------------------------------------------------- 141 | * GENERIC API 142 | * ------------------------------------------------------------ */ 143 | 144 | public function columns() { 145 | return $this->definition['columns']; 146 | } 147 | 148 | public function keys() { 149 | return $this->definition['keys']; 150 | } 151 | 152 | public function table_name() { 153 | return $this->name; 154 | } 155 | 156 | public function create_table() { 157 | $ci =& get_instance(); 158 | $ci->load->dbforge(); 159 | 160 | $ci->dbforge->add_field($this->columns()); 161 | 162 | foreach ($this->keys() as $key => $primary) { 163 | $ci->dbforge->add_key($key, $primary); 164 | } 165 | 166 | $ci->dbforge->create_table($this->table_name()); 167 | } 168 | 169 | /* -------------------------------------------------------------- 170 | * COLUMN API 171 | * ------------------------------------------------------------ */ 172 | 173 | public function integer($column_name, $options = array()) { 174 | $this->add_definition_rule($column_name, array( 175 | 'type' => 'INT' 176 | ), $options); 177 | } 178 | 179 | public function tinyint($column_name, $options = array()) { 180 | $this->add_definition_rule($column_name, array( 181 | 'type' => 'TINYINT' 182 | ), $options); 183 | } 184 | 185 | public function decimal($column_name, $constraint = '10,2', $options = array()) { 186 | $this->add_definition_rule($column_name, array( 187 | 'type' => 'DECIMAL', 188 | 'constraint' => $constraint, 189 | 'unsigned' => FALSE 190 | ), $options); 191 | } 192 | 193 | public function auto_increment_integer($column_name, $options = array()) { 194 | $this->integer($column_name, array_merge(array( 195 | 'unsigned' => TRUE, 196 | 'auto_increment' => TRUE 197 | ), $options)); 198 | 199 | $this->key($column_name, TRUE); 200 | } 201 | 202 | public function string($column_name, $constraint = 200, $options = array()) { 203 | $this->add_definition_rule($column_name, array( 204 | 'type' => 'VARCHAR', 205 | 'constraint' => $constraint 206 | ), $options); 207 | } 208 | 209 | public function char($column_name, $constraint = 2, $options = array()) { 210 | $this->add_definition_rule($column_name, array( 211 | 'type' => 'CHAR', 212 | 'constraint' => $constraint 213 | ), $options); 214 | } 215 | 216 | public function text($column_name, $options = array()) { 217 | $this->add_definition_rule($column_name, array( 218 | 'type' => 'TEXT' 219 | ), $options); 220 | } 221 | 222 | public function longtext($column_name, $options = array()) { 223 | $this->add_definition_rule($column_name, array( 224 | 'type' => 'LONGTEXT' 225 | ), $options); 226 | } 227 | 228 | public function boolean($column_name, $options = array()) { 229 | $this->add_definition_rule($column_name, array( 230 | 'type' => 'TINYINT' 231 | ), $options); 232 | } 233 | 234 | public function date($column_name, $options = array()) { 235 | $this->add_definition_rule($column_name, array( 236 | 'type' => 'DATE' 237 | ), $options); 238 | } 239 | 240 | public function datetime($column_name, $options = array()) { 241 | $this->add_definition_rule($column_name, array( 242 | 'type' => 'DATETIME' 243 | ), $options); 244 | } 245 | 246 | public function timestamps($options = array()) { 247 | $this->datetime('created_at', $options); 248 | $this->datetime('updated_at', $options); 249 | } 250 | 251 | /* -------------------------------------------------------------- 252 | * MISC API 253 | * ------------------------------------------------------------ */ 254 | 255 | public function primary_key($column_name) { 256 | $this->key($column_name, TRUE); 257 | } 258 | 259 | public function key($column_name, $primary = FALSE) { 260 | $this->definition['keys'][$column_name] = $primary; 261 | } 262 | 263 | public function add_definition_rule($column_name, $rule, $options) { 264 | $this->definition['columns'][$column_name] = array_merge($rule, $options); 265 | } 266 | 267 | } -------------------------------------------------------------------------------- /spark.info: -------------------------------------------------------------------------------- 1 | name: schema 2 | version: 0.2.0 3 | compatibility: 2.0.2 -------------------------------------------------------------------------------- /tests/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jamierumbelow/codeigniter-schema/59ecfa05f022056ebf8481df54997904c5e8bb71/tests/.DS_Store -------------------------------------------------------------------------------- /tests/Schema_Table_Definition_test.php: -------------------------------------------------------------------------------- 1 | 8 | * @version 0.2.0 9 | * @copyright (c)2011 Jamie Rumbelow 10 | */ 11 | 12 | require_once 'support/environment.php'; 13 | 14 | class Schema_Table_Definition_Test extends UnitTest { 15 | 16 | public function test_create_table() { 17 | $ci =& get_instance(); 18 | $ci->load = new Mock_Loader(); 19 | $ci->dbforge = new Mock_DBForge(); 20 | 21 | $ci->load->expect_call('dbforge'); 22 | 23 | $ci->dbforge->expect_call('add_field', 1, array('column_name' => array('type' => 'INT'))); 24 | $ci->dbforge->expect_call('create_table', 1, 'table_name'); 25 | 26 | $mock_schema_table_definition = new Mock_Schema_Table_Definition('table_name'); 27 | $mock_schema_table_definition->create_table(); 28 | 29 | $ci->load->assert($this); 30 | $ci->dbforge->assert($this); 31 | } 32 | 33 | /* -------------------------------------------------------------- 34 | * COLUMN API TESTS 35 | * ------------------------------------------------------------ */ 36 | 37 | public function test_integer() { 38 | $schema_table_definition = new Schema_Table_Definition('table_name'); 39 | $schema_table_definition->integer('column_name', array( 'option' => 'here' )); 40 | 41 | $this->assert_equal($schema_table_definition->columns(), array( 42 | 'column_name' => array('type' => 'INT', 'option' => 'here')) 43 | ); 44 | } 45 | 46 | public function test_tinyint() { 47 | $schema_table_definition = new Schema_Table_Definition('table_name'); 48 | $schema_table_definition->tinyint('column_name', array( 'option' => 'here' )); 49 | 50 | $this->assert_equal($schema_table_definition->columns(), array( 51 | 'column_name' => array('type' => 'TINYINT', 'option' => 'here')) 52 | ); 53 | } 54 | 55 | public function test_auto_increment_integer() { 56 | $schema_table_definition = new Schema_Table_Definition('table_name'); 57 | $schema_table_definition->auto_increment_integer('column_name', array( 'option' => 'here' )); 58 | 59 | $this->assert_equal($schema_table_definition->columns(), array( 60 | 'column_name' => array('type' => 'INT', 'unsigned' => TRUE, 'auto_increment' => TRUE, 'option' => 'here')) 61 | ); 62 | } 63 | 64 | public function test_string() { 65 | $schema_table_definition = new Schema_Table_Definition('table_name'); 66 | $schema_table_definition->string('column_name', 100, array( 'option' => 'here' )); 67 | 68 | $this->assert_equal($schema_table_definition->columns(), array( 69 | 'column_name' => array('type' => 'VARCHAR', 'constraint' => 100, 'option' => 'here')) 70 | ); 71 | } 72 | 73 | public function test_char() { 74 | $schema_table_definition = new Schema_Table_Definition('table_name'); 75 | $schema_table_definition->string('column_name', 2, array( 'option' => 'here' )); 76 | 77 | $this->assert_equal($schema_table_definition->columns(), array( 78 | 'column_name' => array('type' => 'CHAR', 'constraint' => 2, 'option' => 'here')) 79 | ); 80 | } 81 | 82 | public function test_text() { 83 | $schema_table_definition = new Schema_Table_Definition('table_name'); 84 | $schema_table_definition->text('column_name', array( 'option' => 'here' )); 85 | 86 | $this->assert_equal($schema_table_definition->columns(), array( 87 | 'column_name' => array('type' => 'TEXT', 'option' => 'here')) 88 | ); 89 | } 90 | 91 | public function test_longtext() { 92 | $schema_table_definition = new Schema_Table_Definition('table_name'); 93 | $schema_table_definition->longtext('column_name', array( 'option' => 'here' )); 94 | 95 | $this->assert_equal($schema_table_definition->columns(), array( 96 | 'column_name' => array('type' => 'LONGTEXT', 'option' => 'here')) 97 | ); 98 | } 99 | 100 | public function test_date() { 101 | $schema_table_definition = new Schema_Table_Definition('table_name'); 102 | $schema_table_definition->date('column_name', array( 'option' => 'here' )); 103 | 104 | $this->assert_equal($schema_table_definition->columns(), array( 105 | 'column_name' => array('type' => 'DATE', 'option' => 'here')) 106 | ); 107 | } 108 | 109 | public function test_datetime() { 110 | $schema_table_definition = new Schema_Table_Definition('table_name'); 111 | $schema_table_definition->datetime('column_name', array( 'option' => 'here' )); 112 | 113 | $this->assert_equal($schema_table_definition->columns(), array( 114 | 'column_name' => array('type' => 'DATETIME', 'option' => 'here')) 115 | ); 116 | } 117 | 118 | public function test_decimal() { 119 | $schema_table_definition = new Schema_Table_Definition('table_name'); 120 | $schema_table_definition->decimal('column_name', '5,3', array( 'option' => 'here' )); 121 | 122 | $this->assert_equal($schema_table_definition->columns(), array( 123 | 'column_name' => array('type' => 'DECIMAL', 'constraint' => '5,3', 'option' => 'here')) 124 | ); 125 | } 126 | 127 | public function test_boolean() { 128 | $schema_table_definition = new Schema_Table_Definition('table_name'); 129 | $schema_table_definition->boolean('column_name', array( 'option' => 'here' )); 130 | 131 | $this->assert_equal($schema_table_definition->columns(), array( 132 | 'column_name' => array('type' => 'TINYINT', 'option' => 'here') 133 | )); 134 | } 135 | 136 | public function test_timestamps() { 137 | $schema_table_definition = new Schema_Table_Definition('table_name'); 138 | $schema_table_definition->timestamps(); 139 | 140 | $this->assert_equal($schema_table_definition->columns(), array( 141 | 'created_at' => array('type' => 'DATETIME'), 142 | 'updated_at' => array('type' => 'DATETIME') 143 | )); 144 | } 145 | 146 | /* -------------------------------------------------------------- 147 | * MISC API TESTS 148 | * ------------------------------------------------------------ */ 149 | 150 | public function test__construct() { 151 | $schema_table_definition = new Schema_Table_Definition('table_name'); 152 | $this->assert_equal($schema_table_definition->table_name(), 'table_name'); 153 | } 154 | 155 | public function test_primary_key() { 156 | $schema_table_definition = new Schema_Table_Definition('table_name'); 157 | $schema_table_definition->primary_key('column_name'); 158 | 159 | $this->assert_equal($schema_table_definition->keys(), array('column_name' => TRUE)); 160 | } 161 | 162 | public function test_key() { 163 | $schema_table_definition = new Schema_Table_Definition('table_name'); 164 | $schema_table_definition->key('column_name'); 165 | 166 | $this->assert_equal($schema_table_definition->keys(), array('column_name' => FALSE)); 167 | } 168 | 169 | public function test_add_definition_rule() { 170 | $schema_table_definition = new Schema_Table_Definition('table_name'); 171 | $schema_table_definition->add_definition_rule('column_name', array('type' => 'INT'), array('opts' => 'here')); 172 | 173 | $this->assert_equal($schema_table_definition->columns(), array('column_name' => array('type' => 'INT', 'opts' => 'here'))); 174 | } 175 | } 176 | 177 | if (!defined('SCHEMA_TEST_ALL')) { 178 | UnitTest::test(); 179 | } -------------------------------------------------------------------------------- /tests/Schema_test.php: -------------------------------------------------------------------------------- 1 | 8 | * @version 0.2.0 9 | * @copyright (c)2011 Jamie Rumbelow 10 | */ 11 | 12 | require_once 'support/environment.php'; 13 | 14 | class Schema_Test extends UnitTest { 15 | 16 | /* -------------------------------------------------------------- 17 | * FACTORY API TESTS 18 | * ------------------------------------------------------------ */ 19 | 20 | public function test_create_table_53() { 21 | $ci =& get_instance(); 22 | $ci->load = new Mock_Loader(); 23 | $ci->dbforge = new Mock_DBForge(); 24 | 25 | $test =& $this; 26 | 27 | Schema::create_table('name', function($table) use (&$test) { 28 | $test->assert_class($table, 'Schema_Table_Definition'); 29 | }); 30 | } 31 | 32 | public function test_create_table_52() { 33 | $table = Schema::create_table('name', FALSE); 34 | $this->assert_class($table, 'Schema_Table_Definition'); 35 | } 36 | 37 | public function test_add_column() { 38 | $ci =& get_instance(); 39 | $ci->load = new Mock_Loader(); 40 | $ci->dbforge = new Mock_DBForge(); 41 | 42 | $ci->load->expect_call('dbforge'); 43 | $ci->dbforge->expect_call('add_column', 1, array('table_name', Schema_Test_Data::mock_column_data()), array(), 'after_column'); 44 | 45 | Schema::add_column('table_name', 'column_name', 'integer', array(), 'after_column'); 46 | 47 | $ci->load->assert($this); 48 | $ci->dbforge->assert($this); 49 | } 50 | 51 | public function test_remove_column() { 52 | $ci =& get_instance(); 53 | $ci->load = new Mock_Loader(); 54 | $ci->dbforge = new Mock_DBForge(); 55 | 56 | $ci->load->expect_call('dbforge'); 57 | $ci->dbforge->expect_call('drop_column', 1, array('table_name', 'column_name')); 58 | 59 | Schema::remove_column('table_name', 'column_name'); 60 | 61 | $ci->load->assert($this); 62 | $ci->dbforge->assert($this); 63 | } 64 | 65 | public function test_rename_column() { 66 | $ci =& get_instance(); 67 | $ci->load = new Mock_Loader(); 68 | $ci->dbforge = new Mock_DBForge(); 69 | $ci->db = new Mock_DB(); 70 | 71 | $ci->load->expect_call('dbforge'); 72 | 73 | $ci->db->expect_call('field_data', 1, array('table_name')); 74 | $ci->dbforge->expect_call('modify_column', 1, array('table_name', array('column_name' => array( 'name' => 'new_column_name', 'type' => 'INT' )))); 75 | 76 | Schema::rename_column('table_name', 'column_name', 'new_column_name'); 77 | 78 | $ci->load->assert($this); 79 | $ci->dbforge->assert($this); 80 | } 81 | 82 | public function test_modify_column() { 83 | $ci =& get_instance(); 84 | $ci->load = new Mock_Loader(); 85 | $ci->dbforge = new Mock_DBForge(); 86 | 87 | $ci->load->expect_call('dbforge'); 88 | $ci->dbforge->expect_call('modify_column', 1, array('table_name', array('column_name' => array( 'type' => 'INT', 'other' => 'here' )))); 89 | 90 | Schema::modify_column('table_name', 'column_name', 'integer', array( 'other' => 'here' )); 91 | 92 | $ci->load->assert($this); 93 | $ci->dbforge->assert($this); 94 | } 95 | } 96 | 97 | if (!defined('SCHEMA_TEST_ALL')) { 98 | UnitTest::test(); 99 | } -------------------------------------------------------------------------------- /tests/all.php: -------------------------------------------------------------------------------- 1 | 8 | * @version 0.2.0 9 | * @copyright (c)2011 Jamie Rumbelow 10 | */ 11 | 12 | define('SCHEMA_TEST_ALL', TRUE); 13 | 14 | require_once 'Schema_test.php'; 15 | require_once 'Schema_Table_Definition_test.php'; 16 | 17 | UnitTest::test(); -------------------------------------------------------------------------------- /tests/support/environment.php: -------------------------------------------------------------------------------- 1 | 8 | * @version 0.2.0 9 | * @copyright (c)2011 Jamie Rumbelow 10 | */ 11 | 12 | define('BASEPATH', true); 13 | 14 | require_once 'libraries/Schema.php'; 15 | 16 | require_once 'tests/support/inferno/lib/inferno.php'; 17 | require_once 'tests/support/mocks.php'; 18 | 19 | class Schema_Test_Data { 20 | 21 | /* -------------------------------------------------------------- 22 | * HELPER METHODS 23 | * ------------------------------------------------------------ */ 24 | 25 | static public function mock_column_data() { 26 | return array('column_name' => array('type' => 'INT')); 27 | } 28 | } -------------------------------------------------------------------------------- /tests/support/mocks.php: -------------------------------------------------------------------------------- 1 | 8 | * @version 0.2.0 9 | * @copyright (c)2011 Jamie Rumbelow 10 | */ 11 | 12 | 13 | class Mocked { 14 | protected $_calls = array(); 15 | protected $_expectations = array(); 16 | 17 | public function expect_call($method, $count = 1, $params = array()) { 18 | $this->_expectations[$method] = array($count, $params); 19 | } 20 | 21 | public function assert(&$test) { 22 | foreach ($this->_expectations as $method => $details) { 23 | if (isset($this->_calls[$method])) { 24 | $test->assert_equal($details[1], $this->_calls[$method][0], "$method expected to be called with " . str_replace("\n", "", var_export($details[1], TRUE)) . ", instead called with " . str_replace("\n", "", var_export($this->_calls[$method][0], TRUE))); 25 | } else { 26 | $test->failure("$method expected to be called on " . get_class($this) . ", no calls found"); 27 | } 28 | } 29 | } 30 | 31 | protected function _track_call($method, $params) { 32 | if (!isset($this->_calls[$method])) { 33 | $this->_calls[$method] = array(); 34 | } 35 | 36 | $this->_calls[$method][] = $params; 37 | } 38 | } 39 | 40 | class Mock_CI { 41 | public $load; 42 | public $dbforge; 43 | } 44 | 45 | class Mock_Loader extends Mocked { 46 | public function dbforge() { $this->_track_call('dbforge', array()); } 47 | } 48 | 49 | class Mock_DB extends Mocked { 50 | public function field_data($table) 51 | { 52 | $this->_track_call('field_data', array()); 53 | 54 | $table = new stdClass; 55 | $table->name = 'column_name'; 56 | $table->type = 'INT'; 57 | 58 | return array( $table ); 59 | } 60 | } 61 | 62 | class Mock_DBForge extends Mocked { 63 | public function add_field($columns) { $this->_track_call('add_field', $columns); } 64 | public function add_key($key, $primary = FALSE) { $this->_track_call('add_key', array($key, $primary)); } 65 | public function create_table($name) { $this->_track_call('create_table', $name); } 66 | public function add_column($table, $column) { $this->_track_call('add_column', array($table, $column)); } 67 | public function drop_column($table, $column) { $this->_track_call('drop_column', array($table, $column)); } 68 | public function modify_column($table, $column) { $this->_track_call('modify_column', array($table, $column)); } 69 | } 70 | 71 | class Mock_Schema_Table_Definition extends Schema_Table_Definition { 72 | public function columns() { return Schema_Test_Data::mock_column_data(); } 73 | public function keys() { return array(); } 74 | public function table_name() { return 'table_name'; } 75 | } 76 | 77 | function &get_instance() { 78 | static $instance; 79 | 80 | if (!$instance) { 81 | $instance = new Mock_CI(); 82 | } 83 | 84 | return $instance; 85 | } --------------------------------------------------------------------------------