Skip to content

Commit

Permalink
Enhancement: Add support for willExtend()
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 27, 2019
1 parent 46b4f00 commit 73de662
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 10 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`0.5.1...master`][0.5.1...master].
For a full diff see [`0.6.0...master`][0.6.0...master].

## [`0.6.0`][0.6.0]

For a full diff see [`0.5.1...0.6.0`][0.5.1...0.6.0].

### Added

* Support for `willImplement()` ([#92]), by [@localheinz]
* Support for `willExtend()` ([#94]), by [@localheinz]

## [`0.5.1`][0.5.1]

Expand Down Expand Up @@ -70,6 +75,7 @@ For a full diff see [`afd6fd9...0.1`][afd6fd9...0.1].
[0.4.2]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.4.2
[0.5.0]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.5.0
[0.5.1]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.5.1
[0.6.0]: https://github.com/Jan0707/phpstan-prophecy/releases/tag/0.6.0

[afd6fd9...0.1]: https://github.com/Jan0707/phpstan-prophecy/compare/afd6fd9...0.1
[0.1...0.1.1]: https://github.com/Jan0707/phpstan-prophecy/compare/0.1...0.1.1
Expand All @@ -81,11 +87,13 @@ For a full diff see [`afd6fd9...0.1`][afd6fd9...0.1].
[0.4.1...0.4.2]: https://github.com/Jan0707/phpstan-prophecy/compare/0.4.1...0.4.2
[0.4.2...0.5.0]: https://github.com/Jan0707/phpstan-prophecy/compare/0.4.2...0.5.0
[0.5.0...0.5.1]: https://github.com/Jan0707/phpstan-prophecy/compare/0.5.0...0.5.1
[0.5.1...master]: https://github.com/Jan0707/phpstan-prophecy/compare/0.5.1...master
[0.5.1...0.6.0]: https://github.com/Jan0707/phpstan-prophecy/compare/0.5.1...0.6.0
[0.6.0...master]: https://github.com/Jan0707/phpstan-prophecy/compare/0.6.0...master

[#67]: https://github.com/Jan0707/phpstan-prophecy/pull/67
[#79]: https://github.com/Jan0707/phpstan-prophecy/pull/79
[#92]: https://github.com/Jan0707/phpstan-prophecy/pull/92
[#94]: https://github.com/Jan0707/phpstan-prophecy/pull/94

[@localheinz]: https://github.com/localheinz
[@PedroTroller]: https://github.com/PedroTroller
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;

class ObjectProphecyWillImplementDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
class ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
Expand All @@ -22,7 +22,16 @@ public function getClass(): string

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return 'willImplement' === $methodReflection->getName();
$methodNames = [
'willImplement',
'willExtend',
];

return \in_array(
$methodReflection->getName(),
$methodNames,
true
);
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
$returnType = $parametersAcceptor->getReturnType();

if (0 === \count($methodCall->args)) {
return $returnType;
return new ObjectProphecyType();
}

$argumentType = $scope->getType($methodCall->args[0]->value);
Expand Down
6 changes: 2 additions & 4 deletions src/Type/ObjectProphecyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ class ObjectProphecyType extends ObjectType
*/
protected $prophesizedClasses;

public function __construct(string $prophesizedClass)
public function __construct(string ...$prophesizedClasses)
{
$this->prophesizedClasses = [
$prophesizedClass,
];
$this->prophesizedClasses = $prophesizedClasses;

parent::__construct('Prophecy\Prophecy\ObjectProphecy');
}
Expand Down
2 changes: 1 addition & 1 deletion src/extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: JanGregor\Prophecy\Extension\ObjectProphecyWillImplementDynamicReturnTypeExtension
class: JanGregor\Prophecy\Extension\ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

Expand Down
5 changes: 5 additions & 0 deletions tests/Model/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public function bar(Bar $bar): string
{
return $bar->bar();
}

public function baz(Baz $baz): string
{
return $baz->baz();
}
}
10 changes: 10 additions & 0 deletions tests/Model/Baz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace JanGregor\Prophecy\Test\Model;

abstract class Baz
{
abstract public function baz(): string;
}
15 changes: 15 additions & 0 deletions tests/Test/BaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use JanGregor\Prophecy\Test\Model\Bar;
use JanGregor\Prophecy\Test\Model\BaseModel;
use JanGregor\Prophecy\Test\Model\Baz;
use JanGregor\Prophecy\Test\Model\Foo;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
Expand Down Expand Up @@ -62,6 +63,20 @@ public function testProphesizedAttributesShouldAlsoWork(): void
self::assertEquals(5, $subject->doubleTheNumber(2));
}

public function testWillExtendWorks(): void
{
$baz = $this->prophesize()->willExtend(Baz::class);

$baz
->baz()
->shouldBeCalled()
->willReturn('Hmm');

$subject = new BaseModel();

self::assertSame('Hmm', $subject->baz($baz->reveal()));
}

public function testWillImplementWorks(): void
{
$fooThatAlsoBars = $this->prophesize(Foo::class);
Expand Down

0 comments on commit 73de662

Please sign in to comment.