Skip to content

Commit da53eed

Browse files
committed
Remove prophecy, change tests to use phpunit native mocking
1 parent 7ff74c0 commit da53eed

64 files changed

Lines changed: 3686 additions & 3440 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

phpstan-baseline.neon

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -330,24 +330,6 @@ parameters:
330330
count: 1
331331
path: tests/Unit/CommandBus/Handler/CreateAggregateHandlerTest.php
332332

333-
-
334-
message: '#^Method class@anonymous/tests/Unit/CommandBus/Handler/DefaultParameterResolverTest\.php\:69\:\:handle\(\) has parameter \$foo with no type specified\.$#'
335-
identifier: missingType.parameter
336-
count: 1
337-
path: tests/Unit/CommandBus/Handler/DefaultParameterResolverTest.php
338-
339-
-
340-
message: '#^Method class@anonymous/tests/Unit/CommandBus/Handler/DefaultParameterResolverTest\.php\:95\:\:handle\(\) has parameter \$foo with no type specified\.$#'
341-
identifier: missingType.parameter
342-
count: 1
343-
path: tests/Unit/CommandBus/Handler/DefaultParameterResolverTest.php
344-
345-
-
346-
message: '#^Method class@anonymous/tests/Unit/CommandBus/HandlerFinderTest\.php\:40\:\:handle\(\) has parameter \$command with no type specified\.$#'
347-
identifier: missingType.parameter
348-
count: 1
349-
path: tests/Unit/CommandBus/HandlerFinderTest.php
350-
351333
-
352334
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertTrue\(\) with true will always evaluate to true\.$#'
353335
identifier: method.alreadyNarrowedType
@@ -462,12 +444,6 @@ parameters:
462444
count: 1
463445
path: tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php
464446

465-
-
466-
message: '#^Method class@anonymous/tests/Unit/QueryBus/HandlerFinderTest\.php\:40\:\:handle\(\) has parameter \$query with no type specified\.$#'
467-
identifier: missingType.parameter
468-
count: 1
469-
path: tests/Unit/QueryBus/HandlerFinderTest.php
470-
471447
-
472448
message: '#^Cannot access offset 0 on iterable\<int, Patchlevel\\EventSourcing\\QueryBus\\HandlerDescriptor\>\.$#'
473449
identifier: offsetAccess.nonOffsetAccessible
@@ -480,18 +456,6 @@ parameters:
480456
count: 2
481457
path: tests/Unit/QueryBus/ServiceHandlerProviderTest.php
482458

483-
-
484-
message: '#^Trying to invoke mixed but it''s not a callable\.$#'
485-
identifier: callable.nonCallable
486-
count: 15
487-
path: tests/Unit/Store/DoctrineDbalStoreTest.php
488-
489-
-
490-
message: '#^Trying to invoke mixed but it''s not a callable\.$#'
491-
identifier: callable.nonCallable
492-
count: 13
493-
path: tests/Unit/Store/StreamDoctrineDbalStoreTest.php
494-
495459
-
496460
message: '#^Match expression does not handle remaining value\: string$#'
497461
identifier: match.unhandled
@@ -504,18 +468,6 @@ parameters:
504468
count: 8
505469
path: tests/Unit/Subscription/Engine/DefaultSubscriptionEngineTest.php
506470

507-
-
508-
message: '#^Trying to invoke mixed but it''s not a callable\.$#'
509-
identifier: callable.nonCallable
510-
count: 2
511-
path: tests/Unit/Subscription/Engine/DefaultSubscriptionEngineTest.php
512-
513-
-
514-
message: '#^Trying to invoke mixed but it''s not a callable\.$#'
515-
identifier: callable.nonCallable
516-
count: 1
517-
path: tests/Unit/Subscription/Engine/SubscriptionManagerTest.php
518-
519471
-
520472
message: '#^Offset ''args'' on array\{file\: literal\-string&non\-falsy\-string, line\: int, function\: ''createException'', class\: ''Patchlevel\\\\EventSourcing\\\\Tests\\\\Unit\\\\Subscription\\\\ErrorContextTest'', type\: ''\-\>'', args\: array\<mixed\>\} on left side of \?\? always exists and is not nullable\.$#'
521473
identifier: nullCoalesce.offset

tests/ReturnCallback.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\Tests;
6+
7+
use PHPUnit\Framework\Assert;
8+
9+
use function array_shift;
10+
11+
final class ReturnCallback
12+
{
13+
/** @param array{list<mixed>, mixed} $series */
14+
public function __construct(
15+
private array $series,
16+
) {
17+
}
18+
19+
public function __invoke(mixed ...$args): mixed
20+
{
21+
[$expectedArgs, $return] = array_shift($this->series);
22+
Assert::assertEquals($expectedArgs, $args);
23+
24+
return $return;
25+
}
26+
}

tests/Unit/Aggregate/UuidTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ public function uuid7(DateTimeInterface|null $dateTime = null): UuidInterface
3232
}
3333
};
3434

35-
RamseyUuid::setFactory($factory);
36-
$id = Uuid::generate();
35+
$oldFactory = RamseyUuid::getFactory();
3736

38-
self::assertSame('018d6a97-6aba-7104-825f-67313a77a2a4', $id->toString());
37+
try {
38+
RamseyUuid::setFactory($factory);
39+
$id = Uuid::generate();
40+
41+
self::assertSame('018d6a97-6aba-7104-825f-67313a77a2a4', $id->toString());
42+
} finally {
43+
RamseyUuid::setFactory($oldFactory);
44+
}
3945
}
4046
}

tests/Unit/CommandBus/AggregateHandlerProviderTest.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@
1515
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileWithHandler;
1616
use PHPUnit\Framework\Attributes\CoversClass;
1717
use PHPUnit\Framework\TestCase;
18-
use Prophecy\PhpUnit\ProphecyTrait;
1918

2019
#[CoversClass(AggregateHandlerProvider::class)]
2120
final class AggregateHandlerProviderTest extends TestCase
2221
{
23-
use ProphecyTrait;
24-
2522
public function testEmpty(): void
2623
{
27-
$repositoryManager = $this->prophesize(RepositoryManager::class);
24+
$repositoryManager = $this->createMock(RepositoryManager::class);
2825

2926
$provider = new AggregateHandlerProvider(
3027
new AggregateRootRegistry([]),
31-
$repositoryManager->reveal(),
28+
$repositoryManager,
3229
);
3330

3431
$result = $provider->handlerForCommand(CreateProfile::class);
@@ -38,17 +35,17 @@ public function testEmpty(): void
3835

3936
public function testGetCreateHandler(): void
4037
{
41-
$repositoryManager = $this->prophesize(RepositoryManager::class);
38+
$repositoryManager = $this->createMock(RepositoryManager::class);
4239

4340
$provider = new AggregateHandlerProvider(
4441
new AggregateRootRegistry(['profile' => ProfileWithHandler::class]),
45-
$repositoryManager->reveal(),
42+
$repositoryManager,
4643
);
4744

4845
$result = $provider->handlerForCommand(CreateProfile::class);
4946

5047
$handler = new CreateAggregateHandler(
51-
$repositoryManager->reveal(),
48+
$repositoryManager,
5249
ProfileWithHandler::class,
5350
'create',
5451
new DefaultParameterResolver(),
@@ -60,17 +57,17 @@ public function testGetCreateHandler(): void
6057

6158
public function testGetUpdateHandler(): void
6259
{
63-
$repositoryManager = $this->prophesize(RepositoryManager::class);
60+
$repositoryManager = $this->createMock(RepositoryManager::class);
6461

6562
$provider = new AggregateHandlerProvider(
6663
new AggregateRootRegistry(['profile' => ProfileWithHandler::class]),
67-
$repositoryManager->reveal(),
64+
$repositoryManager,
6865
);
6966

7067
$result = $provider->handlerForCommand(ChangeProfileName::class);
7168

7269
$handler = new UpdateAggregateHandler(
73-
$repositoryManager->reveal(),
70+
$repositoryManager,
7471
ProfileWithHandler::class,
7572
'updateName',
7673
new DefaultParameterResolver(),

tests/Unit/CommandBus/ChainHandlerProviderTest.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
use Patchlevel\EventSourcing\Tests\Unit\Fixture\CreateProfile;
1111
use PHPUnit\Framework\Attributes\CoversClass;
1212
use PHPUnit\Framework\TestCase;
13-
use Prophecy\PhpUnit\ProphecyTrait;
1413

1514
#[CoversClass(ChainHandlerProvider::class)]
1615
final class ChainHandlerProviderTest extends TestCase
1716
{
18-
use ProphecyTrait;
19-
2017
public function testEmpty(): void
2118
{
2219
$provider = new ChainHandlerProvider([]);
@@ -32,18 +29,23 @@ public function testFindHandler(): void
3229
$handler2 = new HandlerDescriptor(static fn () => null);
3330
$handler3 = new HandlerDescriptor(static fn () => null);
3431

35-
$provider1 = $this->prophesize(HandlerProvider::class);
36-
$provider1->handlerForCommand(CreateProfile::class)->willReturn([
37-
$handler1,
38-
$handler2,
39-
]);
32+
$provider1 = $this->createMock(HandlerProvider::class);
33+
$provider1
34+
->method('handlerForCommand')
35+
->with(CreateProfile::class)
36+
->willReturn(
37+
[
38+
$handler1,
39+
$handler2,
40+
],
41+
);
4042

41-
$provider2 = $this->prophesize(HandlerProvider::class);
42-
$provider2->handlerForCommand(CreateProfile::class)->willReturn([$handler3]);
43+
$provider2 = $this->createMock(HandlerProvider::class);
44+
$provider2->method('handlerForCommand')->with(CreateProfile::class)->willReturn([$handler3]);
4345

4446
$chainProvider = new ChainHandlerProvider([
45-
$provider1->reveal(),
46-
$provider2->reveal(),
47+
$provider1,
48+
$provider2,
4749
]);
4850

4951
$result = $chainProvider->handlerForCommand(CreateProfile::class);

tests/Unit/CommandBus/Handler/CreateAggregateHandlerTest.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,21 @@
1414
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileWithHandler;
1515
use PHPUnit\Framework\Attributes\CoversClass;
1616
use PHPUnit\Framework\TestCase;
17-
use Prophecy\Argument;
18-
use Prophecy\PhpUnit\ProphecyTrait;
1917

2018
#[CoversClass(CreateAggregateHandler::class)]
2119
final class CreateAggregateHandlerTest extends TestCase
2220
{
23-
use ProphecyTrait;
24-
2521
public function testSuccess(): void
2622
{
27-
$repository = $this->prophesize(Repository::class);
28-
$repository->save(Argument::type(ProfileWithHandler::class))->shouldBeCalled();
23+
$repository = $this->createMock(Repository::class);
24+
$repository->expects($this->atLeastOnce())->method('save')->with($this->isInstanceOf(ProfileWithHandler::class));
2925

30-
$repositoryManager = $this->prophesize(RepositoryManager::class);
31-
$repositoryManager
32-
->get(ProfileWithHandler::class)
33-
->willReturn($repository->reveal())
34-
->shouldBeCalled();
26+
$repositoryManager = $this->createMock(RepositoryManager::class);
27+
$repositoryManager->expects($this->atLeastOnce())->method('get')->with(ProfileWithHandler::class)
28+
->willReturn($repository);
3529

3630
$handler = new CreateAggregateHandler(
37-
$repositoryManager->reveal(),
31+
$repositoryManager,
3832
ProfileWithHandler::class,
3933
'create',
4034
new DefaultParameterResolver(),
@@ -57,16 +51,14 @@ public static function create(): string
5751
}
5852
};
5953

60-
$repository = $this->prophesize(Repository::class);
54+
$repository = $this->createMock(Repository::class);
6155

62-
$repositoryManager = $this->prophesize(RepositoryManager::class);
63-
$repositoryManager
64-
->get($class::class)
65-
->willReturn($repository->reveal())
66-
->shouldBeCalled();
56+
$repositoryManager = $this->createMock(RepositoryManager::class);
57+
$repositoryManager->expects($this->atLeastOnce())->method('get')->with($class::class)
58+
->willReturn($repository);
6759

6860
$handler = new CreateAggregateHandler(
69-
$repositoryManager->reveal(),
61+
$repositoryManager,
7062
$class::class,
7163
'create',
7264
new DefaultParameterResolver(),

tests/Unit/CommandBus/Handler/DefaultParameterResolverTest.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
use Patchlevel\EventSourcing\CommandBus\ServiceNotFound;
1111
use PHPUnit\Framework\Attributes\CoversClass;
1212
use PHPUnit\Framework\TestCase;
13-
use Prophecy\PhpUnit\ProphecyTrait;
1413
use Psr\Container\ContainerInterface;
1514
use ReflectionMethod;
1615
use stdClass;
1716

1817
#[CoversClass(DefaultParameterResolver::class)]
1918
final class DefaultParameterResolverTest extends TestCase
2019
{
21-
use ProphecyTrait;
22-
2320
public function testNoParameters(): void
2421
{
2522
$class = new class () {
@@ -68,7 +65,7 @@ public function testMissingContainer(): void
6865

6966
$class = new class () {
7067
// phpcs:disable
71-
public function handle(stdClass $command, $foo): void
68+
public function handle(stdClass $command, mixed $foo): void
7269
{
7370
}
7471
// phpcs:enable
@@ -94,15 +91,15 @@ public function testNoType(): void
9491

9592
$class = new class () {
9693
// phpcs:disable
97-
public function handle(stdClass $command, $foo): void
94+
public function handle(stdClass $command, mixed $foo): void
9895
{
9996
}
10097
// phpcs:enable
10198
};
10299

103-
$container = $this->prophesize(ContainerInterface::class);
100+
$container = $this->createMock(ContainerInterface::class);
104101

105-
$resolver = new DefaultParameterResolver($container->reveal());
102+
$resolver = new DefaultParameterResolver($container);
106103

107104
$command = new stdClass();
108105

@@ -126,9 +123,9 @@ public function handle(stdClass $command, string $foo): void
126123
}
127124
};
128125

129-
$container = $this->prophesize(ContainerInterface::class);
126+
$container = $this->createMock(ContainerInterface::class);
130127

131-
$resolver = new DefaultParameterResolver($container->reveal());
128+
$resolver = new DefaultParameterResolver($container);
132129

133130
$command = new stdClass();
134131

@@ -152,10 +149,14 @@ public function handle(stdClass $command, stdClass $foo): void
152149
}
153150
};
154151

155-
$container = $this->prophesize(ContainerInterface::class);
156-
$container->get(stdClass::class)->willThrow(new ServiceNotFound(stdClass::class))->shouldBeCalledOnce();
152+
$container = $this->createMock(ContainerInterface::class);
153+
$container
154+
->expects($this->once())
155+
->method('get')
156+
->with(stdClass::class)
157+
->willThrowException(new ServiceNotFound(stdClass::class));
157158

158-
$resolver = new DefaultParameterResolver($container->reveal());
159+
$resolver = new DefaultParameterResolver($container);
159160

160161
$command = new stdClass();
161162

@@ -179,10 +180,14 @@ public function handle(stdClass $command, stdClass $foo): void
179180

180181
$service = new stdClass();
181182

182-
$container = $this->prophesize(ContainerInterface::class);
183-
$container->get(stdClass::class)->willReturn($service)->shouldBeCalledOnce();
183+
$container = $this->createMock(ContainerInterface::class);
184+
$container
185+
->expects($this->once())
186+
->method('get')
187+
->with(stdClass::class)
188+
->willReturn($service);
184189

185-
$resolver = new DefaultParameterResolver($container->reveal());
190+
$resolver = new DefaultParameterResolver($container);
186191

187192
$command = new stdClass();
188193

@@ -209,10 +214,13 @@ public function handle(
209214

210215
$service = new stdClass();
211216

212-
$container = $this->prophesize(ContainerInterface::class);
213-
$container->get('foo')->willReturn($service)->shouldBeCalledOnce();
217+
$container = $this->createMock(ContainerInterface::class);
218+
$container->expects($this->once())
219+
->method('get')
220+
->with('foo')
221+
->willReturn($service);
214222

215-
$resolver = new DefaultParameterResolver($container->reveal());
223+
$resolver = new DefaultParameterResolver($container);
216224

217225
$command = new stdClass();
218226

0 commit comments

Comments
 (0)