Skip to content

Commit

Permalink
#444 - added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilpiech97 committed Jun 10, 2024
1 parent 1fb1702 commit 01d679f
Show file tree
Hide file tree
Showing 21 changed files with 484 additions and 23 deletions.
7 changes: 7 additions & 0 deletions app/Actions/OvertimeRequest/CreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use Toby\Models\OvertimeRequest;
use Toby\Models\User;
use Toby\Models\VacationRequest;
use Toby\Validation\OvertimeRequestValidator;

class CreateAction
{
public function __construct(
protected OvertimeRequestStateManager $stateManager,
protected WaitForTechApprovalAction $waitForTechApprovalAction,
protected OvertimeCalculator $overtimeCalculator,
protected OvertimeRequestValidator $overtimeRequestValidator,
) {}

/**
Expand All @@ -31,12 +33,17 @@ public function execute(array $data, User $creator): OvertimeRequest
return $overtimeRequest;
}

/**
* @throws ValidationException
*/
protected function createVacationRequest(array $data, User $creator): OvertimeRequest
{
/** @var OvertimeRequest $overtimeRequest */
$overtimeRequest = $creator->createdOvertimeRequests()->make($data);
$overtimeRequest->hours = $this->overtimeCalculator->calculateHours($overtimeRequest->from, $overtimeRequest->to);

$this->overtimeRequestValidator->validate($overtimeRequest);

$overtimeRequest->save();

$this->stateManager->markAsCreated($overtimeRequest);
Expand Down
4 changes: 2 additions & 2 deletions app/Domain/OvertimeCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class OvertimeCalculator
{
public function calculateHours(CarbonInterface $from, CarbonInterface $to): int
{
$hours = Carbon::create($from)->diffInHours($to);
$hours = Carbon::create($from)->diffInMinutes($to, true);

return (int)$hours;
return (int)ceil($hours / 60);
}
}
20 changes: 20 additions & 0 deletions app/Http/Controllers/Api/CalculateOvertimeHoursController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Toby\Http\Controllers\Api;

use Illuminate\Http\JsonResponse;
use Toby\Domain\OvertimeCalculator;
use Toby\Http\Controllers\Controller;
use Toby\Http\Requests\Api\CalculateOvertimeHoursRequest;

class CalculateOvertimeHoursController extends Controller
{
public function __invoke(CalculateOvertimeHoursRequest $request, OvertimeCalculator $overtimeCalculator): JsonResponse
{
$hours = $overtimeCalculator->calculateHours($request->from(), $request->to());

return new JsonResponse($hours);
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/OvertimeRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ public function show(OvertimeRequest $overtimeRequest): Response
]);
}

public function create(): Response
public function create(Request $request): Response
{
return inertia("OvertimeRequest/Create", [
"settlementTypes" => SettlementType::casesToSelect(),
"overtimeFromDate" => $request->get("from_date"),
]);
}

Expand Down
36 changes: 36 additions & 0 deletions app/Http/Requests/Api/CalculateOvertimeHoursRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Toby\Http\Requests\Api;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Carbon;
use Toby\Http\Rules\YearPeriodExists;
use Toby\Models\YearPeriod;

class CalculateOvertimeHoursRequest extends FormRequest
{
public function rules(): array
{
return [
"from" => ["required", "date_format:Y-m-d H:i", new YearPeriodExists()],
"to" => ["required", "date_format:Y-m-d H:i", new YearPeriodExists()],
];
}

public function from(): Carbon
{
return Carbon::create($this->request->get("from"));
}

public function to(): Carbon
{
return Carbon::create($this->request->get("to"));
}

public function yearPeriod(): YearPeriod
{
return YearPeriod::findByYear(Carbon::create($this->request->get("from"))->year);
}
}
6 changes: 6 additions & 0 deletions app/Models/OvertimeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public function scopeStates(Builder $query, OvertimeRequestState|array $states):
return $query->whereState("state", $states);
}

public function scopeOverlapsWith(Builder $query, self $overtimeRequest): Builder
{
return $query->where("from", "<=", $overtimeRequest->to)
->where("to", ">=", $overtimeRequest->from);
}

protected static function newFactory(): OvertimeRequestFactory
{
return OvertimeRequestFactory::new();
Expand Down
43 changes: 43 additions & 0 deletions app/Validation/OvertimeRequestValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Toby\Validation;

use Illuminate\Contracts\Container\Container;
use Illuminate\Validation\ValidationException;
use Toby\Models\OvertimeRequest;
use Toby\Validation\Rules\OvertimeRequest\NoPendingOvertimeRequestInRange;
use Toby\Validation\Rules\OvertimeRequest\OvertimeRequestRule;

class OvertimeRequestValidator
{
protected array $rules = [
NoPendingOvertimeRequestInRange::class,
];

public function __construct(
protected Container $container,
) {}

/**
* @throws ValidationException
*/
public function validate(OvertimeRequest $overtimeRequest): void
{
foreach ($this->rules as $rule) {
$rule = $this->makeRule($rule);

if (!$rule->check($overtimeRequest)) {
throw ValidationException::withMessages([
"overtimeRequest" => $rule->errorMessage(),
]);
}
}
}

protected function makeRule(string $class): OvertimeRequestRule
{
return $this->container->make($class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Toby\Validation\Rules\OvertimeRequest;

use Toby\Domain\OvertimeRequestStatesRetriever;
use Toby\Models\OvertimeRequest;

class NoPendingOvertimeRequestInRange implements OvertimeRequestRule
{
public function check(OvertimeRequest $overtimeRequest): bool
{
return !$overtimeRequest
->user
->overtimeRequests()
->overlapsWith($overtimeRequest)
->states(OvertimeRequestStatesRetriever::pendingStates())
->exists();
}

public function errorMessage(): string
{
return __("You have a pending request in this date range.");
}
}
14 changes: 14 additions & 0 deletions app/Validation/Rules/OvertimeRequest/OvertimeRequestRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Toby\Validation\Rules\OvertimeRequest;

use Toby\Models\OvertimeRequest;

interface OvertimeRequestRule
{
public function check(OvertimeRequest $overtimeRequest): bool;

public function errorMessage(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Toby\Validation\Rules;
namespace Toby\Validation\Rules\VacationRequest;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Toby\Validation\Rules;
namespace Toby\Validation\Rules\VacationRequest;

use Toby\Domain\WorkDaysCalculator;
use Toby\Models\VacationRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Toby\Validation\Rules;
namespace Toby\Validation\Rules\VacationRequest;

use Toby\Domain\VacationRequestStatesRetriever;
use Toby\Models\VacationRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Toby\Validation\Rules;
namespace Toby\Validation\Rules\VacationRequest;

use Toby\Domain\VacationRequestStatesRetriever;
use Toby\Models\VacationRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Toby\Validation\Rules;
namespace Toby\Validation\Rules\VacationRequest;

use Toby\Models\VacationRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Toby\Validation\Rules;
namespace Toby\Validation\Rules\VacationRequest;

use Toby\Models\VacationRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Toby\Validation\Rules;
namespace Toby\Validation\Rules\VacationRequest;

use Toby\Domain\VacationTypeConfigRetriever;
use Toby\Enums\VacationType;
Expand Down
14 changes: 7 additions & 7 deletions app/Validation/VacationRequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Validation\ValidationException;
use Toby\Models\VacationRequest;
use Toby\Validation\Rules\DoesNotExceedLimitRule;
use Toby\Validation\Rules\MinimumOneVacationDayRule;
use Toby\Validation\Rules\NoApprovedVacationRequestsInRange;
use Toby\Validation\Rules\NoPendingVacationRequestInRange;
use Toby\Validation\Rules\VacationRangeIsInTheSameYearRule;
use Toby\Validation\Rules\VacationRequestRule;
use Toby\Validation\Rules\VacationTypeCanBeSelected;
use Toby\Validation\Rules\VacationRequest\DoesNotExceedLimitRule;
use Toby\Validation\Rules\VacationRequest\MinimumOneVacationDayRule;
use Toby\Validation\Rules\VacationRequest\NoApprovedVacationRequestsInRange;
use Toby\Validation\Rules\VacationRequest\NoPendingVacationRequestInRange;
use Toby\Validation\Rules\VacationRequest\VacationRangeIsInTheSameYearRule;
use Toby\Validation\Rules\VacationRequest\VacationRequestRule;
use Toby\Validation\Rules\VacationRequest\VacationTypeCanBeSelected;

class VacationRequestValidator
{
Expand Down
2 changes: 2 additions & 0 deletions database/factories/OvertimeRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Factories\Factory;
use Toby\Domain\OvertimeRequestStatesRetriever;
use Toby\Enums\SettlementType;
use Toby\Models\OvertimeRequest;
use Toby\Models\User;
use Toby\Models\YearPeriod;
Expand All @@ -29,6 +30,7 @@ public function definition(): array
"from" => $from,
"to" => $to,
"hours" => $hours,
"settlement_type" => $this->faker->randomElement(SettlementType::cases())->value,
"comment" => $this->faker->boolean ? $this->faker->paragraph() : null,
];
}
Expand Down
Loading

0 comments on commit 01d679f

Please sign in to comment.