| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * phpDocumentor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * PHP Version 5.5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright 2010-2015 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\Reflection\Php\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use phpDocumentor\Reflection\Fqsen; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use phpDocumentor\Reflection\Php\Argument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use phpDocumentor\Reflection\Php\Function_ as FunctionDescriptor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use phpDocumentor\Reflection\DocBlock as DocBlockDescriptor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use phpDocumentor\Reflection\Php\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use phpDocumentor\Reflection\Php\Factory\Function_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Mockery as m; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use phpDocumentor\Reflection\Php\StrategyContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use phpDocumentor\Reflection\Types\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use phpDocumentor\Reflection\Types\Integer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use phpDocumentor\Reflection\Types\Nullable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use PhpParser\Comment\Doc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use PhpParser\Node\NullableType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * Test case for \phpDocumentor\Reflection\Php\Factory\Function_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @coversDefaultClass \phpDocumentor\Reflection\Php\Factory\Function_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @covers ::<!public> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class Function_Test extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->fixture = new Function_(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @covers ::matches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function testMatches() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->assertFalse($this->fixture->matches(new \stdClass())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->assertTrue($this->fixture->matches(m::mock(\PhpParser\Node\Stmt\Function_::class))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @covers ::create | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     public function testCreateWithoutParameters() | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $functionMock = m::mock(\PhpParser\Node\Stmt\Function_::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $functionMock->fqsen = new Fqsen('\SomeSpace::function()'); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $functionMock->params = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $functionMock->shouldReceive('getDocComment')->andReturnNull(); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $functionMock->shouldReceive('getLine')->andReturn(1); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $functionMock->shouldReceive('getReturnType')->andReturnNull(); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $containerMock = m::mock(StrategyContainer::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $containerMock->shouldReceive('findMatching')->never(); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         /** @var FunctionDescriptor $function */ | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         $function = $this->fixture->create($functionMock, $containerMock); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         $this->assertEquals('\SomeSpace::function()', (string)$function->getFqsen()); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @covers ::create | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function testCreateWithParameters() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $functionMock = m::mock(\PhpParser\Node\Stmt\Function_::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $functionMock->fqsen = new Fqsen('\SomeSpace::function()'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $functionMock->params = array('param1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $functionMock->shouldReceive('getDocComment')->andReturnNull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $functionMock->shouldReceive('getLine')->andReturn(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $functionMock->shouldReceive('getReturnType')->andReturnNull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $containerMock = m::mock(StrategyContainer::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $containerMock->shouldReceive('findMatching->create') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ->with('param1', $containerMock, null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ->andReturn(new Argument('param1')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         /** @var FunctionDescriptor $function */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $function = $this->fixture->create($functionMock, $containerMock); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->assertEquals('\SomeSpace::function()', (string)$function->getFqsen()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @covers ::create | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function testReturnTypeResolving() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $functionMock = m::mock(\PhpParser\Node\Stmt\Function_::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $functionMock->fqsen = new Fqsen('\SomeSpace::function()'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $functionMock->params = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $functionMock->shouldReceive('getDocComment')->andReturnNull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $functionMock->shouldReceive('getLine')->andReturn(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $functionMock->shouldReceive('getReturnType')->times(3)->andReturn('int'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $containerMock = m::mock(StrategyContainer::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $containerMock->shouldReceive('findMatching')->never(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         /** @var FunctionDescriptor $function */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $function = $this->fixture->create($functionMock, $containerMock); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->assertEquals(new Integer(), $function->getReturnType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @covers ::create | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function testReturnTypeNullableResolving() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $functionMock = m::mock(\PhpParser\Node\Stmt\Function_::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $functionMock->fqsen = new Fqsen('\SomeSpace::function()'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $functionMock->params = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $functionMock->shouldReceive('getDocComment')->andReturnNull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $functionMock->shouldReceive('getLine')->andReturn(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $functionMock->shouldReceive('getReturnType')->times(3)->andReturn(new NullableType('int')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $containerMock = m::mock(StrategyContainer::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $containerMock->shouldReceive('findMatching')->never(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         /** @var FunctionDescriptor $method */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $function = $this->fixture->create($functionMock, $containerMock); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $this->assertEquals(new Nullable(new Integer()), $function->getReturnType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @covers ::create | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     public function testCreateWithDocBlock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $doc = m::mock(Doc::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $functionMock = m::mock(\PhpParser\Node\Stmt\Function_::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $functionMock->fqsen = new Fqsen('\SomeSpace::function()'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $functionMock->params = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $functionMock->shouldReceive('getDocComment')->andReturn($doc); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $functionMock->shouldReceive('getLine')->andReturn(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $functionMock->shouldReceive('getReturnType')->andReturnNull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $docBlock = new DocBlockDescriptor(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $containerMock = m::mock(StrategyContainer::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $containerMock->shouldReceive('findMatching->create') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             ->once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             ->with($doc, $containerMock, null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             ->andReturn($docBlock); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         /** @var FunctionDescriptor $function */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $function = $this->fixture->create($functionMock, $containerMock); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $this->assertEquals('\SomeSpace::function()', (string)$function->getFqsen()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $this->assertSame($docBlock, $function->getDocBlock()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |  |