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

LabsHelperTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Tests\AppBundle\Helper;
4
5
use AppBundle\Helper\LabsHelper;
6
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7
use Symfony\Component\DependencyInjection\Container;
8
9
class LabsHelperTest extends WebTestCase
10
{
11
12
    /** @var Container */
13
    protected $container;
14
15
    /** @var LabsHelper */
16
    protected $labsHelper;
17
18
    public function setUp()
19
    {
20
        $client = static::createClient();
21
        $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...
22
        $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...
23
    }
24
25
    public function testGetTable()
26
    {
27
        if ($this->container->getParameter('app.is_labs')) {
28
            // When using Labs.
29
            $this->assertEquals('_p.page', $this->labsHelper->getTable('page'));
30
            $this->assertEquals('_p.logging_userindex', $this->labsHelper->getTable('logging'));
31
        } else {
32
            // When using wiki databases directly.
33
            $this->assertEquals('page', $this->labsHelper->getTable('page'));
34
            $this->assertEquals('logging', $this->labsHelper->getTable('logging'));
35
        }
36
    }
37
38 View Code Duplication
    public function testDatabasePrepare()
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...
39
    {
40
        if ($this->container->getParameter('app.is_labs')) {
41
            // When using Labs.
42
            $dbVales = $this->labsHelper->databasePrepare('en.wikipedia');
43
            $this->assertEquals('enwiki', $dbVales['dbName']);
44
            $this->assertEquals('https://en.wikipedia.org', $dbVales['url']);
45
        }
46
    }
47
48
    public function testNormalizeProject()
49
    {
50
        if ($this->container->getParameter('app.is_labs')) {
51
            // When using Labs.
52
            $this->assertEquals('en.wikipedia.org', $this->labsHelper->normalizeProject('enwiki'));
53
            $this->assertEquals('en.wikipedia.org', $this->labsHelper->normalizeProject('en.wikipedia'));
54
            $this->assertEquals(false, $this->labsHelper->normalizeProject('invalid.wiki'));
55
        }
56
    }
57
}
58