[ 'type' => 'integer', 'length' => 11, ], 'title' => [ 'type' => 'string', 'length' => 255 ] ]; $table = new Table('articles', $columns); $this->assertEquals(['id', 'title'], $table->columns()); } /** * Test adding columns. * * @return void */ public function testAddColumn() { $table = new Table('articles'); $result = $table->addColumn('title', [ 'type' => 'string', 'length' => 25, 'null' => false ]); $this->assertSame($table, $result); $this->assertEquals(['title'], $table->columns()); $result = $table->addColumn('body', 'text'); $this->assertSame($table, $result); $this->assertEquals(['title', 'body'], $table->columns()); } /** * Test columnType method * * @return void */ public function testColumnType() { $table = new Table('articles'); $table->addColumn('title', [ 'type' => 'string', 'length' => 25, 'null' => false ]); $this->assertEquals('string', $table->columnType('title')); $this->assertNull($table->columnType('not there')); } /** * Attribute keys should be filtered and have defaults set. * * @return void */ public function testAddColumnFiltersAttributes() { $table = new Table('articles'); $table->addColumn('title', [ 'type' => 'string' ]); $result = $table->column('title'); $expected = [ 'type' => 'string', 'length' => null, 'precision' => null, 'default' => null, 'null' => null, 'fixed' => null, 'comment' => null, ]; $this->assertEquals($expected, $result); $table->addColumn('author_id', [ 'type' => 'integer' ]); $result = $table->column('author_id'); $expected = [ 'type' => 'integer', 'length' => null, 'precision' => null, 'default' => null, 'null' => null, 'unsigned' => null, 'comment' => null, 'autoIncrement' => null, ]; $this->assertEquals($expected, $result); $table->addColumn('amount', [ 'type' => 'decimal' ]); $result = $table->column('amount'); $expected = [ 'type' => 'decimal', 'length' => null, 'precision' => null, 'default' => null, 'null' => null, 'unsigned' => null, 'comment' => null, ]; $this->assertEquals($expected, $result); } /** * Test adding an constraint. * * @return void */ public function testAddConstraint() { $table = new Table('articles'); $table->addColumn('id', [ 'type' => 'integer' ]); $result = $table->addConstraint('primary', [ 'type' => 'primary', 'columns' => ['id'] ]); $this->assertSame($result, $table); $this->assertEquals(['primary'], $table->constraints()); } /** * Dataprovider for invalid addConstraint calls. * * @return array */ public static function addConstaintErrorProvider() { return [ // No properties [[]], // Empty columns [['columns' => '']], [['columns' => []]], // Missing column [['columns' => ['derp']]], // Invalid type [['columns' => 'author_id', 'type' => 'derp']], ]; } /** * Test that an exception is raised when constraints * are added for fields that do not exist. * * @dataProvider addConstaintErrorProvider * @expectedException Cake\Database\Exception * @return void */ public function testAddConstraintError($props) { $table = new Table('articles'); $table->addColumn('author_id', 'integer'); $table->addConstraint('author_idx', $props); } /** * Test adding an index. * * @return void */ public function testAddIndex() { $table = new Table('articles'); $table->addColumn('title', [ 'type' => 'string' ]); $result = $table->addIndex('faster', [ 'type' => 'index', 'columns' => ['title'] ]); $this->assertSame($result, $table); $this->assertEquals(['faster'], $table->indexes()); } /** * Dataprovider for invalid addIndex calls * * @return array */ public static function addIndexErrorProvider() { return [ // Empty [[]], // No columns [['columns' => '']], [['columns' => []]], // Missing column [['columns' => ['not_there']]], // Invalid type [['columns' => 'author_id', 'type' => 'derp']], ]; } /** * Test that an exception is raised when indexes * are added for fields that do not exist. * * @dataProvider addIndexErrorProvider * @expectedException Cake\Database\Exception * @return void */ public function testAddIndexError($props) { $table = new Table('articles'); $table->addColumn('author_id', 'integer'); $table->addIndex('author_idx', $props); } /** * Test adding different kinds of indexes. * * @return void */ public function testAddIndexTypes() { $table = new Table('articles'); $table->addColumn('id', 'integer') ->addColumn('title', 'string') ->addColumn('author_id', 'integer'); $table->addIndex('author_idx', [ 'columns' => ['author_id'], 'type' => 'index' ])->addIndex('texty', [ 'type' => 'fulltext', 'columns' => ['title'] ]); $this->assertEquals( ['author_idx', 'texty'], $table->indexes() ); } /** * Test getting the primary key. * * @return void */ public function testPrimaryKey() { $table = new Table('articles'); $table->addColumn('id', 'integer') ->addColumn('title', 'string') ->addColumn('author_id', 'integer') ->addConstraint('author_idx', [ 'columns' => ['author_id'], 'type' => 'unique' ])->addConstraint('primary', [ 'type' => 'primary', 'columns' => ['id'] ]); $this->assertEquals(['id'], $table->primaryKey()); } /** * Test the options method. * * @return void */ public function testOptions() { $table = new Table('articles'); $options = [ 'engine' => 'InnoDB' ]; $return = $table->options($options); $this->assertInstanceOf('Cake\Database\Schema\Table', $return); $this->assertEquals($options, $table->options()); } /** * Add a basic foreign key constraint. * * @return void */ public function testAddConstraintForeignKey() { $table = new Table('articles'); $table->addColumn('author_id', 'integer') ->addConstraint('author_id_idx', [ 'type' => Table::CONSTRAINT_FOREIGN, 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade', 'delete' => 'cascade', ]); $this->assertEquals(['author_id_idx'], $table->constraints()); } /** * Provider for exceptionally bad foreign key data. * * @return array */ public static function badForeignKeyProvider() { return [ 'references is bad' => [[ 'type' => Table::CONSTRAINT_FOREIGN, 'columns' => ['author_id'], 'references' => ['authors'], 'delete' => 'derp', ]], 'bad update value' => [[ 'type' => Table::CONSTRAINT_FOREIGN, 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'derp', ]], 'bad delete value' => [[ 'type' => Table::CONSTRAINT_FOREIGN, 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'delete' => 'derp', ]], ]; } /** * Add a foreign key constraint with bad data * * @dataProvider badForeignKeyProvider * @expectedException Cake\Database\Exception * @return void */ public function testAddConstraintForeignKeyBadData($data) { $table = new Table('articles'); $table->addColumn('author_id', 'integer') ->addConstraint('author_id_idx', $data); } }