ModuleTest::testInvalidServiceManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/entry-name-resolver
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\EntryNameResolver\PhpUnit\Test;
7
8
use Nnx\EntryNameResolver\PhpUnit\TestData\TestPaths;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
11
use Nnx\EntryNameResolver\Module;
12
use Zend\ModuleManager\ModuleManagerInterface;
13
use Zend\ModuleManager\ModuleManager;
14
use Zend\ModuleManager\ModuleEvent;
15
16
/**
17
 * Class ModuleTest
18
 *
19
 * @package Nnx\EntryNameResolver\PhpUnit\Test
20
 */
21
class ModuleTest extends AbstractHttpControllerTestCase
22
{
23
24
    /**
25
     * Установка окружения
26
     *
27
     * @throws \Zend\Stdlib\Exception\LogicException
28
     */
29
    public function setUp()
30
    {
31
        /** @noinspection PhpIncludeInspection */
32
        $this->setApplicationConfig(
33
            include TestPaths::getPathToDefaultAppConfig()
34
        );
35
36
        parent::setUp();
37
    }
38
39
    /**
40
     * Проверка что модуль загружается
41
     *
42
     * @return void
43
     */
44
    public function testLoadModule()
45
    {
46
        $this->assertModulesLoaded([Module::MODULE_NAME]);
47
    }
48
49
    /**
50
     * Проверка ситуации когда в модуль придет некорректный ModuleManager
51
     *
52
     * @expectedException \Nnx\EntryNameResolver\Exception\InvalidArgumentException
53
     * @expectedExceptionMessage Module manager not implement Zend\ModuleManager\ModuleManager
54
     *
55
     * @throws \PHPUnit_Framework_Exception
56
     * @throws \Nnx\EntryNameResolver\Exception\InvalidArgumentException
57
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
58
     */
59
    public function testInvalidModuleManager()
60
    {
61
        $module = new Module();
62
        /** @var ModuleManagerInterface $moduleManagerMock */
63
        $moduleManagerMock = $this->getMock(ModuleManagerInterface::class);
64
65
        $module->init($moduleManagerMock);
66
    }
67
68
69
70
    /**
71
     * Проверка ситуации когда не удается получить ServiceLocator
72
     *
73
     * @expectedException \Nnx\EntryNameResolver\Exception\InvalidArgumentException
74
     * @expectedExceptionMessage Service locator not implement Zend\ServiceManager\ServiceLocatorInterface
75
     *
76
     * @throws \PHPUnit_Framework_Exception
77
     * @throws \Nnx\EntryNameResolver\Exception\InvalidArgumentException
78
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
79
     */
80
    public function testInvalidServiceManager()
81
    {
82
        $module = new Module();
83
        /** @var ModuleManager|\PHPUnit_Framework_MockObject_MockObject $moduleManagerMock */
84
        $moduleManagerMock = $this->getMock(ModuleManager::class, [], [], '', false);
85
86
        /** @var ModuleEvent|\PHPUnit_Framework_MockObject_MockObject $eventMock */
87
        $eventMock = $this->getMock(ModuleEvent::class);
88
        $eventMock->expects(static::once())->method('getParam')->with(static::equalTo('ServiceManager'))->will(static::returnValue(null));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\ModuleManager\ModuleEvent.

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...
89
90
        $moduleManagerMock->expects(static::once())->method('getEvent')->will(static::returnValue($eventMock));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\ModuleManager\ModuleManager.

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...
91
92
        $module->init($moduleManagerMock);
93
    }
94
95
96
97
    /**
98
     * Проверка ситуации когда не удается получить ServiceListener
99
     *
100
     * @expectedException \Nnx\EntryNameResolver\Exception\InvalidArgumentException
101
     * @expectedExceptionMessage ServiceListener not implement Zend\ModuleManager\Listener\ServiceListenerInterface
102
     *
103
     * @throws \PHPUnit_Framework_Exception
104
     * @throws \Nnx\EntryNameResolver\Exception\InvalidArgumentException
105
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
106
     */
107
    public function testInvalidServiceListener()
108
    {
109
        $module = new Module();
110
        /** @var ModuleManager|\PHPUnit_Framework_MockObject_MockObject $moduleManagerMock */
111
        $moduleManagerMock = $this->getMock(ModuleManager::class, [], [], '', false);
112
113
        /** @var ModuleEvent|\PHPUnit_Framework_MockObject_MockObject $eventMock */
114
        $eventMock = $this->getMock(ModuleEvent::class);
115
        $moduleManagerMock->expects(static::once())->method('getEvent')->will(static::returnValue($eventMock));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\ModuleManager\ModuleManager.

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...
116
117
        /** @var ServiceLocatorInterface||\PHPUnit_Framework_MockObject_MockObject $slMock */
0 ignored issues
show
Documentation introduced by
The doc-type ServiceLocatorInterface|...k_MockObject_MockObject could not be parsed: Unknown type name "|" at position 24. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
118
        $slMock = $this->getMock(ServiceLocatorInterface::class);
119
        $slMock->expects(static::once())->method('get')->with(static::equalTo('ServiceListener'))->will(static::returnValue(null));
120
121
        $eventMock->expects(static::once())->method('getParam')->with(static::equalTo('ServiceManager'))->will(static::returnValue($slMock));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\ModuleManager\ModuleEvent.

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...
122
123
124
        $module->init($moduleManagerMock);
125
    }
126
}
127