Completed
Push — master ( 61d54e...4a5166 )
by Sam
03:19
created

ApiControllerTest::testNamespaces()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 22
c 1
b 1
f 0
rs 8.9197
cc 4
eloc 13
nc 4
nop 0
1
<?php
2
3
namespace Tests\AppBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
use Symfony\Component\DependencyInjection\Container;
7
8
class ApiControllerTest extends WebTestCase
9
{
10
    /** @var Container */
11
    protected $container;
12
13
    public function setUp()
14
    {
15
        $client = static::createClient();
16
        $this->container = $client->getContainer();
0 ignored issues
show
Documentation Bug introduced by
It seems like $client->getContainer() can also be of type object<Symfony\Component...ion\ContainerInterface>. However, the property $container is declared as type object<Symfony\Component...ncyInjection\Container>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
17
    }
18
19
    public function testNamespaces()
20
    {
21
        $client = static::createClient();
22
        $isSingle = $this->container->getParameter('app.single_wiki');
23
24
        // Test 404 (for single-wiki setups, that wiki's namespaces are always returned).
25
        $crawler = $client->request('GET', '/api/namespaces/wiki.that.doesnt.exist.org');
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...
26
        if ($isSingle) {
27
            $this->assertEquals(200, $client->getResponse()->getStatusCode());
28
        } else {
29
            $this->assertEquals(404, $client->getResponse()->getStatusCode());
30
        }
31
32
        if (!$isSingle && $this->container->getParameter('app.is_labs')) {
33
            $crawler = $client->request('GET', '/api/namespaces/fr.wikipedia.org');
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...
34
            $this->assertEquals(200, $client->getResponse()->getStatusCode());
35
36
            // Check that a correct namespace value was returned
37
            $namespaces = (array) json_decode($client->getResponse()->getContent());
38
            $this->assertEquals('Utilisateur', array_values($namespaces)[2]); // User in French
39
        }
40
    }
41
}
42