/** * Tes menambahkan Tag dan relasinya dengan Post */ public function testPostRelation() { $cat = Category::create(['title' => 'uncategorized']); $user = User::create(['name' => 'user', 'email' => 'user', 'password' => 'user', 'picture' => 'user']); $posts = []; $posts[] = Post::create(['title' => 'title', 'category_id' => $cat->id, 'user_id' => $user->id]); $obj = new Tag(); $obj->title = 'title'; $saved = $obj->save(); $this->assertTrue($saved); if ($saved) { $ids = Collection::make($posts)->map(function ($post) { return $post->id; })->toArray(); $obj->posts()->attach($ids); $temp_posts = $obj->posts()->get(); $this->assertEquals(count($posts), count($temp_posts)); foreach ($temp_posts as $key => $temp_post) { $this->assertEquals($temp_post->id, $posts[$key]->id); } } }
public function testGetPosts() { $user = User::create(['name' => 'user', 'email' => 'user', 'password' => 'user', 'picture' => 'user']); $post = Post::create(['title' => 'test_category', 'category_id' => $this->obj->id, 'user_id' => $user->id]); // tes pemanggilan getPosts sukses $response = $this->call('GET', '/' . self::$endpoint . '/' . $this->obj->id . '/posts'); $this->assertEquals(200, $response->getStatusCode()); $result = $response->getOriginalContent()->toArray(); // tes apakah hasil return adalah yang sesuai foreach ($post->attributesToArray() as $key => $val) { $this->assertArrayHasKey($key, $result[0]); if (isset($result[0][$key]) && $key != 'created_at' && $key != 'updated_at') { $this->assertEquals($val, $result[0][$key]); } } }
/** * Run the database seeds. * * @return void */ public function run() { // $this->command->info(Post::all()->count()); $p = Post::create(array('title' => 'This is My First Post', 'description' => 'This is my first post yo.', 'category_id' => '2', 'user_id' => '1', 'content' => 'Hello, this is my first post. So long and good night.', 'header_image' => 'blog/img/post-sample-image.jpg')); $p->tags()->attach([1, 2, 3]); }