Skip to content

Commit

Permalink
[persistence] add ability to remove ResetPasswordRequest objects prog…
Browse files Browse the repository at this point in the history
…rammatically
  • Loading branch information
jrushlow committed Mar 1, 2024
1 parent df64d82 commit 1ec478f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Persistence/Fake/FakeResetPasswordInternalRepository.php
Expand Up @@ -60,4 +60,9 @@ public function removeExpiredResetPasswordRequests(): int
{
throw new FakeRepositoryException();
}

public function removeRequests(?object $user = null): void
{
throw new FakeRepositoryException();
}
}
28 changes: 28 additions & 0 deletions src/Persistence/Repository/ResetPasswordRequestRepositoryTrait.php
Expand Up @@ -82,4 +82,32 @@ public function removeExpiredResetPasswordRequests(): int

return $query->execute();
}

/**
* Remove ResetPasswordRequest objects from persistence.
*
* Warning - This is a destructive operation. Calling this method
* may have undesired consequences for users who have valid
* ResetPasswordRequests but have not "checked their email" yet.
*
* @TODO _ @legacy This method is useful for something.....
*
* If a "user" object is passed, only the requests for that user
* will be removed.
*/
public function removeRequests(?object $user = null): void
{
$query = $this->createQueryBuilder('t')
->delete()
;

if (null !== $user) {
$query
->where('t.user = :user')
->setParameter('user', $user)
;
}

$query->getQuery()->execute();
}
}
2 changes: 2 additions & 0 deletions src/Persistence/ResetPasswordRequestRepositoryInterface.php
Expand Up @@ -14,6 +14,8 @@
/**
* @author Jesse Rushlow <jr@rushlow.dev>
* @author Ryan Weaver <ryan@symfonycasts.com>
*
* @method void removeRequests(?object $user = null) Remove ResetPasswordRequest objects from persistence.
*/
interface ResetPasswordRequestRepositoryInterface
{
Expand Down
Expand Up @@ -208,6 +208,45 @@ public function testRemovedExpiredResetPasswordRequestsOnlyRemovedExpiredRequest
self::assertSame($futureFixture, $result[0]);
}

public function testRemoveRequestsRemovesAllRequestsFromPersistence(): void
{
$this->manager->persist(new ResetPasswordTestFixtureRequest());
$this->manager->persist(new ResetPasswordTestFixtureRequest());

$this->manager->flush();

$this->repository->removeRequests();

self::assertCount(0, $this->repository->findAll());
}

public function testRemoveRequestsRemovesAllRequestsForASingleUser(): void
{
$this->manager->persist($userFixture = new ResetPasswordTestFixtureUser());
$requestFixtures = [new ResetPasswordTestFixtureRequest(), new ResetPasswordTestFixtureRequest()];

foreach ($requestFixtures as $fixture) {
$fixture->user = $userFixture;

$this->manager->persist($fixture);
}

$this->manager->persist($differentUserFixture = new ResetPasswordTestFixtureUser());

$existingRequestFixture = new ResetPasswordTestFixtureRequest();
$existingRequestFixture->user = $differentUserFixture;

$this->manager->persist($existingRequestFixture);
$this->manager->flush();

self::assertCount(3, $this->repository->findAll());

$this->repository->removeRequests($userFixture);

self::assertCount(1, $result = $this->repository->findAll());
self::assertSame($existingRequestFixture, $result[0]);
}

private function configureDatabase(): void
{
$metaData = $this->manager->getMetadataFactory();
Expand Down
Expand Up @@ -28,6 +28,7 @@ public function methodDataProvider(): \Generator
yield ['findResetPasswordRequest', ['']];
yield ['getMostRecentNonExpiredRequestDate', [new \stdClass()]];
yield ['removeResetPasswordRequest', [$this->createMock(ResetPasswordRequestInterface::class)]];
yield ['removeRequests', []];
}

/**
Expand Down

0 comments on commit 1ec478f

Please sign in to comment.