From 5ba17d6c6953bdb596d20fd1916fe0daf83c78f8 Mon Sep 17 00:00:00 2001 From: David Badura Date: Fri, 6 Nov 2026 17:06:21 +0100 Subject: [PATCH] Handle string keyed arguments in remove() remove() had the same problem as insert() and update(): called with named arguments, $id[0] does not exist and nothing gets deleted. The new test covers all three methods with string keyed arguments. --- src/Repository/MongoDBRepository.php | 2 ++ src/Repository/RangoRepository.php | 2 ++ tests/Integration/RepositoryTestCase.php | 28 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/Repository/MongoDBRepository.php b/src/Repository/MongoDBRepository.php index 0ae81d7..fb8de09 100644 --- a/src/Repository/MongoDBRepository.php +++ b/src/Repository/MongoDBRepository.php @@ -191,6 +191,8 @@ public function find(string $id): object|null public function remove(string ...$id): void { + $id = array_values($id); + if (count($id) === 1) { $this->collection->deleteOne($this->withDiscriminator(['_id' => $id[0]])); diff --git a/src/Repository/RangoRepository.php b/src/Repository/RangoRepository.php index 59279a6..e9a1c8e 100644 --- a/src/Repository/RangoRepository.php +++ b/src/Repository/RangoRepository.php @@ -194,6 +194,8 @@ public function find(string $id): object|null public function remove(string ...$id): void { + $id = array_values($id); + if (count($id) === 1) { $this->collection->deleteOne($this->withDiscriminator(['_id' => $id[0]])); diff --git a/tests/Integration/RepositoryTestCase.php b/tests/Integration/RepositoryTestCase.php index 248e9d4..760859b 100644 --- a/tests/Integration/RepositoryTestCase.php +++ b/tests/Integration/RepositoryTestCase.php @@ -427,6 +427,34 @@ public function testRemoveMany(): void self::assertSame(1, $repository->count()); } + public function testWriteOperationsAcceptStringKeyedArguments(): void + { + $repository = $this->repositoryManager->get(Profile::class); + + $repository->insert(...['first' => new Profile('r-1', 'Rango', Status::ACTIVE, [new Skill('php')])]); + $repository->insert(...[ + 'second' => new Profile('r-2', 'Beans', Status::INACTIVE, [new Skill('js')]), + 'third' => new Profile('r-3', 'Elsa', Status::ACTIVE, [new Skill('go')]), + ]); + + self::assertSame(3, $repository->count()); + + $repository->update(...['first' => new Profile('r-1', 'Rango Updated', Status::ACTIVE, [new Skill('php')])]); + $repository->update(...[ + 'second' => new Profile('r-2', 'Beans Updated', Status::INACTIVE, [new Skill('js')]), + 'third' => new Profile('r-3', 'Elsa Updated', Status::ACTIVE, [new Skill('go')]), + ]); + + self::assertSame('Rango Updated', $repository->get('r-1')->name); + self::assertSame('Beans Updated', $repository->get('r-2')->name); + self::assertSame('Elsa Updated', $repository->get('r-3')->name); + + $repository->remove(...['first' => 'r-1']); + $repository->remove(...['second' => 'r-2', 'third' => 'r-3']); + + self::assertSame(0, $repository->count()); + } + public function testDropCollection(): void { $repository = $this->repositoryManager->get(Profile::class);