Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for use trait adaptation #45

Merged
merged 2 commits into from
Jan 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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