/** * Tes menambahkan Post */ public function testAdd() { $cat = Category::create(['title' => 'uncategorized']); $user = User::create(['name' => 'user', 'email' => 'user', 'password' => 'user', 'picture' => 'user']); $obj = new Post(); $obj->title = 'Title Unimportant'; $obj->content = 'content'; $obj->description = 'description'; $obj->header_image = 'header_image'; $obj->category_id = $cat->id; $obj->user_id = $user->id; $saved = $obj->save(); $this->assertTrue($saved); $obj_2 = Post::find($obj->id); if ($obj_2 !== null) { $this->assertEquals($obj, $obj_2); } $this->assertArrayHasKey('category', $obj_2->toArray()); $this->assertArrayHasKey('tags', $obj_2->toArray()); $this->assertArrayHasKey('user', $obj_2->toArray()); $this->assertEquals('title-unimportant', $obj_2->slug); $this->assertEquals($user->name, $obj_2->toArray()['user']['name']); $cat_2 = $obj_2->getCategory(); $this->assertEquals($cat->id, $cat_2->id); $this->assertEquals(1, $cat_2->posts()->count()); // testing slug already exist $obj_3 = new Post(); $obj_3->title = 'Title Unimportant'; $obj_3->category_id = $cat->id; $obj_3->user_id = $user->id; $saved = $obj_3->save(); $this->assertTrue($saved); $this->assertEquals('title-unimportant-new', $obj_3->slug); // testing slug already exist #2 $obj_3 = new Post(); $obj_3->title = 'Title Unimportant'; $obj_3->category_id = $cat->id; $obj_3->user_id = $user->id; $saved = $obj_3->save(); $this->assertTrue($saved); $this->assertEquals('title-unimportant-new-new', $obj_3->slug); }
/** * 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'); }