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