Skip to content

Commit 314aad1

Browse files
committed
fix bug in fetchIdentities when using first()
1 parent 692878c commit 314aad1

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Models/UserModel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ protected function fetchIdentities(array $data): array
142142

143143
$mappedUsers = $this->assignIdentities($data, $identities);
144144

145+
if ($data['method'] === 'first') {
146+
$data['id'] = $data['data']->id;
147+
}
148+
145149
$data['data'] = $data['singleton'] ? $mappedUsers[$data['id']] : $mappedUsers;
146150

147151
return $data;

tests/Unit/UserModelTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,49 @@ public function testSaveArrayNoDataToUpdate(): void
278278

279279
$users->save(['id' => $user->id]);
280280
}
281+
282+
public function testGetFirstIdentity(): void
283+
{
284+
$errors = [];
285+
$users = $this->createUserModel();
286+
$user = $this->createNewUser();
287+
$users->save($user);
288+
289+
// Custom error handler
290+
set_error_handler(static function ($severity, $message, $file, $line) use (&$errors) {
291+
$errors[] = [
292+
'message' => $message,
293+
'severity' => $severity,
294+
'file' => $file,
295+
'line' => $line,
296+
];
297+
298+
// Return true so that the default handler is not executed
299+
return true;
300+
});
301+
302+
try {
303+
$user = $users->withIdentities()->first();
304+
$this->assertSame(
305+
$user->getIdentity('all')->secret,
306+
'foo@bar.com',
307+
'Verify first() method retrieves user with identity',
308+
);
309+
} finally {
310+
// restore handler
311+
restore_error_handler();
312+
}
313+
314+
if ($errors !== []) {
315+
$errorMessages = array_map(static fn ($error) => sprintf(
316+
'[%s] %s in %s:%s',
317+
$error['severity'],
318+
$error['message'],
319+
$error['file'],
320+
$error['line'],
321+
), $errors);
322+
323+
$this->fail("Errors found:\n" . implode("\n", $errorMessages));
324+
}
325+
}
281326
}

0 commit comments

Comments
 (0)