Completed
Push — master ( 9c2e57...960d6d )
by Zura
03:25
created

CheckPromocodeValidationTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A it_throws_exception_if_there_is_not_such_promocode() 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

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
use Gabievi\Promocodes\Exceptions\InvalidPromocodeException;
9
10
class CheckPromocodeValidationTest extends TestCase
11
{
12
    /** @test */
13
    public function it_throws_exception_if_there_is_not_such_promocode()
14
    {
15
        $this->expectException(InvalidPromocodeException::class);
16
17
        Promocodes::check('INVALID-CODE');
18
    }
19
20
    /** @test */
21
    public function it_returns_false_if_promocode_is_expired()
22
    {
23
        $promocodes = Promocodes::create();
24
        $promocode = $promocodes->first();
25
26
        Promocode::byCode($promocode['code'])->update([
27
            'expires_at' => date('Y-m-d H:i:s', strtotime('-1 day')),
28
        ]);
29
30
        $this->assertCount(1, $promocodes);
31
32
        $checkPromocode = Promocodes::check($promocode['code']);
33
34
        $this->assertFalse($checkPromocode);
35
    }
36
37
    /** @test */
38
    public function it_returns_false_if_promocode_is_disposable_and_used()
39
    {
40
        $promocodes = Promocodes::createDisposable();
41
        $promocode = $promocodes->first();
42
43
        $promocode = Promocode::byCode($promocode['code'])->first();
44
        $user = User::find(1);
45
46
        $promocode->users()->attach($user->id, [
47
            'promocode_id' => $promocode->id,
48
            'used_at' => date('Y-m-d H:i:s'),
49
        ]);
50
51
        $this->assertCount(1, $promocodes);
52
        $this->assertCount(1, $user->promocodes);
53
54
        $checkPromocode = Promocodes::check($promocode['code']);
55
56
        $this->assertFalse($checkPromocode);
57
    }
58
59
    /** @test */
60 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...
61
    {
62
        $promocodes = Promocodes::create();
63
        $promocode = $promocodes->first();
64
65
        $this->assertCount(1, $promocodes);
66
67
        $checkPromocode = Promocodes::check($promocode['code']);
68
69
        $this->assertTrue($checkPromocode instanceof Promocode);
70
        $this->assertEquals($promocode['code'], $checkPromocode->code);
71
    }
72
}
73