-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRentsTest.php
130 lines (112 loc) · 3.23 KB
/
RentsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Tests\Feature\Me;
use App\Models\User;
use Database\Factories\BookFactory;
use Database\Factories\SubscriptionPlanFactory;
use Database\Factories\UserFactory;
use Tests\TestCase;
/**
* @see \App\Http\Controllers\Api\Me\RentController
*/
class RentsTest extends TestCase
{
protected User $user;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
parent::setUp();
$this->user = UserFactory::new()->create();
}
public function testIndex(): void
{
$this->actingAs($this->user);
$books = BookFactory::new()->count(2)->create();
foreach ($books as $book) {
$book->rent($this->user);
}
$this->getJson(route('api.me.rents.index', ['sort' => '-id']))
->assertSuccessful()
->assertJsonStructure([
'data' => [
[
'id',
'created_at',
'book' => [
'id',
'title',
],
],
],
])
->assertJsonCount(2, 'data')
->assertJson([
'data' => [
[
'book' => [
'id' => $books[1]->id,
],
],
[
'book' => [
'id' => $books[0]->id,
],
],
],
]);
}
public function testStore(): void
{
$this->actingAs($this->user);
$book = BookFactory::new()->create();
$subscriptionPlan = SubscriptionPlanFactory::new()->create([
'max_book_price' => $book->price + 1,
]);
$subscriptionPlan->subscribe($this->user);
$this->postJson(route('api.me.rents.store'), ['book_id' => $book->id])
->assertSuccessful()
->assertJsonStructure([
'data' => [
'id',
'created_at',
'book' => [
'id',
],
],
])
->assertJson([
'data' => [
'book' => [
'id' => $book->id,
],
],
]);
$this->assertTrue($this->user->rents()->where('book_id', $book->id)->exists());
}
public function testShow(): void
{
$this->actingAs($this->user);
$book = BookFactory::new()->create();
$rent = $book->rent($this->user);
$this->getJson(route('api.me.rents.show', [$rent]))
->assertSuccessful()
->assertJsonStructure([
'data' => [
'id',
'created_at',
'book' => [
'id',
],
],
])
->assertJson([
'data' => [
'id' => $rent->id,
'book' => [
'id' => $book->id,
],
],
]);
}
}