private function setUpObj() { $obj = new Category(); $obj->title = 'uncategorized'; $obj->save(); return $obj; }
/** * Tes menambahkan Category */ public function testAdd() { $obj = new Category(); $obj->title = 'Title Unimportant'; $saved = $obj->save(); $this->assertTrue($saved); $obj_2 = Category::find($obj->id); if ($obj_2 !== null) { $this->assertEquals($obj, $obj_2); } // testing slug already exist $obj_3 = new Category(); $obj_3->title = 'Title Unimportant'; $saved = $obj_3->save(); $this->assertTrue($saved); $this->assertEquals('title-unimportant-new', $obj_3->slug); // testing slug already exist #2 $obj_3 = new Category(); $obj_3->title = 'Title Unimportant'; $saved = $obj_3->save(); $this->assertTrue($saved); $this->assertEquals('title-unimportant-new-new', $obj_3->slug); }
private function setUpObj() { $slug = 'slug_' . rand(0, 10000); $cat = Category::create(['title' => $slug, 'slug' => $slug]); $tag = Tag::create(['title' => $slug, 'slug' => $slug]); $user = User::create(['name' => 'user', 'email' => rand(0, 10000), 'password' => 'user', 'picture' => 'user']); $obj = new Post(); $obj->title = 'The Title'; $obj->content = 'content'; $obj->description = 'description'; $obj->header_image = 'header_image'; $obj->category_id = $cat->id; $obj->user_id = $user->id; $obj->save(); $obj->tags()->attach($tag->id); $obj->save(); return $obj; }
/** * 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); } } }
/** * Tes menambahkan Post dan relasinya dengan Tag */ public function testTagRelation() { $cat = Category::create(['title' => 'uncategorized']); $user = User::create(['name' => 'user', 'email' => 'user', 'password' => 'user', 'picture' => 'user']); $tags = []; $tags[] = Tag::create(['title' => 'title']); $obj = new Post(); $obj->title = 'title'; $obj->category_id = $cat->id; $obj->user_id = $user->id; $saved = $obj->save(); $this->assertTrue($saved); if ($saved) { $ids = Collection::make($tags)->map(function ($tag) { return $tag->id; })->toArray(); $obj->tags()->attach($ids); $temp_tags = $obj->tags()->get(); $this->assertEquals(count($tags), count($temp_tags)); foreach ($temp_tags as $key => $temp_tag) { $this->assertEquals($temp_tag->id, $tags[$key]->id); } } }
/** * Get posts of the category. * * @return Response */ public function getPosts($id) { $category = Category::find($id); if ($category) { $posts = $category->posts()->get(); return $posts; } throw new CrudException('category:getPosts'); }
/** * Run the database seeds. * * @return void */ public function run() { Category::create(array('title' => 'uncategorized', 'slug' => 'uncategorized')); Category::create(array('title' => 'story', 'slug' => 'story')); Category::create(array('title' => 'geeks', 'slug' => 'geeks')); }
/** * Display the specified Post by Category. * * @param int $id * @return Response */ public function category($slug) { $category = Category::where('slug', '=', $slug)->first(); $posts = Post::where('category_id', '=', $category->id)->get(); return $posts; }
public function testGetPosts() { $cat = Category::create(['title' => 'uncategorized']); $user = User::create(['name' => 'user', 'email' => 'user', 'password' => 'user', 'picture' => 'user']); $post = Post::create(['title' => 'test_tag', 'category_id' => $cat->id, 'user_id' => $user->id]); $this->obj->posts()->attach($post->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]); } } }