/**
  * @test
  */
 function it_creates_and_updates_a_nested_relation_using_multiple_distinct_temporary_ids()
 {
     $post = $this->createPost();
     $comment = $this->createComment($post);
     $data = ['comments' => [['id' => $comment->id, 'title' => 'updated title', 'author' => ['_tmp_id' => 'auth_1', 'name' => 'new shared author', 'posts' => [['title' => 'new nested title', 'body' => 'new nested body', 'genre' => ['_tmp_id' => 'genre_2']]]]], ['title' => 'new title', 'body' => 'for new comment', 'author' => ['_tmp_id' => 'auth_1']]], 'genre' => ['_tmp_id' => 'genre_2', 'name' => 'new shared genre']];
     $updater = new ModelUpdater(Post::class);
     $updater->update($data, $post);
     $this->assertEquals(1, Author::count(), "Exactly one author should have been created");
     /** @var Author $author */
     $author = Author::first();
     $this->assertEquals(1, Genre::count(), "Exactly one tag should have been created");
     /** @var Genre $genre */
     $genre = Genre::first();
     $this->assertEquals(2, Post::count(), "Exactly two posts should exist (1 created by nesting)");
     /** @var Post $newPost */
     $newPost = Post::orderBy('id', 'desc')->first();
     $this->seeInDatabase('comments', ['id' => $comment->id, 'title' => 'updated title']);
     $this->seeInDatabase('comments', ['title' => 'new title', 'body' => 'for new comment']);
     $this->seeInDatabase('authors', ['id' => $author->id, 'name' => 'new shared author']);
     $this->seeInDatabase('posts', ['id' => $post->id, 'genre_id' => $genre->id]);
     $this->seeInDatabase('posts', ['id' => $newPost->id, 'title' => 'new nested title', 'genre_id' => $genre->id]);
     $this->seeInDatabase('genres', ['id' => $genre->id, 'name' => 'new shared genre']);
 }
 /**
  * @test
  */
 function it_creates_deeply_nested_structure_of_all_new_models()
 {
     // allow creating authors through posts
     $this->app['config']->set('nestedmodelupdater.relations.' . Post::class . '.authors', ['link-only' => false]);
     $data = ['title' => 'new title', 'body' => 'new body', 'genre' => ['name' => 'new genre'], 'comments' => [['title' => 'title 1', 'body' => 'body 1', 'author' => ['name' => 'Author B']], ['title' => 'title 2', 'body' => 'body 2', 'author' => ['name' => 'Author C']]], 'authors' => [['name' => 'Author A', 'gender' => 'f']]];
     $updater = new ModelUpdater(Post::class);
     $updater->create($data);
     // check the whole structure
     $post = Post::latest()->first();
     $this->assertInstanceOf(Post::class, $post);
     $genre = Genre::latest()->first();
     $this->assertInstanceOf(Genre::class, $genre);
     $commentAuthorB = Author::where('name', 'Author B')->first();
     $this->assertInstanceOf(Author::class, $commentAuthorB);
     $commentAuthorC = Author::where('name', 'Author C')->first();
     $this->assertInstanceOf(Author::class, $commentAuthorC);
     $this->seeInDatabase('posts', ['id' => $post->id, 'title' => 'new title', 'body' => 'new body', 'genre_id' => $genre->id]);
     $this->seeInDatabase('genres', ['id' => $genre->id, 'name' => 'new genre']);
     $this->seeInDatabase('comments', ['title' => 'title 1', 'body' => 'body 1', 'author_id' => $commentAuthorB->id]);
     $this->seeInDatabase('comments', ['title' => 'title 2', 'body' => 'body 2', 'author_id' => $commentAuthorC->id]);
     $this->seeInDatabase('authors', ['name' => 'Author A']);
 }
예제 #3
0
 /**
  * @param string $name
  * @return Genre
  */
 protected function createGenre($name = 'testing genre')
 {
     return Genre::create(['name' => $name]);
 }