From e844c988714b84287a90f879abcfc1e68f27f43b Mon Sep 17 00:00:00 2001 From: Thomas Rupprecht Date: Tue, 24 Jan 2023 19:23:57 +0100 Subject: [PATCH] Convert class const's to enum's (#1050) * convert `Migrate::(UP|DOWN)` class const to `MigrateDirection` enum * convert `Handler::(ENV_PRODUCTION|ENV_DEVELOPMENT)` class const to `Environment` enum * convert `ShiftSignupState::(FREE|...|NOT_ARRIVED)` class const to `ShiftSignupStatus::(FREE|...|NOT_ARRIVED)` enum --- bin/migrate | 10 +- .../controller/shift_entries_controller.php | 26 ++--- includes/controller/shifts_controller.php | 3 +- includes/model/ShiftSignupState.php | 73 +++--------- includes/model/Shifts_model.php | 25 +++-- includes/view/ShiftCalendarShiftRenderer.php | 104 +++++++----------- includes/view/Shifts_view.php | 5 +- src/Database/Migration/Direction.php | 11 ++ src/Database/Migration/Migrate.php | 36 ++---- src/Environment.php | 11 ++ src/Exceptions/ExceptionsServiceProvider.php | 5 +- src/Exceptions/Handler.php | 26 ++--- .../ConfigureEnvironmentServiceProvider.php | 3 +- src/Models/Shifts/ShiftSignupStatus.php | 53 +++++++++ tests/Unit/Database/Migration/MigrateTest.php | 41 +++---- .../ExceptionsServiceProviderTest.php | 5 +- tests/Unit/Exceptions/HandlerTest.php | 25 +++-- ...onfigureEnvironmentServiceProviderTest.php | 5 +- 18 files changed, 235 insertions(+), 232 deletions(-) create mode 100644 src/Database/Migration/Direction.php create mode 100644 src/Environment.php create mode 100644 src/Models/Shifts/ShiftSignupStatus.php diff --git a/bin/migrate b/bin/migrate index 7a70e527..2aa71548 100755 --- a/bin/migrate +++ b/bin/migrate @@ -4,7 +4,9 @@ use Composer\Autoload\ClassLoader; use Engelsystem\Application; use Engelsystem\Database\Migration\Migrate; +use Engelsystem\Database\Migration\Direction; use Engelsystem\Database\Migration\MigrationServiceProvider; +use Engelsystem\Environment; use Engelsystem\Exceptions\Handler; use Engelsystem\Exceptions\Handlers\NullHandler; @@ -19,7 +21,7 @@ $app->register(MigrationServiceProvider::class); /** @var Handler $errorHandler */ $errorHandler = $app->get(Handler::class); -$errorHandler->setHandler(Handler::ENV_PRODUCTION, new NullHandler()); +$errorHandler->setHandler(Environment::PRODUCTION, new NullHandler()); /** @var Migrate $migration */ $migration = $app->get('db.migration'); @@ -32,10 +34,10 @@ if (in_array('help', $argv) || in_array('--help', $argv) || in_array('-h', $argv exit; } -$method = Migrate::UP; +$direction = Direction::UP; if (in_array('down', $argv)) { $argv = array_values($argv); - $method = Migrate::DOWN; + $direction = Direction::DOWN; } $oneStep = false; @@ -48,4 +50,4 @@ if (in_array('force', $argv) || in_array('--force', $argv) || in_array('-f', $ar $force = true; } -$migration->run($baseDir, $method, $oneStep, $force); +$migration->run($baseDir, $direction, $oneStep, $force); diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index 7cd7b352..b8c904b6 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -3,6 +3,7 @@ use Engelsystem\Models\AngelType; use Engelsystem\Models\Shifts\Shift; use Engelsystem\Models\Shifts\ShiftEntry; +use Engelsystem\Models\Shifts\ShiftSignupStatus; use Engelsystem\Models\User\User; use Engelsystem\Models\UserAngelType; use Engelsystem\ShiftSignupState; @@ -180,21 +181,16 @@ function shift_entry_create_controller_supporter(Shift $shift, AngelType $angelt */ function shift_entry_error_message(ShiftSignupState $shift_signup_state) { - if ($shift_signup_state->getState() == ShiftSignupState::ANGELTYPE) { - error(__('You need be accepted member of the angeltype.')); - } elseif ($shift_signup_state->getState() == ShiftSignupState::COLLIDES) { - error(__('This shift collides with one of your shifts.')); - } elseif ($shift_signup_state->getState() == ShiftSignupState::OCCUPIED) { - error(__('This shift is already occupied.')); - } elseif ($shift_signup_state->getState() == ShiftSignupState::SHIFT_ENDED) { - error(__('This shift ended already.')); - } elseif ($shift_signup_state->getState() == ShiftSignupState::NOT_ARRIVED) { - error(__('You are not marked as arrived.')); - } elseif ($shift_signup_state->getState() == ShiftSignupState::NOT_YET) { - error(__('You are not allowed to sign up yet.')); - } elseif ($shift_signup_state->getState() == ShiftSignupState::SIGNED_UP) { - error(__('You are signed up for this shift.')); - } + match ($shift_signup_state->getState()) { + ShiftSignupStatus::ANGELTYPE => error(__('You need be accepted member of the angeltype.')), + ShiftSignupStatus::COLLIDES => error(__('This shift collides with one of your shifts.')), + ShiftSignupStatus::OCCUPIED => error(__('This shift is already occupied.')), + ShiftSignupStatus::SHIFT_ENDED => error(__('This shift ended already.')), + ShiftSignupStatus::NOT_ARRIVED => error(__('You are not marked as arrived.')), + ShiftSignupStatus::NOT_YET => error(__('You are not allowed to sign up yet.')), + ShiftSignupStatus::SIGNED_UP => error(__('You are signed up for this shift.')), + default => null, // ShiftSignupStatus::FREE|ShiftSignupStatus::ADMIN + }; } /** diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index bfae83c1..157485c2 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -7,6 +7,7 @@ use Engelsystem\Models\Shifts\NeededAngelType; use Engelsystem\Models\Shifts\ScheduleShift; use Engelsystem\Models\Shifts\Shift; use Engelsystem\Models\Shifts\ShiftType; +use Engelsystem\Models\Shifts\ShiftSignupStatus; use Engelsystem\ShiftSignupState; use Illuminate\Support\Collection; @@ -313,7 +314,7 @@ function shift_controller() $angeltypes = AngelType::all(); $user_shifts = Shifts_by_user($user->id); - $shift_signup_state = new ShiftSignupState(ShiftSignupState::OCCUPIED, 0); + $shift_signup_state = new ShiftSignupState(ShiftSignupStatus::OCCUPIED, 0); foreach ($angeltypes as $angeltype) { $needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype); if (empty($needed_angeltype)) { diff --git a/includes/model/ShiftSignupState.php b/includes/model/ShiftSignupState.php index 9bbde7bb..54539cfc 100644 --- a/includes/model/ShiftSignupState.php +++ b/includes/model/ShiftSignupState.php @@ -2,67 +2,24 @@ namespace Engelsystem; +use Engelsystem\Models\Shifts\ShiftSignupStatus; + /** * BO to represent if there are free slots on a shift for a given angeltype * and if signup for a given user is possible (or not, because of collisions, etc.) */ class ShiftSignupState { - /** - * Shift has free places - */ - public const FREE = 'FREE'; - - /** - * Shift collides with users shifts - */ - public const COLLIDES = 'COLLIDES'; - - /** - * User cannot join because of a restricted angeltype or user is not in the angeltype - */ - public const ANGELTYPE = 'ANGELTYPE'; - - /** - * Shift is full - */ - public const OCCUPIED = 'OCCUPIED'; - - /** - * User is admin and can do what he wants. - */ - public const ADMIN = 'ADMIN'; - - /** - * Shift has already ended, no signup - */ - public const SHIFT_ENDED = 'SHIFT_ENDED'; - - /** - * Shift is not available yet - */ - public const NOT_YET = 'NOT_YET'; - - /** - * User is already signed up - */ - public const SIGNED_UP = 'SIGNED_UP'; - - /** - * User has to be arrived - */ - public const NOT_ARRIVED = 'NOT_ARRIVED'; - /** @var int */ private $freeEntries; /** * ShiftSignupState constructor. * - * @param string $state - * @param int $free_entries + * @param ShiftSignupStatus $state + * @param int $free_entries */ - public function __construct(private $state, $free_entries) + public function __construct(private ShiftSignupStatus $state, $free_entries) { $this->freeEntries = $free_entries; } @@ -82,17 +39,17 @@ class ShiftSignupState } /** - * @param string $state + * @param ShiftSignupStatus $state * @return int */ - private function valueForState($state) + private function valueForState(ShiftSignupStatus $state) { return match ($state) { - ShiftSignupState::NOT_ARRIVED, ShiftSignupState::NOT_YET, ShiftSignupState::SHIFT_ENDED => 100, - ShiftSignupState::SIGNED_UP => 90, - ShiftSignupState::FREE => 80, - ShiftSignupState::ANGELTYPE, ShiftSignupState::COLLIDES => 70, - ShiftSignupState::OCCUPIED, ShiftSignupState::ADMIN => 60, + ShiftSignupStatus::NOT_ARRIVED, ShiftSignupStatus::NOT_YET, ShiftSignupStatus::SHIFT_ENDED => 100, + ShiftSignupStatus::SIGNED_UP => 90, + ShiftSignupStatus::FREE => 80, + ShiftSignupStatus::ANGELTYPE, ShiftSignupStatus::COLLIDES => 70, + ShiftSignupStatus::OCCUPIED, ShiftSignupStatus::ADMIN => 60, default => 0, }; } @@ -105,7 +62,7 @@ class ShiftSignupState public function isSignupAllowed() { return match ($this->state) { - ShiftSignupState::FREE, ShiftSignupState::ADMIN => true, + ShiftSignupStatus::FREE, ShiftSignupStatus::ADMIN => true, default => false, }; } @@ -113,9 +70,9 @@ class ShiftSignupState /** * Return the shift signup state * - * @return string + * @return ShiftSignupStatus */ - public function getState() + public function getState(): ShiftSignupStatus { return $this->state; } diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 397cd184..e00fff91 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -5,6 +5,7 @@ use Engelsystem\Helpers\Carbon; use Engelsystem\Models\AngelType; use Engelsystem\Models\Shifts\Shift; use Engelsystem\Models\Shifts\ShiftEntry; +use Engelsystem\Models\Shifts\ShiftSignupStatus; use Engelsystem\Models\User\User; use Engelsystem\Models\UserAngelType; use Engelsystem\ShiftsFilter; @@ -323,11 +324,11 @@ function Shift_signup_allowed_angel( $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); if (config('signup_requires_arrival') && !$user->state->arrived) { - return new ShiftSignupState(ShiftSignupState::NOT_ARRIVED, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::NOT_ARRIVED, $free_entries); } if (config('signup_advance_hours') && $shift->start->timestamp > time() + config('signup_advance_hours') * 3600) { - return new ShiftSignupState(ShiftSignupState::NOT_YET, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::NOT_YET, $free_entries); } if (empty($user_shifts)) { @@ -344,7 +345,7 @@ function Shift_signup_allowed_angel( if ($signed_up) { // you cannot join if you already signed up for this shift - return new ShiftSignupState(ShiftSignupState::SIGNED_UP, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::SIGNED_UP, $free_entries); } $shift_post_signup_total_allowed_seconds = @@ -353,11 +354,11 @@ function Shift_signup_allowed_angel( if (time() > $shift->start->timestamp + $shift_post_signup_total_allowed_seconds) { // you can only join if the shift is in future - return new ShiftSignupState(ShiftSignupState::SHIFT_ENDED, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::SHIFT_ENDED, $free_entries); } if ($free_entries == 0) { // you cannot join if shift is full - return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::OCCUPIED, $free_entries); } if (empty($user_angeltype)) { @@ -373,16 +374,16 @@ function Shift_signup_allowed_angel( // you cannot join if you are not confirmed // you cannot join if angeltype has no self signup - return new ShiftSignupState(ShiftSignupState::ANGELTYPE, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::ANGELTYPE, $free_entries); } if (Shift_collides($shift, $user_shifts)) { // you cannot join if user already joined a parallel of this shift - return new ShiftSignupState(ShiftSignupState::COLLIDES, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::COLLIDES, $free_entries); } // Hooray, shift is free for you! - return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::FREE, $free_entries); } /** @@ -396,10 +397,10 @@ function Shift_signup_allowed_angeltype_supporter(AngelType $needed_angeltype, $ { $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); if ($free_entries == 0) { - return new ShiftSignupState(ShiftSignupState::OCCUPIED, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::OCCUPIED, $free_entries); } - return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::FREE, $free_entries); } /** @@ -415,10 +416,10 @@ function Shift_signup_allowed_admin(AngelType $needed_angeltype, $shift_entries) if ($free_entries == 0) { // User shift admins may join anybody in every shift - return new ShiftSignupState(ShiftSignupState::ADMIN, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::ADMIN, $free_entries); } - return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); + return new ShiftSignupState(ShiftSignupStatus::FREE, $free_entries); } /** diff --git a/includes/view/ShiftCalendarShiftRenderer.php b/includes/view/ShiftCalendarShiftRenderer.php index 9e5bfb1c..e1f104bb 100644 --- a/includes/view/ShiftCalendarShiftRenderer.php +++ b/includes/view/ShiftCalendarShiftRenderer.php @@ -5,6 +5,7 @@ namespace Engelsystem; use Engelsystem\Models\AngelType; use Engelsystem\Models\Shifts\Shift; use Engelsystem\Models\Shifts\ShiftEntry; +use Engelsystem\Models\Shifts\ShiftSignupStatus; use Engelsystem\Models\User\User; use Illuminate\Support\Collection; @@ -72,11 +73,11 @@ class ShiftCalendarShiftRenderer private function classForSignupState(ShiftSignupState $shiftSignupState) { return match ($shiftSignupState->getState()) { - ShiftSignupState::ADMIN, ShiftSignupState::OCCUPIED => 'success', - ShiftSignupState::SIGNED_UP => 'primary', - ShiftSignupState::NOT_ARRIVED, ShiftSignupState::NOT_YET, ShiftSignupState::SHIFT_ENDED => 'secondary', - ShiftSignupState::ANGELTYPE, ShiftSignupState::COLLIDES => 'warning', - ShiftSignupState::FREE => 'danger', + ShiftSignupStatus::ADMIN, ShiftSignupStatus::OCCUPIED => 'success', + ShiftSignupStatus::SIGNED_UP => 'primary', + ShiftSignupStatus::NOT_ARRIVED, ShiftSignupStatus::NOT_YET, ShiftSignupStatus::SHIFT_ENDED => 'secondary', + ShiftSignupStatus::ANGELTYPE, ShiftSignupStatus::COLLIDES => 'warning', + ShiftSignupStatus::FREE => 'danger', default => 'light', }; } @@ -118,7 +119,7 @@ class ShiftCalendarShiftRenderer } } if (is_null($shift_signup_state)) { - $shift_signup_state = new ShiftSignupState(ShiftSignupState::SHIFT_ENDED, 0); + $shift_signup_state = new ShiftSignupState(ShiftSignupStatus::SHIFT_ENDED, 0); } if (auth()->can('user_shifts_admin')) { @@ -173,63 +174,42 @@ class ShiftCalendarShiftRenderer $freeEntriesCount = $shift_signup_state->getFreeEntries(); $inner_text = _e('%d helper needed', '%d helpers needed', $freeEntriesCount, [$freeEntriesCount]); - switch ($shift_signup_state->getState()) { - case ShiftSignupState::ADMIN: - case ShiftSignupState::FREE: - // When admin or free display a link + button for sign up - $entry_list[] = '' - . $inner_text - . ' ' + $entry = match ($shift_signup_state->getState()) { + // When admin or free display a link + button for sign up + ShiftSignupStatus::ADMIN, ShiftSignupStatus::FREE => + '' + . $inner_text + . ' ' + . button( + shift_entry_create_link($shift, $angeltype), + __('Sign up'), + 'btn-sm btn-primary text-nowrap d-print-none' + ), + // No link and add a text hint, when the shift ended + ShiftSignupStatus::SHIFT_ENDED => $inner_text . ' (' . __('ended') . ')', + // No link and add a text hint, when the shift ended + ShiftSignupStatus::NOT_ARRIVED => $inner_text . ' (' . __('please arrive for signup') . ')', + ShiftSignupStatus::NOT_YET => $inner_text . ' (' . __('not yet') . ')', + ShiftSignupStatus::ANGELTYPE => $angeltype->restricted + // User has to be confirmed on the angeltype first + ? $inner_text . icon('mortarboard-fill') + // Add link to join the angeltype first + : $inner_text . '
' . button( - shift_entry_create_link($shift, $angeltype), - __('Sign up'), - 'btn-sm btn-primary text-nowrap d-print-none' - ); - break; - - case ShiftSignupState::SHIFT_ENDED: - // No link and add a text hint, when the shift ended - $entry_list[] = $inner_text . ' (' . __('ended') . ')'; - break; - - case ShiftSignupState::NOT_ARRIVED: - // No link and add a text hint, when the shift ended - $entry_list[] = $inner_text . ' (' . __('please arrive for signup') . ')'; - break; - - case ShiftSignupState::NOT_YET: - $entry_list[] = $inner_text . ' (' . __('not yet') . ')'; - break; - - case ShiftSignupState::ANGELTYPE: - if ($angeltype->restricted) { - // User has to be confirmed on the angeltype first - $entry_list[] = $inner_text . icon('mortarboard-fill'); - } else { - // Add link to join the angeltype first - $entry_list[] = $inner_text . '
' - . button( - page_link_to( - 'user_angeltypes', - ['action' => 'add', 'angeltype_id' => $angeltype->id] - ), - sprintf(__('Become %s'), $angeltype->name), - 'btn-sm' - ); - } - break; - - case ShiftSignupState::COLLIDES: - case ShiftSignupState::SIGNED_UP: - // Shift collides or user is already signed up: No signup allowed - $entry_list[] = $inner_text; - break; - - case ShiftSignupState::OCCUPIED: - // Shift is full - break; + page_link_to('user_angeltypes', ['action' => 'add', 'angeltype_id' => $angeltype->id]), + sprintf(__('Become %s'), $angeltype->name), + 'btn-sm' + ), + // Shift collides or user is already signed up: No signup allowed + ShiftSignupStatus::COLLIDES, ShiftSignupStatus::SIGNED_UP => $inner_text, + // Shift is full + ShiftSignupStatus::OCCUPIED => null, + default => null, + }; + if (!is_null($entry)) { + $entry_list[] = $entry; } $shifts_row = '
  • '; diff --git a/includes/view/Shifts_view.php b/includes/view/Shifts_view.php index 7da4bdc5..67a72acf 100644 --- a/includes/view/Shifts_view.php +++ b/includes/view/Shifts_view.php @@ -5,6 +5,7 @@ use Engelsystem\Models\Room; use Engelsystem\Models\Shifts\Shift; use Engelsystem\Models\Shifts\ShiftEntry; use Engelsystem\Models\Shifts\ShiftType; +use Engelsystem\Models\Shifts\ShiftSignupStatus; use Engelsystem\Models\UserAngelType; use Engelsystem\ShiftSignupState; use Illuminate\Support\Collection; @@ -152,11 +153,11 @@ function Shift_view(Shift $shift, ShiftType $shifttype, Room $room, $angeltypes_ $content = [msg()]; - if ($shift_signup_state->getState() == ShiftSignupState::COLLIDES) { + if ($shift_signup_state->getState() === ShiftSignupStatus::COLLIDES) { $content[] = info(__('This shift collides with one of your shifts.'), true); } - if ($shift_signup_state->getState() == ShiftSignupState::SIGNED_UP) { + if ($shift_signup_state->getState() === ShiftSignupStatus::SIGNED_UP) { $content[] = info(__('You are signed up for this shift.'), true); } diff --git a/src/Database/Migration/Direction.php b/src/Database/Migration/Direction.php new file mode 100644 index 00000000..2439f1cc --- /dev/null +++ b/src/Database/Migration/Direction.php @@ -0,0 +1,11 @@ +getMigrated() ); - if ($type == self::DOWN) { + if ($direction === Direction::DOWN) { $migrations = $migrations->reverse(); } @@ -62,19 +54,19 @@ class Migrate $name = $migration['migration']; if ( - ($type == self::UP && isset($migration['id'])) - || ($type == self::DOWN && !isset($migration['id'])) + ($direction === Direction::UP && isset($migration['id'])) + || ($direction === Direction::DOWN && !isset($migration['id'])) ) { ($this->output)('Skipping ' . $name); continue; } - ($this->output)('Migrating ' . $name . ' (' . $type . ')'); + ($this->output)('Migrating ' . $name . ' (' . $direction->value . ')'); if (isset($migration['path'])) { - $this->migrate($migration['path'], $name, $type); + $this->migrate($migration['path'], $name, $direction); } - $this->setMigrated($name, $type); + $this->setMigrated($name, $direction); if ($oneStep) { break; @@ -145,10 +137,8 @@ class Migrate /** * Migrate a migration - * - * @param string $type (up|down) */ - protected function migrate(string $file, string $migration, string $type = self::UP): void + protected function migrate(string $file, string $migration, Direction $direction = Direction::UP): void { require_once $file; @@ -156,21 +146,19 @@ class Migrate /** @var Migration $class */ $class = $this->app->make('Engelsystem\\Migrations\\' . $className); - if (method_exists($class, $type)) { - $class->{$type}(); + if (method_exists($class, $direction->value)) { + $class->{$direction->value}(); } } /** * Set a migration to migrated - * - * @param string $type (up|down) */ - protected function setMigrated(string $migration, string $type = self::UP): void + protected function setMigrated(string $migration, Direction $direction = Direction::UP): void { $table = $this->getTableQuery(); - if ($type == self::DOWN) { + if ($direction === Direction::DOWN) { $table->where(['migration' => $migration])->delete(); return; } diff --git a/src/Environment.php b/src/Environment.php new file mode 100644 index 00000000..7ab80813 --- /dev/null +++ b/src/Environment.php @@ -0,0 +1,11 @@ +app->make(Legacy::class); $this->app->instance('error.handler.production', $handler); - $errorHandler->setHandler(Handler::ENV_PRODUCTION, $handler); + $errorHandler->setHandler(Environment::PRODUCTION, $handler); $this->app->bind(HandlerInterface::class, 'error.handler.production'); } @@ -49,7 +50,7 @@ class ExceptionsServiceProvider extends ServiceProvider } $this->app->instance('error.handler.development', $handler); - $errorHandler->setHandler(Handler::ENV_DEVELOPMENT, $handler); + $errorHandler->setHandler(Environment::DEVELOPMENT, $handler); } protected function addLogger(Handler $handler): void diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php index 47a01c7f..7cb8d486 100644 --- a/src/Exceptions/Handler.php +++ b/src/Exceptions/Handler.php @@ -2,6 +2,7 @@ namespace Engelsystem\Exceptions; +use Engelsystem\Environment; use Engelsystem\Exceptions\Handlers\HandlerInterface; use Engelsystem\Exceptions\Handlers\Legacy; use Engelsystem\Http\Request; @@ -15,18 +16,12 @@ class Handler protected ?Request $request = null; - /** @var string */ - public const ENV_PRODUCTION = 'prod'; - - /** @var string */ - public const ENV_DEVELOPMENT = 'dev'; - /** * Handler constructor. * - * @param string $environment prod|dev + * @param Environment $environment prod|dev */ - public function __construct(protected string $environment = self::ENV_PRODUCTION) + public function __construct(protected Environment $environment = Environment::PRODUCTION) { } @@ -51,7 +46,8 @@ class Handler $this->request = new Request(); } - $handler = isset($this->handler[$this->environment]) ? $this->handler[$this->environment] : new Legacy(); + $handler = isset($this->handler[$this->environment->value]) + ? $this->handler[$this->environment->value] : new Legacy(); $handler->report($e); ob_start(); $handler->render($this->request, $e); @@ -81,12 +77,12 @@ class Handler die(1); } - public function getEnvironment(): string + public function getEnvironment(): Environment { return $this->environment; } - public function setEnvironment(string $environment): void + public function setEnvironment(Environment $environment): void { $this->environment = $environment; } @@ -94,18 +90,18 @@ class Handler /** * @return HandlerInterface|HandlerInterface[] */ - public function getHandler(string $environment = null): HandlerInterface|array + public function getHandler(Environment $environment = null): HandlerInterface|array { if (!is_null($environment)) { - return $this->handler[$environment]; + return $this->handler[$environment->value]; } return $this->handler; } - public function setHandler(string $environment, HandlerInterface $handler): void + public function setHandler(Environment $environment, HandlerInterface $handler): void { - $this->handler[$environment] = $handler; + $this->handler[$environment->value] = $handler; } public function getRequest(): Request diff --git a/src/Helpers/ConfigureEnvironmentServiceProvider.php b/src/Helpers/ConfigureEnvironmentServiceProvider.php index 3a7d10d9..0af82c6e 100644 --- a/src/Helpers/ConfigureEnvironmentServiceProvider.php +++ b/src/Helpers/ConfigureEnvironmentServiceProvider.php @@ -5,6 +5,7 @@ namespace Engelsystem\Helpers; use Carbon\CarbonTimeZone; use Engelsystem\Config\Config; use Engelsystem\Container\ServiceProvider; +use Engelsystem\Environment; use Engelsystem\Exceptions\Handler; use Engelsystem\Exceptions\Handlers\HandlerInterface; @@ -58,7 +59,7 @@ class ConfigureEnvironmentServiceProvider extends ServiceProvider { /** @var Handler $errorHandler */ $errorHandler = $this->app->get('error.handler'); - $errorHandler->setEnvironment(Handler::ENV_DEVELOPMENT); + $errorHandler->setEnvironment(Environment::DEVELOPMENT); $this->app->bind(HandlerInterface::class, 'error.handler.development'); } } diff --git a/src/Models/Shifts/ShiftSignupStatus.php b/src/Models/Shifts/ShiftSignupStatus.php new file mode 100644 index 00000000..4d398b45 --- /dev/null +++ b/src/Models/Shifts/ShiftSignupStatus.php @@ -0,0 +1,53 @@ +expects($this->atLeastOnce()) ->method('migrate') ->withConsecutive( - ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Migrate::UP], - ['foo/9999_99_99_999999_another_foo.php', '9999_99_99_999999_another_foo', Migrate::UP], - ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Migrate::UP], - ['foo/9999_99_99_999999_another_foo.php', '9999_99_99_999999_another_foo', Migrate::UP], - ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Migrate::UP], - ['foo/4567_11_01_000000_do_stuff.php', '4567_11_01_000000_do_stuff', Migrate::DOWN] + ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Direction::UP], + ['foo/9999_99_99_999999_another_foo.php', '9999_99_99_999999_another_foo', Direction::UP], + ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Direction::UP], + ['foo/9999_99_99_999999_another_foo.php', '9999_99_99_999999_another_foo', Direction::UP], + ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Direction::UP], + ['foo/4567_11_01_000000_do_stuff.php', '4567_11_01_000000_do_stuff', Direction::DOWN] ); $migration->expects($this->atLeastOnce()) ->method('setMigrated') ->withConsecutive( - ['9876_03_22_210000_random_hack', Migrate::UP], - ['9999_99_99_999999_another_foo', Migrate::UP], - ['9876_03_22_210000_random_hack', Migrate::UP], - ['9999_99_99_999999_another_foo', Migrate::UP], - ['9876_03_22_210000_random_hack', Migrate::UP], - ['4567_11_01_000000_do_stuff', Migrate::DOWN] + ['9876_03_22_210000_random_hack', Direction::UP], + ['9999_99_99_999999_another_foo', Direction::UP], + ['9876_03_22_210000_random_hack', Direction::UP], + ['9999_99_99_999999_another_foo', Direction::UP], + ['9876_03_22_210000_random_hack', Direction::UP], + ['4567_11_01_000000_do_stuff', Direction::DOWN] ); $this->setExpects($migration, 'lockTable', null, null, $this->atLeastOnce()); $this->setExpects($migration, 'unlockTable', null, null, $this->atLeastOnce()); - $migration->run('foo', Migrate::UP); + $migration->run('foo', Direction::UP); $messages = []; $migration->setOutput(function ($text) use (&$messages): void { $messages[] = $text; }); - $migration->run('foo', Migrate::UP); + $migration->run('foo', Direction::UP); $this->assertCount(4, $messages); foreach ( @@ -113,10 +114,10 @@ class MigrateTest extends TestCase } $messages = []; - $migration->run('foo', Migrate::UP, true); + $migration->run('foo', Direction::UP, true); $this->assertCount(3, $messages); - $migration->run('foo', Migrate::DOWN, true); + $migration->run('foo', Direction::DOWN, true); } /** @@ -192,7 +193,7 @@ class MigrateTest extends TestCase $messages[] = $msg; }); - $migration->run(__DIR__ . '/Stub', Migrate::UP); + $migration->run(__DIR__ . '/Stub', Direction::UP); $this->assertTrue($schema->hasTable('migrations')); @@ -206,12 +207,12 @@ class MigrateTest extends TestCase $this->assertTrue($schema->hasTable('lorem_ipsum')); - $migration->run(__DIR__ . '/Stub', Migrate::DOWN, true); + $migration->run(__DIR__ . '/Stub', Direction::DOWN, true); $migrations = $db->table('migrations')->get(); $this->assertCount(2, $migrations); - $migration->run(__DIR__ . '/Stub', Migrate::DOWN); + $migration->run(__DIR__ . '/Stub', Direction::DOWN); $migrations = $db->table('migrations')->get(); $this->assertCount(0, $migrations); @@ -220,6 +221,6 @@ class MigrateTest extends TestCase $db->table('migrations')->insert(['migration' => 'lock']); $this->expectException(Exception::class); - $migration->run(__DIR__ . '/Stub', Migrate::UP); + $migration->run(__DIR__ . '/Stub', Direction::UP); } } diff --git a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php index c3c92a67..69af9213 100644 --- a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php +++ b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php @@ -2,6 +2,7 @@ namespace Engelsystem\Test\Unit\Exceptions; +use Engelsystem\Environment; use Engelsystem\Exceptions\ExceptionsServiceProvider; use Engelsystem\Exceptions\Handler; use Engelsystem\Exceptions\Handlers\HandlerInterface; @@ -69,8 +70,8 @@ class ExceptionsServiceProviderTest extends ServiceProviderTest $handler->expects($this->exactly(2)) ->method('setHandler') ->withConsecutive( - [Handler::ENV_PRODUCTION, $legacyHandler], - [Handler::ENV_DEVELOPMENT, $whoopsHandler] + [Environment::PRODUCTION, $legacyHandler], + [Environment::DEVELOPMENT, $whoopsHandler] ); $serviceProvider = new ExceptionsServiceProvider($app); diff --git a/tests/Unit/Exceptions/HandlerTest.php b/tests/Unit/Exceptions/HandlerTest.php index 28ba5996..456c7ad1 100644 --- a/tests/Unit/Exceptions/HandlerTest.php +++ b/tests/Unit/Exceptions/HandlerTest.php @@ -2,6 +2,7 @@ namespace Engelsystem\Test\Unit\Exceptions; +use Engelsystem\Environment; use Engelsystem\Exceptions\Handler; use Engelsystem\Exceptions\Handlers\HandlerInterface; use Engelsystem\Http\Request; @@ -20,10 +21,10 @@ class HandlerTest extends TestCase /** @var Handler|MockObject $handler */ $handler = new Handler(); $this->assertInstanceOf(Handler::class, $handler); - $this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment()); + $this->assertEquals(Environment::PRODUCTION, $handler->getEnvironment()); - $anotherHandler = new Handler(Handler::ENV_DEVELOPMENT); - $this->assertEquals(Handler::ENV_DEVELOPMENT, $anotherHandler->getEnvironment()); + $anotherHandler = new Handler(Environment::DEVELOPMENT); + $this->assertEquals(Environment::DEVELOPMENT, $anotherHandler->getEnvironment()); } /** @@ -70,7 +71,7 @@ class HandlerTest extends TestCase $handler->expects($this->once()) ->method('terminateApplicationImmediately'); - $handler->setHandler(Handler::ENV_PRODUCTION, $handlerMock); + $handler->setHandler(Environment::PRODUCTION, $handlerMock); $this->expectOutputString($errorMessage); $handler->exceptionHandler($exception); @@ -106,11 +107,11 @@ class HandlerTest extends TestCase { $handler = new Handler(); - $handler->setEnvironment(Handler::ENV_DEVELOPMENT); - $this->assertEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment()); + $handler->setEnvironment(Environment::DEVELOPMENT); + $this->assertEquals(Environment::DEVELOPMENT, $handler->getEnvironment()); - $handler->setEnvironment(Handler::ENV_PRODUCTION); - $this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment()); + $handler->setEnvironment(Environment::PRODUCTION); + $this->assertEquals(Environment::PRODUCTION, $handler->getEnvironment()); } /** @@ -125,10 +126,10 @@ class HandlerTest extends TestCase /** @var HandlerInterface|MockObject $prodHandler */ $prodHandler = $this->getMockForAbstractClass(HandlerInterface::class); - $handler->setHandler(Handler::ENV_DEVELOPMENT, $devHandler); - $handler->setHandler(Handler::ENV_PRODUCTION, $prodHandler); - $this->assertEquals($devHandler, $handler->getHandler(Handler::ENV_DEVELOPMENT)); - $this->assertEquals($prodHandler, $handler->getHandler(Handler::ENV_PRODUCTION)); + $handler->setHandler(Environment::DEVELOPMENT, $devHandler); + $handler->setHandler(Environment::PRODUCTION, $prodHandler); + $this->assertEquals($devHandler, $handler->getHandler(Environment::DEVELOPMENT)); + $this->assertEquals($prodHandler, $handler->getHandler(Environment::PRODUCTION)); $this->assertCount(2, $handler->getHandler()); } diff --git a/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php b/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php index 19f43ba7..e0d582cc 100644 --- a/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php +++ b/tests/Unit/Helpers/ConfigureEnvironmentServiceProviderTest.php @@ -4,6 +4,7 @@ namespace Engelsystem\Test\Unit\Helpers; use Carbon\CarbonTimeZone; use Engelsystem\Config\Config; +use Engelsystem\Environment; use Engelsystem\Exceptions\Handler; use Engelsystem\Helpers\ConfigureEnvironmentServiceProvider; use Engelsystem\Test\Unit\ServiceProviderTest; @@ -42,10 +43,10 @@ class ConfigureEnvironmentServiceProviderTest extends ServiceProviderTest ->with(E_ALL); $serviceProvider->register(); - $this->assertNotEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment()); + $this->assertNotEquals(Environment::DEVELOPMENT, $handler->getEnvironment()); $config->set('environment', 'development'); $serviceProvider->register(); - $this->assertEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment()); + $this->assertEquals(Environment::DEVELOPMENT, $handler->getEnvironment()); } }