|
| 1 | +<?php |
| 2 | + |
| 3 | +use Complete\Resolver\NodeTypeResolver; |
| 4 | +use Entity\Completion\Scope; |
| 5 | +use Entity\FQCN; |
| 6 | +use Entity\Index; |
| 7 | +use Entity\Node\ClassData; |
| 8 | +use Entity\Node\ClassProperty; |
| 9 | +use Entity\Node\MethodData; |
| 10 | +use Entity\Node\Variable; |
| 11 | +use Parser\UseParser; |
| 12 | +use PhpParser\Node\Expr\Variable as NodeVar; |
| 13 | +use PhpParser\Node\Expr\PropertyFetch; |
| 14 | +use PhpParser\Node\Expr\MethodCall; |
| 15 | +use Monolog\Logger; |
| 16 | +use Monolog\Handler\NullHandler; |
| 17 | + |
| 18 | +function createClass($classFQN, $fqcn){ |
| 19 | + $class = new ClassData($classFQN, 'dummy/path/class.php'); |
| 20 | + $method = new MethodData('method2'); |
| 21 | + $param = new ClassProperty('param2'); |
| 22 | + $param->type = $fqcn; |
| 23 | + $method->setReturn($fqcn); |
| 24 | + $class->addMethod($method); |
| 25 | + $class->addProp($param); |
| 26 | + return $class; |
| 27 | +} |
| 28 | + |
| 29 | +describe('NodeTypeResolver', function(){ |
| 30 | + beforeEach(function(){ |
| 31 | + $logger = new Logger('spec'); |
| 32 | + $logger->pushHandler(new NullHandler); |
| 33 | + $this->resolver = new NodeTypeResolver($logger, new UseParser); |
| 34 | + $this->scope = new Scope; |
| 35 | + $this->index = new Index; |
| 36 | + $this->var = new Variable('test'); |
| 37 | + $fqcn = new FQCN('ClassName', 'Some\\Path'); |
| 38 | + $fqcn2 = new FQCN('AnotherClassName', 'Another\\Path\\To\\It'); |
| 39 | + $this->anotherFQCN = $fqcn2; |
| 40 | + $this->var->setType($fqcn); |
| 41 | + $this->scope->addVar($this->var); |
| 42 | + $class = createClass($fqcn, $fqcn2); |
| 43 | + $class2 = createClass($fqcn2, $fqcn); |
| 44 | + $this->index->addClass($class); |
| 45 | + $this->index->addClass($class2); |
| 46 | + }); |
| 47 | + describe('->getType()', function(){ |
| 48 | + it('returns variable type from scope', function(){ |
| 49 | + $node = new NodeVar; |
| 50 | + $node->name = $this->var->getName(); |
| 51 | + expect($this->resolver->getChainType($node, $this->index, $this->scope)) |
| 52 | + ->to->equal($this->var->getType()); |
| 53 | + }); |
| 54 | + describe('Properties', function(){ |
| 55 | + beforeEach(function(){ |
| 56 | + $this->node = new PropertyFetch; |
| 57 | + $this->node->var = new NodeVar; |
| 58 | + $this->node->var->name = $this->var->getName(); |
| 59 | + }); |
| 60 | + it('returns null for unknown property', function(){ |
| 61 | + $this->node->name = 'param'; |
| 62 | + expect($this->resolver->getChainType($this->node, $this->index, $this->scope)) |
| 63 | + ->to->be->null; |
| 64 | + }); |
| 65 | + it('returns type for known property', function(){ |
| 66 | + $this->node->name = 'param2'; |
| 67 | + expect($this->resolver->getChainType($this->node, $this->index, $this->scope)) |
| 68 | + ->to->equal($this->anotherFQCN); |
| 69 | + }); |
| 70 | + }); |
| 71 | + describe('Method', function(){ |
| 72 | + beforeEach(function(){ |
| 73 | + $this->node = new MethodCall; |
| 74 | + $this->node->var = new NodeVar; |
| 75 | + $this->node->var->name = $this->var->getName(); |
| 76 | + }); |
| 77 | + it('returns null for unknown method', function(){ |
| 78 | + $this->node->name = 'method'; |
| 79 | + expect($this->resolver->getChainType($this->node, $this->index, $this->scope)) |
| 80 | + ->to->be->null; |
| 81 | + }); |
| 82 | + it('returns type for known method', function(){ |
| 83 | + $this->node->name = 'method2'; |
| 84 | + expect($this->resolver->getChainType($this->node, $this->index, $this->scope)) |
| 85 | + ->to->equal($this->anotherFQCN); |
| 86 | + }); |
| 87 | + }); |
| 88 | + describe('Complex', function(){ |
| 89 | + beforeEach(function(){ |
| 90 | + $this->node = new MethodCall; |
| 91 | + $this->node->name = 'method2'; |
| 92 | + $this->node->var = new PropertyFetch; |
| 93 | + $this->node->var->name = 'param2'; |
| 94 | + $this->node->var->var = new MethodCall; |
| 95 | + $this->node->var->var->name = 'method2'; |
| 96 | + $this->node->var->var->var = new NodeVar; |
| 97 | + $this->node->var->var->var->name = $this->var->getName(); |
| 98 | + }); |
| 99 | + it('returns type for known property in complex chain', function(){ |
| 100 | + $node = new PropertyFetch; |
| 101 | + $node->var = $this->node; |
| 102 | + $node->name = 'param2'; |
| 103 | + expect($this->resolver->getChainType($node, $this->index, $this->scope)) |
| 104 | + ->to->equal($this->var->getType()); |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments