1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gabievi\Promocodes\Tests; |
4
|
|
|
|
5
|
|
|
use Promocodes; |
6
|
|
|
use Gabievi\Promocodes\Models\Promocode; |
7
|
|
|
use Gabievi\Promocodes\Tests\Models\User; |
8
|
|
|
|
9
|
|
|
class RewardableTraitTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
// |
12
|
|
|
public $user; |
13
|
|
|
|
14
|
|
|
// |
15
|
|
|
public function setUp(): void |
16
|
|
|
{ |
17
|
|
|
parent::setUp(); |
18
|
|
|
|
19
|
|
|
$this->user = User::find(1); |
20
|
|
|
$this->actingAs($this->user); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** @test */ |
24
|
|
|
public function it_returns_false_if_could_not_apply_promocode() |
25
|
|
|
{ |
26
|
|
|
$applyCode = $this->user->applyCode('INVALID-CODE'); |
27
|
|
|
|
28
|
|
|
$this->assertFalse($applyCode); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @test */ |
32
|
|
|
public function it_returns_null_in_callback_if_could_not_apply_promocode() |
33
|
|
|
{ |
34
|
|
|
$this->user->applyCode('INVALID-CODE', function ($applyCode) { |
35
|
|
|
$this->assertNull($applyCode); |
36
|
|
|
}); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @test */ |
40
|
|
View Code Duplication |
public function it_can_be_user_multiple_times_if_it_is_not_disposable() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$promocodes = Promocodes::create(); |
43
|
|
|
$promocode = $promocodes->first(); |
44
|
|
|
|
45
|
|
|
$this->assertCount(1, $promocodes); |
46
|
|
|
|
47
|
|
|
$this->user->applyCode($promocode['code']); |
48
|
|
|
$this->user->applyCode($promocode['code']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @test */ |
52
|
|
|
public function it_returns_false_for_second_use_of_disposable_code() |
53
|
|
|
{ |
54
|
|
|
$promocodes = Promocodes::setDisposable()->create(); |
55
|
|
|
$promocode = $promocodes->first(); |
56
|
|
|
|
57
|
|
|
$this->assertCount(1, $promocodes); |
58
|
|
|
|
59
|
|
|
$this->assertInstanceOf(Promocode::class, $this->user->applyCode($promocode['code'])); |
60
|
|
|
$this->assertFalse($this->user->applyCode($promocode['code'])); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @test */ |
64
|
|
|
public function it_attaches_current_user_as_applied_to_promocode() |
65
|
|
|
{ |
66
|
|
|
$promocodes = Promocodes::create(); |
67
|
|
|
$promocode = $promocodes->first(); |
68
|
|
|
|
69
|
|
|
$this->assertCount(1, $promocodes); |
70
|
|
|
|
71
|
|
|
$this->user->applyCode($promocode['code']); |
72
|
|
|
|
73
|
|
|
$this->assertCount(1, $this->user->promocodes); |
74
|
|
|
|
75
|
|
|
$userPromocode = $this->user->promocodes()->first(); |
76
|
|
|
|
77
|
|
|
$this->assertNotNull($userPromocode->pivot->used_at); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** @test */ |
81
|
|
|
public function is_returns_promocode_with_user_if_applied_successfuly() |
82
|
|
|
{ |
83
|
|
|
$promocodes = Promocodes::create(); |
84
|
|
|
$promocode = $promocodes->first(); |
85
|
|
|
|
86
|
|
|
$this->assertCount(1, $promocodes); |
87
|
|
|
|
88
|
|
|
$appliedPromocode = $this->user->applyCode($promocode['code']); |
89
|
|
|
|
90
|
|
|
$this->assertTrue($appliedPromocode instanceof Promocode); |
91
|
|
|
|
92
|
|
|
$this->assertCount(1, $appliedPromocode->users); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @test */ |
96
|
|
View Code Duplication |
public function it_returns_promocode_with_user_in_callback_if_applied_successfuly() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$promocodes = Promocodes::create(); |
99
|
|
|
$promocode = $promocodes->first(); |
100
|
|
|
|
101
|
|
|
$this->assertCount(1, $promocodes); |
102
|
|
|
|
103
|
|
|
$this->user->applyCode($promocode['code'], function ($appliedPromocode) { |
104
|
|
|
$this->assertTrue($appliedPromocode instanceof Promocode); |
105
|
|
|
}); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** @test */ |
109
|
|
View Code Duplication |
public function it_has_alias_named_reedem_code() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$promocodes = Promocodes::create(); |
112
|
|
|
$promocode = $promocodes->first(); |
113
|
|
|
|
114
|
|
|
$this->assertCount(1, $promocodes); |
115
|
|
|
|
116
|
|
|
$this->user->redeemCode($promocode['code']); |
117
|
|
|
|
118
|
|
|
$this->assertCount(1, $this->user->promocodes); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.