Skip to content

Commit

Permalink
Merge pull request #45 from maglnet/bugfix/44-error-on-trait-adaptation
Browse files Browse the repository at this point in the history
add tests for use trait adaptation
  • Loading branch information
maglnet authored Jan 14, 2018
2 parents 1771c4c + 9463daa commit 5357399
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed
- displays more detailed errors if json config format is not valid
- fixed fatal error when parsing trait usage with modified visibility (#44)

## [0.1.6] - 2017-09-24
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ private function recordTraitUsage(Node $node)
array_map([$this, 'recordUsageOf'], $node->traits);

foreach ($node->adaptations as $adaptation) {
$this->recordUsageOf($adaptation->trait);
if (null !== $adaptation->trait) {
$this->recordUsageOf($adaptation->trait);
}

if ($adaptation instanceof Node\Stmt\TraitUseAdaptation\Precedence) {
array_map([$this, 'recordUsageOf'], $adaptation->insteadof);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ public function testWillCollectNamespacedConstDefinition()
);
}

public function testTraitAdaptionDefinition()
{
$this->traverseStringAST('namespace Foo; trait BarTrait { protected function test(){}} class UseTrait { use BarTrait {test as public;} }');

self::assertSameCollectedSymbols(
['Foo\BarTrait', 'Foo\UseTrait'],
$this->collector->getDefinedSymbols()
);
}


private function traverseStringAST(string $phpSource): array
{
return $this->traverser->traverse($this->parser->parse('<?php ' . $phpSource));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ public function testWontCollectAnyUsageTypes()
);
}

public function testUseTraitAdaptionAlias()
{
$this->traverseStringAST('<?php namespace Foo; trait BarTrait { protected function test(){}} class UseTrait { use BarTrait {test as public;} }');

self::assertSameCollectedSymbols(
['Foo\BarTrait'],
$this->collector->getCollectedSymbols()
);
}

private function traverseStringAST(string $stringAST)
{
return $this->traverser->traverse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\TraitUse;
use PhpParser\Node\Stmt\TraitUseAdaptation\Alias;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -304,6 +305,18 @@ public function testTraits()
$this->assertContains('Foo', $symbols);
}

public function testTraitUseVisibilityAdaptation()
{
$traitUseAdaption = new Alias(null, 'testMethod', Class_::MODIFIER_PUBLIC, null);
$traitUse = new TraitUse([new Name('Foo')], [$traitUseAdaption]);

$this->visitor->enterNode($traitUse);

$symbols = $this->visitor->getCollectedSymbols();
$this->assertCount(1, $symbols);
$this->assertContains('Foo', $symbols);
}

public function testBeforeTraverseResetsRecordedSymbols()
{
$node = new Class_('Foo');
Expand Down

0 comments on commit 5357399

Please sign in to comment.