-
Notifications
You must be signed in to change notification settings - Fork 66
/
BaseTestCase.php
97 lines (82 loc) · 2.86 KB
/
BaseTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace React\Tests\MySQL;
use PHPUnit\Framework\TestCase;
use React\EventLoop\LoopInterface;
use React\MySQL\ConnectionInterface;
use React\MySQL\Factory;
class BaseTestCase extends TestCase
{
protected function getConnectionOptions($debug = false)
{
// can be controlled through ENV or by changing defaults in phpunit.xml
return [
'host' => getenv('DB_HOST'),
'port' => (int)getenv('DB_PORT'),
'dbname' => getenv('DB_DBNAME'),
'user' => getenv('DB_USER'),
'passwd' => getenv('DB_PASSWD'),
] + ($debug ? ['debug' => true] : []);
}
protected function getConnectionString($params = [])
{
$parts = $params + $this->getConnectionOptions();
return rawurlencode($parts['user']) . ':' . rawurlencode($parts['passwd']) . '@' . $parts['host'] . ':' . $parts['port'] . '/' . rawurlencode($parts['dbname']);
}
/**
* @param LoopInterface $loop
* @return ConnectionInterface
*/
protected function createConnection(LoopInterface $loop)
{
$factory = new Factory($loop);
$promise = $factory->createConnection($this->getConnectionString());
return \React\Async\await($promise);
}
protected function getDataTable()
{
return <<<SQL
CREATE TABLE `book` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`isbn` VARCHAR(255) NULL,
`author` VARCHAR(255) NULL,
`created` INT(11) NULL,
PRIMARY KEY (`id`)
)
SQL;
}
protected function expectCallableOnce()
{
$mock = $this->getMockBuilder('stdClass')->setMethods(['__invoke'])->getMock();
$mock->expects($this->once())->method('__invoke');
return $mock;
}
protected function expectCallableOnceWith($value)
{
$mock = $this->getMockBuilder('stdClass')->setMethods(['__invoke'])->getMock();
$mock->expects($this->once())->method('__invoke')->with($value);
return $mock;
}
protected function expectCallableNever()
{
$mock = $this->getMockBuilder('stdClass')->setMethods(['__invoke'])->getMock();
$mock->expects($this->never())->method('__invoke');
return $mock;
}
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
{
if (method_exists($this, 'expectException')) {
// PHPUnit 5.2+
$this->expectException($exception);
if ($exceptionMessage !== '') {
$this->expectExceptionMessage($exceptionMessage);
}
if ($exceptionCode !== null) {
$this->expectExceptionCode($exceptionCode);
}
} else {
// legacy PHPUnit 4 - PHPUnit 5.1
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
}
}
}