RestApiControllerTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 136
Duplicated Lines 32.35 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 44
loc 136
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 12 36 3
A testViewACardDetails() 0 14 1
A testGetLimitCard() 0 14 1
A testSearchValidCards() 16 16 1
A testSearchInvalidCards() 0 13 1
A testSearchValidCardsWithoutCategory() 16 16 1
A testFilterCardsByCategory() 0 18 2

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
/*
4
 * This file is part of the Moo\FlashCard package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Moo\FlashCard\Tests\Controller;
13
14
use Illuminate\Foundation\Testing\TestResponse;
15
use Moo\FlashCard\Entity\Card;
16
use Moo\FlashCard\Entity\Category;
17
use Moo\FlashCard\Tests\BaseTestCase;
18
19
/**
20
 * RestApiControllerTest contains test cases for the REST API controller.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 */
24
class RestApiControllerTest extends BaseTestCase
25
{
26
    public function setUp()
27
    {
28
        parent::setUp();
29
30
        // Create categories
31
        $category1 = $this->category();
32
        $category2 = $this->category([
33
            'title' => 'Category 2',
34
        ]);
35
        $category3 = $this->category([
36
            'title' => 'Category 3',
37
        ]);
38
        $this->category([
39
            'title' => 'Category 4',
40
        ]);
41
42
        // Create cards
43 View Code Duplication
        for ($i = 1; $i < 5; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
44
            $this->card([
45
                'title'       => 'Card ' . $i,
46
                'category_id' => $category1->id,
47
            ]);
48
        }
49
50 View Code Duplication
        for ($i = 5; $i < 8; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
            $this->card([
52
                'title'       => 'Card ' . $i,
53
                'category_id' => $category2->id,
54
            ]);
55
        }
56
57
        $this->card([
58
            'title'       => 'Card 9',
59
            'category_id' => $category3->id,
60
        ]);
61
    }
62
63
    public function testViewACardDetails()
64
    {
65
        // Get card by slug
66
        $card = Card::where('slug', 'card-1')->first();
67
68
        /** @var TestResponse $response */
69
        // Request to get all cards
70
        $response = $this->getJson('/api/cards');
71
72
        // Assert that the above card exists in the response
73
        $response->assertStatus(200);
74
        $response->assertJsonFragment(['title' => $card->title]);
75
        $response->assertJsonFragment(['category_id' => (string) $card->category_id]);
76
    }
77
78
    public function testGetLimitCard()
79
    {
80
        // Count of cards to return
81
        $count = 1;
82
83
        /** @var TestResponse $response */
84
        // Request to get limit cards
85
        $response = $this->getJson('/api/cards?limit=' . $count);
86
87
        // Assert the response contains same amount of records based on count above
88
        $response->assertJsonFragment(['current_page' => 1]);
89
        $response->assertJsonCount($count, 'data');
90
        $response->assertStatus(200);
91
    }
92
93 View Code Duplication
    public function testSearchValidCards()
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...
94
    {
95
        // Search query
96
        $keyword = 'card 1';
97
98
        /** @var TestResponse $response */
99
        // Request to get cards by a search query and include category details
100
        $response = $this->getJson(sprintf('/api/cards?filter[search]=%s&include=category', $keyword));
101
102
        // Assert that records found and category included
103
        $response->assertJsonFragment(['category']);
104
        $this->assertEquals('Category 1', $response->json('0.category.title'));
105
        $response->assertJsonFragment(['slug' => 'card-1']);
106
        $response->assertJsonCount(1);
107
        $response->assertStatus(200);
108
    }
109
110
    public function testSearchInvalidCards()
111
    {
112
        // Search query
113
        $keyword = 'ca 1';
114
115
        /** @var TestResponse $response */
116
        // Request to get cards by search query
117
        $response = $this->getJson(sprintf('/api/cards?filter[search]=%s', $keyword));
118
119
        // Assert that nothing found based on above search query
120
        $response->assertJsonCount(0);
121
        $response->assertStatus(200);
122
    }
123
124 View Code Duplication
    public function testSearchValidCardsWithoutCategory()
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...
125
    {
126
        // Search query
127
        $keyword = 'card 1';
128
129
        /** @var TestResponse $response */
130
        // Request to get cards by a search query and does not include category details
131
        $response = $this->getJson(sprintf('/api/cards?filter[search]=%s', $keyword));
132
133
        // Assert that records does not include category details
134
        $response->assertJsonMissing(['category']);
135
        $this->assertNull($response->json('0.category.title'));
136
        $response->assertJsonFragment(['slug' => 'card-1']);
137
        $response->assertJsonCount(1);
138
        $response->assertStatus(200);
139
    }
140
141
    public function testFilterCardsByCategory()
142
    {
143
        // Get a category
144
        $category = Category::all()->first();
145
146
        /** @var TestResponse $response */
147
        // Request to get cards filtered by a category
148
        $response = $this->getJson(sprintf('/api/cards?filter[category_id]=%s&include=category', $category->id));
149
150
        // Assert that the records are belongs to the category above
151
        $cards = $response->json();
152
        foreach ($cards as $card) {
153
            $this->assertArrayHasKey('category', $card);
154
            $this->assertEquals($category->id, $card['category']['id']);
155
        }
156
        $this->assertTrue(count($cards) > 0);
157
        $response->assertStatus(200);
158
    }
159
}
160