UserTest::getWithHeader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace User\Tests\Controller;
5
6
use Common\Response as View;
7
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
8
use Doctrine\Common\DataFixtures\Loader;
9
use Doctrine\ORM\Tools\SchemaTool;
10
use Silex\WebTestCase;
11
12
/**
13
 * User Controller Test Case
14
 *
15
 * @author Thiago Paes <[email protected]>
16
 */
17
class UserTest extends WebTestCase
18
{
19
    /**
20
     * @var \Silex\Application
21
     */
22
    protected static $application;
23
24
    /**
25
     * @var array
26
     */
27
    protected $header = [
28
        'HTTP_Content-type' => 'application/json'
29
    ];
30
31
    /**
32
     * Class Bootstrap
33
     *
34
     * Load fixtures before controllers tests
35
     *
36
     * @return void
37
     */
38
    public static function setUpBeforeClass()
39
    {
40
        if (!empty(self::$application) && self::$application['doctrine_orm.em']->isOpen()) {
41
            return self::$application;
42
        }
43
44
        $app = include __DIR__ . '/../../../../bootstrap.php';
45
46
        /* @var $metadata array */
47
        $metadata = $app['doctrine_orm.em']->getMetadataFactory()->getAllMetadata();
48
49
        /* @var $tool SchemaTool */
50
        $tool = new SchemaTool($app['doctrine_orm.em']);
51
        $tool->createSchema($metadata);
52
53
        /* @var $loader Loader */
54
        $loader = new Loader();
55
        $loader->loadFromDirectory(__DIR__ . '/../../../../fixtures');
56
57
        /* @var $executor ORMExecutor */
58
        $executor = new ORMExecutor($app['doctrine_orm.em']);
59
        $executor->execute($loader->getFixtures(), true);
60
61
        $app['logger']->addDebug('Fixtures loaded to ' . static::class);
62
63
        self::$application = $app;
64
    }
65
66
    /**
67
     * Test bootstrap
68
     *
69
     * @return \Silex\Application
70
     */
71
    public function createApplication()
72
    {
73
        return $this->app = self::$application;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->app = self::$application; (Silex\Application) is incompatible with the return type declared by the abstract method Silex\WebTestCase::createApplication of type Symfony\Component\HttpKernel\HttpKernel.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
74
    }
75
76
    /**
77
     * Data Provider
78
     *
79
     * @return array
80
     */
81
    public function validObjects()
82
    {
83
        return [
84
            [
85
                [
86
                    "name" => "Fooo Bar bar",
87
                    "email" => "[email protected]"
88
                ]
89
            ]
90
        ];
91
    }
92
93
    /**
94
     * Data Provider
95
     *
96
     * @return multitype:multitype:multitype:string
0 ignored issues
show
Documentation introduced by
The doc-type multitype:multitype:multitype:string could not be parsed: Unknown type name "multitype:multitype:multitype:string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
97
     */
98
    public function validProfileIds()
99
    {
100
        return
101
            [
102
                [
103
                    1
104
                ]
105
            ];
106
    }
107
108
    /**
109
     * @test
110
     */
111
    public function getWithoutHeader()
112
    {
113
        /* @var $client \Symfony\Component\HttpKernel\Client */
114
        $client = $this->createClient();
115
116
        /* @var $crawler \Symfony\Component\DomCrawler\Crawler */
117
        $crawler = $client->request('GET', '/user/');
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...
118
119
        /* @var $response \Symfony\Component\HttpFoundation\Response */
120
        $response = $client->getResponse();
121
122
        /* @var $content string */
123
        $content = $response->getContent();
124
125
        /* @var $object \stdClass */
126
        $object = json_decode($content);
127
128
        $this->assertJson($content);
129
        $this->assertEquals(View::HTTP_OK, $response->getStatusCode());
130
        $this->assertEquals(View::HTTP_OK, $object->code);
131
        $this->assertTrue(is_array($object->data));
132
    }
133
134
    /**
135
     * @test
136
     */
137 View Code Duplication
    public function getWithHeader()
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...
138
    {
139
        /* @var $client \Symfony\Component\HttpKernel\Client */
140
        $client = $this->createClient();
141
142
        /* @var $crawler \Symfony\Component\DomCrawler\Crawler */
143
        $crawler = $client->request('GET', '/user/', [], [], $this->header);
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...
144
145
        /* @var $response \Symfony\Component\HttpFoundation\Response */
146
        $response = $client->getResponse();
147
148
        /* @var $content string */
149
        $content = $response->getContent();
150
151
        /* @var $object \stdClass */
152
        $object = json_decode($content);
153
154
        $this->assertJson($content);
155
        $this->assertEquals(View::HTTP_OK, $response->getStatusCode());
156
        $this->assertEquals(View::HTTP_OK, $object->code);
157
        $this->assertTrue(is_array($object->data));
158
    }
159
160
    /**
161
     * @test
162
     * @dataProvider validProfileIds
163
     */
164 View Code Duplication
    public function getWithUserIdWithoutHeader($id)
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...
165
    {
166
        /* @var $client \Symfony\Component\HttpKernel\Client */
167
        $client = $this->createClient();
168
169
        /* @var $crawler \Symfony\Component\DomCrawler\Crawler */
170
        $crawler = $client->request('GET', '/user/' . $id);
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...
171
172
        /* @var $response \Symfony\Component\HttpFoundation\Response */
173
        $response = $client->getResponse();
174
175
        /* @var $content string */
176
        $content = $response->getContent();
177
178
        /* @var $object \stdClass */
179
        $object = json_decode($content);
180
181
        $this->assertJson($content);
182
        $this->assertEquals(View::HTTP_OK, $response->getStatusCode());
183
        $this->assertEquals(View::HTTP_OK, $object->code);
184
        $this->assertNotEmpty($object->data);
185
    }
186
187
    /**
188
     * @test
189
     * @dataProvider validProfileIds
190
     */
191 View Code Duplication
    public function getUserUserIdWithHeader($id)
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...
192
    {
193
        /* @var $client \Symfony\Component\HttpKernel\Client */
194
        $client = $this->createClient();
195
196
        /* @var $crawler \Symfony\Component\DomCrawler\Crawler */
197
        $crawler = $client->request('GET', '/user/' . $id, [], [], $this->header);
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...
198
199
        /* @var $response \Symfony\Component\HttpFoundation\Response */
200
        $response = $client->getResponse();
201
202
        /* @var $content string */
203
        $content = $response->getContent();
204
205
        /* @var $object \stdClass */
206
        $object = json_decode($content);
207
208
        $this->assertJson($content);
209
        $this->assertEquals(View::HTTP_OK, $response->getStatusCode());
210
        $this->assertEquals(View::HTTP_OK, $object->code);
211
        $this->assertNotEmpty($object->data);
212
    }
213
}
214