Completed
Push — master ( f94c4d...339dcc )
by Daniel
03:14
created

YamlRpcLoaderTest::createParser()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 2
eloc 18
nc 1
nop 0
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Tests\Routing\Loader;
4
5
use Cmobi\RabbitmqBundle\Routing\Loader\YamlRpcLoader;
6
use Cmobi\RabbitmqBundle\Tests\BaseTestCase;
7
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
8
9
class YamlRpcLoaderTest extends BaseTestCase
10
{
11
12 View Code Duplication
    public function testLoadRouteInstance()
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...
13
    {
14
        $parser = $this->createParser();
15
        $path = __DIR__ . '/../../app/config';
16
        $loader = new YamlRpcLoader($this->getContainer(), $path, $parser);
0 ignored issues
show
Bug introduced by
It seems like $this->getContainer() 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...
17
        $methodCollection = $loader->load('rpc_routing.yml');
18
        $method = $methodCollection->get('cmobi_rabbitmq');
19
20
        $this->assertInstanceOf('Cmobi\RabbitmqBundle\Routing\Method', $method);
21
    }
22
23 View Code Duplication
    public function testLoadRouteName()
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...
24
    {
25
        $parser = $this->createParser();
26
        $path = __DIR__ . '/../../app/config';
27
        $loader = new YamlRpcLoader($this->getContainer(), $path, $parser);
0 ignored issues
show
Bug introduced by
It seems like $this->getContainer() 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...
28
        $methodCollection = $loader->load('rpc_routing.yml');
29
        $method = $methodCollection->get('cmobi_rabbitmq');
30
31
        $this->assertSame('default', $method->getName());
32
    }
33
34
    private function createParser()
35
    {
36
        $bundles = array(
37
            'FooBundle' => array($this->getBundle('TestBundle\FooBundle', 'FooBundle')),
38
        );
39
40
        $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
41
        $kernel
42
            ->expects($this->any())
43
            ->method('getBundle')
44
            ->will($this->returnCallback(function ($bundle) use ($bundles) {
45
                if (!isset($bundles[$bundle])) {
46
                    throw new \InvalidArgumentException(sprintf('Invalid bundle name "%s"', $bundle));
47
                }
48
49
                return $bundles[$bundle];
50
            }))
51
        ;
52
        $bundles = [
53
            'FooBundle' => $this->getBundle('Cmobi\RabbitmqBUndle\FooBundle', 'CmobiRabbitmqBundle'),
54
        ];
55
        $kernel
56
            ->expects($this->any())
57
            ->method('getBundles')
58
            ->will($this->returnValue($bundles))
59
        ;
60
61
        return new ControllerNameParser($kernel);
62
    }
63
64
    private function getBundle($namespace, $name)
65
    {
66
        $bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
67
        $bundle->expects($this->any())->method('getName')->will($this->returnValue($name));
68
        $bundle->expects($this->any())->method('getNamespace')->will($this->returnValue($namespace));
69
70
        return $bundle;
71
    }
72
}