Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

ApiKeyServiceTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 157
Duplicated Lines 11.46 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 18
loc 157
rs 10
wmc 11
lcom 1
cbo 5

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A keyIsProperlyCreated() 0 8 1
A keyIsProperlyCreatedWithExpirationDate() 0 9 1
A checkReturnsFalseWhenKeyIsInvalid() 9 9 1
A checkReturnsFalseWhenKeyIsDisabled() 0 11 1
A checkReturnsFalseWhenKeyIsExpired() 0 11 1
A checkReturnsTrueWhenConditionsAreFavorable() 9 9 1
A disableThrowsExceptionWhenNoTokenIsFound() 0 9 1
A disableReturnsDisabledKeyWhenFOund() 0 15 1
A listFindsAllApiKeys() 0 9 1
A listEnabledFindsOnlyEnabledApiKeys() 0 9 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
namespace ShlinkioTest\Shlink\Rest\Service;
3
4
use Doctrine\ORM\EntityManager;
5
use Doctrine\ORM\EntityRepository;
6
use PHPUnit_Framework_TestCase as TestCase;
7
use Prophecy\Argument;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Shlinkio\Shlink\Rest\Entity\ApiKey;
10
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
11
12
class ApiKeyServiceTest extends TestCase
13
{
14
    /**
15
     * @var ApiKeyService
16
     */
17
    protected $service;
18
    /**
19
     * @var ObjectProphecy
20
     */
21
    protected $em;
22
23
    public function setUp()
24
    {
25
        $this->em = $this->prophesize(EntityManager::class);
26
        $this->service = new ApiKeyService($this->em->reveal());
27
    }
28
29
    /**
30
     * @test
31
     */
32
    public function keyIsProperlyCreated()
33
    {
34
        $this->em->flush()->shouldBeCalledTimes(1);
35
        $this->em->persist(Argument::type(ApiKey::class))->shouldBeCalledTimes(1);
36
37
        $key = $this->service->create();
38
        $this->assertNull($key->getExpirationDate());
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function keyIsProperlyCreatedWithExpirationDate()
45
    {
46
        $this->em->flush()->shouldBeCalledTimes(1);
47
        $this->em->persist(Argument::type(ApiKey::class))->shouldBeCalledTimes(1);
48
49
        $date = new \DateTime('2030-01-01');
50
        $key = $this->service->create($date);
51
        $this->assertSame($date, $key->getExpirationDate());
52
    }
53
54
    /**
55
     * @test
56
     */
57 View Code Duplication
    public function checkReturnsFalseWhenKeyIsInvalid()
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...
58
    {
59
        $repo = $this->prophesize(EntityRepository::class);
60
        $repo->findOneBy(['key' => '12345'])->willReturn(null)
61
                                            ->shouldBeCalledTimes(1);
62
        $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
63
64
        $this->assertFalse($this->service->check('12345'));
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function checkReturnsFalseWhenKeyIsDisabled()
71
    {
72
        $key = new ApiKey();
73
        $key->disable();
74
        $repo = $this->prophesize(EntityRepository::class);
75
        $repo->findOneBy(['key' => '12345'])->willReturn($key)
76
                                            ->shouldBeCalledTimes(1);
77
        $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
78
79
        $this->assertFalse($this->service->check('12345'));
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function checkReturnsFalseWhenKeyIsExpired()
86
    {
87
        $key = new ApiKey();
88
        $key->setExpirationDate((new \DateTime())->sub(new \DateInterval('P1D')));
89
        $repo = $this->prophesize(EntityRepository::class);
90
        $repo->findOneBy(['key' => '12345'])->willReturn($key)
91
                                            ->shouldBeCalledTimes(1);
92
        $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
93
94
        $this->assertFalse($this->service->check('12345'));
95
    }
96
97
    /**
98
     * @test
99
     */
100 View Code Duplication
    public function checkReturnsTrueWhenConditionsAreFavorable()
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...
101
    {
102
        $repo = $this->prophesize(EntityRepository::class);
103
        $repo->findOneBy(['key' => '12345'])->willReturn(new ApiKey())
104
                                            ->shouldBeCalledTimes(1);
105
        $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
106
107
        $this->assertTrue($this->service->check('12345'));
108
    }
109
110
    /**
111
     * @test
112
     * @expectedException \Shlinkio\Shlink\Common\Exception\InvalidArgumentException
113
     */
114
    public function disableThrowsExceptionWhenNoTokenIsFound()
115
    {
116
        $repo = $this->prophesize(EntityRepository::class);
117
        $repo->findOneBy(['key' => '12345'])->willReturn(null)
118
                                            ->shouldBeCalledTimes(1);
119
        $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
120
121
        $this->service->disable('12345');
122
    }
123
124
    /**
125
     * @test
126
     */
127
    public function disableReturnsDisabledKeyWhenFOund()
128
    {
129
        $key = new ApiKey();
130
        $repo = $this->prophesize(EntityRepository::class);
131
        $repo->findOneBy(['key' => '12345'])->willReturn($key)
132
                                            ->shouldBeCalledTimes(1);
133
        $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
134
135
        $this->em->flush()->shouldBeCalledTimes(1);
136
137
        $this->assertTrue($key->isEnabled());
138
        $returnedKey = $this->service->disable('12345');
139
        $this->assertFalse($key->isEnabled());
140
        $this->assertSame($key, $returnedKey);
141
    }
142
143
    /**
144
     * @test
145
     */
146
    public function listFindsAllApiKeys()
147
    {
148
        $repo = $this->prophesize(EntityRepository::class);
149
        $repo->findBy([])->willReturn([])
150
                         ->shouldBeCalledTimes(1);
151
        $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
152
153
        $this->service->listKeys();
154
    }
155
156
    /**
157
     * @test
158
     */
159
    public function listEnabledFindsOnlyEnabledApiKeys()
160
    {
161
        $repo = $this->prophesize(EntityRepository::class);
162
        $repo->findBy(['enabled' => true])->willReturn([])
163
                                          ->shouldBeCalledTimes(1);
164
        $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
165
166
        $this->service->listKeys(true);
167
    }
168
}
169