Skip to content

Commit 418c780

Browse files
committed
test: Fix mock related notices in Ldap-backed service tests
1 parent ae99e32 commit 418c780

2 files changed

Lines changed: 61 additions & 72 deletions

File tree

test/Service/LdapGroupServiceTest.php

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,15 @@ protected function setUp(): void
4444
$this->ldapService = $this->createStub(HordeLdapService::class);
4545
}
4646

47-
/**
48-
* Create a stub of Horde_Ldap_Search without calling constructor/destructor
49-
*/
50-
private function createSearchStub(): Horde_Ldap_Search
51-
{
52-
return $this->getMockBuilder(Horde_Ldap_Search::class)
53-
->disableOriginalConstructor()
54-
->disableOriginalClone()
55-
->getMock();
56-
}
57-
58-
/**
59-
* Create a stub of Horde_Ldap_Entry without calling constructor
60-
*/
61-
private function createEntryStub(): Horde_Ldap_Entry
62-
{
63-
return $this->getMockBuilder(Horde_Ldap_Entry::class)
64-
->disableOriginalConstructor()
65-
->getMock();
66-
}
67-
6847
public function testListAllGroups(): void
6948
{
7049
$ldapAdapter = $this->createMock(Horde_Ldap::class);
7150
$search = $this->createMock(Horde_Ldap_Search::class);
7251

73-
$entry1 = $this->createEntryStub();
74-
$entry1->method('getValue')->willReturnCallback(function ($attr, $mode = null) {
52+
$entry1 = $this->getMockBuilder(Horde_Ldap_Entry::class)
53+
->disableOriginalConstructor()
54+
->getMock();
55+
$entry1->expects($this->exactly(3))->method('getValue')->willReturnCallback(function ($attr, $mode = null) {
7556
if ($attr === 'cn' && $mode === 'single') {
7657
return 'developers';
7758
}
@@ -84,8 +65,10 @@ public function testListAllGroups(): void
8465
return null;
8566
});
8667

87-
$entry2 = $this->createEntryStub();
88-
$entry2->method('getValue')->willReturnCallback(function ($attr, $mode = null) {
68+
$entry2 = $this->getMockBuilder(Horde_Ldap_Entry::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$entry2->expects($this->exactly(3))->method('getValue')->willReturnCallback(function ($attr, $mode = null) {
8972
if ($attr === 'cn' && $mode === 'single') {
9073
return 'admins';
9174
}
@@ -120,8 +103,10 @@ public function testListAllGroups(): void
120103
public function testGetGroup(): void
121104
{
122105
$ldapAdapter = $this->createMock(Horde_Ldap::class);
123-
$entry = $this->createEntryStub();
124-
$entry->method('getValue')->willReturnCallback(function ($attr, $mode = null) {
106+
$entry = $this->getMockBuilder(Horde_Ldap_Entry::class)
107+
->disableOriginalConstructor()
108+
->getMock();
109+
$entry->expects($this->exactly(2))->method('getValue')->willReturnCallback(function ($attr, $mode = null) {
125110
if ($attr === 'memberUid') {
126111
return ['alice', 'bob'];
127112
}
@@ -149,9 +134,12 @@ public function testGetGroup(): void
149134
public function testCreateGroup(): void
150135
{
151136
$ldapAdapter = $this->createMock(Horde_Ldap::class);
152-
// Stub search for generating next GID
153-
$search = $this->createSearchStub();
154-
$search->method('valid')->willReturn(false); // Empty result
137+
// Mock search for generating next GID - verifies search is attempted
138+
$search = $this->getMockBuilder(Horde_Ldap_Search::class)
139+
->disableOriginalConstructor()
140+
->disableOriginalClone()
141+
->getMock();
142+
$search->expects($this->once())->method('valid')->willReturn(false); // Empty result
155143
$ldapAdapter->method('search')->willReturn($search);
156144

157145
$ldapAdapter->expects($this->once())
@@ -204,8 +192,10 @@ public function testDeleteGroup(): void
204192
public function testExistsTrue(): void
205193
{
206194
$ldapAdapter = $this->createStub(Horde_Ldap::class);
207-
$entry = $this->createEntryStub();
208-
$entry->method('getValue')->willReturnCallback(function ($attr, $mode = null) {
195+
$entry = $this->getMockBuilder(Horde_Ldap_Entry::class)
196+
->disableOriginalConstructor()
197+
->getMock();
198+
$entry->expects($this->exactly(2))->method('getValue')->willReturnCallback(function ($attr, $mode = null) {
209199
if ($attr === 'memberUid') {
210200
return [];
211201
}
@@ -291,8 +281,10 @@ public function testRemoveMember(): void
291281
public function testGetMembers(): void
292282
{
293283
$ldapAdapter = $this->createStub(Horde_Ldap::class);
294-
$entry = $this->createEntryStub();
295-
$entry->method('getValue')->willReturnCallback(function ($attr, $mode = null) {
284+
$entry = $this->getMockBuilder(Horde_Ldap_Entry::class)
285+
->disableOriginalConstructor()
286+
->getMock();
287+
$entry->expects($this->exactly(2))->method('getValue')->willReturnCallback(function ($attr, $mode = null) {
296288
if ($attr === 'memberUid') {
297289
return ['alice', 'bob'];
298290
}

test/Service/LdapPrefsServiceTest.php

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,6 @@ protected function setUp(): void
4444
$this->ldapService = $this->createStub(HordeLdapService::class);
4545
}
4646

47-
/**
48-
* Create a stub of Horde_Ldap_Search without calling constructor/destructor
49-
*/
50-
private function createSearchStub(): Horde_Ldap_Search
51-
{
52-
return $this->getMockBuilder(Horde_Ldap_Search::class)
53-
->disableOriginalConstructor()
54-
->disableOriginalClone()
55-
->getMock();
56-
}
57-
58-
/**
59-
* Create a stub of Horde_Ldap_Entry without calling constructor
60-
*/
61-
private function createEntryStub(): Horde_Ldap_Entry
62-
{
63-
return $this->getMockBuilder(Horde_Ldap_Entry::class)
64-
->disableOriginalConstructor()
65-
->getMock();
66-
}
67-
6847
public function testGetValueFromHordePerson(): void
6948
{
7049
$ldapAdapter = $this->createStub(Horde_Ldap::class);
@@ -212,18 +191,23 @@ public function testGetAllInScope(): void
212191
$ldapAdapter = $this->createStub(Horde_Ldap::class);
213192
$ldapAdapter->method('findUserDN')->willReturn('uid=alice,ou=users,dc=example,dc=com');
214193

215-
$search = $this->createSearchStub();
216-
$search->method('count')->willReturn(1);
194+
$search = $this->getMockBuilder(Horde_Ldap_Search::class)
195+
->disableOriginalConstructor()
196+
->disableOriginalClone()
197+
->getMock();
198+
$search->expects($this->once())->method('count')->willReturn(1);
217199

218-
$entry = $this->createEntryStub();
219-
$entry->method('getValues')->willReturn([
200+
$entry = $this->getMockBuilder(Horde_Ldap_Entry::class)
201+
->disableOriginalConstructor()
202+
->getMock();
203+
$entry->expects($this->once())->method('getValues')->willReturn([
220204
'cn' => ['Alice'],
221205
'hordePrefhordeTheme' => ['silver'],
222206
'hordePrefhordeLanguage' => ['en_US'],
223207
'hordePrefimpLayout' => ['wide'],
224208
]);
225209

226-
$search->method('shiftEntry')->willReturn($entry);
210+
$search->expects($this->once())->method('shiftEntry')->willReturn($entry);
227211
$ldapAdapter->method('search')->willReturn($search);
228212

229213
$this->ldapService->method('getAdapter')->willReturn($ldapAdapter);
@@ -243,15 +227,20 @@ public function testGetAllInScopeEmpty(): void
243227
$ldapAdapter = $this->createStub(Horde_Ldap::class);
244228
$ldapAdapter->method('findUserDN')->willReturn('uid=alice,ou=users,dc=example,dc=com');
245229

246-
$search = $this->createSearchStub();
247-
$search->method('count')->willReturn(1);
230+
$search = $this->getMockBuilder(Horde_Ldap_Search::class)
231+
->disableOriginalConstructor()
232+
->disableOriginalClone()
233+
->getMock();
234+
$search->expects($this->once())->method('count')->willReturn(1);
248235

249-
$entry = $this->createEntryStub();
250-
$entry->method('getValues')->willReturn([
236+
$entry = $this->getMockBuilder(Horde_Ldap_Entry::class)
237+
->disableOriginalConstructor()
238+
->getMock();
239+
$entry->expects($this->once())->method('getValues')->willReturn([
251240
'cn' => ['Alice'],
252241
]);
253242

254-
$search->method('shiftEntry')->willReturn($entry);
243+
$search->expects($this->once())->method('shiftEntry')->willReturn($entry);
255244
$ldapAdapter->method('search')->willReturn($search);
256245

257246
$this->ldapService->method('getAdapter')->willReturn($ldapAdapter);
@@ -267,12 +256,17 @@ public function testExistsTrue(): void
267256
$ldapAdapter = $this->createStub(Horde_Ldap::class);
268257
$ldapAdapter->method('findUserDN')->willReturn('uid=alice,ou=users,dc=example,dc=com');
269258

270-
$search = $this->createSearchStub();
271-
$search->method('count')->willReturn(1);
259+
$search = $this->getMockBuilder(Horde_Ldap_Search::class)
260+
->disableOriginalConstructor()
261+
->disableOriginalClone()
262+
->getMock();
263+
$search->expects($this->once())->method('count')->willReturn(1);
272264

273-
$entry = $this->createEntryStub();
274-
$entry->method('getValue')->willReturn('silver');
275-
$search->method('shiftEntry')->willReturn($entry);
265+
$entry = $this->getMockBuilder(Horde_Ldap_Entry::class)
266+
->disableOriginalConstructor()
267+
->getMock();
268+
$entry->expects($this->once())->method('getValue')->willReturn('silver');
269+
$search->expects($this->once())->method('shiftEntry')->willReturn($entry);
276270

277271
$ldapAdapter->method('search')->willReturn($search);
278272

@@ -300,8 +294,11 @@ public function testAttributeNaming(): void
300294
$ldapAdapter = $this->createStub(Horde_Ldap::class);
301295
$ldapAdapter->method('findUserDN')->willReturn('uid=alice,ou=users,dc=example,dc=com');
302296

303-
$search = $this->createSearchStub();
304-
$search->method('count')->willReturn(0);
297+
$search = $this->getMockBuilder(Horde_Ldap_Search::class)
298+
->disableOriginalConstructor()
299+
->disableOriginalClone()
300+
->getMock();
301+
$search->expects($this->once())->method('count')->willReturn(0);
305302
$ldapAdapter->method('search')->willReturn($search);
306303

307304
$entry = $this->createMock(Horde_Ldap_Entry::class);

0 commit comments

Comments
 (0)