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
|
|
|
use Gabievi\Promocodes\Exceptions\AlreadyUsedException; |
9
|
|
|
|
10
|
|
|
class RewardableTraitTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
// |
13
|
|
|
public $user; |
14
|
|
|
|
15
|
|
|
// |
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
$this->user = User::find(1); |
21
|
|
|
$this->actingAs($this->user); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** @test */ |
25
|
|
|
public function it_returns_null_if_could_not_apply_promocode() |
26
|
|
|
{ |
27
|
|
|
$applyCode = $this->user->applyCode('INVALID-CODE'); |
28
|
|
|
|
29
|
|
|
$this->assertNull($applyCode); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @test */ |
33
|
|
|
public function it_returns_null_in_callback_if_could_not_apply_promocode() |
34
|
|
|
{ |
35
|
|
|
$this->user->applyCode('INVALID-CODE', function ($applyCode) { |
36
|
|
|
$this->assertNull($applyCode); |
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @test */ |
41
|
|
View Code Duplication |
public function it_throws_exception_if_user_already_applied_to_code() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->expectException(AlreadyUsedException::class); |
44
|
|
|
|
45
|
|
|
$promocodes = Promocodes::create(); |
46
|
|
|
$promocode = $promocodes->first(); |
47
|
|
|
|
48
|
|
|
$this->assertCount(1, $promocodes); |
49
|
|
|
|
50
|
|
|
$this->user->applyCode($promocode['code']); |
51
|
|
|
$this->user->applyCode($promocode['code']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** @test */ |
55
|
|
|
public function it_attaches_current_user_as_applied_to_promocode() |
56
|
|
|
{ |
57
|
|
|
$promocodes = Promocodes::create(); |
58
|
|
|
$promocode = $promocodes->first(); |
59
|
|
|
|
60
|
|
|
$this->assertCount(1, $promocodes); |
61
|
|
|
|
62
|
|
|
$this->user->applyCode($promocode['code']); |
63
|
|
|
|
64
|
|
|
$this->assertCount(1, $this->user->promocodes); |
65
|
|
|
|
66
|
|
|
$userPromocode = $this->user->promocodes()->first(); |
67
|
|
|
|
68
|
|
|
$this->assertNotNull($userPromocode->pivot->used_at); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @test */ |
72
|
|
View Code Duplication |
public function is_returns_promocode_with_user_if_applied_successfuly() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$promocodes = Promocodes::create(); |
75
|
|
|
$promocode = $promocodes->first(); |
76
|
|
|
|
77
|
|
|
$this->assertCount(1, $promocodes); |
78
|
|
|
|
79
|
|
|
$appliedPromocode = $this->user->applyCode($promocode['code']); |
80
|
|
|
|
81
|
|
|
$this->assertTrue($appliedPromocode instanceof Promocode); |
82
|
|
|
|
83
|
|
|
$this->assertCount(1, $appliedPromocode->users); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** @test */ |
87
|
|
View Code Duplication |
public function is_returns_promocode_with_user_in_callback_if_applied_successfuly() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$promocodes = Promocodes::create(); |
90
|
|
|
$promocode = $promocodes->first(); |
91
|
|
|
|
92
|
|
|
$this->assertCount(1, $promocodes); |
93
|
|
|
|
94
|
|
|
$this->user->applyCode($promocode['code'], function ($appliedPromocode) { |
95
|
|
|
$this->assertTrue($appliedPromocode instanceof Promocode); |
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** @test */ |
100
|
|
|
public function it_has_alias_named_reedem_code() |
101
|
|
|
{ |
102
|
|
|
$promocodes = Promocodes::create(); |
103
|
|
|
$promocode = $promocodes->first(); |
104
|
|
|
|
105
|
|
|
$this->assertCount(1, $promocodes); |
106
|
|
|
|
107
|
|
|
$this->user->redeemCode($promocode['code']); |
108
|
|
|
|
109
|
|
|
$this->assertCount(1, $this->user->promocodes); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.