Visitar URL original
Set correct types for mocks of multiple classes by Khartir · Pull Request #61 · phpstan/phpstan-phpunit · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fixes CI errors
  • Loading branch information
Khartir committed Feb 7, 2020
commit 613e460c8064764083d8d01085d52aecdffce182
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ includes:
parameters:
excludes_analyse:
- tests/*/data/*
treatPhpDocTypesAsCertain: false

services:
scopeIsInClass:
Expand Down
5 changes: 3 additions & 2 deletions src/Type/PHPUnit/CreateMockDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

class CreateMockDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
class CreateMockDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

/** @var int[] */
Expand Down Expand Up @@ -51,7 +52,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

if ($argType instanceof ConstantArrayType) {
$types = array_map(function (Type $argType): ObjectType {
$types = array_map(function (ConstantStringType $argType): ObjectType {
return new ObjectType($argType->getValue());
}, $argType->getValueTypes());
}
Expand Down
5 changes: 3 additions & 2 deletions src/Type/PHPUnit/GetMockBuilderDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;

class GetMockBuilderDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
class GetMockBuilderDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
Expand Down Expand Up @@ -43,7 +44,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

if ($argType instanceof ConstantArrayType) {
$classes = array_map(function (Type $argType): string {
$classes = array_map(function (ConstantStringType $argType): string {
return $argType->getValue();
}, $argType->getValueTypes());

Expand Down
4 changes: 3 additions & 1 deletion src/Type/PHPUnit/MockBuilderDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\BrokerAwareExtension;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;

class MockBuilderDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension, \PHPStan\Reflection\BrokerAwareExtension
class MockBuilderDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension, BrokerAwareExtension
{

/** @var \PHPStan\Broker\Broker */
Expand Down
3 changes: 2 additions & 1 deletion src/Type/PHPUnit/MockBuilderType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace PHPStan\Type\PHPUnit;

use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VerbosityLevel;

class MockBuilderType extends \PHPStan\Type\ObjectType
class MockBuilderType extends ObjectType
{

/** @var array<string> */
Expand Down
2 changes: 1 addition & 1 deletion tests/Type/PHPUnit/CreateMockExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testCreateMock(string $expression, string $type): void
__DIR__ . '/data/create-mock.php',
$expression,
$type,
[new CreateMockDynamicReturnTypeExtension(), new GetMockBuilderDynamicReturnTypeExtension()]
[new CreateMockDynamicReturnTypeExtension()]
);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Type/PHPUnit/ExtensionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,31 @@
use PHPStan\PhpDoc\PhpDocNodeResolver;
use PHPStan\PhpDoc\PhpDocStringResolver;
use PHPStan\Testing\TestCase;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\VerbosityLevel;

abstract class ExtensionTestCase extends TestCase
{

/**
* @param string $file
* @param string $expression
* @param string $type
* @param array<DynamicMethodReturnTypeExtension> $extensions
*/
protected function processFile(
string $file,
string $expression,
string $type,
array $extensions
): void
{
foreach ($extensions as $extension) {
if (!$extension instanceof DynamicMethodReturnTypeExtension) {
throw new \InvalidArgumentException();
}
}
$broker = $this->createBroker($extensions);
$parser = $this->getParser();
$currentWorkingDirectory = $this->getCurrentWorkingDirectory();
Expand Down