Skip to content

Commit

Permalink
Updated cascade entity and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien70 authored and jmsche committed Aug 24, 2021
1 parent 159d49a commit d02f296
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
37 changes: 37 additions & 0 deletions migrations/Version20210824073653.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210824073653 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE backup DROP FOREIGN KEY FK_3FF0D1ACF0AA09DB');
$this->addSql('ALTER TABLE backup ADD CONSTRAINT FK_3FF0D1ACF0AA09DB FOREIGN KEY (database_id) REFERENCES `database` (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE `database` DROP FOREIGN KEY FK_C953062E7E3C61F9');
$this->addSql('ALTER TABLE `database` ADD CONSTRAINT FK_C953062E7E3C61F9 FOREIGN KEY (owner_id) REFERENCES user (id) ON DELETE CASCADE');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE backup DROP FOREIGN KEY FK_3FF0D1ACF0AA09DB');
$this->addSql('ALTER TABLE backup ADD CONSTRAINT FK_3FF0D1ACF0AA09DB FOREIGN KEY (database_id) REFERENCES `database` (id)');
$this->addSql('ALTER TABLE `database` DROP FOREIGN KEY FK_C953062E7E3C61F9');
$this->addSql('ALTER TABLE `database` ADD CONSTRAINT FK_C953062E7E3C61F9 FOREIGN KEY (owner_id) REFERENCES user (id)');
}
}
2 changes: 1 addition & 1 deletion src/Entity/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Backup implements \Stringable
private ?string $originalName = null;

#[ORM\ManyToOne(targetEntity: Database::class, inversedBy: 'backups')]
#[ORM\JoinColumn(nullable: false)]
#[ORM\JoinColumn(nullable: false, onDelete: 'cascade')]
private Database $database;

public function __construct()
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Database implements \Stringable
#[ORM\Column(type: 'integer')]
private int $maxBackups;

#[ORM\OneToMany(mappedBy: 'database', targetEntity: Backup::class, cascade: ['remove'], orphanRemoval: true)]
#[ORM\OneToMany(mappedBy: 'database', targetEntity: Backup::class, orphanRemoval: true)]
private Collection $backups;

#[ORM\Column(type: 'datetime')]
Expand Down
24 changes: 24 additions & 0 deletions tests/Controller/Admin/UserCrudControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ public function testEditWithAdminUser(): void
self::assertResponseIsSuccessful('User with ROLE_ADMIN could edit another User');
}

public function testDeleteWithAdminUser(): void
{
$url = $this->getActionUrl(Action::DELETE, self::USER_ROLE_USER);

self::$client->request('GET', $url);
self::assertResponseRedirects('/');

$this->loginAsAdmin();
self::$client->request('GET', $url);
self::assertResponseRedirects();
}

public function testDeleteWithSimpleUser(): void
{
$url = $this->getActionUrl(Action::DELETE, self::USER_ROLE_ADMIN);

self::$client->request('GET', $url);
self::assertResponseRedirects('/');

$this->loginAsUser();
self::$client->request('GET', $url);
self::assertResponseRedirects();
}

protected function getControllerClass(): string
{
return UserCrudController::class;
Expand Down

0 comments on commit d02f296

Please sign in to comment.