Skip to content

Commit

Permalink
feature: add a repository method that can delete a user
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimorillo committed Mar 16, 2024
1 parent 33c34d4 commit ae8b7dc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/User/Domain/ValueObject/UserRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public function findById(UserId $userId): User;

public function findByEmail(Email $email): User;
public function exists(Email $email, Password $password): bool;

public function delete(UserId $userId): void;
}
5 changes: 5 additions & 0 deletions src/User/Infrastructure/Repository/UserRepositoryInMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public function exists(Email $email, Password $password): bool
});
return count($filteredArray) > 0;
}

public function delete(UserId $userId): void
{
unset($this->users[$userId->toString()]);
}
}
8 changes: 8 additions & 0 deletions src/User/Infrastructure/Repository/UserRepositoryInMySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,12 @@ public function exists(Email $email, Password $password): bool
}
return true;
}

public function delete(UserId $userId): void
{
$stmt = $this->mysqli->prepare("DELETE FROM users WHERE user_id=?");
$userIdString = $userId->toString();
$stmt->bind_param('s', $userIdString);
$stmt->execute();
}
}
10 changes: 10 additions & 0 deletions tests/User/Infrastructure/Repository/UserRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ public function testCanCheckIfAUserExists(UserRepositoryInterface $userRepositor
$exists = $userRepository->exists($user->getEmail(), $user->getPassword());
self::assertTrue($exists);
}

/** @dataProvider dataProvider() */
public function testCanDeleteAUser(UserRepositoryInterface $userRepository): void
{
$user = Users::aUser();
$userRepository->save($user);
$userRepository->delete($user->getId());
$this->expectException(UserNotFoundException::class);
$userRepository->findById($user->getId());
}
}

0 comments on commit ae8b7dc

Please sign in to comment.