TestWithTransaction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 27
ccs 0
cts 9
cp 0
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A getEntityManager() 0 3 1
A tearDown() 0 5 1
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