Skip to content

Commit f9bdc0c

Browse files
committed
Drop Prophecy in favor of PHPUnit's mocking
See sebastianbergmann/phpunit#4141.
1 parent d17b079 commit f9bdc0c

File tree

5 files changed

+97
-29
lines changed

5 files changed

+97
-29
lines changed

psalm.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,16 @@
1313
<directory name="vendor" />
1414
</ignoreFiles>
1515
</projectFiles>
16+
17+
<issueHandlers>
18+
<InternalMethod>
19+
<errorLevel type="suppress">
20+
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::method" />
21+
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::with" />
22+
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::willReturn" />
23+
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::willReturnOnConsecutiveCalls" />
24+
<referencedMethod name="PHPUnit\Framework\TestCase::__construct" />
25+
</errorLevel>
26+
</InternalMethod>
27+
</issueHandlers>
1628
</psalm>

src/Client/ClientMocking.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,17 @@
1313

1414
namespace Tarantool\PhpUnit\Client;
1515

16-
use Prophecy\Prophecy\ObjectProphecy;
1716
use Tarantool\Client\Client;
1817

1918
trait ClientMocking
2019
{
2120
protected function getMockClientBuilder() : MockClientBuilder
2221
{
23-
return new MockClientBuilder(\Closure::fromCallable([$this, 'prophesize']));
22+
return new MockClientBuilder($this);
2423
}
2524

2625
protected function createMockClient() : Client
2726
{
2827
return $this->getMockClientBuilder()->build();
2928
}
30-
31-
abstract protected function prophesize(?string $classOrInterface = null) : ObjectProphecy;
3229
}

src/Client/IsRequestType.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the tarantool/phpunit-extras package.
5+
*
6+
* (c) Eugene Leonovich <gen.work@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Tarantool\PhpUnit\Client;
15+
16+
use PHPUnit\Framework\Constraint\Constraint;
17+
use Tarantool\Client\Request\Request;
18+
use Tarantool\Client\RequestTypes;
19+
20+
final class IsRequestType extends Constraint
21+
{
22+
/** @var int */
23+
private $requestType;
24+
25+
public function __construct(int $requestType)
26+
{
27+
// needed for backward compatibility with PHPUnit 7
28+
if (\is_callable('parent::__construct')) {
29+
parent::__construct();
30+
}
31+
32+
$this->requestType = $requestType;
33+
}
34+
35+
public function toString() : string
36+
{
37+
return sprintf('is a "%s" request', strtoupper(RequestTypes::getName($this->requestType)));
38+
}
39+
40+
protected function matches($other) : bool
41+
{
42+
return $other instanceof Request && $other->getType() === $this->requestType;
43+
}
44+
}

src/Client/MockClientBuilder.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
namespace Tarantool\PhpUnit\Client;
1515

16-
use Prophecy\Argument;
17-
use Prophecy\Argument\Token\TokenInterface;
18-
use Prophecy\Prophecy\ObjectProphecy;
19-
use Prophecy\Prophet;
16+
use PHPUnit\Framework\Constraint\Constraint;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
2019
use Tarantool\Client\Client;
2120
use Tarantool\Client\Connection\Connection;
2221
use Tarantool\Client\Handler\Handler;
@@ -26,8 +25,8 @@
2625

2726
final class MockClientBuilder
2827
{
29-
/** @var \Closure */
30-
private $prophesize;
28+
/** @var TestCase */
29+
private $testCase;
3130

3231
/** @var \SplObjectStorage<object, array<int, Response>> */
3332
private $requests;
@@ -38,21 +37,23 @@ final class MockClientBuilder
3837
/** @var Packer|null */
3938
private $packer;
4039

41-
public function __construct(\Closure $prophesize)
40+
public function __construct(TestCase $testCase)
4241
{
43-
$this->prophesize = $prophesize;
42+
$this->testCase = $testCase;
4443
$this->requests = new \SplObjectStorage();
4544
}
4645

4746
public static function buildDefault() : Client
4847
{
49-
$self = new self(\Closure::fromCallable([new Prophet(), 'prophesize']));
48+
/** @psalm-suppress PropertyNotSetInConstructor */
49+
$self = new self(new class() extends TestCase {
50+
});
5051

5152
return $self->build();
5253
}
5354

5455
/**
55-
* @param Request|TokenInterface $request
56+
* @param Request|Constraint $request
5657
* @param Response ...$responses
5758
*/
5859
public function shouldHandle($request, ...$responses) : self
@@ -79,63 +80,77 @@ public function willUsePacker(Packer $packer) : self
7980
public function build() : Client
8081
{
8182
/** @var Handler $handler */
82-
$handler = $this->createHandler()->reveal();
83+
$handler = $this->createHandler();
8384

8485
return new Client($handler);
8586
}
8687

87-
private function createHandler() : ObjectProphecy
88+
private function createHandler() : MockObject
8889
{
89-
$handler = ($this->prophesize)(Handler::class);
90+
$handler = $this->createMock(Handler::class);
9091

9192
$connection = $this->createConnection();
92-
$handler->getConnection()->willReturn($connection);
93+
$handler->method('getConnection')->willReturn($connection);
9394

9495
$packer = $this->createPacker();
95-
$handler->getPacker()->willReturn($packer);
96+
$handler->method('getPacker')->willReturn($packer);
9697

9798
$defaultResponse = DummyFactory::createEmptyResponse();
9899

99100
if (!$this->requests->count()) {
100-
$handler->handle(Argument::type(Request::class))->willReturn($defaultResponse);
101+
$handler->method('handle')->willReturn($defaultResponse);
101102

102103
return $handler;
103104
}
104105

105106
foreach ($this->requests as $request) {
106107
if (!$responses = $this->requests->getInfo()) {
107-
$handler->handle($request)->willReturn($defaultResponse);
108+
$handler->method('handle')->with($request)->willReturn($defaultResponse);
108109
continue;
109110
}
110111

111-
$handler->handle($request)->willReturn(...$responses)
112-
->shouldBeCalledTimes(\count($responses));
112+
$handler->expects(TestCase::exactly(\count($responses)))
113+
->method('handle')->with($request)
114+
->willReturnOnConsecutiveCalls(...$responses);
113115
}
114116

115117
return $handler;
116118
}
117119

118120
/**
119-
* @return Connection|ObjectProphecy
121+
* @return Connection|MockObject
120122
*/
121123
private function createConnection()
122124
{
123125
if ($this->connection) {
124126
return $this->connection;
125127
}
126128

127-
return ($this->prophesize)(Connection::class);
129+
return $this->createMock(Connection::class);
128130
}
129131

130132
/**
131-
* @return Packer|ObjectProphecy
133+
* @return Packer|MockObject
132134
*/
133135
private function createPacker()
134136
{
135137
if ($this->packer) {
136138
return $this->packer;
137139
}
138140

139-
return ($this->prophesize)(Packer::class);
141+
return $this->createMock(Packer::class);
142+
}
143+
144+
/**
145+
* @param class-string $originalClassName
146+
*/
147+
private function createMock(string $originalClassName) : MockObject
148+
{
149+
return $this->testCase->getMockBuilder($originalClassName)
150+
->disableOriginalConstructor()
151+
->disableOriginalClone()
152+
->disableArgumentCloning()
153+
->disallowMockingUnknownTypes()
154+
->getMock();
140155
}
141156
}

tests/Expectation/ExpressionContext/RequestCountContextTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
use PHPUnit\Framework\TestCase;
1717
use PHPUnitExtras\Expectation\ExpressionContext;
18-
use Prophecy\Argument;
1918
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
2019
use Tarantool\Client\RequestTypes;
2120
use Tarantool\PhpUnit\Client\ClientMocking;
2221
use Tarantool\PhpUnit\Client\DummyFactory;
22+
use Tarantool\PhpUnit\Client\IsRequestType;
2323
use Tarantool\PhpUnit\Expectation\ExpressionContext\RequestCountContext;
2424
use Tarantool\PhpUnit\Expectation\ExpressionContext\RequestCounter;
2525

@@ -31,7 +31,7 @@ public function testGetValuesReturnsCorrectValues() : void
3131
{
3232
$mockClient = $this->getMockClientBuilder()
3333
->shouldHandle(
34-
Argument::which('getType', RequestTypes::EVALUATE),
34+
new IsRequestType(RequestTypes::EVALUATE),
3535
DummyFactory::createResponseFromData([2]),
3636
DummyFactory::createResponseFromData([3])
3737
)->build();

0 commit comments

Comments
 (0)