|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
|
|
7
|
|
|
trait DatabaseHelperTrait |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Truncates the specified table to remove all data. |
|
11
|
|
|
* |
|
12
|
|
|
* @param EntityManagerInterface $entityManager The entity manager instance. |
|
13
|
|
|
* @param string $tableName The name of the table to truncate. |
|
14
|
|
|
*/ |
|
15
|
|
|
private function truncateTable(EntityManagerInterface $entityManager, string $tableName): void |
|
16
|
|
|
{ |
|
17
|
|
|
$connection = $entityManager->getConnection(); |
|
18
|
|
|
$platform = $connection->getDatabasePlatform(); |
|
19
|
|
|
|
|
20
|
|
|
// Execute a truncate table SQL statement for the specified table |
|
21
|
|
|
$connection->executeQuery($platform->getTruncateTableSQL(ucfirst($tableName), true)); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Asserts that the specified table is empty. |
|
26
|
|
|
* |
|
27
|
|
|
* @param EntityManagerInterface $entityManager The entity manager instance. |
|
28
|
|
|
* @param string $tableName The name of the table to check. |
|
29
|
|
|
*/ |
|
30
|
|
|
private function assertEmptyTable(EntityManagerInterface $entityManager, string $tableName): void |
|
31
|
|
|
{ |
|
32
|
|
|
$entityNamespace = $this->getEntityNameSpace($entityManager, $tableName); |
|
33
|
|
|
$query = $entityManager->createQuery("SELECT COUNT(e) FROM {$entityNamespace} e"); |
|
34
|
|
|
$count = $query->getSingleScalarResult(); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertSame(0, $count, "The '$tableName' table is not empty."); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Asserts that the specified table is not empty. |
|
41
|
|
|
* |
|
42
|
|
|
* @param EntityManagerInterface $entityManager The entity manager instance. |
|
43
|
|
|
* @param string $tableName The name of the table to check. |
|
44
|
|
|
*/ |
|
45
|
|
|
private function assertNonEmptyTable(EntityManagerInterface $entityManager, string $tableName): void |
|
46
|
|
|
{ |
|
47
|
|
|
$entityNamespace = $this->getEntityNameSpace($entityManager, $tableName); |
|
48
|
|
|
$query = $entityManager->createQuery("SELECT COUNT(e) FROM {$entityNamespace} e"); |
|
49
|
|
|
$count = $query->getSingleScalarResult(); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertGreaterThan(0, $count, "The '$tableName' table is empty."); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Retrieves the namespace for the given table. |
|
56
|
|
|
* |
|
57
|
|
|
* @param EntityManagerInterface $entityManager The entity manager instance. |
|
58
|
|
|
* @param string $tableName The name associated with the entity to retrieve. |
|
59
|
|
|
* |
|
60
|
|
|
* @return string|null The namespace if found, or null if not found. |
|
61
|
|
|
*/ |
|
62
|
|
|
private function getEntityNameSpace(EntityManagerInterface $entityManager, string $tableName): ?string |
|
63
|
|
|
{ |
|
64
|
|
|
$configuration = $entityManager->getConfiguration(); |
|
65
|
|
|
$entityNamespaces = $configuration->getEntityNamespaces(); |
|
66
|
|
|
foreach ($entityNamespaces as $namespace) { |
|
67
|
|
|
$entityClass = $namespace . '\\' . ucfirst($tableName); // Assume that the name's first character should be uppercase |
|
68
|
|
|
|
|
69
|
|
|
if (class_exists($entityClass)) { |
|
70
|
|
|
return $entityClass; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return null; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|