|
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
|
|
View Code Duplication |
public function it_throws_exception_if_user_tries_to_apply_code_twice() |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
$this->expectException(AlreadyUsedException::class); |
|
41
|
|
|
|
|
42
|
|
|
$user = User::find(1); |
|
43
|
|
|
$this->actingAs($user); |
|
44
|
|
|
|
|
45
|
|
|
$promocodes = Promocodes::create(); |
|
|
|
|
|
|
46
|
|
|
$promocode = $promocodes->first(); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertCount(1, $promocodes); |
|
49
|
|
|
|
|
50
|
|
|
Promocodes::apply($promocode['code']); |
|
51
|
|
|
Promocodes::apply($promocode['code']); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** @test */ |
|
55
|
|
|
public function it_attaches_authenticated_user_as_applied_to_promocode() |
|
56
|
|
|
{ |
|
57
|
|
|
$user = User::find(1); |
|
58
|
|
|
$this->actingAs($user); |
|
59
|
|
|
|
|
60
|
|
|
$promocodes = Promocodes::create(); |
|
|
|
|
|
|
61
|
|
|
$promocode = $promocodes->first(); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertCount(1, $promocodes); |
|
64
|
|
|
|
|
65
|
|
|
Promocodes::apply($promocode['code']); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertCount(1, $user->promocodes); |
|
68
|
|
|
|
|
69
|
|
|
$userPromocode = $user->promocodes()->first(); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertNotNull($userPromocode->pivot->used_at); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** @test */ |
|
75
|
|
View Code Duplication |
public function is_returns_promocode_with_user_if_applied_successfuly() |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$user = User::find(1); |
|
78
|
|
|
$this->actingAs($user); |
|
79
|
|
|
|
|
80
|
|
|
$promocodes = Promocodes::create(); |
|
|
|
|
|
|
81
|
|
|
$promocode = $promocodes->first(); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertCount(1, $promocodes); |
|
84
|
|
|
|
|
85
|
|
|
$appliedPromocode = Promocodes::apply($promocode['code']); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertTrue($appliedPromocode instanceof Promocode); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertCount(1, $appliedPromocode->users); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** @test */ |
|
93
|
|
View Code Duplication |
public function it_has_alias_named_reedem() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$user = User::find(1); |
|
96
|
|
|
$this->actingAs($user); |
|
97
|
|
|
|
|
98
|
|
|
$promocodes = Promocodes::create(); |
|
|
|
|
|
|
99
|
|
|
$promocode = $promocodes->first(); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertCount(1, $promocodes); |
|
102
|
|
|
|
|
103
|
|
|
Promocodes::redeem($promocode['code']); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertCount(1, $user->promocodes); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.