Passed
Pull Request — master (#18)
by Marc
03:37
created

BaseTestCase::createConnection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\IntegrationTest;
5
6
use Mockery;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\Serializer\Encoder\JsonEncoder;
9
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
10
use Symfony\Component\Serializer\Serializer;
11
use TBolier\RethinkQL\Connection\Connection;
12
use TBolier\RethinkQL\Connection\ConnectionInterface;
13
use TBolier\RethinkQL\Connection\Options;
14
use TBolier\RethinkQL\Connection\Socket\Handshake;
15
use TBolier\RethinkQL\Connection\Socket\Socket;
16
use TBolier\RethinkQL\Rethink;
17
use TBolier\RethinkQL\RethinkInterface;
18
use TBolier\RethinkQL\Serializer\QueryNormalizer;
19
use TBolier\RethinkQL\Types\VersionDummy\Version;
20
21
class BaseTestCase extends TestCase
22
{
23
    /**
24
     * @var RethinkInterface
25
     */
26
    private $r;
27
28
    /**
29
     * @var ConnectionInterface[]
30
     */
31
    private $connections = [];
32
33
    protected function setUp()
34
    {
35
        Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
36
37
        parent::setUp();
38
    }
39
40
    protected function r()
41
    {
42
        if ($this->r !== null) {
43
            return $this->r;
44
        }
45
46
        /** @var ConnectionInterface $connection */
47
        $connection = $this->createConnection('phpunit_default')->connect();
48
        $connection->connect()->use('test');
49
50
        $this->r = new Rethink($connection);
51
52
        if (!\in_array('test', $this->r->dbList()->run()->getData()[0], true)) {
53
            $this->r->dbCreate('test')->run();
54
        }
55
56
        return $this->r;
57
    }
58
59
    protected function tearDown()
60
    {
61
        if ($this->r !== null && \in_array('test',
62
                $this->r->dbList()->run()->getData()[0], true)) {
63
            $this->r->dbDrop('test')->run();
64
        }
65
    }
66
67
    /**
68
     * @param string $name
69
     * @return ConnectionInterface
70
     */
71
    protected function createConnection(string $name): ConnectionInterface
72
    {
73
        $options = new Options(PHPUNIT_CONNECTIONS[$name]);
74
75
        $connection = new Connection(
76
            function () use ($options) {
77
                return new Socket(
78
                    $options
79
                );
80
            },
81
            new Handshake($options->getUser(), $options->getPassword(), Version::V1_0),
82
            $options->getDbName(),
83
            new Serializer(
84
                [new QueryNormalizer()],
85
                [new JsonEncoder()]
86
            ),
87
            new Serializer(
88
                [new ObjectNormalizer()],
89
                [new JsonEncoder()]
90
            )
91
        );
92
93
        $this->connections[] = $connection;
94
95
        return $connection;
96
    }
97
98
    public function __destruct()
99
    {
100
        foreach ($this->connections as $connection) {
101
            $connection->close();
102
        }
103
104
        Mockery::close();
105
    }
106
}
107