/**
  * @test
  */
 function it_creates_a_new_nested_model_related_as_belongs_to()
 {
     $data = ['title' => 'created', 'body' => 'fresh', 'comments' => [['title' => 'created comment', 'body' => 'comment body', 'author' => ['name' => 'new author']]]];
     $post = Post::create($data);
     $this->assertInstanceOf(Post::class, $post);
     $this->assertTrue($post->exists);
     $author = Author::latest()->first();
     $this->assertInstanceOf(Author::class, $author, "Author model should have been created");
     $this->seeInDatabase('posts', ['id' => $post->id, 'title' => 'created', 'body' => 'fresh']);
     $this->seeInDatabase('comments', ['post_id' => $post->id, 'title' => 'created comment', 'body' => 'comment body', 'author_id' => $author->id]);
     $this->seeInDatabase('authors', ['id' => $author->id, 'name' => 'new author']);
 }
 /**
  * @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']);
 }
Example #4
0
 /**
  * @param string $name
  * @param string $gender
  * @return Author
  */
 protected function createAuthor($name = 'Test Author', $gender = 'm')
 {
     return Author::create(['name' => $name, 'gender' => $gender]);
 }