Completed
Push — develop ( 80740b...61b5c3 )
by Mike
10:20
created

LegacyNamespaceFilterTest.php (14 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 *  For the full copyright and license information, please view the LICENSE
6
 *  file that was distributed with this source code.
7
 *
8
 *  @copyright 2010-2018 Mike van Riel<[email protected]>
9
 *  @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 *  @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Plugin\LegacyNamespaceConverter;
14
15
use Mockery as m;
16
use phpDocumentor\Descriptor\DescriptorAbstract;
17
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
18
19
/**
20
 * Tests the phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter class.
21
 */
22
class LegacyNamespaceFilterTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
23
{
24
    /** @var LegacyNamespaceFilter */
25
    private $filter;
26
27
    /** @var ProjectDescriptorBuilder|m\MockInterface $builder */
28
    protected $builderMock;
29
30
    /**
31
     * Initializes the fixture and mocks any dependencies.
32
     */
33
    protected function setUp()
34
    {
35
        $this->builderMock = m::mock('phpDocumentor\Descriptor\ProjectDescriptorBuilder');
36
        $this->filter = new LegacyNamespaceFilter($this->builderMock);
37
    }
38
39
    /**
40
     * @covers \phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
41
     */
42
    public function testConvertClassNameWithUnderscoreWillBeConvertedToNamespace()
43
    {
44
        $descriptor = $this->createDescriptorMock();
45
        $descriptor->shouldReceive('getName')->andReturn('LegacyNamespace_ClassName');
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\DescriptorAbstract.

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...
46
        $descriptor->shouldReceive('getNamespace')->andReturn('\\');
47
48
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
49
        $descriptor->shouldReceive('setNamespace')->with('\LegacyNamespace')->once();
50
51
        $this->filter->filter($descriptor);
0 ignored issues
show
It seems like $descriptor defined by $this->createDescriptorMock() on line 44 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Plugin\Leg...mespaceFilter::filter() does only seem to accept object<phpDocumentor\Des...tor\DescriptorAbstract>, 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...
52
53
        $this->assertTrue(true);
54
    }
55
56
    /**
57
     * @covers \phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
58
     */
59
    public function testMultiLevelLegacyNamespace()
60
    {
61
        $descriptor = $this->createDescriptorMock();
62
        $descriptor->shouldReceive('getName')->andReturn('LegacyNamespace_Sub_ClassName');
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\DescriptorAbstract.

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...
63
        $descriptor->shouldReceive('getNamespace')->andReturn('\\');
64
65
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
66
        $descriptor->shouldReceive('setNamespace')->with('\LegacyNamespace\Sub')->once();
67
68
        $this->filter->filter($descriptor);
0 ignored issues
show
It seems like $descriptor defined by $this->createDescriptorMock() on line 61 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Plugin\Leg...mespaceFilter::filter() does only seem to accept object<phpDocumentor\Des...tor\DescriptorAbstract>, 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...
69
70
        $this->assertTrue(true);
71
    }
72
73
    /**
74
     * @covers phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
75
     */
76
    public function testMixedNamespacesCanBeUnified()
77
    {
78
        $descriptor = $this->createDescriptorMock();
79
        $descriptor->shouldReceive('getName')->andReturn('LegacyNamespace_ClassName');
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\DescriptorAbstract.

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
        $descriptor->shouldReceive('getNamespace')->andReturn('\\NewNamespace');
81
82
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
83
        $descriptor->shouldReceive('setNamespace')->with('\\NewNamespace\\LegacyNamespace')->once();
84
85
        $this->filter->filter($descriptor);
0 ignored issues
show
It seems like $descriptor defined by $this->createDescriptorMock() on line 78 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Plugin\Leg...mespaceFilter::filter() does only seem to accept object<phpDocumentor\Des...tor\DescriptorAbstract>, 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...
86
87
        $this->assertTrue(true);
88
    }
89
90
    /**
91
     * @covers phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
92
     */
93
    public function testClassNameWithNewNamespaceWillNotBeModified()
94
    {
95
        $descriptor = $this->createDescriptorMock();
96
        $descriptor->shouldReceive('getName')->andReturn('ClassName');
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\DescriptorAbstract.

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
        $descriptor->shouldReceive('getNamespace')->andReturn('\\NewNamespace');
98
99
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
100
        $descriptor->shouldReceive('setNamespace')->with('\\NewNamespace')->once();
101
102
        $this->filter->filter($descriptor);
0 ignored issues
show
It seems like $descriptor defined by $this->createDescriptorMock() on line 95 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Plugin\Leg...mespaceFilter::filter() does only seem to accept object<phpDocumentor\Des...tor\DescriptorAbstract>, 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...
103
104
        $this->assertTrue(true);
105
    }
106
107
    /**
108
     * @covers phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
109
     */
110
    public function testClassNameWithEmptyNamespace()
111
    {
112
        $descriptor = $this->createDescriptorMock();
113
        $descriptor->shouldReceive('getName')->andReturn('ClassName');
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\DescriptorAbstract.

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...
114
        $descriptor->shouldReceive('getNamespace')->andReturn('\\');
115
116
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
117
        $descriptor->shouldReceive('setNamespace')->with('\\')->once();
118
119
        $this->filter->filter($descriptor);
0 ignored issues
show
It seems like $descriptor defined by $this->createDescriptorMock() on line 112 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Plugin\Leg...mespaceFilter::filter() does only seem to accept object<phpDocumentor\Des...tor\DescriptorAbstract>, 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...
120
121
        $this->assertTrue(true);
122
    }
123
124
    /**
125
     * @covers phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
126
     */
127
    public function testPrefixedNamespace()
128
    {
129
        $this->filter->setNamespacePrefix('Vendor');
130
131
        $descriptor = $this->createDescriptorMock();
132
        $descriptor->shouldReceive('getName')->andReturn('ClassName');
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\DescriptorAbstract.

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...
133
        $descriptor->shouldReceive('getNamespace')->andReturn('');
134
135
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
136
        $descriptor->shouldReceive('setNamespace')->with('\\Vendor')->once();
137
138
        $this->filter->filter($descriptor);
0 ignored issues
show
It seems like $descriptor defined by $this->createDescriptorMock() on line 131 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Plugin\Leg...mespaceFilter::filter() does only seem to accept object<phpDocumentor\Des...tor\DescriptorAbstract>, 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...
139
140
        $this->assertTrue(true);
141
    }
142
143
    /**
144
     * @covers phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
145
     */
146
    public function testPrefixedNamespaceWithNamespacedClassWillNotBeModified()
147
    {
148
        $this->filter->setNamespacePrefix('Vendor');
149
150
        $descriptor = $this->createDescriptorMock();
151
        $descriptor->shouldReceive('getName')->andReturn('ClassName');
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\DescriptorAbstract.

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...
152
        $descriptor->shouldReceive('getNamespace')->andReturn('\\');
153
154
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
155
        $descriptor->shouldReceive('setNamespace')->with('\\')->once();
156
157
        $this->filter->filter($descriptor);
0 ignored issues
show
It seems like $descriptor defined by $this->createDescriptorMock() on line 150 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Plugin\Leg...mespaceFilter::filter() does only seem to accept object<phpDocumentor\Des...tor\DescriptorAbstract>, 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...
158
159
        $this->assertTrue(true);
160
    }
161
162
    /**
163
     * Creates a mocked Descriptor.
164
     *
165
     * @return m\MockInterface|DescriptorAbstract
166
     */
167
    private function createDescriptorMock()
168
    {
169
        return m::mock('phpDocumentor\Descriptor\DescriptorAbstract');
170
    }
171
}
172