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 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); } } }
/** * 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); } } }
/** * Remove the specified Post from storage. * * @param int $id * @return Response */ public function destroy($id) { $post = Post::find($id); if ($post) { $post->delete(); $params = Request::all(); return $post; } throw new CrudException('post:destroy'); }
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]); } } }
/** * Show the main page. * * @return Response */ public function index() { $posts = Post::all()->sortByDesc('created_at')->toArray(); $data = ['posts' => $posts]; return view('blog.index')->with($data); }
/** * 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]); }