From 9f234190496a20cccd4c1b2b1886b8eaea958645 Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Sat, 16 Jun 2018 10:54:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Feature/TopicApiTest.php | 115 +++++++++++++++++++++++++++++++++ tests/Traits/ActingJWTUser.php | 16 +++++ 2 files changed, 131 insertions(+) create mode 100644 tests/Feature/TopicApiTest.php create mode 100644 tests/Traits/ActingJWTUser.php diff --git a/tests/Feature/TopicApiTest.php b/tests/Feature/TopicApiTest.php new file mode 100644 index 0000000..45a0c74 --- /dev/null +++ b/tests/Feature/TopicApiTest.php @@ -0,0 +1,115 @@ +user = factory(User::class)->create(); + } + + public function testStoreTopic() + { + $data = ['category_id' => 1, 'body' => 'test body', 'title' => 'test title']; + + $token = \Auth::guard('api')->fromUser($this->user); + $request = $this->withHeaders(['Authorization' => 'Bearer ' . $token]) + ->json('POST', '/api/topics', $data); + + $assertData = [ + 'category_id' => 1, + 'user_id' => $this->user->id, + 'title' => 'test title', + 'body' => clean('test body', 'user_topic_body'), + ]; + + $request->assertStatus(201) + ->assertJsonFragment($assertData); + } + + /** + * A basic test example. + * + * @return void + */ + public function testExample() + { + $this->assertTrue(true); + } + + public function testUpdateTopic() + { + $topic = $this->makeTopic(); + + $editData = ['category_id' => 2, 'body' => 'edit body', 'title' => 'edit title']; + + $response = $this->JWTActingAs($this->user) + ->json('PATCH', '/api/topics/' . $topic->id, $editData); + + $assertData = [ + 'category_id' => 2, + 'user_id' => $this->user->id, + 'title' => 'edit title', + 'body' => clean('edit body', 'user_topic_body'), + ]; + + $response->assertStatus(200) + ->assertJsonFragment($assertData); + } + + protected function makeTopic() + { + return factory(Topic::class)->create([ + 'user_id' => $this->user->id, + 'category_id' => 1, + ]); + } + + + public function testShowTopic() + { + $topic = $this->makeTopic(); + $response = $this->json('GET', '/api/topics/' . $topic->id); + + $assertData = [ + 'category_id' => $topic->category_id, + 'user_id' => $topic->user_id, + 'title' => $topic->title, + 'body' => $topic->body, + ]; + + $response->assertStatus(200) + ->assertJsonFragment($assertData); + } + + public function testIndexTopic() + { + $response = $this->json('GET', '/api/topics'); + + $response->assertStatus(200) + ->assertJsonStructure(['data', 'meta']); + } + + public function testDeleteTopic() + { + $topic = $this->makeTopic(); + $response = $this->JWTActingAs($this->user) + ->json('DELETE', '/api/topics/' . $topic->id); + $response->assertStatus(204); + + $response = $this->json('GET', '/api/topics/' . $topic->id); + $response->assertStatus(404); + } + +} diff --git a/tests/Traits/ActingJWTUser.php b/tests/Traits/ActingJWTUser.php new file mode 100644 index 0000000..72ddc25 --- /dev/null +++ b/tests/Traits/ActingJWTUser.php @@ -0,0 +1,16 @@ +fromUser($user); + $this->withHeaders(['Authorization' => 'Bearer ' . $token]); + + return $this; + } +} \ No newline at end of file