Completed
Push — master ( c2c212...38c104 )
by Sam
03:48
created

LabsHelperTest::testGetTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
/**
3
 * This file contains only the LabsHelperTest class.
4
 */
5
6
namespace Tests\AppBundle\Helper;
7
8
use AppBundle\Helper\LabsHelper;
9
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
10
use Symfony\Component\DependencyInjection\Container;
11
12
/**
13
 * The Labs helper provides information relating to the WMF Labs installation of XTools.
14
 */
15
class LabsHelperTest extends WebTestCase
16
{
17
18
    /** @var Container The DI container. */
19
    protected $container;
20
21
    /** @var LabsHelper The  */
22
    protected $labsHelper;
23
24
    /**
25
     * Set up the tests.
26
     */
27
    public function setUp()
28
    {
29
        $client = static::createClient();
30
        $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...
31
        $this->labsHelper = new LabsHelper($this->container);
0 ignored issues
show
Bug introduced by
It seems like $this->container can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
32
    }
33
34
    /**
35
     * Test that the table-name transformations are correct.
36
     */
37
    public function testGetTable()
38
    {
39
        if ($this->container->getParameter('app.is_labs')) {
40
            // When using Labs.
41
            $this->assertEquals('_p.page', $this->labsHelper->getTable('page'));
42
            $this->assertEquals('_p.logging_userindex', $this->labsHelper->getTable('logging'));
43
        } else {
44
            // When using wiki databases directly.
45
            $this->assertEquals('page', $this->labsHelper->getTable('page'));
46
            $this->assertEquals('logging', $this->labsHelper->getTable('logging'));
47
        }
48
    }
49
}
50