|
|
@@ -4125,6 +4125,66 @@ class TableTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Integration test for replacing entities with HasMany.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testReplaceHasMany()
|
|
|
+ {
|
|
|
+ $authors = TableRegistry::get('Authors');
|
|
|
+ $articles = TableRegistry::get('Articles');
|
|
|
+
|
|
|
+ $authors->hasMany('Articles', [
|
|
|
+ 'foreignKey' => 'author_id'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $author = $authors->newEntity(['name' => 'mylux']);
|
|
|
+ $author = $authors->save($author);
|
|
|
+
|
|
|
+ $newArticles = $articles->newEntities(
|
|
|
+ [
|
|
|
+ [
|
|
|
+ 'title' => 'New bakery next corner',
|
|
|
+ 'body' => 'They sell tastefull cakes'
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'title' => 'Spicy cake recipe',
|
|
|
+ 'body' => 'chocolate and peppers'
|
|
|
+ ]
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ $sizeArticles = count($newArticles);
|
|
|
+
|
|
|
+ $this->assertTrue($authors->Articles->link($author, $newArticles));
|
|
|
+
|
|
|
+ $this->assertEquals($authors->Articles->findAllByAuthorId($author->id)->count(), $sizeArticles);
|
|
|
+ $this->assertEquals(count($author->articles), $sizeArticles);
|
|
|
+
|
|
|
+ $newArticles = array_merge(
|
|
|
+ $author->articles,
|
|
|
+ $articles->newEntities(
|
|
|
+ [
|
|
|
+ [
|
|
|
+ 'title' => 'Cheese cake recipe',
|
|
|
+ 'body' => 'The secrets of mixing salt and sugar'
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'title' => 'Not another piece of cake',
|
|
|
+ 'body' => 'This is the best'
|
|
|
+ ]
|
|
|
+ ]
|
|
|
+ )
|
|
|
+ );
|
|
|
+ unset($newArticles[0]);
|
|
|
+
|
|
|
+ $this->assertTrue($authors->Articles->replace($author, $newArticles));
|
|
|
+ $this->assertEquals(count($newArticles), count($author->articles));
|
|
|
+ $this->assertEquals((new Collection($newArticles))->extract('title'), (new Collection($author->articles))->extract('title'));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
* Integration test to show how to unlink a single record from a belongsToMany
|
|
|
*
|
|
|
* @return void
|
|
|
@@ -4276,60 +4336,6 @@ class TableTest extends TestCase
|
|
|
$this->assertEquals(3, $article->tags[1]->id);
|
|
|
}
|
|
|
|
|
|
- public function testReplaceLinksHasMany()
|
|
|
- {
|
|
|
- $authors = TableRegistry::get('Authors');
|
|
|
- $articles = TableRegistry::get('Articles');
|
|
|
-
|
|
|
- $authors->hasMany('Articles', [
|
|
|
- 'foreignKey' => 'author_id'
|
|
|
- ]);
|
|
|
-
|
|
|
- $author = $authors->newEntity(['name' => 'mylux']);
|
|
|
- $author = $authors->save($author);
|
|
|
-
|
|
|
- $newArticles = $articles->newEntities(
|
|
|
- [
|
|
|
- [
|
|
|
- 'title' => 'New bakery next corner',
|
|
|
- 'body' => 'They sell tastefull cakes'
|
|
|
- ],
|
|
|
- [
|
|
|
- 'title' => 'Spicy cake recipe',
|
|
|
- 'body' => 'chocolate and peppers'
|
|
|
- ]
|
|
|
- ]
|
|
|
- );
|
|
|
-
|
|
|
- $sizeArticles = count($newArticles);
|
|
|
-
|
|
|
- $this->assertTrue($authors->Articles->link($author, $newArticles));
|
|
|
-
|
|
|
- $this->assertEquals($authors->Articles->findAllByAuthorId($author->id)->count(), $sizeArticles);
|
|
|
- $this->assertEquals(count($author->articles), $sizeArticles);
|
|
|
-
|
|
|
- $newArticles = array_merge(
|
|
|
- $author->articles,
|
|
|
- $articles->newEntities(
|
|
|
- [
|
|
|
- [
|
|
|
- 'title' => 'Cheese cake recipe',
|
|
|
- 'body' => 'The secrets of mixing salt and sugar'
|
|
|
- ],
|
|
|
- [
|
|
|
- 'title' => 'Not another piece of cake',
|
|
|
- 'body' => 'This is the best'
|
|
|
- ]
|
|
|
- ]
|
|
|
- )
|
|
|
- );
|
|
|
- unset($newArticles[0]);
|
|
|
-
|
|
|
- $this->assertTrue($authors->Articles->replaceLinks($author, $newArticles));
|
|
|
- $this->assertEquals(count($newArticles), count($author->articles));
|
|
|
- $this->assertEquals((new Collection($newArticles))->extract('title'), (new Collection($author->articles))->extract('title'));
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Tests that it is possible to call find with no arguments
|
|
|
*
|