diff --git a/app/Http/Controllers/Admin/DataManagement/FixturesController.php b/app/Http/Controllers/Admin/DataManagement/FixturesController.php index b358d41a..6105d96b 100644 --- a/app/Http/Controllers/Admin/DataManagement/FixturesController.php +++ b/app/Http/Controllers/Admin/DataManagement/FixturesController.php @@ -159,10 +159,13 @@ public function update(Request $request, $id) */ public function destroy($id) { - Fixture::destroy($id); - - \Flash::success('Fixture deleted!'); - + $canBeDeleted = empty(Fixture::find($id)->available_appointments->toArray()); + if ($canBeDeleted) { + Fixture::destroy($id); + \Flash::success('Fixture deleted!'); + } else { + \Flash::error('Cannot delete because they are existing appointments for this fixture.'); + } return redirect('admin/data-management/fixtures'); } diff --git a/app/Http/Controllers/Admin/DataManagement/RolesController.php b/app/Http/Controllers/Admin/DataManagement/RolesController.php index 2dfdcdfa..0ec6395e 100644 --- a/app/Http/Controllers/Admin/DataManagement/RolesController.php +++ b/app/Http/Controllers/Admin/DataManagement/RolesController.php @@ -107,10 +107,14 @@ public function update(Request $request, $id) */ public function destroy($id) { - Role::destroy($id); - - \Flash::success('Role deleted!'); - + $canBeDeleted = empty(Role::find($id)->available_appointment->toArray()); + if ($canBeDeleted) { + Role::destroy($id); + \Flash::success('Role deleted!'); + } else { + \Flash::error('Cannot delete because they are existing appointments for this role.'); + } + return redirect('admin/data-management/roles'); } diff --git a/tests/Admin/DataManagement/AvailableAppointmentsTableTest.php b/tests/Admin/DataManagement/AvailableAppointmentsTableTest.php index 4c8e665e..482f249c 100644 --- a/tests/Admin/DataManagement/AvailableAppointmentsTableTest.php +++ b/tests/Admin/DataManagement/AvailableAppointmentsTableTest.php @@ -223,9 +223,8 @@ public function testDeleteAvailableAppointment() 'fixture_id' => $appointment->fixture_id, 'role_id' => $appointment->role_id, ]) - ->call('DELETE', route(self::BASE_ROUTE . '.destroy', [$appointment->id])) - ->isRedirect(route(self::BASE_ROUTE . '.index')); - - $this->dontSeeInDatabase('available_appointments', ['id' => $appointmentId]); + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$appointment->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->dontSeeInDatabase('available_appointments', ['id' => $appointmentId]); } } diff --git a/tests/Admin/DataManagement/ClubsTableTest.php b/tests/Admin/DataManagement/ClubsTableTest.php index dbe9fe81..2f8689f8 100644 --- a/tests/Admin/DataManagement/ClubsTableTest.php +++ b/tests/Admin/DataManagement/ClubsTableTest.php @@ -8,6 +8,7 @@ namespace Admin\DataManagement; +use App\Models\Team; use Tests\TestCase; use App\Models\Club; @@ -144,9 +145,26 @@ public function testDeleteClub() 'id' => $club->id, 'club' => $club->club, ]) - ->call('DELETE', route(self::BASE_ROUTE . '.destroy', [$club->id])) - ->isRedirect(route(self::BASE_ROUTE . '.index')); + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$club->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-success', 'Club deleted!') + ->dontSeeInDatabase('clubs', ['id' => $clubId]); - $this->dontSeeInDatabase('clubs', ['id' => $clubId]); + // Delete a clubs with teams + /** @var Club $club */ + $club = factory(Club::class)->create(); + /** @var Team $team */ + $team = factory(Team::class)->create(['club_id' => $club->id]); + $this->seeInDatabase('clubs', [ + 'id' => $club->id, + 'club' => $club->club, + ]) + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$club->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-danger', 'Cannot delete because they are existing teams in this club.') + ->seeInDatabase('clubs', [ + 'id' => $club->id, + 'club' => $club->club, + ]); } } diff --git a/tests/Admin/DataManagement/DivisionsTableTest.php b/tests/Admin/DataManagement/DivisionsTableTest.php index 32696499..fcae02aa 100644 --- a/tests/Admin/DataManagement/DivisionsTableTest.php +++ b/tests/Admin/DataManagement/DivisionsTableTest.php @@ -8,6 +8,7 @@ namespace Admin\DataManagement; +use App\Models\Fixture; use Tests\TestCase; use App\Models\Division; use App\Models\Season; @@ -177,9 +178,29 @@ public function testDeleteDivision() 'season_id' => $division->season_id, 'division' => $division->division, ]) - ->call('DELETE', route(self::BASE_ROUTE . '.destroy', [$division->id])) - ->isRedirect(route(self::BASE_ROUTE . '.index')); + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$division->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-success', 'Division deleted!') + ->dontSeeInDatabase('divisions', ['id' => $divisionId]); + + // Delete division with fixtures + /** @var Division $division */ + $division = factory(Division::class)->create(); + /** @var Fixture $fixture */ + $fixture = factory(Fixture::class)->create(['division_id' => $division->id]); - $this->dontSeeInDatabase('divisions', ['id' => $divisionId]); + $this->seeInDatabase('divisions', [ + 'id' => $division->id, + 'season_id' => $division->season_id, + 'division' => $division->division, + ]) + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$division->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-danger', 'Cannot delete because they are existing fixtures in this division.') + ->seeInDatabase('divisions', [ + 'id' => $division->id, + 'season_id' => $division->season_id, + 'division' => $division->division, + ]); } } diff --git a/tests/Admin/DataManagement/FixturesTableTest.php b/tests/Admin/DataManagement/FixturesTableTest.php index a4ab2a4f..c3c52651 100644 --- a/tests/Admin/DataManagement/FixturesTableTest.php +++ b/tests/Admin/DataManagement/FixturesTableTest.php @@ -8,6 +8,7 @@ namespace Admin\DataManagement; +use App\Models\AvailableAppointment; use Tests\TestCase; use App\Models\Fixture; @@ -243,9 +244,41 @@ public function testDeleteFixture() 'warm_up_time' => $fixture->warm_up_time, 'start_time' => $fixture->start_time, ]) - ->call('DELETE', route(self::BASE_ROUTE . '.destroy', [$fixture->id])) - ->isRedirect(route(self::BASE_ROUTE . '.index')); + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$fixture->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-success', 'Fixture deleted!') + ->dontSeeInDatabase('fixtures', ['id' => $fixtureId]); + + // Delete fixture with existing appointment + /** @var Fixture $fixture */ + $fixture = factory(Fixture::class)->create(); + /** @var AvailableAppointment $appointment */ + $appointment = factory(AvailableAppointment::class)->create(['fixture_id' => $fixture->id]); - $this->dontSeeInDatabase('fixtures', ['id' => $fixtureId]); + $this->seeInDatabase('fixtures', [ + 'id' => $fixture->id, + 'division_id' => $fixture->division_id, + 'home_team_id' => $fixture->home_team_id, + 'away_team_id' => $fixture->away_team_id, + 'venue_id' => $fixture->venue_id, + 'match_number' => $fixture->match_number, + 'match_date' => $fixture->match_date->format('Y-m-d'), + 'warm_up_time' => $fixture->warm_up_time, + 'start_time' => $fixture->start_time, + ]) + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$fixture->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-danger', 'Cannot delete because they are existing appointments for this fixture.') + ->seeInDatabase('fixtures', [ + 'id' => $fixture->id, + 'division_id' => $fixture->division_id, + 'home_team_id' => $fixture->home_team_id, + 'away_team_id' => $fixture->away_team_id, + 'venue_id' => $fixture->venue_id, + 'match_number' => $fixture->match_number, + 'match_date' => $fixture->match_date->format('Y-m-d'), + 'warm_up_time' => $fixture->warm_up_time, + 'start_time' => $fixture->start_time, + ]); } } diff --git a/tests/Admin/DataManagement/RolesTableTest.php b/tests/Admin/DataManagement/RolesTableTest.php index ea705dfc..33cbfbad 100644 --- a/tests/Admin/DataManagement/RolesTableTest.php +++ b/tests/Admin/DataManagement/RolesTableTest.php @@ -8,6 +8,7 @@ namespace Admin\DataManagement; +use App\Models\AvailableAppointment; use Tests\TestCase; use App\Models\Role; @@ -144,9 +145,27 @@ public function testDeleteRole() 'id' => $role->id, 'role' => $role->role, ]) - ->call('DELETE', route(self::BASE_ROUTE . '.destroy', [$role->id])) - ->isRedirect(route(self::BASE_ROUTE . '.index')); + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$role->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-success', 'Role deleted!') + ->dontSeeInDatabase('roles', ['id' => $roleId]); + + // Delete a role with available appointments + /** @var Role $role */ + $role = factory(Role::class)->create(); + /** @var AvailableAppointment $appointment */ + $appointment = factory(AvailableAppointment::class)->create(['role_id' => $role->id]); - $this->dontSeeInDatabase('roles', ['id' => $roleId]); + $this->seeInDatabase('roles', [ + 'id' => $role->id, + 'role' => $role->role, + ]) + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$role->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-danger', 'Cannot delete because they are existing appointments for this role.') + ->seeInDatabase('roles', [ + 'id' => $role->id, + 'role' => $role->role, + ]); } } diff --git a/tests/Admin/DataManagement/SeasonsTableTest.php b/tests/Admin/DataManagement/SeasonsTableTest.php index a003026b..f80d9021 100644 --- a/tests/Admin/DataManagement/SeasonsTableTest.php +++ b/tests/Admin/DataManagement/SeasonsTableTest.php @@ -8,6 +8,8 @@ namespace Admin\DataManagement; +use App\Models\Division; +use PhpParser\Node\Expr\AssignOp\Div; use Tests\TestCase; use App\Models\Season; @@ -144,9 +146,27 @@ public function testDeleteSeason() 'id' => $season->id, 'season' => $season->season, ]) - ->call('DELETE', route(self::BASE_ROUTE . '.destroy', [$season->id])) - ->isRedirect(route(self::BASE_ROUTE . '.index')); + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$season->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-success', 'Season deleted!') + ->dontSeeInDatabase('seasons', ['id' => $seasonId]); + + // Delete a season with divisions + /** @var Season $season */ + $season = factory(Season::class)->create(); + /** @var Division $division */ + $division = factory(Division::class)->create(['season_id' => $season->id]); - $this->dontSeeInDatabase('seasons', ['id' => $seasonId]); + $this->seeInDatabase('seasons', [ + 'id' => $season->id, + 'season' => $season->season, + ]) + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$season->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-danger', 'Cannot delete because they are existing divisions in this season.') + ->seeInDatabase('seasons', [ + 'id' => $season->id, + 'season' => $season->season, + ]); } } diff --git a/tests/Admin/DataManagement/TeamsTableTest.php b/tests/Admin/DataManagement/TeamsTableTest.php index 20cf835d..6a9624d0 100644 --- a/tests/Admin/DataManagement/TeamsTableTest.php +++ b/tests/Admin/DataManagement/TeamsTableTest.php @@ -8,6 +8,7 @@ namespace Admin\DataManagement; +use App\Models\Fixture; use Tests\TestCase; use App\Models\Team; use App\Models\Club; @@ -175,9 +176,47 @@ public function testDeleteTeam() 'club_id' => $team->club_id, 'team' => $team->team, ]) - ->call('DELETE', route(self::BASE_ROUTE . '.destroy', [$team->id])) - ->isRedirect(route(self::BASE_ROUTE . '.index')); - - $this->dontSeeInDatabase('teams', ['id' => $teamId]); + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$team->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-success', 'Team deleted!') + ->dontSeeInDatabase('teams', ['id' => $teamId]); + + // Delete team with fixtures + /** @var Team $team */ + $team = factory(Team::class)->create(); + /** @var Fixture $fixture */ + $fixture = factory(Fixture::class)->create(['home_team_id' => $team->id]); + + $this->seeInDatabase('teams', [ + 'id' => $team->id, + 'club_id' => $team->club_id, + 'team' => $team->team, + ]) + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$team->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-danger', 'Cannot delete because they are existing fixtures for this team.') + ->seeInDatabase('teams', [ + 'id' => $team->id, + 'club_id' => $team->club_id, + 'team' => $team->team, + ]); + + Fixture::destroy($fixture->id); + /** @var Fixture $fixture */ + $fixture = factory(Fixture::class)->create(['away_team_id' => $team->id]); + + $this->seeInDatabase('teams', [ + 'id' => $team->id, + 'club_id' => $team->club_id, + 'team' => $team->team, + ]) + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$team->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-danger', 'Cannot delete because they are existing fixtures for this team.') + ->seeInDatabase('teams', [ + 'id' => $team->id, + 'club_id' => $team->club_id, + 'team' => $team->team, + ]); } } diff --git a/tests/Admin/DataManagement/VenuesTableTest.php b/tests/Admin/DataManagement/VenuesTableTest.php index 397da480..7fd8da3c 100644 --- a/tests/Admin/DataManagement/VenuesTableTest.php +++ b/tests/Admin/DataManagement/VenuesTableTest.php @@ -8,6 +8,7 @@ namespace Admin\DataManagement; +use App\Models\Fixture; use Tests\TestCase; use App\Models\Venue; @@ -144,9 +145,26 @@ public function testDeleteVenue() 'id' => $venue->id, 'venue' => $venue->venue, ]) - ->call('DELETE', route(self::BASE_ROUTE . '.destroy', [$venue->id])) - ->isRedirect(route(self::BASE_ROUTE . '.index')); + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$venue->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-success', 'Venue deleted!') + ->dontSeeInDatabase('venues', ['id' => $venueId]); + + // Delete a venue with fixtures + /** @var Venue $venue */ + $venue = factory(Venue::class)->create(); + $fixture = factory(Fixture::class)->create(['venue_id' => $venue->id]); - $this->dontSeeInDatabase('venues', ['id' => $venueId]); + $this->seeInDatabase('venues', [ + 'id' => $venue->id, + 'venue' => $venue->venue, + ]) + ->makeRequest('DELETE', route(self::BASE_ROUTE . '.destroy', [$venue->id])) + ->seePageIs(route(self::BASE_ROUTE . '.index')) + ->seeInElement('#flash-notification .alert.alert-danger', 'Cannot delete because they are existing fixtures at this venue.') + ->seeInDatabase('venues', [ + 'id' => $venue->id, + 'venue' => $venue->venue, + ]); } }