BadgeControllerTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 32.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 26
loc 80
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testNewBadgeAction() 0 10 1
A testDeleteAction() 0 11 1
A testDeleteWhenBadgeHasBeenClaimed() 13 13 1
A testDeleteWhenBadgeHasBeenUnlocked() 13 13 1
A createBadge() 0 11 1
A deleteBadge() 0 7 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 Badger\Bundle\GameBundle\Tests\Controller;
4
5
use Badger\BadgerTestCase;
6
use Symfony\Bundle\FrameworkBundle\Client;
7
8
/**
9
 * @author    Marie Bochu <[email protected]>
10
 * @license   http://opensource.org/licenses/MIT The MIT License (MIT)
11
 */
12
class BadgeControllerTest extends BadgerTestCase
13
{
14
    public function testNewBadgeAction()
15
    {
16
        $client = $this->createUser();
17
18
        $count = count($this->get('badger.game.repository.badge')->findAll());
19
20
        $this->createBadge($client);
21
22
        $this->assertCount($count++, $this->get('badger.game.repository.badge')->findAll());
23
    }
24
25
    public function testDeleteAction()
26
    {
27
        $client = $this->createUser();
28
29
        $count = count($this->get('badger.game.repository.badge')->findAll());
30
31
        $badge = $this->get('badger.game.repository.badge')->findOneBy(['title' => 'Master Contributor']);
32
        $this->deleteBadge($client, $badge->getId());
33
34
        $this->assertCount($count -= 1, $this->get('badger.game.repository.badge')->findAll());
35
    }
36
37 View Code Duplication
    public function testDeleteWhenBadgeHasBeenClaimed()
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...
38
    {
39
        $client = $this->createUser();
40
41
        $countBadge = count($this->get('badger.game.repository.badge')->findAll());
42
        $countClaimedBadge = count($this->get('badger.game.repository.badge_completion')->findAll());
43
44
        $badge = $this->get('badger.game.repository.badge')->findOneBy(['title' => 'Bug Hunter']);
45
        $this->deleteBadge($client, $badge->getId());
46
47
        $this->assertCount($countBadge -= 1, $this->get('badger.game.repository.badge')->findAll(), 'Badge is removed');
48
        $this->assertCount($countClaimedBadge -= 1, $this->get('badger.game.repository.badge_completion')->findAll(), 'Claimed badge completion is removed');
49
    }
50
51 View Code Duplication
    public function testDeleteWhenBadgeHasBeenUnlocked()
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...
52
    {
53
        $client = $this->createUser();
54
55
        $countBadge = count($this->get('badger.game.repository.badge')->findAll());
56
        $countUnlockedBadge = count($this->get('badger.game.repository.badge_completion')->findAll());
57
58
        $badge = $this->get('badger.game.repository.badge')->findOneBy(['title' => 'Bug Hunter']);
59
        $this->deleteBadge($client, $badge->getId());
60
61
        $this->assertCount($countBadge -= 1, $this->get('badger.game.repository.badge')->findAll(), 'Badge is removed');
62
        $this->assertCount($countUnlockedBadge -= 1, $this->get('badger.game.repository.badge_completion')->findAll(), 'Badge completion is removed');
63
    }
64
65
    /**
66
     * @param Client $client
67
     */
68
    private function createBadge(Client $client)
69
    {
70
        $tag = $this->get('badger.game.repository.tag')->findOneBy(['code' => 'company']);
71
72
        $client->request('POST', 'admin/badge/new', [
73
            'badge' => [
74
                'title' => 'Part of the team',
75
                'tags' => [$tag->getId()]
76
            ]
77
        ]);
78
    }
79
80
    /**
81
     * @param Client $client
82
     * @param string $id
83
     */
84
    private function deleteBadge(Client $client, $id)
85
    {
86
        $client->request('POST', sprintf('admin/badge/%s/delete', $id), [
87
            '_method' => 'DELETE',
88
            'form' => []
89
        ]);
90
    }
91
}
92