BaseTestCase   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 3
eloc 28
dl 0
loc 57
rs 10
c 5
b 1
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A clearDatabase() 0 21 2
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