Skip to content

Commit d02f296

Browse files
bastien70jmsche
authored andcommitted
Updated cascade entity and added tests
1 parent 159d49a commit d02f296

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20210824073653 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('ALTER TABLE backup DROP FOREIGN KEY FK_3FF0D1ACF0AA09DB');
24+
$this->addSql('ALTER TABLE backup ADD CONSTRAINT FK_3FF0D1ACF0AA09DB FOREIGN KEY (database_id) REFERENCES `database` (id) ON DELETE CASCADE');
25+
$this->addSql('ALTER TABLE `database` DROP FOREIGN KEY FK_C953062E7E3C61F9');
26+
$this->addSql('ALTER TABLE `database` ADD CONSTRAINT FK_C953062E7E3C61F9 FOREIGN KEY (owner_id) REFERENCES user (id) ON DELETE CASCADE');
27+
}
28+
29+
public function down(Schema $schema): void
30+
{
31+
// this down() migration is auto-generated, please modify it to your needs
32+
$this->addSql('ALTER TABLE backup DROP FOREIGN KEY FK_3FF0D1ACF0AA09DB');
33+
$this->addSql('ALTER TABLE backup ADD CONSTRAINT FK_3FF0D1ACF0AA09DB FOREIGN KEY (database_id) REFERENCES `database` (id)');
34+
$this->addSql('ALTER TABLE `database` DROP FOREIGN KEY FK_C953062E7E3C61F9');
35+
$this->addSql('ALTER TABLE `database` ADD CONSTRAINT FK_C953062E7E3C61F9 FOREIGN KEY (owner_id) REFERENCES user (id)');
36+
}
37+
}

src/Entity/Backup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Backup implements \Stringable
5555
private ?string $originalName = null;
5656

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

6161
public function __construct()

src/Entity/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Database implements \Stringable
3737
#[ORM\Column(type: 'integer')]
3838
private int $maxBackups;
3939

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

4343
#[ORM\Column(type: 'datetime')]

tests/Controller/Admin/UserCrudControllerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,30 @@ public function testEditWithAdminUser(): void
101101
self::assertResponseIsSuccessful('User with ROLE_ADMIN could edit another User');
102102
}
103103

104+
public function testDeleteWithAdminUser(): void
105+
{
106+
$url = $this->getActionUrl(Action::DELETE, self::USER_ROLE_USER);
107+
108+
self::$client->request('GET', $url);
109+
self::assertResponseRedirects('/');
110+
111+
$this->loginAsAdmin();
112+
self::$client->request('GET', $url);
113+
self::assertResponseRedirects();
114+
}
115+
116+
public function testDeleteWithSimpleUser(): void
117+
{
118+
$url = $this->getActionUrl(Action::DELETE, self::USER_ROLE_ADMIN);
119+
120+
self::$client->request('GET', $url);
121+
self::assertResponseRedirects('/');
122+
123+
$this->loginAsUser();
124+
self::$client->request('GET', $url);
125+
self::assertResponseRedirects();
126+
}
127+
104128
protected function getControllerClass(): string
105129
{
106130
return UserCrudController::class;

0 commit comments

Comments
 (0)