Файл: gapps/vendor/phpdocumentor/type-resolver/tests/unit/TypeResolverTest.php
Строк: 558
<?php
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentorReflection;
use Mockery as m;
use phpDocumentorReflectionTypesArray_;
use phpDocumentorReflectionTypesCompound;
use phpDocumentorReflectionTypesContext;
use phpDocumentorReflectionTypesObject_;
/**
* @coversDefaultClass phpDocumentorReflectionTypeResolver
*/
class TypeResolverTest extends PHPUnit_Framework_TestCase
{
/**
* @param string $keyword
* @param string $expectedClass
*
* @covers ::__construct
* @covers ::resolve
* @covers ::<private>
*
* @uses phpDocumentorReflectionTypesContext
* @uses phpDocumentorReflectionTypesArray_
* @uses phpDocumentorReflectionTypesObject_
*
* @dataProvider provideKeywords
*/
public function testResolvingKeywords($keyword, $expectedClass)
{
$fixture = new TypeResolver();
$resolvedType = $fixture->resolve($keyword, new Context(''));
$this->assertInstanceOf($expectedClass, $resolvedType);
}
/**
* @param string $fqsen
*
* @covers ::__construct
* @covers ::resolve
* @covers ::<private>
*
* @uses phpDocumentorReflectionTypesContext
* @uses phpDocumentorReflectionTypesObject_
* @uses phpDocumentorReflectionFqsen
* @uses phpDocumentorReflectionFqsenResolver
*
* @dataProvider provideFqcn
*/
public function testResolvingFQSENs($fqsen)
{
$fixture = new TypeResolver();
/** @var Object_ $resolvedType */
$resolvedType = $fixture->resolve($fqsen, new Context(''));
$this->assertInstanceOf('phpDocumentorReflectionTypesObject_', $resolvedType);
$this->assertInstanceOf('phpDocumentorReflectionFqsen', $resolvedType->getFqsen());
$this->assertSame($fqsen, (string)$resolvedType);
}
/**
* @covers ::__construct
* @covers ::resolve
* @covers ::<private>
*
* @uses phpDocumentorReflectionTypesContext
* @uses phpDocumentorReflectionTypesObject_
* @uses phpDocumentorReflectionFqsen
* @uses phpDocumentorReflectionFqsenResolver
*/
public function testResolvingRelativeQSENsBasedOnNamespace()
{
$fixture = new TypeResolver();
/** @var Object_ $resolvedType */
$resolvedType = $fixture->resolve('DocBlock', new Context('phpDocumentorReflection'));
$this->assertInstanceOf('phpDocumentorReflectionTypesObject_', $resolvedType);
$this->assertInstanceOf('phpDocumentorReflectionFqsen', $resolvedType->getFqsen());
$this->assertSame('phpDocumentorReflectionDocBlock', (string)$resolvedType);
}
/**
* @covers ::__construct
* @covers ::resolve
* @covers ::<private>
*
* @uses phpDocumentorReflectionTypesContext
* @uses phpDocumentorReflectionTypesObject_
* @uses phpDocumentorReflectionFqsen
* @uses phpDocumentorReflectionFqsenResolver
*/
public function testResolvingRelativeQSENsBasedOnNamespaceAlias()
{
$fixture = new TypeResolver();
/** @var Object_ $resolvedType */
$resolvedType = $fixture->resolve(
'mMockInterface',
new Context('phpDocumentorReflection', ['m' => 'Mockery'])
);
$this->assertInstanceOf('phpDocumentorReflectionTypesObject_', $resolvedType);
$this->assertInstanceOf('phpDocumentorReflectionFqsen', $resolvedType->getFqsen());
$this->assertSame('MockeryMockInterface', (string)$resolvedType);
}
/**
* @covers ::__construct
* @covers ::resolve
* @covers ::<private>
*
* @uses phpDocumentorReflectionTypesContext
* @uses phpDocumentorReflectionTypesArray_
* @uses phpDocumentorReflectionTypesString_
*/
public function testResolvingTypedArrays()
{
$fixture = new TypeResolver();
/** @var Array_ $resolvedType */
$resolvedType = $fixture->resolve('string[]', new Context(''));
$this->assertInstanceOf('phpDocumentorReflectionTypesArray_', $resolvedType);
$this->assertSame('string[]', (string)$resolvedType);
$this->assertInstanceOf('phpDocumentorReflectionTypesCompound', $resolvedType->getKeyType());
$this->assertInstanceOf('phpDocumentorReflectionTypesString_', $resolvedType->getValueType());
}
/**
* @covers ::__construct
* @covers ::resolve
* @covers ::<private>
*
* @uses phpDocumentorReflectionTypesContext
* @uses phpDocumentorReflectionTypesArray_
* @uses phpDocumentorReflectionTypesString_
*/
public function testResolvingNestedTypedArrays()
{
$fixture = new TypeResolver();
/** @var Array_ $resolvedType */
$resolvedType = $fixture->resolve('string[][]', new Context(''));
/** @var Array_ $childValueType */
$childValueType = $resolvedType->getValueType();
$this->assertInstanceOf('phpDocumentorReflectionTypesArray_', $resolvedType);
$this->assertSame('string[][]', (string)$resolvedType);
$this->assertInstanceOf('phpDocumentorReflectionTypesCompound', $resolvedType->getKeyType());
$this->assertInstanceOf('phpDocumentorReflectionTypesArray_', $childValueType);
$this->assertSame('string[]', (string)$childValueType);
$this->assertInstanceOf('phpDocumentorReflectionTypesCompound', $childValueType->getKeyType());
$this->assertInstanceOf('phpDocumentorReflectionTypesString_', $childValueType->getValueType());
}
/**
* @covers ::__construct
* @covers ::resolve
* @covers ::<private>
*
* @uses phpDocumentorReflectionTypesContext
* @uses phpDocumentorReflectionTypesCompound
* @uses phpDocumentorReflectionTypesString_
* @uses phpDocumentorReflectionTypesObject_
* @uses phpDocumentorReflectionFqsen
* @uses phpDocumentorReflectionFqsenResolver
*/
public function testResolvingCompoundTypes()
{
$fixture = new TypeResolver();
/** @var Compound $resolvedType */
$resolvedType = $fixture->resolve('string|ReflectionDocBlock', new Context('phpDocumentor'));
$this->assertInstanceOf('phpDocumentorReflectionTypesCompound', $resolvedType);
$this->assertSame('string|phpDocumentorReflectionDocBlock', (string)$resolvedType);
/** @var String $secondType */
$firstType = $resolvedType->get(0);
/** @var Object_ $secondType */
$secondType = $resolvedType->get(1);
$this->assertInstanceOf('phpDocumentorReflectionTypesString_', $firstType);
$this->assertInstanceOf('phpDocumentorReflectionTypesObject_', $secondType);
$this->assertInstanceOf('phpDocumentorReflectionFqsen', $secondType->getFqsen());
}
/**
* @covers ::__construct
* @covers ::resolve
* @covers ::<private>
*
* @uses phpDocumentorReflectionTypesContext
* @uses phpDocumentorReflectionTypesCompound
* @uses phpDocumentorReflectionTypesArray_
* @uses phpDocumentorReflectionTypesObject_
* @uses phpDocumentorReflectionFqsen
* @uses phpDocumentorReflectionFqsenResolver
*/
public function testResolvingCompoundTypedArrayTypes()
{
$fixture = new TypeResolver();
/** @var Compound $resolvedType */
$resolvedType = $fixture->resolve('stdClass[]|ReflectionDocBlock[]', new Context('phpDocumentor'));
$this->assertInstanceOf('phpDocumentorReflectionTypesCompound', $resolvedType);
$this->assertSame('stdClass[]|phpDocumentorReflectionDocBlock[]', (string)$resolvedType);
/** @var Array_ $secondType */
$firstType = $resolvedType->get(0);
/** @var Array_ $secondType */
$secondType = $resolvedType->get(1);
$this->assertInstanceOf('phpDocumentorReflectionTypesArray_', $firstType);
$this->assertInstanceOf('phpDocumentorReflectionTypesArray_', $secondType);
$this->assertInstanceOf('phpDocumentorReflectionTypesObject_', $firstType->getValueType());
$this->assertInstanceOf('phpDocumentorReflectionTypesObject_', $secondType->getValueType());
}
/**
* This test asserts that the parameter order is correct.
*
* When you pass two arrays separated by the compound operator (i.e. 'integer[]|string[]') then we always split the
* expression in its compound parts and then we parse the types with the array operators. If we were to switch the
* order around then 'integer[]|string[]' would read as an array of string or integer array; which is something
* other than what we intend.
*
* @covers ::__construct
* @covers ::resolve
* @covers ::<private>
*
* @uses phpDocumentorReflectionTypesContext
* @uses phpDocumentorReflectionTypesCompound
* @uses phpDocumentorReflectionTypesArray_
* @uses phpDocumentorReflectionTypesInteger
* @uses phpDocumentorReflectionTypesString_
*/
public function testResolvingCompoundTypesWithTwoArrays()
{
$fixture = new TypeResolver();
/** @var Compound $resolvedType */
$resolvedType = $fixture->resolve('integer[]|string[]', new Context(''));
$this->assertInstanceOf('phpDocumentorReflectionTypesCompound', $resolvedType);
$this->assertSame('int[]|string[]', (string)$resolvedType);
/** @var Array_ $firstType */
$firstType = $resolvedType->get(0);
/** @var Array_ $secondType */
$secondType = $resolvedType->get(1);
$this->assertInstanceOf('phpDocumentorReflectionTypesArray_', $firstType);
$this->assertInstanceOf('phpDocumentorReflectionTypesInteger', $firstType->getValueType());
$this->assertInstanceOf('phpDocumentorReflectionTypesArray_', $secondType);
$this->assertInstanceOf('phpDocumentorReflectionTypesString_', $secondType->getValueType());
}
/**
* @covers ::__construct
* @covers ::addKeyword
* @uses phpDocumentorReflectionTypeResolver::resolve
* @uses phpDocumentorReflectionTypeResolver::<private>
* @uses phpDocumentorReflectionTypesContext
*/
public function testAddingAKeyword()
{
// Assign
$typeMock = m::mock(Type::class);
// Act
$fixture = new TypeResolver();
$fixture->addKeyword('mock', get_class($typeMock));
// Assert
$result = $fixture->resolve('mock', new Context(''));
$this->assertInstanceOf(get_class($typeMock), $result);
$this->assertNotSame($typeMock, $result);
}
/**
* @covers ::__construct
* @covers ::addKeyword
* @uses phpDocumentorReflectionTypesContext
* @expectedException InvalidArgumentException
*/
public function testAddingAKeywordFailsIfTypeClassDoesNotExist()
{
$fixture = new TypeResolver();
$fixture->addKeyword('mock', 'IDoNotExist');
}
/**
* @covers ::__construct
* @covers ::addKeyword
* @uses phpDocumentorReflectionTypesContext
* @expectedException InvalidArgumentException
*/
public function testAddingAKeywordFailsIfTypeClassDoesNotImplementTypeInterface()
{
$fixture = new TypeResolver();
$fixture->addKeyword('mock', 'stdClass');
}
/**
* @covers ::__construct
* @covers ::resolve
* @uses phpDocumentorReflectionTypesContext
*
* @expectedException InvalidArgumentException
*/
public function testExceptionIsThrownIfTypeIsEmpty()
{
$fixture = new TypeResolver();
$fixture->resolve(' ', new Context(''));
}
/**
* @covers ::__construct
* @covers ::resolve
* @uses phpDocumentorReflectionTypesContext
*
* @expectedException InvalidArgumentException
*/
public function testExceptionIsThrownIfTypeIsNotAString()
{
$fixture = new TypeResolver();
$fixture->resolve(['a'], new Context(''));
}
/**
* Returns a list of keywords and expected classes that are created from them.
*
* @return string[][]
*/
public function provideKeywords()
{
return [
['string', 'phpDocumentorReflectionTypesString_'],
['int', 'phpDocumentorReflectionTypesInteger'],
['integer', 'phpDocumentorReflectionTypesInteger'],
['float', 'phpDocumentorReflectionTypesFloat_'],
['double', 'phpDocumentorReflectionTypesFloat_'],
['bool', 'phpDocumentorReflectionTypesBoolean'],
['boolean', 'phpDocumentorReflectionTypesBoolean'],
['resource', 'phpDocumentorReflectionTypesResource'],
['null', 'phpDocumentorReflectionTypesNull_'],
['callable', 'phpDocumentorReflectionTypesCallable_'],
['callback', 'phpDocumentorReflectionTypesCallable_'],
['array', 'phpDocumentorReflectionTypesArray_'],
['scalar', 'phpDocumentorReflectionTypesScalar'],
['object', 'phpDocumentorReflectionTypesObject_'],
['mixed', 'phpDocumentorReflectionTypesMixed'],
['void', 'phpDocumentorReflectionTypesVoid_'],
['$this', 'phpDocumentorReflectionTypesThis'],
['static', 'phpDocumentorReflectionTypesStatic_'],
['self', 'phpDocumentorReflectionTypesSelf_'],
];
}
/**
* Provides a list of FQSENs to test the resolution patterns with.
*
* @return string[][]
*/
public function provideFqcn()
{
return [
'namespace' => ['phpDocumentorReflection'],
'class' => ['phpDocumentorReflectionDocBlock'],
];
}
}