1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Functional\EventListener; |
4
|
|
|
|
5
|
|
|
use App\Entity\Employee; |
6
|
|
|
use App\Entity\Performance; |
7
|
|
|
use App\Repository\EmployeeRepository; |
8
|
|
|
use App\Repository\PerformanceRepository; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
13
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
14
|
|
|
use Symfony\Component\Security\Core\User\User; |
15
|
|
|
|
16
|
|
|
class EntityDeleteListenerTest extends WebTestCase |
17
|
|
|
{ |
18
|
|
|
protected $user; |
19
|
|
|
/** @var EntityManager */ |
20
|
|
|
protected $em; |
21
|
|
|
|
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
static::bootKernel(); |
25
|
|
|
$container = self::$container; |
26
|
|
|
$this->em = $container->get(EntityManagerInterface::class); |
27
|
|
|
|
28
|
|
|
// login |
29
|
|
|
$tokenStorage = $container->get(TokenStorageInterface::class); |
30
|
|
|
$firewallName = 'secure_area'; |
31
|
|
|
$this->user = new User('admin', '123456'); |
32
|
|
|
$token = new UsernamePasswordToken($this->user, null, $firewallName, ['ROLE_ADMIN']); |
33
|
|
|
$tokenStorage->setToken($token); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testEmployeeSoftDeletable() |
37
|
|
|
{ |
38
|
|
|
$container = self::$container; |
39
|
|
|
$employeeRepository = $container->get(EmployeeRepository::class); |
40
|
|
|
|
41
|
|
|
/** @var Employee $employee */ |
42
|
|
|
$employee = $employeeRepository->findOneBy([]); |
43
|
|
|
$this->assertEquals(Employee::class, get_class($employee)); |
44
|
|
|
$this->assertNull($employee->getDeletedAt()); |
45
|
|
|
$this->assertNull($employee->getDeletedBy()); |
46
|
|
|
|
47
|
|
|
$this->em->remove($employee); |
48
|
|
|
$this->em->flush(); |
49
|
|
|
|
50
|
|
|
$this->assertNotNull($employee->getDeletedAt()); |
51
|
|
|
$this->assertEquals($this->user->getUsername(), $employee->getDeletedBy()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testCascadeDeleteRoles() |
55
|
|
|
{ |
56
|
|
|
$container = self::$container; |
57
|
|
|
$performanceRepository = $container->get(PerformanceRepository::class); |
58
|
|
|
/** @var Performance $performance */ |
59
|
|
|
$performance = $performanceRepository->findOneBy([]); |
60
|
|
|
|
61
|
|
|
$this->assertEquals(Performance::class, get_class($performance)); |
62
|
|
|
$this->assertNull($performance->getDeletedAt()); |
63
|
|
|
$this->assertNull($performance->getDeletedBy()); |
64
|
|
|
|
65
|
|
|
$this->em->remove($performance); |
66
|
|
|
$this->em->flush(); |
67
|
|
|
|
68
|
|
|
$this->assertNotNull($performance->getDeletedAt()); |
69
|
|
|
$this->assertEquals($this->user->getUsername(), $performance->getDeletedBy()); |
70
|
|
|
|
71
|
|
|
$this->assertNotNull($performance->getRoles()[0]->getDeletedAt()); |
72
|
|
|
$this->assertEquals($this->user->getUsername(), $performance->getRoles()[0]->getDeletedBy()); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|