Completed
Pull Request — master (#564)
by Michał
07:58
created

ModuleTest::testAutoLoadingAnnotations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineModuleTest;
21
22
use Doctrine\Common\Annotations\AnnotationException;
23
use Doctrine\Common\Annotations\DocParser;
24
use DoctrineModule\Module;
25
use Symfony\Component\Console\Application;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Zend\Console\Adapter\AdapterInterface;
29
use Zend\ModuleManager\ModuleManager;
30
use Zend\Mvc\Application as ZendApplication;
31
use Zend\Mvc\MvcEvent;
32
use Zend\ServiceManager\ServiceManager;
33
34
/**
35
 * @author Martin Keckeis <[email protected]>
36
 * @covers \DoctrineModule\Module
37
 */
38
class ModuleTest extends \PHPUnit_Framework_TestCase
39
{
40
    /**
41
     * @var \PHPUnit_Framework_MockObject_MockObject|ZendApplication
42
     */
43
    private $application;
44
45
    /**
46
     * @var \PHPUnit_Framework_MockObject_MockObject|MvcEvent
47
     */
48
    private $event;
49
50
    /**
51
     * @var \PHPUnit_Framework_MockObject_MockObject|ServiceManager
52
     */
53
    private $serviceManager;
54
55
    /**
56
     * @var \PHPUnit_Framework_MockObject_MockObject|Application
57
     */
58
    private $cli;
59
60
    protected function setUp()
61
    {
62
        parent::setUp();
63
64
        $this->application    = $this->getMockBuilder(ZendApplication::class)->disableOriginalConstructor()->getMock();
65
        $this->event          = $this->createMock(MvcEvent::class);
66
        $this->serviceManager = $this->createMock(ServiceManager::class);
67
        $this->cli            = $this->getMockBuilder(Application::class)->setMethods(['run'])->getMock();
68
69
        $this
70
            ->serviceManager
71
            ->expects($this->any())
72
            ->method('get')
73
            ->with('doctrine.cli')
74
            ->will($this->returnValue($this->cli));
75
76
        $this
77
            ->application
78
            ->expects($this->any())
79
            ->method('getServiceManager')
80
            ->will($this->returnValue($this->serviceManager));
81
82
        $this
83
            ->event
84
            ->expects($this->any())
85
            ->method('getTarget')
86
            ->will($this->returnValue($this->application));
87
    }
88
89
    /**
90
     * @covers \DoctrineModule\Module::getConfig
91
     */
92
    public function testGetConfig()
93
    {
94
        $module = new Module();
95
96
        $config = $module->getConfig();
97
98
        $this->assertInternalType('array', $config);
99
        $this->assertArrayHasKey('doctrine', $config);
100
        $this->assertArrayHasKey('doctrine_factories', $config);
101
        $this->assertArrayHasKey('service_manager', $config);
102
        $this->assertArrayHasKey('controllers', $config);
103
        $this->assertArrayHasKey('route_manager', $config);
104
        $this->assertArrayHasKey('console', $config);
105
106
        $this->assertSame($config, unserialize(serialize($config)));
107
    }
108
109
    /**
110
     * Should display the help message in plain message
111
     * @covers \DoctrineModule\Module::getConsoleUsage
112
     */
113
    public function testGetConsoleUsage()
114
    {
115
        $this
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Console\Application.

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
            ->cli
117
            ->expects($this->once())
118
            ->method('run')
119
            ->with($this->isInstanceOf(InputInterface::class), $this->isInstanceOf(OutputInterface::class))
120
            ->will($this->returnCallback(function (InputInterface $input, OutputInterface $output) {
121
                $output->write($input->getFirstArgument() . ' - TEST');
122
                $output->write(' - More output');
123
            }));
124
125
        $module = new Module();
126
127
        $module->onBootstrap($this->event);
128
129
        $this->assertSame(
130
            'list - TEST - More output',
131
            $module->getConsoleUsage($this->createMock(AdapterInterface::class))
132
        );
133
    }
134
135
    /**
136
     * @runInSeparateProcess
137
     *
138
     * run in separate process to make sure
139
     * \Doctrine\Common\Annotations\AnnotationRegistry has no any loaders added in other tests
140
     */
141
    public function testAutoLoadingAnnotations()
142
    {
143
        /** @var ModuleManager|\PHPUnit_Framework_MockObject_MockObject $moduleManager */
144
        $moduleManager = $this->createMock(ModuleManager::class);
145
        $module = new Module();
146
        $module->init($moduleManager);
0 ignored issues
show
Bug introduced by
It seems like $moduleManager defined by $this->createMock(\Zend\...r\ModuleManager::class) on line 144 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, DoctrineModule\Module::init() does only seem to accept object<Zend\ModuleManager\ModuleManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
147
148
        $docParser = new DocParser();
149
        $result = $docParser->parse('/** @\DoctrineModuleTest\TestAsset\CustomAnnotation */');
150
151
        $this->assertCount(1, $result);
152
        $this->assertInstanceOf(TestAsset\CustomAnnotation::class, $result[0]);
153
    }
154
155
    /**
156
     * @runInSeparateProcess
157
     *
158
     * run in separate process to make sure
159
     * \Doctrine\Common\Annotations\AnnotationRegistry has no any loaders added in other tests
160
     */
161
    public function testAnnotationClassIsNotLoaded()
162
    {
163
        $this->expectException(AnnotationException::class);
164
        $this->expectExceptionMessage(
165
            '[Semantical Error] The annotation "@\DoctrineModuleTest\TestAsset\CustomAnnotation" in  does not exist,'
166
            . ' or could not be auto-loaded.'
167
        );
168
169
        $docParser = new DocParser();
170
        $docParser->parse('/** @\DoctrineModuleTest\TestAsset\CustomAnnotation */');
171
    }
172
}
173