RewardableTraitTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 32
loc 112
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A it_returns_false_if_could_not_apply_promocode() 0 6 1
A it_returns_null_in_callback_if_could_not_apply_promocode() 0 6 1
A it_can_be_user_multiple_times_if_it_is_not_disposable() 10 10 1
A it_returns_false_for_second_use_of_disposable_code() 0 10 1
A it_attaches_current_user_as_applied_to_promocode() 0 15 1
A it_returns_promocode_with_user_in_callback_if_applied_successfuly() 11 11 1
A is_returns_promocode_with_user_if_applied_successfuly() 0 13 1
A it_has_alias_named_reedem_code() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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

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...
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()
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...
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()
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...
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()
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...
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