Skip to content
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

test: add controller tests #41

Merged
merged 9 commits into from
Feb 18, 2024
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
8 changes: 3 additions & 5 deletions .github/workflows/dusk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ jobs:
run: php artisan route:cache

- name: Upgrade Chrome Driver
run: php artisan dusk:chrome-driver --detect
- name: Start Chrome Driver
run: |
php artisan dusk:chrome-driver --detect
chmod -R 0755 vendor/laravel/dusk/bin/
./vendor/laravel/dusk/bin/chromedriver-linux &
- name: Run Laravel Server
run: php artisan serve --no-reload &

Expand Down Expand Up @@ -125,13 +123,13 @@ jobs:

- name: Upload Screenshots
if: failure()
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: screenshots
path: tests/Browser/screenshots
- name: Upload Console Logs
if: failure()
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: console
path: tests/Browser/console
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
.env
.env.backup
.env.production
.env.dusk.*
.phpunit.result.cache
.php_cs.cache
.phpstorm.meta.php
Expand Down
4 changes: 2 additions & 2 deletions app/Helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class StringHelper
{
public static function sanitizeNameForURL(string $name): string
public static function sanitizeNameForURL(?string $name): ?string
{
$string = str_replace(['[\', \']'], '', $name);
$string = preg_replace('/\[.*\]/U', '', $string);
Expand All @@ -25,7 +25,7 @@ public static function sanitizeNameForURL(string $name): string
* - ucfirst it
* so it becomes "Jean-Jacques"
*/
public static function formatNameFromDB(string $name): string
public static function formatNameFromDB(?string $name): ?string
{
$formattedName = Str::ucfirst(Str::lower($name));

Expand Down
12 changes: 4 additions & 8 deletions app/Http/Controllers/FemaleNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ class FemaleNameController extends Controller
public function index(Request $request): View
{
// get the page parameter from the url
$requestedPage = $request->query('page') ?? 1;
$requestedPage = Paginator::resolveCurrentPage();

$letters = Cache::remember('all-letters-female', 604800, fn () => FemaleNamesViewModel::index());

Paginator::currentPageResolver(fn () => $requestedPage);

$namesPagination = Cache::remember('all-names-female-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('gender', 'female')
->orderBy('total', 'desc')
Expand All @@ -47,15 +45,13 @@ public function index(Request $request): View
public function letter(Request $request): View
{
$requestedLetter = $request->attributes->get('letter');
$requestedPage = $request->query('page') ?? 1;
$requestedPage = Paginator::resolveCurrentPage();

$letters = Cache::remember('all-letters-female', 604800, fn () => FemaleNamesViewModel::index());

Paginator::currentPageResolver(fn () => $requestedPage);

$namesPagination = Cache::remember('female-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('gender', 'female')
->where('name', 'like', $requestedLetter . '%')
->where('name', 'like', Str::upper($requestedLetter) . '%')
->orderBy('total', 'desc')
->paginate(40)
);
Expand All @@ -71,7 +67,7 @@ public function letter(Request $request): View
'letters' => $letters,
'names' => $names,
'namesPagination' => $namesPagination,
'activeLetter' => Str::ucfirst($requestedLetter),
'activeLetter' => Str::upper($requestedLetter),
'favorites' => $favoritedNamesForLoggedUser,
]);
}
Expand Down
19 changes: 0 additions & 19 deletions app/Http/Controllers/LocaleController.php

This file was deleted.

12 changes: 4 additions & 8 deletions app/Http/Controllers/MaleNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ class MaleNameController extends Controller
public function index(Request $request): View
{
// get the page parameter from the url
$requestedPage = $request->query('page') ?? 1;
$requestedPage = Paginator::resolveCurrentPage();

$letters = Cache::remember('all-letters-male', 604800, fn () => MaleNamesViewModel::index());

Paginator::currentPageResolver(fn () => $requestedPage);

$namesPagination = Cache::remember('all-names-male-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('gender', 'male')
->orderBy('total', 'desc')
Expand All @@ -47,15 +45,13 @@ public function index(Request $request): View
public function letter(Request $request): View
{
$requestedLetter = $request->attributes->get('letter');
$requestedPage = $request->query('page') ?? 1;
$requestedPage = Paginator::resolveCurrentPage();

$letters = Cache::remember('all-letters-male', 604800, fn () => MaleNamesViewModel::index());

Paginator::currentPageResolver(fn () => $requestedPage);

$namesPagination = Cache::remember('male-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('gender', 'male')
->where('name', 'like', $requestedLetter . '%')
->where('name', 'like', Str::upper($requestedLetter) . '%')
->orderBy('total', 'desc')
->paginate(40)
);
Expand All @@ -71,7 +67,7 @@ public function letter(Request $request): View
'letters' => $letters,
'names' => $names,
'namesPagination' => $namesPagination,
'activeLetter' => Str::ucfirst($requestedLetter),
'activeLetter' => Str::upper($requestedLetter),
'favorites' => $favoritedNamesForLoggedUser,
]);
}
Expand Down
12 changes: 4 additions & 8 deletions app/Http/Controllers/MixteNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ class MixteNameController extends Controller
public function index(Request $request): View
{
// get the page parameter from the url
$requestedPage = $request->query('page') ?? 1;
$requestedPage = Paginator::resolveCurrentPage();

$letters = Cache::remember('all-letters-mixte', 604800, fn () => MixteNamesViewModel::index());

Paginator::currentPageResolver(fn () => $requestedPage);

$namesPagination = Cache::remember('all-names-mixte-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('unisex', 1)
->orderBy('total', 'desc')
Expand All @@ -47,15 +45,13 @@ public function index(Request $request): View
public function letter(Request $request): View
{
$requestedLetter = $request->attributes->get('letter');
$requestedPage = $request->query('page') ?? 1;
$requestedPage = Paginator::resolveCurrentPage();

$letters = Cache::remember('all-letters-mixte', 604800, fn () => MixteNamesViewModel::index());

Paginator::currentPageResolver(fn () => $requestedPage);

$namesPagination = Cache::remember('mixte-letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('unisex', 1)
->where('name', 'like', $requestedLetter . '%')
->where('name', 'like', Str::upper($requestedLetter) . '%')
->orderBy('total', 'desc')
->paginate(40)
);
Expand All @@ -71,7 +67,7 @@ public function letter(Request $request): View
'letters' => $letters,
'names' => $names,
'namesPagination' => $namesPagination,
'activeLetter' => Str::ucfirst($requestedLetter),
'activeLetter' => Str::upper($requestedLetter),
'favorites' => $favoritedNamesForLoggedUser,
]);
}
Expand Down
12 changes: 4 additions & 8 deletions app/Http/Controllers/NameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ class NameController extends Controller
public function index(Request $request): View
{
// get the page parameter from the url
$requestedPage = $request->query('page') ?? 1;
$requestedPage = Paginator::resolveCurrentPage();

$letters = Cache::remember('all-letters', 604800, fn () => AllNamesViewModel::index());

Paginator::currentPageResolver(fn () => $requestedPage);

$namesPagination = Cache::remember('all-names-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->orderBy('total', 'desc')
->paginate(40)
Expand Down Expand Up @@ -90,14 +88,12 @@ public function show(Request $request): View
public function letter(Request $request): View
{
$requestedLetter = $request->attributes->get('letter');
$requestedPage = $request->query('page') ?? 1;
$requestedPage = Paginator::resolveCurrentPage();

$letters = Cache::remember('all-letters', 604800, fn () => AllNamesViewModel::index());

Paginator::currentPageResolver(fn () => $requestedPage);

$namesPagination = Cache::remember('letter-' . $requestedLetter . '-page-' . $requestedPage, 604800, fn () => Name::where('name', '!=', '_PRENOMS_RARES')
->where('name', 'like', $requestedLetter . '%')
->where('name', 'like', Str::upper($requestedLetter) . '%')
->orderBy('total', 'desc')
->paginate(40)
);
Expand All @@ -113,7 +109,7 @@ public function letter(Request $request): View
'letters' => $letters,
'names' => $names,
'namesPagination' => $namesPagination,
'activeLetter' => Str::ucfirst($requestedLetter),
'activeLetter' => Str::upper($requestedLetter),
'favorites' => $favoritedNamesForLoggedUser,
]);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/CheckLetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function handle(Request $request, Closure $next): Response
return redirect()->route('home.index');
}

$request->attributes->add(['letter' => Str::lcfirst($requestedLetter)]);
$request->attributes->add(['letter' => Str::lower($requestedLetter)]);

return $next($request);
}
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Middleware/CheckList.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public function handle(Request $request, Closure $next): Response
$requestedId = $request->route()->parameter('liste');

try {
$list = Cache::remember('route-list-' . $requestedId, 604800, function () use ($requestedId) {
return NameList::findOrFail($requestedId);
});
$list = Cache::remember('route-list-' . $requestedId, 604800, fn () => NameList::findOrFail($requestedId));

if (! auth()->check() && ! $list->is_public) {
return redirect()->route('home.index');
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Middleware/CheckName.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public function handle(Request $request, Closure $next): Response
$requestedId = $request->route()->parameter('id');

try {
$name = Cache::remember('route-name-' . $requestedId, 604800, function () use ($requestedId) {
return Name::findOrFail($requestedId);
});
$name = Cache::remember('route-name-' . $requestedId, 604800, fn () => Name::findOrFail($requestedId));

$request->attributes->add(['name' => $name]);

Expand Down
12 changes: 6 additions & 6 deletions app/Http/ViewModels/Home/HomeViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ public static function nameSpotlight(): array
->inRandomOrder()
->first();

return Name::whereIn('id', $id)
return Name::whereIn('id', $id ?? [])
->select('id', 'name', 'origins')
->first();
});

return [
'id' => $name->id,
'name' => StringHelper::formatNameFromDB($name->name),
'origins' => Str::words($name->origins, 50, '...'),
'url' => route('name.show', [
'id' => optional($name)->id,
'name' => StringHelper::formatNameFromDB(optional($name)->name),
'origins' => Str::words(optional($name)->origins, 50, '...'),
'url' => $name ? route('name.show', [
'id' => $name->id,
'name' => StringHelper::sanitizeNameForURL($name->name),
]),
]) : '',
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/ViewModels/Names/AllNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function index(): Collection
$letters->push([
'letter' => $letter,
'count' => Number::format($count),
'url' => route('name.letter', ['letter' => Str::lcfirst($letter)]),
'url' => route('name.letter', ['letter' => Str::lower($letter)]),
]);
}

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"require-dev": {
"amirami/localizator": "^0.12.1@alpha",
"barryvdh/laravel-debugbar": "^3.9",
"brianium/paratest": "^7.4",
"fakerphp/faker": "^1.9",
"larastan/larastan": "^2.7",
"laravel/breeze": "^1.26",
Expand Down
Loading
Loading