Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

phpDocumentor/Descriptor/TraitDescriptorTest.php (2 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
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Descriptor;
13
14
use \Mockery as m;
15
use phpDocumentor\Reflection\Types\Mixed_;
16
17
/**
18
 * Tests the functionality for the TraitDescriptor class.
19
 */
20
class TraitDescriptorTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
21
{
22
    /** @var TraitDescriptor $fixture */
23
    protected $fixture;
24
25
    /**
26
     * Creates a new (empty) fixture object.
27
     */
28
    protected function setUp()
29
    {
30
        $this->fixture = new TraitDescriptor();
31
    }
32
33
    /**
34
     * Tests whether all collection objects are properly initialized.
35
     *
36
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::__construct
37
     */
38
    public function testInitialize()
39
    {
40
        $this->assertAttributeInstanceOf('phpDocumentor\Descriptor\Collection', 'properties', $this->fixture);
41
        $this->assertAttributeInstanceOf('phpDocumentor\Descriptor\Collection', 'methods', $this->fixture);
42
    }
43
44
    /**
45
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::setProperties
46
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::getProperties
47
     */
48
    public function testSettingAndGettingProperties()
49
    {
50
        $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getProperties());
51
52
        $mock = m::mock('phpDocumentor\Descriptor\Collection');
53
54
        $this->fixture->setProperties($mock);
55
56
        $this->assertSame($mock, $this->fixture->getProperties());
57
    }
58
59
    /**
60
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::setMethods
61
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::getMethods
62
     */
63
    public function testSettingAndGettingMethods()
64
    {
65
        $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getMethods());
66
67
        $mock = m::mock('phpDocumentor\Descriptor\Collection');
68
69
        $this->fixture->setMethods($mock);
70
71
        $this->assertSame($mock, $this->fixture->getMethods());
72
    }
73
74
    /**
75
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::getInheritedMethods
76
     */
77
    public function testGetInheritedMethods()
78
    {
79
        $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getInheritedMethods());
80
81
        $collection = $this->fixture->getInheritedMethods();
82
83
        $this->assertEquals(0, $collection->count());
84
    }
85
86
    /**
87
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::getMagicMethods
88
     */
89
    public function testMagicMethodsReturnsEmptyCollectionWhenNoTags()
90
    {
91
        $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getMagicMethods());
92
93
        $collection = $this->fixture->getMagicMethods();
94
95
        $this->assertEquals(0, $collection->count());
96
    }
97
98
    /**
99
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::getMagicMethods
100
     * @dataProvider provideMagicMethodProperties
101
     * @param bool $isStatic
102
     */
103
    public function testMagicMethodsReturnsExpectedCollectionWithTags($isStatic)
104
    {
105
        $mockMethodDescriptor = m::mock('phpDocumentor\Descriptor\Tag\MethodDescriptor');
106
        $mockMethodDescriptor->shouldReceive('getMethodName')->andReturn('Sample');
0 ignored issues
show
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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
        $mockMethodDescriptor->shouldReceive('isStatic')->andReturn($isStatic);
108
        $mockMethodDescriptor->shouldReceive('getDescription')->andReturn('Sample description');
109
110
        $methodCollection = new Collection([$mockMethodDescriptor]);
111
        $this->fixture->getTags()->set('method', $methodCollection);
112
113
        $magicMethodsCollection = $this->fixture->getMagicMethods();
114
        $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $magicMethodsCollection);
115
        $this->assertSame(1, $magicMethodsCollection->count());
116
        $this->assertSame('Sample', $magicMethodsCollection[0]->getName());
117
        $this->assertSame('Sample description', $magicMethodsCollection[0]->getDescription());
118
        $this->assertSame($isStatic, $magicMethodsCollection[0]->isStatic());
119
        $this->assertSame($this->fixture, $magicMethodsCollection[0]->getParent());
120
    }
121
122
    /**
123
     * Provider to test different properties for a trait magic method
124
     * (provides isStatic)
125
     * @return bool[][]
126
     */
127
    public function provideMagicMethodProperties()
128
    {
129
        return [
130
            // Instance magic method (default)
131
            [false],
132
            // Static magic method
133
            [true],
134
        ];
135
    }
136
137
    /**
138
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::getInheritedProperties
139
     */
140
    public function testGetInheritedProperties()
141
    {
142
        $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getInheritedProperties());
143
144
        $collection = $this->fixture->getInheritedProperties();
145
146
        $this->assertEquals(0, $collection->count());
147
    }
148
149
    /**
150
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::getMagicProperties
151
     */
152
    public function testMagicPropertiesReturnsEmptyCollectionWhenNoTags()
153
    {
154
        $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getMagicProperties());
155
156
        $collection = $this->fixture->getMagicProperties();
157
158
        $this->assertEquals(0, $collection->count());
159
    }
160
161
    /**
162
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::getMagicProperties
163
     */
164
    public function testMagicPropertiesReturnsExpectedCollectionWithTags()
165
    {
166
        $mockTagPropertyDescriptor = m::mock('phpDocumentor\Descriptor\Tag\PropertyDescriptor');
167
        $mockTagPropertyDescriptor->shouldReceive('getVariableName')->andReturn('Sample');
0 ignored issues
show
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
168
        $mockTagPropertyDescriptor->shouldReceive('getDescription')->andReturn('Sample description');
169
        $mockTagPropertyDescriptor->shouldReceive('getType')->andReturn(new Mixed_());
170
171
        $propertyCollection = new Collection([$mockTagPropertyDescriptor]);
172
        $this->fixture->getTags()->set('property', $propertyCollection);
173
174
        $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getMagicProperties());
175
176
        $magicPropertiesCollection = $this->fixture->getMagicProperties();
177
        $this->assertSame(1, $magicPropertiesCollection->count());
178
        $this->assertSame('Sample', $magicPropertiesCollection[0]->getName());
179
        $this->assertSame('Sample description', $magicPropertiesCollection[0]->getDescription());
180
        $this->assertEquals(new Mixed_(), $magicPropertiesCollection[0]->getType());
181
        $this->assertSame($this->fixture, $magicPropertiesCollection[0]->getParent());
182
    }
183
184
    /**
185
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::setPackage
186
     */
187
    public function testSettingAndGettingPackage()
188
    {
189
        $package = new \phpDocumentor\Descriptor\PackageDescriptor();
190
        $mockPropertyDescriptor = m::mock('phpDocumentor\Descriptor\PropertyDescriptor');
191
        $mockPropertyDescriptor->shouldReceive('setPackage')->with($package);
192
193
        $mockMethodDescriptor = m::mock('phpDocumentor\Descriptor\MethodDescriptor');
194
        $mockMethodDescriptor->shouldReceive('setPackage')->with($package);
195
196
        $propertyCollection = new Collection([$mockPropertyDescriptor]);
197
        $methodCollection = new Collection([$mockMethodDescriptor]);
198
        $this->fixture->setProperties($propertyCollection);
199
        $this->fixture->setMethods($methodCollection);
200
201
        $this->fixture->setPackage($package);
202
203
        $this->assertSame($package, $this->fixture->getPackage());
204
    }
205
206
    /**
207
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::getUsedTraits
208
     * @covers \phpDocumentor\Descriptor\TraitDescriptor::setUsedTraits
209
     */
210
    public function testSettingAndGettingUsedTraits()
211
    {
212
        $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getUsedTraits());
213
214
        $usedTraitsCollection = new Collection();
215
        $this->fixture->setUsedTraits($usedTraitsCollection);
216
217
        $this->assertSame($usedTraitsCollection, $this->fixture->getUsedTraits());
218
    }
219
}
220