CheckPromocodeValidationTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 12
loc 84
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A it_returns_false_if_promocode_is_invalid() 0 6 1
A it_returns_false_if_promocode_is_expired() 0 15 1
A it_returns_false_if_promocode_is_disposable_and_used() 0 20 1
A it_returns_promocode_model_if_validation_passes() 12 12 1
A it_returns_false_if_promocode_exceeds_quantity() 0 19 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 CheckPromocodeValidationTest 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
    /** @test */
12
    public function it_returns_false_if_promocode_is_invalid()
13
    {
14
        $checkPromocode = Promocodes::check('INVALID-CODE');
15
16
        $this->assertFalse($checkPromocode);
17
    }
18
19
    /** @test */
20
    public function it_returns_false_if_promocode_is_expired()
21
    {
22
        $promocodes = Promocodes::create();
23
        $promocode = $promocodes->first();
24
25
        Promocode::byCode($promocode['code'])->update([
26
            'expires_at' => date('Y-m-d H:i:s', strtotime('-1 day')),
27
        ]);
28
29
        $this->assertCount(1, $promocodes);
30
31
        $checkPromocode = Promocodes::check($promocode['code']);
32
33
        $this->assertFalse($checkPromocode);
34
    }
35
36
    /** @test */
37
    public function it_returns_false_if_promocode_is_disposable_and_used()
38
    {
39
        $promocodes = Promocodes::createDisposable();
40
        $promocode = $promocodes->first();
41
42
        $promocode = Promocode::byCode($promocode['code'])->first();
43
        $user = User::find(1);
44
45
        $promocode->users()->attach($user->id, [
46
            'promocode_id' => $promocode->id,
47
            'used_at' => date('Y-m-d H:i:s'),
48
        ]);
49
50
        $this->assertCount(1, $promocodes);
51
        $this->assertCount(1, $user->promocodes);
52
53
        $checkPromocode = Promocodes::check($promocode['code']);
54
55
        $this->assertFalse($checkPromocode);
56
    }
57
58
    /** @test */
59 View Code Duplication
    public function it_returns_promocode_model_if_validation_passes()
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...
60
    {
61
        $promocodes = Promocodes::create();
62
        $promocode = $promocodes->first();
63
64
        $this->assertCount(1, $promocodes);
65
66
        $checkPromocode = Promocodes::check($promocode['code']);
67
68
        $this->assertTrue($checkPromocode instanceof Promocode);
69
        $this->assertEquals($promocode['code'], $checkPromocode->code);
70
    }
71
72
    /** @test */
73
    public function it_returns_false_if_promocode_exceeds_quantity()
74
    {
75
        $promocodes = Promocodes::create(1, null, [], null, 2);
76
        $promocode = $promocodes->first();
77
78
        $this->assertCount(1, $promocodes);
79
80
        $this->actingAs(User::find(1));
0 ignored issues
show
Bug introduced by
The method actingAs() does not seem to exist on object<Gabievi\Promocode...romocodeValidationTest>.

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...
81
        $appliedPromocode = Promocodes::apply($promocode['code']);
82
        $this->assertNotFalse($appliedPromocode);
83
84
        $this->actingAs(User::find(2));
0 ignored issues
show
Bug introduced by
The method actingAs() does not seem to exist on object<Gabievi\Promocode...romocodeValidationTest>.

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...
85
        $appliedPromocode = Promocodes::apply($promocode['code']);
86
        $this->assertNotFalse($appliedPromocode);
87
88
        $this->actingAs(User::find(3));
0 ignored issues
show
Bug introduced by
The method actingAs() does not seem to exist on object<Gabievi\Promocode...romocodeValidationTest>.

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...
89
        $appliedPromocode = Promocodes::apply($promocode['code']);
90
        $this->assertFalse($appliedPromocode);
91
    }
92
}
93