-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllowBookRentRuleTest.php
90 lines (65 loc) · 2.2 KB
/
AllowBookRentRuleTest.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
<?php
namespace Tests\Unit\Rules;
use App\Models\Book;
use App\Models\User;
use App\Rules\AllowBookRentRule;
use Database\Factories\BookFactory;
use Database\Factories\SubscriptionPlanFactory;
use Database\Factories\UserFactory;
use Tests\TestCase;
class AllowBookRentRuleTest extends TestCase
{
protected User $user;
protected Book $book;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
parent::setUp();
$this->user = UserFactory::new()->create();
$this->book = BookFactory::new()->create();
}
public function testBookDoesNotExist()
{
$rule = new AllowBookRentRule($this->user);
$this->book->delete();
$this->assertFalse($rule->passes('book', $this->book->id));
}
public function testNoSubscription()
{
$rule = new AllowBookRentRule($this->user);
$this->assertFalse($rule->passes('book', $this->book->id));
}
public function testDisallowedBySubscriptionPlan()
{
$subscriptionPlan = SubscriptionPlanFactory::new()->create([
'max_book_price' => $this->book->price - 1,
]);
$subscriptionPlan->subscribe($this->user);
$rule = new AllowBookRentRule($this->user);
$this->assertFalse($rule->passes('book', $this->book->id));
}
public function testNoAvailableRentSlots()
{
$subscriptionPlan = SubscriptionPlanFactory::new()->create([
'max_book_price' => $this->book->price + 1,
'max_rent_count' => 1,
]);
$subscriptionPlan->subscribe($this->user);
$extraBook = BookFactory::new()->create();
$extraBook->rent($this->user);
$rule = new AllowBookRentRule($this->user);
$this->assertFalse($rule->passes('book', $this->book->id));
}
public function testAllow()
{
$subscriptionPlan = SubscriptionPlanFactory::new()->create([
'max_book_price' => $this->book->price + 1,
]);
$subscriptionPlan->subscribe($this->user);
$rule = new AllowBookRentRule($this->user);
$this->assertTrue($rule->passes('book', $this->book->id));
$this->assertSame($this->book->id, $rule->getBook()->id);
}
}