it_attaches_authenticated_user_as_applied_to_promocode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method actingAs() does not seem to exist on object<Gabievi\Promocode...plyPromocodeToUserTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method actingAs() does not seem to exist on object<Gabievi\Promocode...plyPromocodeToUserTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method actingAs() does not seem to exist on object<Gabievi\Promocode...plyPromocodeToUserTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
74
    {
75
        $user = User::find(1);
76
        $this->actingAs($user);
0 ignored issues
show
Bug introduced by
The method actingAs() does not seem to exist on object<Gabievi\Promocode...plyPromocodeToUserTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method actingAs() does not seem to exist on object<Gabievi\Promocode...plyPromocodeToUserTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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