TestWithTransaction::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Testing\Traits;
6
7
use Doctrine\ORM\EntityManager;
8
9
/**
10
 * Allow to run test within a database transaction, so database will be unchanged after test.
11
 */
12
trait TestWithTransaction
13
{
14
    /**
15
     * Get EntityManager.
16
     */
17
    public function getEntityManager(): EntityManager
18
    {
19
        return _em();
20
    }
21
22
    /**
23
     * Start transaction.
24
     */
25
    protected function setUp(): void
26
    {
27
        $this->getEntityManager()->clear();
28
        $this->getEntityManager()->beginTransaction();
29
    }
30
31
    /**
32
     * Cancel transaction, to undo all changes made.
33
     */
34
    protected function tearDown(): void
35
    {
36
        $this->getEntityManager()->rollback();
37
        $this->getEntityManager()->clear();
38
        $this->getEntityManager()->getConnection()->close();
39
    }
40
}
41