Completed
Push — develop ( 4b49c4...89d32a )
by Jaap
09:06 queued 05:30
created

LegacyNamespaceFilterTest::createDescriptorMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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-2017 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
use phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter;
19
20
/**
21
 * Tests the phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter class.
22
 */
23
class LegacyNamespaceFilterTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
24
{
25
    /** @var LegacyNamespaceFilter */
26
    private $filter;
27
28
    /** @var ProjectDescriptorBuilder|m\MockInterface $builder */
29
    protected $builderMock;
30
31
    /**
32
     * Initializes the fixture and mocks any dependencies.
33
     *
34
     * @return void
35
     */
36
    public function setUp()
37
    {
38
        $this->builderMock = m::mock('phpDocumentor\Descriptor\ProjectDescriptorBuilder');
39
        $this->filter      = new LegacyNamespaceFilter($this->builderMock);
40
    }
41
42
    /**
43
     * @covers \phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
44
     */
45
    public function testConvertClassNameWithUnderscoreWillBeConvertedToNamespace()
46
    {
47
        $descriptor = $this->createDescriptorMock();
48
        $descriptor->shouldReceive('getName')->andReturn('LegacyNamespace_ClassName');
0 ignored issues
show
Bug introduced by
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...
49
        $descriptor->shouldReceive('getNamespace')->andReturn('\\');
50
51
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
52
        $descriptor->shouldReceive('setNamespace')->with('\LegacyNamespace')->once();
53
54
        $this->filter->filter($descriptor);
0 ignored issues
show
Bug introduced by
It seems like $descriptor defined by $this->createDescriptorMock() on line 47 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...
55
56
        $this->assertTrue(true);
57
    }
58
59
    /**
60
     * @covers \phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
61
     */
62
    public function testMultiLevelLegacyNamespace()
63
    {
64
        $descriptor = $this->createDescriptorMock();
65
        $descriptor->shouldReceive('getName')->andReturn('LegacyNamespace_Sub_ClassName');
0 ignored issues
show
Bug introduced by
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...
66
        $descriptor->shouldReceive('getNamespace')->andReturn('\\');
67
68
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
69
        $descriptor->shouldReceive('setNamespace')->with('\LegacyNamespace\Sub')->once();
70
71
        $this->filter->filter($descriptor);
0 ignored issues
show
Bug introduced by
It seems like $descriptor defined by $this->createDescriptorMock() on line 64 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...
72
73
        $this->assertTrue(true);
74
    }
75
76
    /**
77
     * @covers phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
78
     */
79
    public function testMixedNamespacesCanBeUnified()
80
    {
81
        $descriptor = $this->createDescriptorMock();
82
        $descriptor->shouldReceive('getName')->andReturn('LegacyNamespace_ClassName');
0 ignored issues
show
Bug introduced by
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...
83
        $descriptor->shouldReceive('getNamespace')->andReturn('\\NewNamespace');
84
85
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
86
        $descriptor->shouldReceive('setNamespace')->with('\\NewNamespace\\LegacyNamespace')->once();
87
88
        $this->filter->filter($descriptor);
0 ignored issues
show
Bug introduced by
It seems like $descriptor defined by $this->createDescriptorMock() on line 81 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...
89
90
        $this->assertTrue(true);
91
    }
92
93
    /**
94
     * @covers phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
95
     */
96
    public function testClassNameWithNewNamespaceWillNotBeModified()
97
    {
98
        $descriptor = $this->createDescriptorMock();
99
        $descriptor->shouldReceive('getName')->andReturn('ClassName');
0 ignored issues
show
Bug introduced by
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...
100
        $descriptor->shouldReceive('getNamespace')->andReturn('\\NewNamespace');
101
102
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
103
        $descriptor->shouldReceive('setNamespace')->with('\\NewNamespace')->once();
104
105
        $this->filter->filter($descriptor);
0 ignored issues
show
Bug introduced by
It seems like $descriptor defined by $this->createDescriptorMock() on line 98 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...
106
107
        $this->assertTrue(true);
108
    }
109
110
    /**
111
     * @covers phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
112
     */
113
    public function testClassNameWithEmptyNamespace()
114
    {
115
        $descriptor = $this->createDescriptorMock();
116
        $descriptor->shouldReceive('getName')->andReturn('ClassName');
0 ignored issues
show
Bug introduced by
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...
117
        $descriptor->shouldReceive('getNamespace')->andReturn('\\');
118
119
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
120
        $descriptor->shouldReceive('setNamespace')->with('\\')->once();
121
122
        $this->filter->filter($descriptor);
0 ignored issues
show
Bug introduced by
It seems like $descriptor defined by $this->createDescriptorMock() on line 115 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...
123
124
        $this->assertTrue(true);
125
    }
126
127
    /**
128
     * @covers phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
129
     */
130
    public function testPrefixedNamespace()
131
    {
132
        $this->filter->setNamespacePrefix('Vendor');
133
134
        $descriptor = $this->createDescriptorMock();
135
        $descriptor->shouldReceive('getName')->andReturn('ClassName');
0 ignored issues
show
Bug introduced by
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...
136
        $descriptor->shouldReceive('getNamespace')->andReturn('');
137
138
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
139
        $descriptor->shouldReceive('setNamespace')->with('\\Vendor')->once();
140
141
        $this->filter->filter($descriptor);
0 ignored issues
show
Bug introduced by
It seems like $descriptor defined by $this->createDescriptorMock() on line 134 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...
142
143
        $this->assertTrue(true);
144
    }
145
146
    /**
147
     * @covers phpDocumentor\Plugin\LegacyNamespaceConverter\LegacyNamespaceFilter::filter
148
     */
149
    public function testPrefixedNamespaceWithNamespacedClassWillNotBeModified()
150
    {
151
        $this->filter->setNamespacePrefix('Vendor');
152
153
        $descriptor = $this->createDescriptorMock();
154
        $descriptor->shouldReceive('getName')->andReturn('ClassName');
0 ignored issues
show
Bug introduced by
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...
155
        $descriptor->shouldReceive('getNamespace')->andReturn('\\');
156
157
        $descriptor->shouldReceive('setName')->with('ClassName')->once();
158
        $descriptor->shouldReceive('setNamespace')->with('\\')->once();
159
160
        $this->filter->filter($descriptor);
0 ignored issues
show
Bug introduced by
It seems like $descriptor defined by $this->createDescriptorMock() on line 153 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...
161
162
        $this->assertTrue(true);
163
    }
164
165
    /**
166
     * Creates a mocked Descriptor.
167
     *
168
     * @return m\MockInterface|DescriptorAbstract
169
     */
170
    private function createDescriptorMock()
171
    {
172
        return m::mock('phpDocumentor\Descriptor\DescriptorAbstract');
173
    }
174
}
175