Passed
Pull Request — master (#28)
by Timon
04:16 queued 42s
created

AbstractTestCase::createConnection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 1
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\Rethink;
19
use TBolier\RethinkQL\RethinkInterface;
20
use TBolier\RethinkQL\Serializer\QueryNormalizer;
21
use TBolier\RethinkQL\Types\VersionDummy\Version;
22
23
abstract class AbstractTestCase extends TestCase
24
{
25
    /**
26
     * @var RethinkInterface
27
     */
28
    private $r;
29
30
    /**
31
     * @var ConnectionInterface[]
32
     */
33
    private $connections = [];
34
35
    protected function setUp()
36
    {
37
        Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
38
39
        parent::setUp();
40
    }
41
42
    protected function r()
43
    {
44
        if ($this->r !== null) {
45
            return $this->r;
46
        }
47
48
        /** @var ConnectionInterface $connection */
49
        $connection = $this->createConnection('phpunit_default')->connect();
50
        $connection->connect()->use('test');
51
52
        $this->r = new Rethink($connection);
53
54
        if (!\in_array('test', $this->r->dbList()->run()->getData(), true)) {
0 ignored issues
show
Bug introduced by
It seems like $this->r->dbList()->run()->getData() can also be of type string; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

54
        if (!\in_array('test', /** @scrutinizer ignore-type */ $this->r->dbList()->run()->getData(), true)) {
Loading history...
55
            $this->r->dbCreate('test')->run();
56
        }
57
58
        return $this->r;
59
    }
60
61
    protected function tearDown()
62
    {
63
        if ($this->r !== null && \in_array('test',
64
                $this->r->dbList()->run()->getData(), true)) {
0 ignored issues
show
Bug introduced by
It seems like $this->r->dbList()->run()->getData() can also be of type string; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

64
                /** @scrutinizer ignore-type */ $this->r->dbList()->run()->getData(), true)) {
Loading history...
65
            $this->r->dbDrop('test')->run();
66
        }
67
    }
68
69
    /**
70
     * @param string $name
71
     * @return ConnectionInterface
72
     * @throws Exception
73
     */
74
    protected function createConnection(string $name): ConnectionInterface
75
    {
76
        $options = new Options(PHPUNIT_CONNECTIONS[$name]);
77
78
        $connection = new Connection(
79
            function () use ($options) {
80
                return new Socket(
81
                    $options
82
                );
83
            },
84
            new Handshake($options->getUser(), $options->getPassword(), Version::V1_0),
85
            $options->getDbName(),
86
            new Serializer(
87
                [new QueryNormalizer()],
88
                [new JsonEncoder()]
89
            ),
90
            new Serializer(
91
                [new ObjectNormalizer()],
92
                [new JsonEncoder()]
93
            )
94
        );
95
96
        $this->connections[] = $connection;
97
98
        return $connection;
99
    }
100
101
    public function __destruct()
102
    {
103
        foreach ($this->connections as $connection) {
104
            $connection->close();
105
        }
106
107
        Mockery::close();
108
    }
109
110
    /**
111
     * @param $status
112
     * @param $data
113
     * @throws \Exception
114
     */
115
    protected function assertObStatus($status, $data)
116
    {
117
        $res = [];
118
        $statuses = [
119
            'tables_created',
120
            'tables_dropped',
121
            'unchanged',
122
            'skipped',
123
            'replaced',
124
            'inserted',
125
            'errors',
126
            'deleted',
127
        ];
128
        $data = new ArrayObject($data);
129
130
        foreach ($statuses as $s) {
131
            $status[$s] = $status[$s] ?? 0;
132
        }
133
134
        $data->setFlags($data::ARRAY_AS_PROPS);
135
136
        foreach ($statuses as $s) {
137
            $res[$s] = $data[$s] ?? 0;
138
        }
139
140
        $this->assertEquals($status, $res);
141
    }
142
}
143