AbstractTestCase::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace TBolier\RethinkQL\IntegrationTest;
5
6
use ArrayObject;
7
use Mockery;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Serializer\Encoder\JsonEncoder;
10
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
11
use Symfony\Component\Serializer\Serializer;
12
use TBolier\RethinkQL\Connection\Connection;
13
use TBolier\RethinkQL\Connection\ConnectionInterface;
14
use TBolier\RethinkQL\Connection\Options;
15
use TBolier\RethinkQL\Connection\Socket\Exception;
16
use TBolier\RethinkQL\Connection\Socket\Handshake;
17
use TBolier\RethinkQL\Connection\Socket\Socket;
18
use TBolier\RethinkQL\Response\ResponseInterface;
19
use TBolier\RethinkQL\Rethink;
20
use TBolier\RethinkQL\RethinkInterface;
21
use TBolier\RethinkQL\Serializer\QueryNormalizer;
22
use TBolier\RethinkQL\Types\VersionDummy\Version;
23
24
abstract class AbstractTestCase extends TestCase
25
{
26
    /**
27
     * @var RethinkInterface
28
     */
29
    private $r;
30
31
    /**
32
     * @var ConnectionInterface[]
33
     */
34
    private $connections = [];
35
36
    protected function setUp()
37
    {
38
        Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
39
40
        // Make sure we destroy a failed previous unit test database schema
41
        $this->tearDown();
42
43
        parent::setUp();
44
    }
45
46
    protected function r()
47
    {
48
        if ($this->r !== null) {
49
            return $this->r;
50
        }
51
52
        $name = 'phpunit_default';
53
        $options = new Options(PHPUNIT_CONNECTIONS[$name]);
54
55
        /** @var ConnectionInterface $connection */
56
        $connection = $this->createConnection($name)->connect();
57
        $connection->connect()->use($options->getDbName());
58
59
        $this->r = new Rethink($connection);
60
61
        /** @var ResponseInterface $res */
62
        $res = $this->r->dbList()->run();
63
        if (\is_array($res->getData()) && !\in_array($options->getDbName(), $res->getData(), true)) {
64
            $this->r->dbCreate($options->getDbName())->run();
65
        }
66
67
        return $this->r;
68
    }
69
70
    protected function tearDown()
71
    {
72
        if ($this->r === null) {
73
            return;
74
        }
75
76
        $name = 'phpunit_default';
77
        $options = new Options(PHPUNIT_CONNECTIONS[$name]);
78
79
        /** @var ResponseInterface $res */
80
        $res = $this->r->dbList()->run();
81
        if (\is_array($res->getData()) && \in_array($options->getDbName(), $res->getData(), true)) {
82
            $this->r->dbDrop($options->getDbName())->run();
83
        }
84
    }
85
86
    /**
87
     * @param string $name
88
     * @return ConnectionInterface
89
     * @throws Exception
90
     */
91
    protected function createConnection(string $name): ConnectionInterface
92
    {
93
        $options = new Options(PHPUNIT_CONNECTIONS[$name]);
94
95
        $connection = new Connection(
96
            function () use ($options) {
97
                return new Socket(
98
                    $options
99
                );
100
            },
101
            new Handshake($options->getUser(), $options->getPassword(), Version::V1_0),
102
            $options->getDbName(),
103
            new Serializer(
104
                [new QueryNormalizer()],
105
                [new JsonEncoder()]
106
            ),
107
            new Serializer(
108
                [new ObjectNormalizer()],
109
                [new JsonEncoder()]
110
            )
111
        );
112
113
        $this->connections[] = $connection;
114
115
        return $connection;
116
    }
117
118
    public function __destruct()
119
    {
120
        foreach ($this->connections as $connection) {
121
            $connection->close();
122
        }
123
124
        Mockery::close();
125
    }
126
127
    /**
128
     * @param $status
129
     * @param $data
130
     * @throws \Exception
131
     */
132
    protected function assertObStatus($status, $data)
133
    {
134
        $res = [];
135
        $statuses = [
136
            'tables_created',
137
            'tables_dropped',
138
            'created',
139
            'dropped',
140
            'renamed',
141
            'unchanged',
142
            'skipped',
143
            'replaced',
144
            'inserted',
145
            'errors',
146
            'deleted',
147
        ];
148
        $data = new ArrayObject($data);
149
150
        foreach ($statuses as $s) {
151
            $status[$s] = $status[$s] ?? 0;
152
        }
153
154
        $data->setFlags($data::ARRAY_AS_PROPS);
155
156
        foreach ($statuses as $s) {
157
            $res[$s] = $data[$s] ?? 0;
158
        }
159
160
        $this->assertEquals($status, $res);
161
    }
162
}
163