Completed
Push — master ( 1c5373...ea381d )
by Ross
9s
created

DoctrineMiddlewarePassTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace League\Tactician\Bundle\Tests\DependencyInjection\Compiler;
4
5
use League\Tactician\Bundle\DependencyInjection\Compiler\DoctrineMiddlewarePass;
6
use League\Tactician\Doctrine\ORM\TransactionMiddleware;
7
use Mockery\MockInterface;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Definition;
11
use Symfony\Component\DependencyInjection\Reference;
12
13
class DoctrineMiddlewarePassTest extends TestCase
14
{
15
    /**
16
     * @var ContainerBuilder|MockInterface
17
     */
18
    protected $container;
19
20
    /**
21
     * @var DoctrineMiddlewarePass
22
     */
23
    protected $compiler;
24
25
    protected function setUp()
26
    {
27
        parent::setUp();
28
        $this->container = \Mockery::mock(ContainerBuilder::class);
29
30
        $this->compiler = new DoctrineMiddlewarePass();
31
    }
32
33
    public function testProcess()
34
    {
35
        if (!class_exists(TransactionMiddleware::class)) {
36
            $this->markTestSkipped('"league/tactician-doctrine" is not installed');
37
        }
38
39
        $this->container->shouldReceive('hasParameter')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Symfony\Component\Depend...ection\ContainerBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
40
            ->with('doctrine.entity_managers')
41
            ->once()
42
            ->andReturn(true);
43
44
        $this->container->shouldReceive('getParameter')
45
            ->with('doctrine.entity_managers')
46
            ->once()
47
            ->andReturn([
48
                'default' => 'doctrine.orm.default_entity_manager',
49
                'second' => 'doctrine.orm.second_entity_manager',
50
            ]);
51
52
        $this->container->shouldReceive('getParameter')
53
            ->with('doctrine.default_entity_manager')
54
            ->once()
55
            ->andReturn('default');
56
57
        $this->container->shouldReceive('setDefinition')
58
            ->andReturnUsing(function($name, Definition $def) {
59
                $this->assertEquals('tactician.middleware.doctrine.default', $name);
60
61
                $this->assertEquals(TransactionMiddleware::class, $def->getClass());
62
                $this->assertCount(1, $def->getArguments());
63
                $this->assertInstanceOf(Reference::class, $def->getArgument(0));
64
                $this->assertEquals('doctrine.orm.default_entity_manager', (string)$def->getArgument(0));
65
            })
66
            ->once();
67
68
        $this->container->shouldReceive('setDefinition')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Symfony\Component\Depend...ection\ContainerBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
69
            ->andReturnUsing(function($name, Definition $def) {
70
                $this->assertEquals('tactician.middleware.doctrine.second', $name);
71
72
                $this->assertEquals(TransactionMiddleware::class, $def->getClass());
73
                $this->assertCount(1, $def->getArguments());
74
                $this->assertInstanceOf(Reference::class, $def->getArgument(0));
75
                $this->assertEquals('doctrine.orm.second_entity_manager', (string)$def->getArgument(0));
76
            })
77
            ->once();
78
79
        $this->container->shouldReceive('setDefinition')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Symfony\Component\Depend...ection\ContainerBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
80
            ->with('tactician.middleware.doctrine.second')
81
            ->once();
82
83
        $this->container->shouldReceive('setAlias')
84
            ->once()
85
            ->with('tactician.middleware.doctrine', 'tactician.middleware.doctrine.default');
86
87
        $this->compiler->process($this->container);
88
    }
89
90
    public function testDoNotProcessWhenThereAreNoEntityManagers()
91
    {
92
        if (!class_exists(TransactionMiddleware::class)) {
93
            $this->markTestSkipped('"league/tactician-doctrine" is not installed');
94
        }
95
96
        $this->container->shouldReceive('hasParameter')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Symfony\Component\Depend...ection\ContainerBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
97
            ->with('doctrine.entity_managers')
98
            ->once()
99
            ->andReturn(false);
100
101
        $this->container->shouldNotReceive('getParameter')
0 ignored issues
show
Unused Code introduced by
The call to MockInterface::shouldNotReceive() has too many arguments starting with 'getParameter'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The method shouldNotReceive does only exist in Mockery\MockInterface, but not in Symfony\Component\Depend...ection\ContainerBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
102
            ->withAnyArgs();
103
104
        $this->container->shouldNotReceive('setDefinition')
0 ignored issues
show
Unused Code introduced by
The call to MockInterface::shouldNotReceive() has too many arguments starting with 'setDefinition'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
105
            ->withAnyArgs();
106
107
        $this->container->shouldNotReceive('setAlias')
0 ignored issues
show
Unused Code introduced by
The call to MockInterface::shouldNotReceive() has too many arguments starting with 'setAlias'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
108
            ->withAnyArgs();
109
110
        $this->compiler->process($this->container);
111
    }
112
113
    public function testDoNotProcessWhenTacticianDoctrineIsNotInstalled()
114
    {
115
        if (class_exists(TransactionMiddleware::class)) {
116
            $this->markTestSkipped('"league/tactician-doctrine" is installed');
117
        }
118
119
        $this->container->shouldReceive('hasParameter')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Symfony\Component\Depend...ection\ContainerBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
120
            ->with('doctrine.entity_managers')
121
            ->andReturn(true);
122
123
        $this->container->shouldNotReceive('getParameter')
0 ignored issues
show
Unused Code introduced by
The call to MockInterface::shouldNotReceive() has too many arguments starting with 'getParameter'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The method shouldNotReceive does only exist in Mockery\MockInterface, but not in Symfony\Component\Depend...ection\ContainerBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
124
            ->withAnyArgs();
125
126
        $this->container->shouldNotReceive('setDefinition')
0 ignored issues
show
Unused Code introduced by
The call to MockInterface::shouldNotReceive() has too many arguments starting with 'setDefinition'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
127
            ->withAnyArgs();
128
129
        $this->container->shouldNotReceive('setAlias')
0 ignored issues
show
Unused Code introduced by
The call to MockInterface::shouldNotReceive() has too many arguments starting with 'setAlias'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
130
            ->withAnyArgs();
131
132
        $this->compiler->process($this->container);
133
    }
134
}
135