Skip to content

Commit

Permalink
Merge pull request #30 from weirdan/support-new-style-mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Nov 25, 2022
2 parents 843d545 + 2c0d69e commit 876247d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.phar
/vendor/
composer.lock
.php-version
64 changes: 57 additions & 7 deletions stubs/Mockery.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ public static function notAnyOf(...$args)

namespace Mockery
{
use Mockery\HigherOrderMessage;
use Mockery\MockInterface;
use Mockery\ExpectsHigherOrderMessage;
use Mockery\Exception\BadMethodCallException;

class Mock implements MockInterface
Expand Down Expand Up @@ -99,13 +96,13 @@ interface MockInterface
*
* @param mixed ...$methodNames one or many methods that are expected to be called in this mock
*
* @return \Mockery\ExpectationInterface&static
* @return ExpectationInterface&static
*/
public function shouldReceive(...$methodNames);

/**
* @param mixed $something String method name (optional)
* @return \Mockery\ExpectationInterface&static
* @param mixed $something
* @return ($something is string ? ExpectationInterface : ExpectsHigherOrderMessage)
*/
public function expects($something = null);

Expand All @@ -114,7 +111,7 @@ public function expects($something = null);
*
* @param mixed ...$methodNames one or many methods that are expected to be called in this mock
*
* @return \Mockery\ExpectationInterface&static
* @return ExpectationInterface&static
*/
public function shouldNotReceive(...$methodNames);

Expand All @@ -127,6 +124,12 @@ public function makePartial();
* @return static
*/
public function shouldAllowMockingProtectedMethods();

/**
* @param mixed $something
* @return ($something is string ? ExpectationInterface : ($something is list{} ? HigherOrderMessage : static))
*/
public function allows($something = []);
}

/**
Expand All @@ -140,6 +143,12 @@ interface ExpectationInterface
*/
public function andReturn(...$args);

/**
* @param mixed ...$args
* @return static
*/
public function andReturns(...$args);

/**
* Set a sequential queue of return values with an array
*
Expand Down Expand Up @@ -290,6 +299,47 @@ public function withArgs($argsOrClosure)
{
}
}

/**
* we need a docblock here, otherwise Psalm looks into original one,
* sees method tag there and starts emitting UndefinedMagicMethod
*/
class HigherOrderMessage
{
public function __construct(MockInterface $mock, $method)
{
}

public function withArgs(\Closure|array $args): Expectation
{
}

/**
* @return \Mockery\Expectation
*/
public function __call($method, $args)
{
}
}

/**
* we need a docblock here, otherwise Psalm looks into original one, sees
* that it extends HigherOrderMessage, looks into original
* HigherOrderMessagemethod dockblock, sees method tag there and starts
* emitting UndefinedMagicMethod
*/
class ExpectsHigherOrderMessage extends HigherOrderMessage
{
public function __construct(MockInterface $mock)
{
}
/**
* @return \Mockery\Expectation
*/
public function __call($method, $args)
{
}
}
}

// phpcs:enable
34 changes: 34 additions & 0 deletions tests/acceptance/MockReturn.feature
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,37 @@ Feature: MockReturn
"""
When I run Psalm
Then I see no errors

Scenario: New style stubbing
Given I have the following code
"""
class User
{
public function doThings(): int { return 10; }
}
function f(): int {
$mock = Mockery::mock(User::class);
$mock->allows()->doThings()->andReturns(1);
return $mock->doThings();
}
"""
When I run Psalm
Then I see no errors

Scenario: New style mocking
Given I have the following code
"""
class User
{
public function doThings(): int { return 10; }
}
function f(): int {
$mock = Mockery::mock(User::class);
$mock->expects()->doThings()->andReturns(1);
return $mock->doThings();
}
"""
When I run Psalm
Then I see no errors

0 comments on commit 876247d

Please sign in to comment.