1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gabievi\Promocodes\Tests; |
4
|
|
|
|
5
|
|
|
use Gabievi\Promocodes\Models\Promocode; |
6
|
|
|
use Gabievi\Promocodes\Tests\Models\User; |
7
|
|
|
use Gabievi\Promocodes\Facades\Promocodes; |
8
|
|
|
use Gabievi\Promocodes\Exceptions\AlreadyUsedException; |
9
|
|
|
use Gabievi\Promocodes\Exceptions\UnauthenticatedException; |
10
|
|
|
|
11
|
|
|
class ApplyPromocodeToUserTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** @test */ |
14
|
|
|
public function it_throws_exception_if_user_is_not_authenticated() |
15
|
|
|
{ |
16
|
|
|
$this->expectException(UnauthenticatedException::class); |
17
|
|
|
|
18
|
|
|
$promocodes = Promocodes::create(); |
19
|
|
|
$promocode = $promocodes->first(); |
20
|
|
|
|
21
|
|
|
$this->assertCount(1, $promocodes); |
22
|
|
|
|
23
|
|
|
Promocodes::apply($promocode['code']); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** @test */ |
27
|
|
|
public function it_returns_false_if_promocode_doesnt_exist() |
28
|
|
|
{ |
29
|
|
|
$user = User::find(1); |
30
|
|
|
$this->actingAs($user); |
31
|
|
|
|
32
|
|
|
$appliedPromocode = Promocodes::apply('INVALID-CODE'); |
33
|
|
|
|
34
|
|
|
$this->assertFalse($appliedPromocode); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @test */ |
38
|
|
|
public function it_returns_false_if_user_tries_to_apply_code_twice() |
39
|
|
|
{ |
40
|
|
|
$user = User::find(1); |
41
|
|
|
$this->actingAs($user); |
42
|
|
|
|
43
|
|
|
$promocodes = Promocodes::setDisposable()->create(); |
44
|
|
|
$promocode = $promocodes->first(); |
45
|
|
|
|
46
|
|
|
$this->assertCount(1, $promocodes); |
47
|
|
|
|
48
|
|
|
$this->assertInstanceOf(Promocode::class, Promocodes::apply($promocode['code'])); |
49
|
|
|
$this->assertFalse(Promocodes::apply($promocode['code'])); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @test */ |
53
|
|
|
public function it_attaches_authenticated_user_as_applied_to_promocode() |
54
|
|
|
{ |
55
|
|
|
$user = User::find(1); |
56
|
|
|
$this->actingAs($user); |
57
|
|
|
|
58
|
|
|
$promocodes = Promocodes::create(); |
59
|
|
|
$promocode = $promocodes->first(); |
60
|
|
|
|
61
|
|
|
$this->assertCount(1, $promocodes); |
62
|
|
|
|
63
|
|
|
Promocodes::apply($promocode['code']); |
64
|
|
|
|
65
|
|
|
$this->assertCount(1, $user->promocodes); |
66
|
|
|
|
67
|
|
|
$userPromocode = $user->promocodes()->first(); |
68
|
|
|
|
69
|
|
|
$this->assertNotNull($userPromocode->pivot->used_at); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** @test */ |
73
|
|
View Code Duplication |
public function is_returns_promocode_with_user_if_applied_successfuly() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$user = User::find(1); |
76
|
|
|
$this->actingAs($user); |
77
|
|
|
|
78
|
|
|
$promocodes = Promocodes::create(); |
79
|
|
|
$promocode = $promocodes->first(); |
80
|
|
|
|
81
|
|
|
$this->assertCount(1, $promocodes); |
82
|
|
|
|
83
|
|
|
$appliedPromocode = Promocodes::apply($promocode['code']); |
84
|
|
|
|
85
|
|
|
$this->assertTrue($appliedPromocode instanceof Promocode); |
86
|
|
|
|
87
|
|
|
$this->assertCount(1, $appliedPromocode->users); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** @test */ |
91
|
|
|
public function it_has_alias_named_reedem() |
92
|
|
|
{ |
93
|
|
|
$user = User::find(1); |
94
|
|
|
$this->actingAs($user); |
95
|
|
|
|
96
|
|
|
$promocodes = Promocodes::create(); |
97
|
|
|
$promocode = $promocodes->first(); |
98
|
|
|
|
99
|
|
|
$this->assertCount(1, $promocodes); |
100
|
|
|
|
101
|
|
|
Promocodes::redeem($promocode['code']); |
102
|
|
|
|
103
|
|
|
$this->assertCount(1, $user->promocodes); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.