BaseTestCase::clearDatabase()   A
last analyzed

Complexity

Conditions 2
Paths 7

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 21
rs 9.7666
c 0
b 0
f 0
cc 2
nc 7
nop 0
1
<?php
2
3
namespace Tests\Unit;
4
5
use Exception;
6
use Potievdev\SlimRbac\Component\RbacAccessChecker;
7
use Potievdev\SlimRbac\Component\RbacContainer;
8
use Potievdev\SlimRbac\Component\RbacManager;
9
use Potievdev\SlimRbac\Exception\DatabaseException;
10
use Potievdev\SlimRbac\Models\RepositoryRegistry;
11
12
/**
13
 * Class BaseTestCase
14
 * @package Tests\Unit
15
 */
16
class BaseTestCase extends \PHPUnit_Framework_TestCase
17
{
18
    /** Moderator user identifier */
19
    const MODERATOR_USER_ID = 1;
20
    /** Admin user identifier */
21
    const ADMIN_USER_ID = 2;
22
    /** User with this id not exists in database */
23
    const NOT_USER_ID = 3;
24
25
    /** @var RbacManager $rbacManager */
26
    protected $rbacManager;
27
28
    /** @var RepositoryRegistry $repositoryRegistry */
29
    protected $repositoryRegistry;
30
31
    /** @var RbacAccessChecker $accessChecker */
32
    protected $accessChecker;
33
34
    protected $rbacContainer;
35
36
    /**
37
     * Initializing RbacManager.
38
     * @throws DatabaseException
39
     */
40
    public function setUp(): void
41
    {
42
        $this->rbacContainer = new RbacContainer();
43
        $this->rbacManager = $this->rbacContainer->getRbacManager();
44
        $this->repositoryRegistry = $this->rbacContainer->getInnerContainer()->get('repositoryRegistry');
45
        $this->accessChecker = $this->rbacContainer->getInnerContainer()->get('accessChecker');
46
        $this->clearDatabase();
47
    }
48
49
    /**
50
     * @throws DatabaseException
51
     */
52
    private function clearDatabase(): void
53
    {
54
        $pdo = $this->rbacContainer->getInnerContainer()
55
            ->get('entityManager')
56
            ->getConnection()
57
            ->getNativeConnection();
58
59
        $pdo->beginTransaction();
60
61
        try {
62
            $pdo->exec('DELETE FROM role_permission WHERE 1 > 0');
63
            $pdo->exec('DELETE FROM role_hierarchy WHERE 1 > 0');
64
            $pdo->exec('DELETE FROM permission WHERE 1 > 0');
65
            $pdo->exec('DELETE FROM user_role WHERE 1 > 0');
66
            $pdo->exec('DELETE FROM role WHERE 1 > 0');
67
68
            $pdo->commit();
69
70
        } catch (Exception $e) {
71
            $pdo->rollBack();
72
            throw new DatabaseException($e->getMessage());
73
        }
74
    }
75
}
76