|
|
@@ -45,6 +45,7 @@ class CompositeKeyTest extends TestCase
|
|
|
* @var array
|
|
|
*/
|
|
|
public $fixtures = [
|
|
|
+ 'core.composite_increments',
|
|
|
'core.site_articles',
|
|
|
'core.site_authors',
|
|
|
'core.site_tags',
|
|
|
@@ -103,6 +104,39 @@ class CompositeKeyTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Test that you cannot save rows with composite keys if some columns are missing.
|
|
|
+ *
|
|
|
+ * @group save
|
|
|
+ * @expectedException \RuntimeException
|
|
|
+ * @expectedExceptionMessage Cannot insert row, some of the primary key values are missing
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testSaveNewErrorCompositeKeyNoIncrement()
|
|
|
+ {
|
|
|
+ $articles = TableRegistry::get('SiteArticles');
|
|
|
+ $article = $articles->newEntity(['site_id' => 1, 'author_id' => 1, 'title' => 'testing']);
|
|
|
+ $articles->save($article);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that saving into composite primary keys where one column is missing & autoIncrement works.
|
|
|
+ *
|
|
|
+ * SQLite is skipped because it doesn't support autoincrement composite keys.
|
|
|
+ *
|
|
|
+ * @group save
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testSaveNewCompositeKeyIncrement()
|
|
|
+ {
|
|
|
+ $this->skipIfSqlite();
|
|
|
+ $table = TableRegistry::get('CompositeIncrements');
|
|
|
+ $thing = $table->newEntity(['account_id' => 3, 'name' => 'new guy']);
|
|
|
+ $this->assertSame($thing, $table->save($thing));
|
|
|
+ $this->assertNotEmpty($thing->id, 'Primary key should have been populated');
|
|
|
+ $this->assertSame(3, $thing->account_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* Tests that HasMany associations are correctly eager loaded and results
|
|
|
* correctly nested when multiple foreignKeys are used
|
|
|
*
|
|
|
@@ -636,4 +670,18 @@ class CompositeKeyTest extends TestCase
|
|
|
];
|
|
|
$this->assertEquals($expected, $formatter($items)->toArray());
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Helper method to skip tests when connection is SQLite.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function skipIfSqlite()
|
|
|
+ {
|
|
|
+ $this->skipIf(
|
|
|
+ $this->connection->driver() instanceof \Cake\Database\Driver\Sqlite,
|
|
|
+ 'SQLite does not support the requrirements of this test.'
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
}
|