Beispiel #1
0
 private function setUpObj()
 {
     $obj = new Tag();
     $obj->title = 'uncategorized';
     $obj->save();
     return $obj;
 }
Beispiel #2
0
 /**
  * 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);
         }
     }
 }
Beispiel #3
0
 /**
  * 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);
         }
     }
 }
Beispiel #4
0
 /**
  * Get posts of the tag.
  *
  * @return Response
  */
 public function getPosts($id)
 {
     $tag = Tag::find($id);
     if ($tag) {
         $posts = $tag->posts()->get();
         return $posts;
     }
     throw new CrudException('tag:getPosts');
 }
Beispiel #5
0
 public function testUpdate()
 {
     $tags[] = Tag::create(['title' => 'tagx']);
     $tags[] = Tag::create(['title' => 'tagy']);
     $tags[] = Tag::create(['title' => 'tagz']);
     // tes pemanggilan update sukses
     $params = $this->setUpParams();
     $params['id'] = $this->obj->id . '000';
     $params['tags'] = array_map(function ($x) {
         return $x->id;
     }, $tags);
     $response = $this->call('PUT', '/' . self::$endpoint . '/' . $this->obj->id, $params);
     $this->assertEquals(200, $response->getStatusCode());
     $result = $response->getOriginalContent()->toArray();
     // tes apakah hasil return adalah yang sesuai
     unset($params['tags']);
     foreach ($params as $key => $val) {
         $this->assertArrayHasKey($key, $result);
         if (isset($result[$key]) && $key != 'created_at' && $key != 'updated_at' && $key != 'slug') {
             $this->assertEquals($val, $result[$key]);
         }
     }
     $this->assertEquals(3, count($result['tags']));
     // tes tak ada yang dicari
     $response = $this->call('GET', '/' . self::$endpoint . '/696969', $params);
     $this->assertEquals(404, $response->getStatusCode());
 }
Beispiel #6
0
 /**
  * Display the specified Post by Category.
  *
  * @param  int  $id
  * @return Response
  */
 public function tag($slug)
 {
     $posts = Tag::where('slug', '=', $slug)->first()->posts()->get();
     return $posts;
 }
Beispiel #7
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     Tag::create(array('title' => 'funny', 'slug' => 'funny'));
     Tag::create(array('title' => 'sad', 'slug' => 'sad'));
     Tag::create(array('title' => 'heartwarming', 'slug' => 'heartwarming'));
 }