Completed
Push — master ( 5fd068...fdcd72 )
by Laurent
17s
created

UserControllerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 2
dl 13
loc 104
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreate() 0 12 1
A testCreateError() 0 8 1
A testEdit() 0 15 1
A testEditError() 0 11 1
A testDelete() 0 10 1
A testFilter() 13 13 1
A testSort() 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
3
namespace AppBundle\Tests\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
7
class UserControllerTest extends WebTestCase {
8
9
    public function testCreate() {
10
        $client = static::createClient();
11
        $crawler = $client->request('GET', '/admin/users/');
12
        $this->assertCount(0, $crawler->filter('table.records_list tbody tr'));
13
        $crawler = $client->click($crawler->filter('.new_entry a')->link());
14
        $form = $crawler->filter('form button[type="submit"]')->form(array(
15
        ));
16
        $client->submit($form);
17
        $crawler = $client->followRedirect();
18
        $crawler = $client->click($crawler->filter('.record_actions a')->link());
19
        $this->assertCount(1, $crawler->filter('table.records_list tbody tr'));
20
    }
21
22
    public function testCreateError() {
23
        $client = static::createClient();
24
        $crawler = $client->request('GET', '/admin/users/new');
25
        $form = $crawler->filter('form button[type="submit"]')->form();
26
        $crawler = $client->submit($form);
27
        $this->assertGreaterThan(0, $crawler->filter('form div.has-error')->count());
28
        $this->assertTrue($client->getResponse()->isSuccessful());
29
    }
30
31
    /**
32
     * @depends testCreate
33
     */
34
    public function testEdit() {
35
        $client = static::createClient();
36
        $crawler = $client->request('GET', '/admin/users/');
37
        $this->assertCount(1, $crawler->filter('table.records_list tbody tr:contains("First value")'));
38
        $this->assertCount(0, $crawler->filter('table.records_list tbody tr:contains("Changed")'));
39
        $crawler = $client->click($crawler->filter('table.records_list tbody tr td .btn-group a')->eq(1)->link());
40
        $form = $crawler->filter('form button[type="submit"]')->form(array(
41
            // ... adapt fields value here ...
42
        ));
43
        $client->submit($form);
44
        $crawler = $client->followRedirect();
45
        $crawler = $client->click($crawler->filter('.record_actions a')->link());
46
        $this->assertCount(0, $crawler->filter('table.records_list tbody tr:contains("First value")'));
47
        $this->assertCount(1, $crawler->filter('table.records_list tbody tr:contains("Changed")'));
48
    }
49
50
    /**
51
     * @depends testCreate
52
     */
53
    public function testEditError() {
54
        $client = static::createClient();
55
        $crawler = $client->request('GET', '/admin/users/');
56
        $crawler = $client->click($crawler->filter('table.records_list tbody tr td .btn-group a')->eq(1)->link());
57
        $form = $crawler->filter('form button[type="submit"]')->form(array(
58
            'user[field_name]' => '',
59
            // ... use a required field here ...
60
        ));
61
        $crawler = $client->submit($form);
62
        $this->assertGreaterThan(0, $crawler->filter('form div.has-error')->count());
63
    }
64
65
    /**
66
     * @depends testCreate
67
     */
68
    public function testDelete() {
69
        $client = static::createClient();
70
        $crawler = $client->request('GET', '/admin/users/');
71
        $this->assertTrue($client->getResponse()->isSuccessful());
72
        $this->assertCount(1, $crawler->filter('table.records_list tbody tr'));
73
        $crawler = $client->click($crawler->filter('table.records_list tbody tr td .btn-group a')->eq(0)->link());
74
        $client->submit($crawler->filter('form#delete button[type="submit"]')->form());
75
        $crawler = $client->followRedirect();
76
        $this->assertCount(0, $crawler->filter('table.records_list tbody tr'));
77
    }
78
79
    /**
80
     * @depends testCreate
81
     */
82 View Code Duplication
    public function testFilter() {
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...
83
        $client = static::createClient();
84
        $crawler = $client->request('GET', '/admin/users/');
85
        $form = $crawler->filter('div#filter form button[type="submit"]')->form(array(
86
            // ... maybe use just one field here ...
87
        ));
88
        $client->submit($form);
89
        $crawler = $client->followRedirect();
90
        $this->assertTrue($client->getResponse()->isSuccessful());
91
        $crawler = $client->click($crawler->filter('div#filter a')->link());
0 ignored issues
show
Unused Code introduced by
$crawler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
        $crawler = $client->followRedirect();
1 ignored issue
show
Unused Code introduced by
$crawler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
        $this->assertTrue($client->getResponse()->isSuccessful());
94
    }
95
96
/**
97
     * @depends testCreate
98
     */
99
100
    public function testSort() {
101
        $client = static::createClient();
102
        $crawler = $client->request('GET', '/admin/users/');
103
        $this->assertCount(1, $crawler->filter('table.records_list th')->eq(0)->filter('a i.fa-sort'));
104
        $crawler = $client->click($crawler->filter('table.records_list th a')->link());
1 ignored issue
show
Unused Code introduced by
$crawler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
        $crawler = $client->followRedirect();
106
        $this->assertTrue($client->getResponse()->isSuccessful());
107
        $this->assertCount(1, $crawler->filter('table.records_list th a i.fa-sort-up'));
108
    }
109
110
}
111