Skip to content

Commit

Permalink
话题单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
liyu001989 committed Aug 7, 2020
1 parent 74330ce commit 6358f4c
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 5 deletions.
22 changes: 17 additions & 5 deletions app/Http/Resources/TopicResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ class TopicResource extends JsonResource
{
public function toArray($request)
{
$data = parent::toArray($request);
$data['user'] = new UserResource($this->whenLoaded('user'));
$data['category'] = new CategoryResource($this->whenLoaded('category'));

return $data;
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'category_id' => (int)$this->category_id,
'user_id' => (int)$this->user_id,
'reply_count' => (int)$this->reply_count,
'view_count' => (int)$this->view_count,
'last_reply_user_id' => (int)$this->last_reply_user_id,
'order' => (int)$this->order,
'excerpt' => $this->excerpt,
'slug' => $this->slug,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
'user' => new UserResource($this->whenLoaded('user')),
'category' => new CategoryResource($this->whenLoaded('category')),
];
}
}
106 changes: 106 additions & 0 deletions tests/Feature/TopicApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\User;
use App\Models\Topic;
use Tests\Traits\ActingJWTUser;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class TopicApiTest extends TestCase
{
use RefreshDatabase;
use ActingJWTUser;

protected $user;

protected function setUp(): void
{
parent::setUp();

$this->user = factory(User::class)->create();
}

public function testStoreTopic()
{
$data = ['category_id' => 1, 'body' => 'test body', 'title' => 'test title'];

$response = $this->JWTActingAs($this->user)
->json('POST', '/api/v1/topics', $data);

$assertData = [
'category_id' => 1,
'user_id' => $this->user->id,
'title' => 'test title',
'body' => clean('test body', 'user_topic_body'),
];

$response->assertStatus(201)
->assertJsonFragment($assertData);
}

public function testUpdateTopic()
{
$topic = $this->makeTopic();

$editData = ['category_id' => 2, 'body' => 'edit body', 'title' => 'edit title'];

$response = $this->JWTActingAs($this->user)
->json('PATCH', '/api/v1/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/v1/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/v1/topics');

$response->assertStatus(200)
->assertJsonStructure(['data', 'meta']);
}

public function testDeleteTopic()
{
$topic = $this->makeTopic();
$response = $this->JWTActingAs($this->user)
->json('DELETE', '/api/v1/topics/'.$topic->id);
$response->assertStatus(204);

$response = $this->json('GET', '/api/v1/topics/'.$topic->id);
$response->assertStatus(404);
}
}
16 changes: 16 additions & 0 deletions tests/Traits/ActingJWTUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Traits;

use App\Models\User;

trait ActingJWTUser
{
public function JWTActingAs(User $user)
{
$token = auth('api')->fromUser($user);
$this->withHeaders(['Authorization' => 'Bearer '.$token]);

return $this;
}
}

0 comments on commit 6358f4c

Please sign in to comment.