Skip to content

Commit

Permalink
Provide the same functionality in simpler way with stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 13, 2019
1 parent 79d30f7 commit 846a5fa
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 188 deletions.
11 changes: 3 additions & 8 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ parameters:
- fail
- markTestIncomplete
- markTestSkipped
stubFiles:
- stubs/MockBuilder.php
- stubs/TestCase.php

services:
-
Expand All @@ -22,14 +25,6 @@ services:
class: PHPStan\Type\PHPUnit\Assert\AssertStaticMethodTypeSpecifyingExtension
tags:
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
-
class: PHPStan\Type\PHPUnit\CreateMockDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\PHPUnit\GetMockBuilderDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\PHPUnit\MockBuilderDynamicReturnTypeExtension
tags:
Expand Down
57 changes: 0 additions & 57 deletions src/Type/PHPUnit/CreateMockDynamicReturnTypeExtension.php

This file was deleted.

47 changes: 0 additions & 47 deletions src/Type/PHPUnit/GetMockBuilderDynamicReturnTypeExtension.php

This file was deleted.

53 changes: 10 additions & 43 deletions src/Type/PHPUnit/MockBuilderDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,34 @@

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;
use PHPUnit\Framework\MockObject\MockBuilder;

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

/** @var \PHPStan\Broker\Broker */
private $broker;

public function setBroker(Broker $broker): void
{
$this->broker = $broker;
}

public function getClass(): string
{
$testCase = $this->broker->getClass('PHPUnit\Framework\TestCase');
$mockBuilderType = ParametersAcceptorSelector::selectSingle(
$testCase->getNativeMethod('getMockBuilder')->getVariants()
)->getReturnType();
if (!$mockBuilderType instanceof TypeWithClassName) {
throw new \PHPStan\ShouldNotHappenException();
}

return $mockBuilderType->getClassName();
return MockBuilder::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return true;
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$calledOnType = $scope->getType($methodCall->var);
if (!in_array(
return !in_array(
$methodReflection->getName(),
[
'getMock',
'getMockForAbstractClass',
'getMockForTrait',
],
true
)) {
return $calledOnType;
}

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

if (!$calledOnType instanceof MockBuilderType) {
return $parametersAcceptor->getReturnType();
}

return TypeCombinator::intersect(
new ObjectType($calledOnType->getMockedClass()),
$parametersAcceptor->getReturnType()
);
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
return $scope->getType($methodCall->var);
}

}
33 changes: 0 additions & 33 deletions src/Type/PHPUnit/MockBuilderType.php

This file was deleted.

29 changes: 29 additions & 0 deletions stubs/MockBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace PHPUnit\Framework\MockObject;

use PHPUnit\Framework\TestCase;

/**
* @template TMockedClass
*/
class MockBuilder
{

/**
* @param TestCase $testCase
* @param class-string<TMockedClass> $type
*/
public function __construct(TestCase $testCase, $type) {}

/**
* @return MockObject&TMockedClass
*/
public function getMock() {}

/**
* @return MockObject&TMockedClass
*/
public function getMockForAbstractClass() {}

}
77 changes: 77 additions & 0 deletions stubs/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace PHPUnit\Framework;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\MockBuilder;

class TestCase
{

/**
* @template T
* @param class-string<T> $originalClassName
* @return MockObject&T
*/
public function createMock($originalClassName) {}

/**
* @template T
* @param class-string<T> $className
* @return MockBuilder<T>
*/
public function getMockBuilder(string $className) {}

/**
* @template T
* @param class-string<T> $originalClassName
* @return MockObject&T
*/
public function createConfiguredMock($originalClassName) {}

/**
* @template T
* @param class-string<T> $originalClassName
* @param string[] $methods
* @return MockObject&T
*/
public function createPartialMock($originalClassName, array $methods) {}

/**
* @template T
* @param class-string<T> $originalClassName
* @return MockObject&T
*/
public function createTestProxy($originalClassName) {}

/**
* @template T
* @param class-string<T> $originalClassName
* @param string $mockClassName
* @param bool $callOriginalConstructor
* @param bool $callOriginalClone
* @param bool $callAutoload
* @param string[] $mockedMethods
* @param bool $cloneArguments
* @return MockObject&T
*/
protected function getMockForAbstractClass($originalClassName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = false) {}

/**
* @template T
* @param string $wsdlFile
* @param class-string<T> $originalClassName
* @param string $mockClassName
* @param bool $callOriginalConstructor
* @param array $options
* @return MockObject&T
*/
protected function getMockFromWsdl($wsdlFile, $originalClassName = '', $mockClassName = '', array $methods = [], $callOriginalConstructor = true, array $options = []) {}

/**
* @param class-string<\Throwable> $exception
* @return void
*/
public function expectException(string $exception) {}

}

0 comments on commit 846a5fa

Please sign in to comment.