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

ApiHelperTest::testNamespaces()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 8
loc 8
c 1
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Tests\AppBundle\Helper;
4
5
use AppBundle\Helper\ApiHelper;
6
use AppBundle\Helper\LabsHelper;
7
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
8
use Symfony\Component\DependencyInjection\Container;
9
10
class ApiHelperTest extends WebTestCase
11
{
12
13
    /** @var Container */
14
    protected $container;
15
16
    /** @var ApiHelper */
17
    protected $apiHelper;
18
19
    public function setUp()
20
    {
21
        $client = static::createClient();
22
        $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...
23
        $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...
24
        $this->apiHelper = new ApiHelper($this->container, $labsHelper);
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...
25
        $this->cache = $this->container->get('cache.app');
0 ignored issues
show
Bug introduced by
The property cache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
    }
27
28
    public function testSiteInfo()
29
    {
30
        if ($this->container->getParameter('app.is_labs')) {
31
            $siteInfo = $this->apiHelper->getSiteInfo('enwiki');
32
            $this->assertEquals($siteInfo['general']['articlePath'], '/wiki/$1');
33
        }
34
    }
35
36 View Code Duplication
    public function testNamespaces()
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...
37
    {
38
        if ($this->container->getParameter('app.is_labs')) {
39
            $namespaces = $this->apiHelper->namespaces('enwiki');
40
            $this->assertEquals($namespaces['1'], 'Talk');
41
            $this->assertEquals($namespaces['2'], 'User');
42
        }
43
    }
44
}
45