Skip to content

Commit

Permalink
dev: added test for cascading delete
Browse files Browse the repository at this point in the history
  • Loading branch information
troccoli committed May 14, 2016
1 parent bdbf501 commit da5af88
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 34 deletions.
11 changes: 7 additions & 4 deletions app/Http/Controllers/Admin/DataManagement/FixturesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
12 changes: 8 additions & 4 deletions app/Http/Controllers/Admin/DataManagement/RolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
7 changes: 3 additions & 4 deletions tests/Admin/DataManagement/AvailableAppointmentsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
24 changes: 21 additions & 3 deletions tests/Admin/DataManagement/ClubsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Admin\DataManagement;

use App\Models\Team;
use Tests\TestCase;
use App\Models\Club;

Expand Down Expand Up @@ -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,
]);
}
}
27 changes: 24 additions & 3 deletions tests/Admin/DataManagement/DivisionsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Admin\DataManagement;

use App\Models\Fixture;
use Tests\TestCase;
use App\Models\Division;
use App\Models\Season;
Expand Down Expand Up @@ -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,
]);
}
}
39 changes: 36 additions & 3 deletions tests/Admin/DataManagement/FixturesTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Admin\DataManagement;

use App\Models\AvailableAppointment;
use Tests\TestCase;
use App\Models\Fixture;

Expand Down Expand Up @@ -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,
]);
}
}
25 changes: 22 additions & 3 deletions tests/Admin/DataManagement/RolesTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Admin\DataManagement;

use App\Models\AvailableAppointment;
use Tests\TestCase;
use App\Models\Role;

Expand Down Expand Up @@ -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,
]);
}
}
26 changes: 23 additions & 3 deletions tests/Admin/DataManagement/SeasonsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Admin\DataManagement;

use App\Models\Division;
use PhpParser\Node\Expr\AssignOp\Div;
use Tests\TestCase;
use App\Models\Season;

Expand Down Expand Up @@ -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,
]);
}
}
47 changes: 43 additions & 4 deletions tests/Admin/DataManagement/TeamsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Admin\DataManagement;

use App\Models\Fixture;
use Tests\TestCase;
use App\Models\Team;
use App\Models\Club;
Expand Down Expand Up @@ -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,
]);
}
}
24 changes: 21 additions & 3 deletions tests/Admin/DataManagement/VenuesTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Admin\DataManagement;

use App\Models\Fixture;
use Tests\TestCase;
use App\Models\Venue;

Expand Down Expand Up @@ -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,
]);
}
}

0 comments on commit da5af88

Please sign in to comment.