Passed
Push — main ( 5011e1...c36c7b )
by Alex
04:43
created

DatabaseHelperTrait::truncateTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
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($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.");
0 ignored issues
show
Bug introduced by
It seems like assertSame() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $this->/** @scrutinizer ignore-call */ 
37
               assertSame(0, $count, "The '$tableName' table is not empty.");
Loading history...
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.");
0 ignored issues
show
Bug introduced by
It seems like assertGreaterThan() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $this->/** @scrutinizer ignore-call */ 
52
               assertGreaterThan(0, $count, "The '$tableName' table is empty.");
Loading history...
52
    }
53
54
    private function getEntityNameSpace(EntityManagerInterface $entityManager, string $tableName): string
55
    {
56
        $configuration = $entityManager->getConfiguration();
57
        $entityNamespaces = $configuration->getEntityNamespaces();
58
        foreach ($entityNamespaces as $namespace) {
59
            $entityClass = $namespace . '\\' . ucfirst($tableName); // Assume that the name's first character should be uppercase
60
61
            if (class_exists($entityClass)) {
62
                return $entityClass;
63
            }
64
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
65
    }
66
}
67