Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

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