-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…-stubbing-them-to-empty-classes #67 stub out dependencies that cannot be located by using empty classes
- Loading branch information
Showing
4 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\BackwardCompatibility\SourceLocator; | ||
|
||
use Roave\BetterReflection\Identifier\Identifier; | ||
use Roave\BetterReflection\SourceLocator\Located\LocatedSource; | ||
use Roave\BetterReflection\SourceLocator\Type\AbstractSourceLocator; | ||
use function array_slice; | ||
use function count; | ||
use function explode; | ||
use function implode; | ||
use function sprintf; | ||
|
||
final class StubClassSourceLocator extends AbstractSourceLocator | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function createLocatedSource(Identifier $identifier) : ?LocatedSource | ||
{ | ||
if (! $identifier->isClass()) { | ||
return null; | ||
} | ||
|
||
if ($identifier->getName() === Identifier::WILDCARD) { | ||
return null; | ||
} | ||
|
||
$fqcn = $identifier->getName(); | ||
$classNameParts = explode('\\', $fqcn); | ||
$shortName = array_slice($classNameParts, -1)[0]; | ||
$namespaceName = implode('\\', array_slice($classNameParts, 0, count($classNameParts) - 1)); | ||
|
||
return new LocatedSource( | ||
sprintf('<?php namespace %s{interface %s {}}', $namespaceName, $shortName), | ||
null | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RoaveTest\BackwardCompatibility\SourceLocator; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Roave\BackwardCompatibility\SourceLocator\StubClassSourceLocator; | ||
use Roave\BetterReflection\BetterReflection; | ||
use Roave\BetterReflection\Identifier\Identifier; | ||
use Roave\BetterReflection\Identifier\IdentifierType; | ||
use Roave\BetterReflection\Reflection\ReflectionClass; | ||
use Roave\BetterReflection\Reflector\Reflector; | ||
|
||
/** | ||
* @covers \Roave\BackwardCompatibility\SourceLocator\StubClassSourceLocator | ||
*/ | ||
final class StubClassSourceLocatorTest extends TestCase | ||
{ | ||
/** @var StubClassSourceLocator */ | ||
private $stubLocator; | ||
|
||
/** @var Reflector */ | ||
private $reflector; | ||
|
||
protected function setUp() : void | ||
{ | ||
parent::setUp(); | ||
|
||
$betterReflection = new BetterReflection(); | ||
|
||
$this->stubLocator = new StubClassSourceLocator($betterReflection->astLocator()); | ||
$this->reflector = $betterReflection->classReflector(); | ||
} | ||
|
||
public function testWillNotRetrieveSymbolsByType() : void | ||
{ | ||
self::assertEmpty($this->stubLocator->locateIdentifiersByType( | ||
$this->reflector, | ||
new IdentifierType(IdentifierType::IDENTIFIER_CLASS) | ||
)); | ||
} | ||
|
||
public function testWillNotRetrieveFunctionReflections() : void | ||
{ | ||
self::assertNull($this->stubLocator->locateIdentifier( | ||
$this->reflector, | ||
new Identifier('foo', new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION)) | ||
)); | ||
} | ||
|
||
public function testWillReflectNonNamespacedClass() : void | ||
{ | ||
/** @var ReflectionClass $class */ | ||
$class = $this->stubLocator->locateIdentifier( | ||
$this->reflector, | ||
new Identifier('AClass', new IdentifierType(IdentifierType::IDENTIFIER_CLASS)) | ||
); | ||
|
||
self::assertInstanceOf(ReflectionClass::class, $class); | ||
|
||
self::assertSame('AClass', $class->getName()); | ||
self::assertTrue($class->isInterface()); | ||
self::assertFalse($class->inNamespace()); | ||
} | ||
|
||
public function testWillReflectNamespacedClass() : void | ||
{ | ||
/** @var ReflectionClass $class */ | ||
$class = $this->stubLocator->locateIdentifier( | ||
$this->reflector, | ||
new Identifier('Foo\Bar\AClass', new IdentifierType(IdentifierType::IDENTIFIER_CLASS)) | ||
); | ||
|
||
self::assertInstanceOf(ReflectionClass::class, $class); | ||
|
||
self::assertSame('Foo\Bar\AClass', $class->getName()); | ||
self::assertTrue($class->isInterface()); | ||
self::assertTrue($class->inNamespace()); | ||
} | ||
} |