Completed
Push — master ( 1e949f...87ce65 )
by
unknown
05:29 queued 03:21
created

DatabaseTest::testTableList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkConnect\Test\Connection;
5
6
use ArrayObject;
7
use TBolier\RethinkQL\Connection\ConnectionInterface;
8
use TBolier\RethinkQL\Rethink;
9
use TBolier\RethinkQL\RethinkInterface;
10
use TBolier\RethinkQL\Test\BaseTestCase;
11
12
class DatabaseTest extends BaseTestCase
13
{
14
    /**
15
     * @var RethinkInterface
16
     */
17
    private $r;
18
19
    public function setUp()
20
    {
21
        parent::setUp();
22
23
        /** @var ConnectionInterface $connection */
24
        $connection = $this->createConnection('phpunit_default')->connect();
25
        $connection->connect()->use('test');
26
27
        $this->r = new Rethink($connection);
28
    }
29
30
    /**
31
     * @throws \Exception
32
     */
33
    public function testTableList()
34
    {
35
        $res = $this->r
36
            ->db()
37
            ->tableList()
38
            ->run();
39
40
        $this->assertInternalType('array', $res[0]);
41
    }
42
43
    /**
44
     * @throws \Exception
45
     */
46
    public function testCreateTable()
47
    {
48
        $res = $this->r
49
            ->db()
50
            ->tableCreate('en')
51
            ->run();
52
53
        $this->assertObStatus(['tables_created' => 1], $res[0]);
54
    }
55
56
    /**
57
     * @throws \Exception
58
     */
59
    public function testDropTable()
60
    {
61
        $res = $this->r
62
            ->db()
63
            ->tableDrop('en')
64
            ->run();
65
66
        $this->assertObStatus(['tables_dropped' => 1], $res[0]);
67
    }
68
69
    /**
70
     * @param $status
71
     * @param $data
72
     * @throws \Exception
73
     */
74
    protected function assertObStatus($status, $data)
75
    {
76
        $res = [];
77
        $statuses = [
78
            'tables_created',
79
            'tables_dropped',
80
            'errors',
81
        ];
82
        $data = new ArrayObject($data);
83
84
        foreach ($statuses as $s) {
85
            $status[$s] = $status[$s] ?? 0;
86
        }
87
88
        $data->setFlags($data::ARRAY_AS_PROPS);
89
90
        foreach ($statuses as $s) {
91
            $res[$s] = $data[$s] ?? 0;
92
        }
93
94
        $this->assertEquals($status, $res);
95
    }
96
}
97