|
|
@@ -49,7 +49,7 @@ class HasManyTest extends TestCase
|
|
|
$this->author = TableRegistry::get('Authors', [
|
|
|
'schema' => [
|
|
|
'id' => ['type' => 'integer'],
|
|
|
- 'username' => ['type' => 'string'],
|
|
|
+ 'name' => ['type' => 'string'],
|
|
|
'_constraints' => [
|
|
|
'primary' => ['type' => 'primary', 'columns' => ['id']]
|
|
|
]
|
|
|
@@ -642,4 +642,55 @@ class HasManyTest extends TestCase
|
|
|
}
|
|
|
$this->assertEquals($expected, $query->clause('select'));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Tests that unlinking calls the right methods
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testUnlinkSuccess()
|
|
|
+ {
|
|
|
+ $articles = TableRegistry::get('Articles');
|
|
|
+ $assoc = $this->author->hasMany('Articles', [
|
|
|
+ 'sourceTable' => $this->author,
|
|
|
+ 'targetTable' => $articles
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $entity = $this->author->get(1, ['contain' => 'Articles']);
|
|
|
+ $initial = $entity->articles;
|
|
|
+ $this->assertCount(2, $initial);
|
|
|
+
|
|
|
+ $assoc->unlink($entity, $entity->articles);
|
|
|
+ $this->assertEmpty($entity->get('articles'), 'Property should be empty');
|
|
|
+
|
|
|
+ $new = $this->author->get(2, ['contain' => 'Articles']);
|
|
|
+ $this->assertCount(0, $new->articles, 'DB should be clean');
|
|
|
+ $this->assertSame(4, $this->author->find()->count(), 'Authors should still exist');
|
|
|
+ $this->assertSame(3, $articles->find()->count(), 'Articles should still exist');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Tests that unlink with an empty array does nothing
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testUnlinkWithEmptyArray()
|
|
|
+ {
|
|
|
+ $articles = TableRegistry::get('Articles');
|
|
|
+ $assoc = $this->author->hasMany('Articles', [
|
|
|
+ 'sourceTable' => $this->author,
|
|
|
+ 'targetTable' => $articles
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $entity = $this->author->get(1, ['contain' => 'Articles']);
|
|
|
+ $initial = $entity->articles;
|
|
|
+ $this->assertCount(2, $initial);
|
|
|
+
|
|
|
+ $assoc->unlink($entity, []);
|
|
|
+
|
|
|
+ $new = $this->author->get(1, ['contain' => 'Articles']);
|
|
|
+ $this->assertCount(2, $new->articles, 'Articles should remain linked');
|
|
|
+ $this->assertSame(4, $this->author->find()->count(), 'Authors should still exist');
|
|
|
+ $this->assertSame(3, $articles->find()->count(), 'Articles should still exist');
|
|
|
+ }
|
|
|
}
|