Completed
Pull Request — dev (#44)
by
unknown
03:04
created

UserControllerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 123
Duplicated Lines 13.82 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 11
Bugs 4 Features 1
Metric Value
wmc 5
lcom 1
cbo 4
dl 17
loc 123
rs 10
c 11
b 4
f 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
B testAddAction() 0 30 1
A testListAction() 17 17 1
A testActivationAction() 0 22 1
B testEditAction() 0 33 1
A testUpdatePassword() 0 14 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 Tests\AppBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
use AppBundle\Entity\User;
7
8
class UserControllerTest extends WebTestCase
9
{
10 View Code Duplication
    public function testListAction()
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...
11
    {
12
        exec('./bin/console d:d:c --env=test');
13
        exec('./bin/console d:s:c --env=test');
14
        exec('./bin/console h:f:l -n --env=test');
15
16
        $client = static::createClient();
17
        $client->request('GET', '/users');
18
        $this->assertEquals('302', $client->getResponse()->getStatusCode());
19
        $crawler = $client->request('GET', '/login');
20
21
        $form = $crawler->selectButton('Login')->form();
22
        $client->submit($form, ['_username' => '[email protected]', '_password' => 'admin']);
23
        $client->request('GET', '/users');
24
25
        $this->assertEquals('200', $client->getResponse()->getStatusCode());
26
    }
27
28
    public function testAddAction()
29
    {
30
        $client = static::createClient();
31
        $client->request('GET', '/users');
32
33
        $this->assertEquals('302', $client->getResponse()->getStatusCode());
34
35
        $crawler = $client->request('GET', '/login');
36
        $form = $crawler->selectButton('Login')->form();
37
        $client->submit($form, ['_username' => '[email protected]', '_password' => 'admin']);
38
39
        $em = static::$kernel->getContainer()
40
            ->get('doctrine')
41
            ->getManager();
42
43
        $usersCount1 = count($em->getRepository('AppBundle:User')->findAll());
44
45
        $crawler = $client->request('GET', '/user/add');
46
        $form = $crawler->selectButton('Register')->form();
47
        $client->submit($form, [
48
            'edit[lastName]' => 'test',
49
            'edit[firstName]' => 'test',
50
            'edit[email]' => '[email protected]',
51
            'edit[plainPassword][first]' => 'test',
52
            'edit[plainPassword][second]' => 'test',
53
        ]);
54
        $usersCount2 = count($em->getRepository('AppBundle:User')->findAll());
55
56
        $this->assertEquals($usersCount1 + 1, $usersCount2);
57
    }
58
59
    public function testActivationAction()
60
    {
61
        $client = static::createClient();
62
        $crawler = $client->request('GET', '/login');
63
        $form = $crawler->selectButton('Login')->form();
64
        $client->submit($form, ['_username' => '[email protected]', '_password' => 'admin']);
65
66
        $em = static::$kernel->getContainer()
67
            ->get('doctrine')
68
            ->getManager();
69
70
        $crawler = $client->request('GET', '/users');
71
72
        $form = $crawler->filter('table td form')->form();
73
        $url_params = explode('/', $form->getUri());
74
        $userid = $url_params[count($url_params) - 1];
75
76
        $client->submit($form, ['activation[enabled]' => false]);
77
        $user = $em->getRepository('AppBundle:User')->findOneBy(array('id' => $userid));
78
79
        $this->assertEquals(false, $user->isEnabled());
80
    }
81
82
    public function testEditAction()
83
    {
84
        $client = static::createClient();
85
        $client->request('GET', '/users');
86
        $this->assertEquals('302', $client->getResponse()->getStatusCode());
87
        $crawler = $client->request('GET', '/login');
88
        $form = $crawler->selectButton('Login')->form();
89
        $client->submit($form, ['_username' => '[email protected]', '_password' => 'admin']);
90
91
        $em = static::$kernel->getContainer()
92
            ->get('doctrine')
93
            ->getManager();
94
95
        $crawler = $client->request('GET', '/users');
96
97
        $form = $crawler->filter('table td form')->form();
98
        $url_params = explode('/', $form->getUri());
99
        $userid = $url_params[count($url_params) - 1];
100
        $crawler = $client->request('GET', '/user/edit/'.$userid);
101
        $form = $crawler->selectButton('Save')->form();
102
        $client->submit($form, [
103
            'edit[lastName]' => 'test1',
104
            'edit[firstName]' => 'test1',
105
            'edit[email]' => '[email protected]',
106
            'edit[plainPassword][first]' => 'test1',
107
            'edit[plainPassword][second]' => 'test1',
108
        ]);
109
110
        $this->assertInstanceOf(
111
            User::class,
112
            $em->getRepository(User::class)->findOneBy(['email' => '[email protected]'])
113
        );
114
    }
115
116
    public function testUpdatePassword()
117
    {
118
        $client = static::createClient();
119
        $crawler = $client->request('GET', '/password_update/2');
120
        $this->assertEquals('200', $client->getResponse()->getStatusCode());
121
122
        $form = $crawler->selectButton('Submit')->form();
123
        $form['reset_password[plainPassword][first]'] = 'new';
124
        $form['reset_password[plainPassword][second]'] = 'new';
125
        $client->submit($form);
0 ignored issues
show
Documentation introduced by
$form is of type array<string,string,{"re...rd][second]":"string"}>, but the function expects a object<Symfony\Component\DomCrawler\Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
127
128
        exec('./bin/console d:d:d --force --env=test');
129
    }
130
}
131