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\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(), true)) { |
|
|
|
|
54
|
|
|
$this->r->dbCreate('test')->run(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->r; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function tearDown() |
61
|
|
|
{ |
62
|
|
|
if ($this->r !== null && \in_array('test', |
63
|
|
|
$this->r->dbList()->run()->getData(), 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
|
|
|
$connection->close(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
Mockery::close(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $status |
110
|
|
|
* @param $data |
111
|
|
|
* @throws \Exception |
112
|
|
|
*/ |
113
|
|
|
protected function assertObStatus($status, $data) |
114
|
|
|
{ |
115
|
|
|
$res = []; |
116
|
|
|
$statuses = [ |
117
|
|
|
'tables_created', |
118
|
|
|
'tables_dropped', |
119
|
|
|
'unchanged', |
120
|
|
|
'skipped', |
121
|
|
|
'replaced', |
122
|
|
|
'inserted', |
123
|
|
|
'errors', |
124
|
|
|
'deleted', |
125
|
|
|
]; |
126
|
|
|
$data = new ArrayObject($data); |
127
|
|
|
|
128
|
|
|
foreach ($statuses as $s) { |
129
|
|
|
$status[$s] = $status[$s] ?? 0; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$data->setFlags($data::ARRAY_AS_PROPS); |
133
|
|
|
|
134
|
|
|
foreach ($statuses as $s) { |
135
|
|
|
$res[$s] = $data[$s] ?? 0; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->assertEquals($status, $res); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|