Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Dec 19, 2023
1 parent 2162669 commit b15f345
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 26 deletions.
9 changes: 8 additions & 1 deletion app/Helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ public static function sanitizeNameForURL(string $name): string
return strtolower(trim($string, '-'));
}

public static function getProperName(string $name): string
/**
* Format the name from the db.
* The name that the files give us is like "JEAN-JACQUES" and we need to
* - lowercase it
* - ucfirst it
* so it becomes "Jean-Jacques"
*/
public static function formatNameFromDB(string $name): string
{
$formattedName = Str::ucfirst(Str::lower($name));

Expand Down
2 changes: 1 addition & 1 deletion app/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Extract the message.
*/
function trans_key(string $key = null): ?string
function trans_key(?string $key = null): ?string
{
return $key;
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/FemaleNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function index(Request $request): View
$names = $namesPagination
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'url' => route('name.show', [
'id' => $name->id,
Expand Down Expand Up @@ -75,7 +75,7 @@ public function letter(Request $request): View
$names = $namesPagination
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'url' => route('name.show', [
'id' => $name->id,
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/MaleNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function index(Request $request): View
$names = $namesPagination
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'url' => route('name.show', [
'id' => $name->id,
Expand Down Expand Up @@ -75,7 +75,7 @@ public function letter(Request $request): View
$names = $namesPagination
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'url' => route('name.show', [
'id' => $name->id,
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/NameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function index(Request $request): View
$names = $namesPagination
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'url' => route('name.show', [
'id' => $name->id,
Expand Down Expand Up @@ -103,7 +103,7 @@ public function letter(Request $request): View
$names = $namesPagination
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'url' => route('name.show', [
'id' => $name->id,
Expand Down
11 changes: 5 additions & 6 deletions app/Http/ViewModels/Home/HomeViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function twentyMostPopularNames(): array
->get()
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'url' => route('name.show', [
'id' => $name->id,
Expand All @@ -33,22 +33,21 @@ public static function twentyMostPopularNames(): array
->get()
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'url' => route('name.show', [
'id' => $name->id,
'name' => StringHelper::sanitizeNameForURL($name->name),
]),
]);

$randomNames = Name::where('gender', 'female')
->where('name', '!=', '_PRENOMS_RARES')
$randomNames = Name::where('name', '!=', '_PRENOMS_RARES')
->inRandomOrder()
->take(10)
->get()
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'url' => route('name.show', [
'id' => $name->id,
Expand All @@ -74,7 +73,7 @@ public static function nameSpotlight(): array

return [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'origins' => Str::words($name->origins, 50, '...'),
'url' => route('name.show', [
Expand Down
12 changes: 6 additions & 6 deletions app/Http/ViewModels/Names/AllNamesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ class AllNamesViewModel
{
public static function index(): Collection
{
// iterate over the alphabet
$alphabet = range('A', 'Z');

$letters = collect();

$allNames = Name::where('name', '!=', '_PRENOMS_RARES')->count();
$letters->push([
'letter' => 'Tous',
'count' => Number::format(Name::where('name', '!=', '_PRENOMS_RARES')->count(), locale: 'fr'),
'count' => Number::format($allNames, locale: 'fr'),
'url' => route('name.index'),
]);

$alphabet = range('A', 'Z');
foreach ($alphabet as $letter) {
$count = Name::where('name', 'like', $letter . '%')->count();

$letters->push([
'letter' => $letter,
'count' => Number::format(Name::where('name', 'like', $letter . '%')->count(), locale: 'fr'),
'count' => Number::format($count, locale: 'fr'),
'url' => route('name.letter', ['letter' => Str::lcfirst($letter)]),
]);
}
Expand Down
9 changes: 5 additions & 4 deletions app/Http/ViewModels/Names/NameViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
use App\Models\Name;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Number;
use Illuminate\Support\Str;

class NameViewModel
{
public static function details(Name $name): array
{
return [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'origins' => Str::of($name->origins)->markdown(),
'personality' => Str::of($name->personality)->markdown(),
Expand Down Expand Up @@ -56,6 +56,7 @@ public static function popularity(Name $name): array
$total = $decadesCollection->sum('popularity');
$decadesCollection = $decadesCollection->map(function ($decade) use ($total) {
$decade['percentage'] = Number::format(round($decade['popularity'] / $total * 100), locale: 'fr');

return $decade;
});

Expand All @@ -67,7 +68,7 @@ public static function popularity(Name $name): array
public static function jsonLdSchema(Name $name): array
{
return [
'headline' => 'Tout savoir sur le prénom ' . StringHelper::getProperName($name->name),
'headline' => 'Tout savoir sur le prénom ' . StringHelper::formatNameFromDB($name->name),
'image' => env('APP_URL') . '/images/facebook.png',
'date' => Carbon::now()->format('Y-m-d'),
'url' => route('name.show', [
Expand All @@ -87,7 +88,7 @@ public static function relatedNames(Name $name): Collection
->get()
->map(fn (Name $name) => [
'id' => $name->id,
'name' => StringHelper::getProperName($name->name),
'name' => StringHelper::formatNameFromDB($name->name),
'avatar' => $name->avatar,
'url' => route('name.show', [
'id' => $name->id,
Expand Down
4 changes: 2 additions & 2 deletions app/Models/MultiAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MultiAvatar

public array $ver;

public function __invoke(string $avatarId, string $sansEnv = null, array $ver = null): string
public function __invoke(string $avatarId, ?string $sansEnv = null, ?array $ver = null): string
{
$svgCode = $this->generate(strval($avatarId), $sansEnv, $ver);

Expand Down Expand Up @@ -48,7 +48,7 @@ public function getFinal(string $part, string $partV, string $theme, array $them
return $resultFinal;
}

public function generate(string $avatarId, string $sansEnv = null, array $ver = null): string
public function generate(string $avatarId, ?string $sansEnv = null, ?array $ver = null): string
{
$themes = [];

Expand Down
38 changes: 38 additions & 0 deletions database/factories/NameFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Name>
*/
class NameFactory extends Factory
{
protected static ?string $password;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'gender' => 'male',
'name' => fake()->name(),
'origins' => fake()->sentence(),
'personality' => fake()->sentence(),
'country_of_origin' => fake()->word(),
'celebrities' => fake()->sentence(),
'elfic_traits' => fake()->sentence(),
'name_day' => fake()->sentence(),
'litterature_artistics_references' => fake()->sentence(),
'similar_names_in_other_languages' => fake()->sentence(),
'klingon_translation' => fake()->sentence(),
'unisex' => true,
'total' => 1,
'page_views' => 1,
];
}
}
40 changes: 40 additions & 0 deletions tests/Unit/Helpers/StringHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Unit\Helpers;

use App\Helpers\StringHelper;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

class StringHelperTest extends TestCase
{
use DatabaseTransactions;

/** @test */
public function it_sanitizes_the_name_for_the_URL(): void
{
$this->assertEquals(
'heloise',
StringHelper::sanitizeNameForURL('Héloïse')
);

$this->assertEquals(
'cedricceheeeliuse',
StringHelper::sanitizeNameForURL('CédriçceHeèélïûse')
);
}

/** @test */
public function it_formats_the_name_from_the_BD(): void
{
$this->assertEquals(
'Héloïse',
StringHelper::formatNameFromDB('HÉLOÏSE')
);

$this->assertEquals(
'Jean-Jacques',
StringHelper::formatNameFromDB('JEAN-JACQUES')
);
}
}
93 changes: 93 additions & 0 deletions tests/Unit/ViewModels/Home/HomeViewModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Tests\Unit\ViewModels\Home;

use App\Http\ViewModels\Home\HomeViewModel;
use App\Models\Name;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

class HomeViewModelTest extends TestCase
{
use DatabaseTransactions;

/** @test */
public function it_gets_the_twenty_popular_names(): void
{
$maleName = Name::factory()->create([
'gender' => 'male',
'name' => 'JEAN-JACQUES',
]);
$femaleName = Name::factory()->create([
'gender' => 'female',
'name' => 'HÉLOÏSE',
]);

$array = HomeViewModel::twentyMostPopularNames();

$this->assertCount(3, $array);
$this->assertArrayHasKey('male_names', $array);
$this->assertArrayHasKey('female_names', $array);
$this->assertArrayHasKey('random_names', $array);

$this->assertEquals(
[
0 => [
'id' => $maleName->id,
'name' => 'Jean-Jacques',
'avatar' => $maleName->avatar,
'url' => env('APP_URL') . '/prenoms/' . $maleName->id . '/jean-jacques',
],
],
$array['male_names']->toArray()
);

$this->assertEquals(
[
0 => [
'id' => $femaleName->id,
'name' => 'Héloïse',
'avatar' => $femaleName->avatar,
'url' => env('APP_URL') . '/prenoms/' . $femaleName->id . '/heloise',
],
],
$array['female_names']->toArray()
);
}

/** @test */
public function it_gets_the_name_that_is_in_the_spotlight(): void
{
$name = Name::factory()->create([
'name' => 'JEAN-JACQUES',
'total' => 10000,
'origins' => 'This is the origins of the name Jean-Jacques and it is very long and this is insane because i want to test if Occaecat tempor aliqua nostrud magna adipisicing nulla excepteur ea. Occaecat tempor aliqua nostrud magna adipisicing nulla excepteur ea. This is the origins of the name Jean-Jacques and it is very long and this is insane because i want to test if Occaecat tempor aliqua nostrud magna adipisicing nulla excepteur ea. Occaecat tempor aliqua nostrud magna adipisicing nulla excepteur ea.',
]);
$array = HomeViewModel::nameSpotlight();

$this->assertEquals(
[
'id' => $name->id,
'name' => 'Jean-Jacques',
'avatar' => $name->avatar,
'origins' => 'This is the origins of the name Jean-Jacques and it is very long and this is insane because i want to test if Occaecat tempor aliqua nostrud magna adipisicing nulla excepteur ea. Occaecat tempor aliqua nostrud magna adipisicing nulla excepteur ea. This is the origins of the name Jean-Jacques and...',
'url' => env('APP_URL') . '/prenoms/' . $name->id . '/jean-jacques',
],
$array
);
}

/** @test */
public function it_gets_the_stats_of_the_server(): void
{
$name = Name::factory()->create();
$array = HomeViewModel::serverStats();

$this->assertEquals(
[
'total_names' => 1,
],
$array
);
}
}
Loading

0 comments on commit b15f345

Please sign in to comment.