| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Database\Schema;
- use Cake\Database\Schema\Table;
- use Cake\Database\Type;
- use Cake\Datasource\ConnectionManager;
- use Cake\ORM\TableRegistry;
- use Cake\TestSuite\TestCase;
- /**
- * Mock class for testing baseType inheritance
- *
- */
- class FooType extends Type
- {
- public function getBaseType()
- {
- return 'integer';
- }
- }
- /**
- * Test case for Table
- */
- class TableTest extends TestCase
- {
- public $fixtures = [
- 'core.articles',
- 'core.tags',
- 'core.articles_tags',
- 'core.orders',
- 'core.products'
- ];
- protected $_map;
- public function setUp()
- {
- $this->_map = Type::map();
- parent::setUp();
- }
- public function tearDown()
- {
- TableRegistry::clear();
- Type::clear();
- Type::map($this->_map);
- parent::tearDown();
- }
- /**
- * Test construction with columns
- *
- * @return void
- */
- public function testConstructWithColumns()
- {
- $columns = [
- 'id' => [
- '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 isNullable method
- *
- * @return void
- */
- public function testIsNullable()
- {
- $table = new Table('articles');
- $table->addColumn('title', [
- 'type' => 'string',
- 'length' => 25,
- 'null' => false
- ])->addColumn('tagline', [
- 'type' => 'string',
- 'length' => 25,
- 'null' => true
- ]);
- $this->assertFalse($table->isNullable('title'));
- $this->assertTrue($table->isNullable('tagline'));
- $this->assertTrue($table->isNullable('missing'));
- }
- /**
- * 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'));
- }
- /**
- * 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'));
- }
- /**
- * Tests getting the baseType as configured when creating the column
- *
- * @return void
- */
- public function testBaseColumnType()
- {
- $table = new Table('articles');
- $table->addColumn('title', [
- 'type' => 'json',
- 'baseType' => 'text',
- 'length' => 25,
- 'null' => false
- ]);
- $this->assertEquals('json', $table->columnType('title'));
- $this->assertEquals('text', $table->baseColumnType('title'));
- }
- /**
- * Tests getting the base type as it is retuned by the Type class
- *
- * @return void
- */
- public function testBaseColumnTypeInherited()
- {
- Type::map('foo', __NAMESPACE__ . '\FooType');
- $table = new Table('articles');
- $table->addColumn('thing', [
- 'type' => 'foo',
- 'null' => false
- ]);
- $this->assertEquals('foo', $table->columnType('thing'));
- $this->assertEquals('integer', $table->baseColumnType('thing'));
- }
- /**
- * 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 reading default values.
- *
- * @return void
- */
- public function testDefaultValues()
- {
- $table = new Table('articles');
- $table->addColumn('id', [
- 'type' => 'integer',
- 'default' => 0
- ])->addColumn('title', [
- 'type' => 'string',
- 'default' => 'A title'
- ])->addColumn('name', [
- 'type' => 'string',
- 'null' => false,
- 'default' => null,
- ])->addColumn('body', [
- 'type' => 'text',
- 'null' => true,
- 'default' => null,
- ]);
- $result = $table->defaultValues();
- $expected = [
- 'id' => 0,
- 'title' => 'A title',
- 'body' => 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());
- }
- /**
- * Test adding an constraint with an overlapping unique index
- * >
- * @return void
- */
- public function testAddConstraintOverwriteUniqueIndex()
- {
- $table = new Table('articles');
- $table->addColumn('project_id', [
- 'type' => 'integer',
- 'default' => null,
- 'limit' => 11,
- 'null' => false,
- ])->addColumn('id', [
- 'type' => 'integer',
- 'autoIncrement' => true,
- 'limit' => 11
- ])->addColumn('user_id', [
- 'type' => 'integer',
- 'default' => null,
- 'limit' => 11,
- 'null' => false,
- ])->addConstraint('users_idx', [
- 'type' => 'unique',
- 'columns' => ['project_id', 'user_id']
- ])->addConstraint('users_idx', [
- 'type' => 'foreign',
- 'references' => ['users', 'project_id', 'id'],
- 'columns' => ['project_id', 'user_id']
- ]);
- $this->assertEquals(['users_idx'], $table->constraints());
- }
- /**
- * Dataprovider for invalid addConstraint calls.
- *
- * @return array
- */
- public static function addConstaintErrorProvider()
- {
- return [
- // No properties
- [[]],
- // Empty columns
- [['columns' => '', 'type' => Table::CONSTRAINT_UNIQUE]],
- [['columns' => [], 'type' => Table::CONSTRAINT_UNIQUE]],
- // Missing column
- [['columns' => ['derp'], 'type' => Table::CONSTRAINT_UNIQUE]],
- // 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
- [[]],
- // Invalid type
- [['columns' => 'author_id', 'type' => 'derp']],
- // No columns
- [['columns' => ''], 'type' => Table::INDEX_INDEX],
- [['columns' => [], 'type' => Table::INDEX_INDEX]],
- // Missing column
- [['columns' => ['not_there'], 'type' => Table::INDEX_INDEX]],
- ];
- }
- /**
- * 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());
- $table = new Table('articles');
- $table->addColumn('id', 'integer')
- ->addColumn('title', 'string')
- ->addColumn('author_id', 'integer');
- $this->assertEquals([], $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());
- }
- /**
- * Test single column foreign keys constraint support
- *
- * @return void
- */
- public function testConstraintForeignKey()
- {
- $table = TableRegistry::get('ArticlesTags');
- $compositeConstraint = $table->schema()->constraint('tag_id_fk');
- $expected = [
- 'type' => 'foreign',
- 'columns' => ['tag_id'],
- 'references' => ['tags', 'id'],
- 'update' => 'cascade',
- 'delete' => 'cascade',
- 'length' => []
- ];
- $this->assertEquals($expected, $compositeConstraint);
- $expectedSubstring = 'CONSTRAINT <tag_id_fk> FOREIGN KEY \(<tag_id>\) REFERENCES <tags> \(<id>\)';
- $this->assertQuotedQuery($expectedSubstring, $table->schema()->createSql(ConnectionManager::get('test'))[0]);
- }
- /**
- * Test composite foreign keys support
- *
- * @return void
- */
- public function testConstraintForeignKeyTwoColumns()
- {
- $table = TableRegistry::get('Orders');
- $compositeConstraint = $table->schema()->constraint('product_category_fk');
- $expected = [
- 'type' => 'foreign',
- 'columns' => [
- 'product_category',
- 'product_id'
- ],
- 'references' => [
- 'products',
- ['category', 'id']
- ],
- 'update' => 'cascade',
- 'delete' => 'cascade',
- 'length' => []
- ];
- $this->assertEquals($expected, $compositeConstraint);
- $expectedSubstring = 'CONSTRAINT <product_category_fk> FOREIGN KEY \(<product_category>, <product_id>\)' .
- ' REFERENCES <products> \(<category>, <id>\)';
- $this->assertQuotedQuery($expectedSubstring, $table->schema()->createSql(ConnectionManager::get('test'))[0]);
- }
- /**
- * 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);
- }
- /**
- * Tests the temporary() method
- *
- * @return void
- */
- public function testTemporary()
- {
- $table = new Table('articles');
- $this->assertFalse($table->temporary());
- $this->assertSame($table, $table->temporary(true));
- $this->assertTrue($table->temporary());
- $table->temporary(false);
- $this->assertFalse($table->temporary());
- }
- /**
- * Assertion for comparing a regex pattern against a query having its identifiers
- * quoted. It accepts queries quoted with the characters `<` and `>`. If the third
- * parameter is set to true, it will alter the pattern to both accept quoted and
- * unquoted queries
- *
- * @param string $pattern
- * @param string $query the result to compare against
- * @param bool $optional
- * @return void
- */
- public function assertQuotedQuery($pattern, $query, $optional = false)
- {
- if ($optional) {
- $optional = '?';
- }
- $pattern = str_replace('<', '[`"\[]' . $optional, $pattern);
- $pattern = str_replace('>', '[`"\]]' . $optional, $pattern);
- $this->assertRegExp('#' . $pattern . '#', $query);
- }
- }
|