Browse Source

Making Table::columnType() a setter too

Jose Lorenzo Rodriguez 11 years ago
parent
commit
669e5c9ce9
2 changed files with 29 additions and 2 deletions
  1. 12 2
      src/Database/Schema/Table.php
  2. 17 0
      tests/TestCase/Database/Schema/TableTest.php

+ 12 - 2
src/Database/Schema/Table.php

@@ -224,6 +224,11 @@ class Table {
  */
 	const ACTION_RESTRICT = 'restrict';
 
+/**
+ * Foreign key restrict default
+ *
+ * @var string
+ */
 	const ACTION_SET_DEFAULT = 'setDefault';
 
 /**
@@ -312,15 +317,20 @@ class Table {
 	}
 
 /**
- * Convenience method for getting the type of a given column.
+ * Sets the type of a column, or returns its current type
+ * if none is passed.
  *
  * @param string $name The column to get the type of.
+ * @param string $type The type to set the column to.
  * @return string|null Either the column type or null.
  */
-	public function columnType($name) {
+	public function columnType($name, $type = null) {
 		if (!isset($this->_columns[$name])) {
 			return null;
 		}
+		if ($type !== null) {
+			$this->_columns[$name]['type'] = $type;
+		}
 		return $this->_columns[$name]['type'];
 	}
 

+ 17 - 0
tests/TestCase/Database/Schema/TableTest.php

@@ -79,6 +79,23 @@ class TableTest extends TestCase {
 	}
 
 /**
+ * Test columnType setter method
+ *
+ * @return void
+ */
+	public function testColumnTypeSet() {
+		$table = new Table('articles');
+		$table->addColumn('title', [
+			'type' => 'string',
+			'length' => 25,
+			'null' => false
+		]);
+		$this->assertEquals('string', $table->columnType('title'));
+		$table->columnType('title', 'json');
+		$this->assertEquals('json', $table->columnType('title'));
+	}
+
+/**
  * Attribute keys should be filtered and have defaults set.
  *
  * @return void