Passed
Push — master ( ae31ea...e44bbb )
by Michel
02:38
created

BaseTestCase   B

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 16
dl 0
loc 88
rs 8.4614
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A r() 0 18 3
A tearDown() 0 7 4
B createConnection() 0 26 1
A __destruct() 0 10 3
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\JsonSerializableNormalizer;
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\Handshake;
16
use TBolier\RethinkQL\Connection\Socket\Socket;
17
use TBolier\RethinkQL\Rethink;
18
use TBolier\RethinkQL\RethinkInterface;
19
use TBolier\RethinkQL\Serializer\QueryNormalizer;
20
use TBolier\RethinkQL\Types\VersionDummy\Version;
21
22
class BaseTestCase extends TestCase
23
{
24
    /**
25
     * @var RethinkInterface
26
     */
27
    private $r;
28
29
    /**
30
     * @var ConnectionInterface[]
31
     */
32
    private $connections;
33
34
    protected function setUp()
35
    {
36
        Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
37
38
        parent::setUp();
39
    }
40
41
    protected function r()
42
    {
43
        if ($this->r !== null) {
44
            return $this->r;
45
        }
46
47
        /** @var ConnectionInterface $connection */
48
        $connection = $this->createConnection('phpunit_default')->connect();
49
        $connection->connect()->use('test');
50
51
        $this->r = new Rethink($connection);
52
53
        if (!\in_array('test', $this->r->dbList()->run()->getData()[0])) {
54
            $this->r->dbCreate('test')->run();
55
        }
56
57
        return $this->r;
58
    }
59
60
    protected function tearDown()
61
    {
62
        if ($this->r !== null && $this->r->connection()->isStreamOpen() && \in_array('test',
63
                $this->r->dbList()->run()->getData()[0], true)) {
64
            $this->r->dbDrop('test')->run();
65
        }
66
    }
67
68
    /**
69
     * @param string $name
70
     * @return ConnectionInterface
71
     */
72
    protected function createConnection(string $name): ConnectionInterface
73
    {
74
        $options = new Options(PHPUNIT_CONNECTIONS[$name]);
75
76
        $connection = new Connection(
77
            function () use ($options) {
78
                return new Socket(
79
                    $options
80
                );
81
            },
82
            new Handshake($options->getUser(), $options->getPassword(), Version::V1_0),
83
            $options->getDbname(),
84
            new Serializer(
85
                [new QueryNormalizer()],
86
                [new JsonEncoder()]
87
            ),
88
            new Serializer(
89
                [new ObjectNormalizer()],
90
                [new JsonEncoder()]
91
            )
92
        );
93
94
        $this->connections[] = $connection;
95
96
        return $connection;
97
    }
98
99
    public function __destruct()
100
    {
101
        foreach ($this->connections as $connection) {
102
            if ($connection->isStreamOpen()) {
103
                $connection->close();
104
            }
105
        }
106
107
        Mockery::close();
108
    }
109
}
110