Skip to content

feat: Add pagination #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
* TOTP authentication
* Add mutation testing
* Reset password
* Pagination
* Bump to PHP 8.4
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
"php": ">=8.3",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^4.5",
"doctrine/dbal": "^3.9.3",
"doctrine/doctrine-bundle": "^2.13.1",
"doctrine/doctrine-migrations-bundle": "^3.3.1",
"doctrine/orm": "^3.3.1",
"pagerfanta/doctrine-orm-adapter": "^4.7",
"pagerfanta/twig": "^4.7",
"scheb/2fa-bundle": "^7.6",
"scheb/2fa-email": "^7.6",
"symfony/asset": "7.2.*",
Expand Down
223 changes: 222 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
Zenstruck\Messenger\Test\ZenstruckMessengerTestBundle::class => ['test' => true],
Scheb\TwoFactorBundle\SchebTwoFactorBundle::class => ['all' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
BabDev\PagerfantaBundle\BabDevPagerfantaBundle::class => ['all' => true],
];
8 changes: 5 additions & 3 deletions src/Controller/Admin/User/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ final class IndexController extends AbstractController
{
public function __construct(private readonly UserRepository $userRepository) {}

#[Route('/admin/user', name: 'app_user_index', methods: ['GET'])]
public function __invoke(): Response
#[Route('/admin/user/{page<\d+>?1}', name: 'app_user_index', methods: ['GET'])]
public function __invoke(int $page): Response
{
$users = $this->userRepository->findBySearch($page);

return $this->render('admin/user/index.html.twig', [
'users' => $this->userRepository->findAll(),
'users' => $users,
]);
}
}
2 changes: 2 additions & 0 deletions src/DataFixtures/UserFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public function load(ObjectManager $manager): void
'password' => 'admin',
'roles' => ['ROLE_ADMIN'],
]);

UserFactory::createMany(20);
}
}
17 changes: 17 additions & 0 deletions src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Pagerfanta\Doctrine\ORM\QueryAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
Expand Down Expand Up @@ -34,4 +36,19 @@ public function upgradePassword(PasswordAuthenticatedUserInterface $user, string
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
}

/**
* @return Pagerfanta<User>
*/
public function findBySearch(int $page = 1, int $maxPerPage = 10): Pagerfanta
{
$query = $this
->createQueryBuilder('user')
->orderBy('user.id')
->getQuery();

return (new Pagerfanta(new QueryAdapter($query)))
->setMaxPerPage($maxPerPage)
->setCurrentPage($page);
}
}
3 changes: 3 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"babdev/pagerfanta-bundle": {
"version": "v4.5.0"
},
"doctrine/doctrine-bundle": {
"version": "2.13",
"recipe": {
Expand Down
2 changes: 2 additions & 0 deletions templates/admin/user/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@
</tbody>
</table>

{{ pagerfanta(users) }}

{% endblock %}
31 changes: 31 additions & 0 deletions tests/Repository/UserRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace App\Tests\Repository;

use App\Entity\User;
use App\Factory\UserFactory;
use App\Repository\UserRepository;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

Expand All @@ -35,4 +38,32 @@ public function testShouldUpgradePassword(): void

self::assertSame($newPassword, $user->getPassword());
}

public function testShouldThrowExceptionWhenUserIsNotSupported(): void
{
$user = new class implements PasswordAuthenticatedUserInterface {
public function getPassword(): ?string
{
return null;
}
};

$repository = self::getContainer()->get(UserRepository::class);
\assert($repository instanceof UserRepository);

$this->expectException(UnsupportedUserException::class);

$repository->upgradePassword($user, 'newPassword');
}

public function testShouldFindUsers(): void
{
UserFactory::createMany(6);

$repository = self::getContainer()->get(UserRepository::class);
\assert($repository instanceof UserRepository);

self::assertCount(5, $repository->findBySearch(maxPerPage: 5)->getCurrentPageResults());
self::assertCount(1, $repository->findBySearch(page: 2, maxPerPage: 5)->getCurrentPageResults());
}
}
Loading