Passed
Pull Request — master (#83)
by Wilmer
24:07 queued 09:08
created

TestCase::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 2
b 1
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Tests;
6
7
use Yiisoft\ActiveRecord\Tests\Stubs\Connection\MysqlConnection;
8
use Yiisoft\ActiveRecord\Tests\Stubs\Connection\PgsqlConnection;
9
use Yiisoft\ActiveRecord\Tests\Stubs\Connection\SqliteConnection;
10
use Yiisoft\Composer\Config\Builder;
11
use Yiisoft\Db\Connection\Connection;
12
use Yiisoft\Di\Container;
13
14
class TestCase extends \PHPUnit\Framework\TestCase
15
{
16
    protected ?Connection $mysqlConnection = null;
17
    protected ?Connection $pgsqlConnection = null;
18
    protected ?Connection $sqliteConnection = null;
19
20
    protected function setUp(): void
21
    {
22
        parent::setUp();
23
24
        $this->configContainer();
25
    }
26
27
    protected function tearDown(): void
28
    {
29
        parent::tearDown();
30
    }
31
32
    protected function configContainer(): void
33
    {
34
        $config = require Builder::path('tests');
35
36
        $this->container = new Container($config);
0 ignored issues
show
Bug Best Practice introduced by
The property container does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
38
        $this->mysqlConnection = $this->container->get(MysqlConnection::class);
39
        $this->pgsqlConnection = $this->container->get(PgsqlConnection::class);
40
        $this->sqliteConnection = $this->container->get(SqliteConnection::class);
41
    }
42
43
    /**
44
     * Invokes a inaccessible method.
45
     *
46
     * @param $object
47
     * @param $method
48
     * @param array $args
49
     * @param bool $revoke whether to make method inaccessible after execution
50
     *
51
     * @return mixed
52
     */
53
    protected function invokeMethod($object, $method, $args = [], $revoke = true)
54
    {
55
        $reflection = new \ReflectionObject($object);
56
        $method = $reflection->getMethod($method);
57
        $method->setAccessible(true);
58
        $result = $method->invokeArgs($object, $args);
59
60
        if ($revoke) {
61
            $method->setAccessible(false);
62
        }
63
64
        return $result;
65
    }
66
67
    protected function loadFixture(Connection $db): void
68
    {
69
        if ($db->isActive()) {
70
            $db->close();
71
        }
72
73
        $db->open();
74
75
        if ($this->driverName === 'oci') {
76
            [$drops, $creates] = explode('/* STATEMENTS */', file_get_contents($db->getFixture()), 2);
0 ignored issues
show
introduced by
The method getFixture() does not exist on Yiisoft\Db\Connection\Connection. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

76
            [$drops, $creates] = explode('/* STATEMENTS */', file_get_contents($db->/** @scrutinizer ignore-call */ getFixture()), 2);
Loading history...
77
            [$statements, $triggers, $data] = explode('/* TRIGGERS */', $creates, 3);
78
            $lines = array_merge(
79
                explode('--', $drops),
80
                explode(';', $statements),
81
                explode('/', $triggers),
82
                explode(';', $data)
83
            );
84
        } else {
85
            $lines = explode(';', file_get_contents($db->getFixture()));
86
        }
87
88
        foreach ($lines as $line) {
89
            if (trim($line) !== '') {
90
                $db->getPDO()->exec($line);
91
            }
92
        }
93
    }
94
95
    /**
96
     * Adjust dbms specific escaping.
97
     *
98
     * @param $sql
99
     *
100
     * @return mixed
101
     */
102
    protected function replaceQuotes($sql)
103
    {
104
        switch ($this->driverName) {
105
            case 'mysql':
106
            case 'sqlite':
107
                return str_replace(['[[', ']]'], '`', $sql);
108
            case 'oci':
109
                return str_replace(['[[', ']]'], '"', $sql);
110
            case 'pgsql':
111
                // more complex replacement needed to not conflict with postgres array syntax
112
                return str_replace(['\\[', '\\]'], ['[', ']'], preg_replace('/(\[\[)|((?<!(\[))\]\])/', '"', $sql));
113
            case 'sqlsrv':
114
                return str_replace(['[[', ']]'], ['[', ']'], $sql);
115
            default:
116
                return $sql;
117
        }
118
    }
119
}
120