Skip to content

Commit d66915c

Browse files
committed
CampRoleVoterTest: fix notice about using stubs instead of mocks
* No expectations were configured for the mock object for xy. Consider refactoring your test code to use a test stub instead. The #[AllowMockObjectsWithoutExpectations] attribute can be used to opt out of this check. We only have one test which wants to test the side effect of the voter, so we use a mock instead of a stub for this test.
1 parent 0f0f6dc commit d66915c

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

api/tests/Security/Voter/CampRoleVoterTest.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class CampRoleVoterTest extends TestCase {
3030

3131
public function setUp(): void {
3232
parent::setUp();
33-
$this->token = $this->createMock(TokenInterface::class);
34-
$this->em = $this->createMock(EntityManagerInterface::class);
35-
$this->responseTagger = $this->createMock(ResponseTagger::class);
33+
$this->token = $this->createStub(TokenInterface::class);
34+
$this->em = $this->createStub(EntityManagerInterface::class);
35+
$this->responseTagger = $this->createStub(ResponseTagger::class);
3636
$this->voter = new CampRoleVoter($this->em, $this->responseTagger);
3737
}
3838

@@ -80,7 +80,7 @@ public function testDeniesAccessWhenNotLoggedIn() {
8080
public function testDeniesAccessWhenGetCampYieldsNull() {
8181
// given
8282
$this->token->method('getUser')->willReturn(new User());
83-
$subject = $this->createMock(Period::class);
83+
$subject = $this->createStub(Period::class);
8484
$subject->method('getCamp')->willReturn(null);
8585

8686
// when
@@ -93,7 +93,7 @@ public function testDeniesAccessWhenGetCampYieldsNull() {
9393
public function testDeniesAccessWhenGetCampYieldsNullAndNotLoggedIn() {
9494
// given
9595
$this->token->method('getUser')->willReturn(null);
96-
$subject = $this->createMock(Period::class);
96+
$subject = $this->createStub(Period::class);
9797
$subject->method('getCamp')->willReturn(null);
9898

9999
// when
@@ -105,11 +105,11 @@ public function testDeniesAccessWhenGetCampYieldsNullAndNotLoggedIn() {
105105

106106
public function testDeniesAccessWhenNoCampCollaborations() {
107107
// given
108-
$user = $this->createMock(User::class);
108+
$user = $this->createStub(User::class);
109109
$user->method('getId')->willReturn('idFromTest');
110110
$this->token->method('getUser')->willReturn($user);
111111
$camp = new Camp();
112-
$subject = $this->createMock(Period::class);
112+
$subject = $this->createStub(Period::class);
113113
$subject->method('getCamp')->willReturn($camp);
114114

115115
// when
@@ -121,9 +121,9 @@ public function testDeniesAccessWhenNoCampCollaborations() {
121121

122122
public function testDeniesAccessWhenNoMatchingCampCollaboration() {
123123
// given
124-
$user = $this->createMock(User::class);
124+
$user = $this->createStub(User::class);
125125
$user->method('getId')->willReturn('idFromTest');
126-
$user2 = $this->createMock(User::class);
126+
$user2 = $this->createStub(User::class);
127127
$user2->method('getId')->willReturn('otherIdFromTest');
128128
$this->token->method('getUser')->willReturn($user);
129129
$collaboration = new CampCollaboration();
@@ -132,7 +132,7 @@ public function testDeniesAccessWhenNoMatchingCampCollaboration() {
132132
$collaboration->role = CampCollaboration::ROLE_MANAGER;
133133
$camp = new Camp();
134134
$camp->collaborations->add($collaboration);
135-
$subject = $this->createMock(Period::class);
135+
$subject = $this->createStub(Period::class);
136136
$subject->method('getCamp')->willReturn($camp);
137137

138138
// when
@@ -144,9 +144,9 @@ public function testDeniesAccessWhenNoMatchingCampCollaboration() {
144144

145145
public function testDeniesAccessWhenMatchingCampCollaborationIsInvitation() {
146146
// given
147-
$user = $this->createMock(User::class);
147+
$user = $this->createStub(User::class);
148148
$user->method('getId')->willReturn('idFromTest');
149-
$user2 = $this->createMock(User::class);
149+
$user2 = $this->createStub(User::class);
150150
$user2->method('getId')->willReturn('idFromTest');
151151
$this->token->method('getUser')->willReturn($user);
152152
$collaboration = new CampCollaboration();
@@ -155,7 +155,7 @@ public function testDeniesAccessWhenMatchingCampCollaborationIsInvitation() {
155155
$collaboration->role = CampCollaboration::ROLE_MANAGER;
156156
$camp = new Camp();
157157
$camp->collaborations->add($collaboration);
158-
$subject = $this->createMock(Period::class);
158+
$subject = $this->createStub(Period::class);
159159
$subject->method('getCamp')->willReturn($camp);
160160

161161
// when
@@ -167,9 +167,9 @@ public function testDeniesAccessWhenMatchingCampCollaborationIsInvitation() {
167167

168168
public function testDeniesAccessWhenMatchingCampCollaborationIsInactive() {
169169
// given
170-
$user = $this->createMock(User::class);
170+
$user = $this->createStub(User::class);
171171
$user->method('getId')->willReturn('idFromTest');
172-
$user2 = $this->createMock(User::class);
172+
$user2 = $this->createStub(User::class);
173173
$user2->method('getId')->willReturn('idFromTest');
174174
$this->token->method('getUser')->willReturn($user);
175175
$collaboration = new CampCollaboration();
@@ -178,7 +178,7 @@ public function testDeniesAccessWhenMatchingCampCollaborationIsInactive() {
178178
$collaboration->role = CampCollaboration::ROLE_MANAGER;
179179
$camp = new Camp();
180180
$camp->collaborations->add($collaboration);
181-
$subject = $this->createMock(Period::class);
181+
$subject = $this->createStub(Period::class);
182182
$subject->method('getCamp')->willReturn($camp);
183183

184184
// when
@@ -190,9 +190,9 @@ public function testDeniesAccessWhenMatchingCampCollaborationIsInactive() {
190190

191191
public function testDeniesAccessWhenRolesDontMatch() {
192192
// given
193-
$user = $this->createMock(User::class);
193+
$user = $this->createStub(User::class);
194194
$user->method('getId')->willReturn('idFromTest');
195-
$user2 = $this->createMock(User::class);
195+
$user2 = $this->createStub(User::class);
196196
$user2->method('getId')->willReturn('idFromTest');
197197
$this->token->method('getUser')->willReturn($user);
198198
$collaboration = new CampCollaboration();
@@ -201,7 +201,7 @@ public function testDeniesAccessWhenRolesDontMatch() {
201201
$collaboration->role = CampCollaboration::ROLE_GUEST;
202202
$camp = new Camp();
203203
$camp->collaborations->add($collaboration);
204-
$subject = $this->createMock(Period::class);
204+
$subject = $this->createStub(Period::class);
205205
$subject->method('getCamp')->willReturn($camp);
206206

207207
// when
@@ -213,9 +213,9 @@ public function testDeniesAccessWhenRolesDontMatch() {
213213

214214
public function testGrantsAccessViaBelongsToCampInterface() {
215215
// given
216-
$user = $this->createMock(User::class);
216+
$user = $this->createStub(User::class);
217217
$user->method('getId')->willReturn('idFromTest');
218-
$user2 = $this->createMock(User::class);
218+
$user2 = $this->createStub(User::class);
219219
$user2->method('getId')->willReturn('idFromTest');
220220
$this->token->method('getUser')->willReturn($user);
221221
$collaboration = new CampCollaboration();
@@ -224,10 +224,12 @@ public function testGrantsAccessViaBelongsToCampInterface() {
224224
$collaboration->role = CampCollaboration::ROLE_MANAGER;
225225
$camp = new Camp();
226226
$camp->collaborations->add($collaboration);
227-
$subject = $this->createMock(Period::class);
227+
$subject = $this->createStub(Period::class);
228228
$subject->method('getCamp')->willReturn($camp);
229229

230+
$this->responseTagger = $this->createMock(ResponseTagger::class);
230231
$this->responseTagger->expects($this->once())->method('addTags')->with([$collaboration->getId()]);
232+
$this->voter = new CampRoleVoter($this->em, $this->responseTagger);
231233

232234
// when
233235
$result = $this->voter->vote($this->token, $subject, ['CAMP_COLLABORATOR']);
@@ -238,9 +240,9 @@ public function testGrantsAccessViaBelongsToCampInterface() {
238240

239241
public function testGrantsAccessViaBelongsToContentNodeTreeInterface() {
240242
// given
241-
$user = $this->createMock(User::class);
243+
$user = $this->createStub(User::class);
242244
$user->method('getId')->willReturn('idFromTest');
243-
$user2 = $this->createMock(User::class);
245+
$user2 = $this->createStub(User::class);
244246
$user2->method('getId')->willReturn('idFromTest');
245247
$this->token->method('getUser')->willReturn($user);
246248
$collaboration = new CampCollaboration();
@@ -249,12 +251,12 @@ public function testGrantsAccessViaBelongsToContentNodeTreeInterface() {
249251
$collaboration->role = CampCollaboration::ROLE_MANAGER;
250252
$camp = new Camp();
251253
$camp->collaborations->add($collaboration);
252-
$activity = $this->createMock(Activity::class);
254+
$activity = $this->createStub(Activity::class);
253255
$activity->method('getCamp')->willReturn($camp);
254-
$root = $this->createMock(ColumnLayout::class);
255-
$subject = $this->createMock(ContentNodeTreeDummy2::class);
256+
$root = $this->createStub(ColumnLayout::class);
257+
$subject = $this->createStub(ContentNodeTreeDummy2::class);
256258
$subject->method('getRoot')->willReturn($root);
257-
$repository = $this->createMock(EntityRepository::class);
259+
$repository = $this->createStub(EntityRepository::class);
258260
$this->em->method('getRepository')->willReturn($repository);
259261
$repository->method('findOneBy')->willReturn($activity);
260262

0 commit comments

Comments
 (0)