Skip to content

Commit bd55301

Browse files
committed
refactor/api: remove usage of Prophecy in TagCollectorTest
api-platform wants to get rid of it, and we don't have a lot of it, so we can remove it easily now.
1 parent c27aaac commit bd55301

File tree

1 file changed

+55
-17
lines changed

1 file changed

+55
-17
lines changed

api/tests/HttpCache/TagCollectorTest.php

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,44 @@
77
use App\HttpCache\ResponseTagger;
88
use App\HttpCache\TagCollector;
99
use App\Tests\HttpCache\Entity\Dummy;
10+
use PHPUnit\Framework\MockObject\MockObject;
1011
use PHPUnit\Framework\TestCase;
11-
use Prophecy\Argument;
12-
use Prophecy\PhpUnit\ProphecyTrait;
13-
use Prophecy\Prophecy\ObjectProphecy;
12+
13+
use function PHPUnit\Framework\exactly;
14+
use function PHPUnit\Framework\never;
15+
use function PHPUnit\Framework\once;
1416

1517
/**
1618
* @internal
1719
*/
1820
class TagCollectorTest extends TestCase {
19-
use ProphecyTrait;
20-
2121
private TagCollectorInterface $tagCollector;
22-
private ObjectProphecy $responseTaggerProphecy;
22+
private MockObject&ResponseTagger $responseTagger;
2323

2424
protected function setUp(): void {
2525
// given
26-
$this->responseTaggerProphecy = $this->prophesize(ResponseTagger::class);
27-
$this->tagCollector = new TagCollector($this->responseTaggerProphecy->reveal());
26+
$this->responseTagger = $this->createMock(ResponseTagger::class);
27+
$this->tagCollector = new TagCollector($this->responseTagger);
2828
}
2929

3030
public function testNoTagForEmptyContext() {
3131
// then
32-
$this->responseTaggerProphecy->addTags(Argument::any())->shouldNotBeCalled();
32+
$this->responseTagger
33+
->expects($this->never())
34+
->method('addTags')
35+
;
3336

3437
// when
3538
$this->tagCollector->collect([]);
3639
}
3740

3841
public function testWithIri() {
3942
// then
40-
$this->responseTaggerProphecy->addTags(['/test-iri'])->shouldBeCalled();
43+
$this->responseTagger
44+
->expects($this->once())
45+
->method('addTags')
46+
->with(['/test-iri'])
47+
;
4148

4249
// when
4350
$this->tagCollector->collect(['iri' => '/test-iri']);
@@ -49,7 +56,11 @@ public function testWithBaseEntity() {
4956
$object->setId('123');
5057

5158
// then
52-
$this->responseTaggerProphecy->addTags(['123'])->shouldBeCalled();
59+
$this->responseTagger
60+
->expects(once())
61+
->method('addTags')
62+
->with(['123'])
63+
;
5364

5465
// when
5566
$this->tagCollector->collect(['iri' => '/dummy/123', 'object' => $object]);
@@ -61,7 +72,11 @@ public function testWithRelation() {
6172
$object->setId('123');
6273

6374
// then
64-
$this->responseTaggerProphecy->addTags(['123#propertyName'])->shouldBeCalled();
75+
$this->responseTagger
76+
->expects(once())
77+
->method('addTags')
78+
->with(['123#propertyName'])
79+
;
6580

6681
// when
6782
$this->tagCollector->collect([
@@ -78,8 +93,19 @@ public function testWithExtraCacheDependency() {
7893
$object->setId('123');
7994

8095
// then
81-
$this->responseTaggerProphecy->addTags(['123#PROPERTY_NAME'])->shouldBeCalled();
82-
$this->responseTaggerProphecy->addTags(['123#OTHER_DEPENDENCY'])->shouldBeCalled();
96+
$seen = [];
97+
$this->responseTagger
98+
->expects(exactly(2))
99+
->method('addTags')
100+
->willReturnCallback(function ($tags) use (&$seen) {
101+
$valid = $tags === ['123#PROPERTY_NAME'] || $tags === ['123#OTHER_DEPENDENCY'];
102+
if ($valid) {
103+
$seen[] = $tags[0];
104+
}
105+
106+
return $valid;
107+
})
108+
;
83109

84110
// when
85111
$this->tagCollector->collect([
@@ -92,11 +118,17 @@ public function testWithExtraCacheDependency() {
92118
),
93119
'api_attribute' => 'propertyName',
94120
]);
121+
122+
$this->assertContains('123#PROPERTY_NAME', $seen);
123+
$this->assertContains('123#OTHER_DEPENDENCY', $seen);
95124
}
96125

97126
public function testNoTagForHalLinks() {
98127
// then
99-
$this->responseTaggerProphecy->addTags(Argument::any())->shouldNotBeCalled();
128+
$this->responseTagger
129+
->expects(never())
130+
->method('addTags')
131+
;
100132

101133
// when
102134
$this->tagCollector->collect([
@@ -108,7 +140,10 @@ public function testNoTagForHalLinks() {
108140

109141
public function testNoTagForJsonLdLinks() {
110142
// then
111-
$this->responseTaggerProphecy->addTags(Argument::any())->shouldNotBeCalled();
143+
$this->responseTagger
144+
->expects(never())
145+
->method('addTags')
146+
;
112147

113148
// when
114149
$this->tagCollector->collect([
@@ -120,7 +155,10 @@ public function testNoTagForJsonLdLinks() {
120155

121156
public function testNoTagForJsonApiLinks() {
122157
// then
123-
$this->responseTaggerProphecy->addTags(Argument::any())->shouldNotBeCalled();
158+
$this->responseTagger
159+
->expects(never())
160+
->method('addTags')
161+
;
124162

125163
// when
126164
$this->tagCollector->collect([

0 commit comments

Comments
 (0)