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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
use \Codeception\Lib\Driver\Db;
use \Codeception\Test\Unit;
/**
* @group appveyor
* @group db
*/
class MysqlTest extends Unit
{
protected static $config = [
'dsn' => 'mysql:host=localhost;dbname=codeception_test',
'user' => 'root',
'password' => ''
];
protected static $sql;
/**
* @var \Codeception\Lib\Driver\MySql
*/
protected $mysql;
public static function setUpBeforeClass()
{
if (getenv('APPVEYOR')) {
self::$config['password'] = 'Password12!';
}
$sql = file_get_contents(\Codeception\Configuration::dataDir() . '/dumps/mysql.sql');
$sql = preg_replace('%/\*(?:(?!\*/).)*\*/%s', "", $sql);
self::$sql = explode("\n", $sql);
try {
$mysql = Db::create(self::$config['dsn'], self::$config['user'], self::$config['password']);
$mysql->cleanup();
} catch (\Exception $e) {
}
}
public function setUp()
{
try {
$this->mysql = Db::create(self::$config['dsn'], self::$config['user'], self::$config['password']);
} catch (\Exception $e) {
$this->markTestSkipped('Couldn\'t establish connection to database: ' . $e->getMessage());
}
$this->mysql->cleanup();
$this->mysql->load(self::$sql);
}
public function tearDown()
{
if (isset($this->mysql)) {
$this->mysql->cleanup();
}
}
public function testCleanupDatabase()
{
$this->assertNotEmpty($this->mysql->getDbh()->query("SHOW TABLES")->fetchAll());
$this->mysql->cleanup();
$this->assertEmpty($this->mysql->getDbh()->query("SHOW TABLES")->fetchAll());
}
/**
* @group appveyor
*/
public function testLoadDump()
{
$res = $this->mysql->getDbh()->query("select * from users where name = 'davert'");
$this->assertNotEquals(false, $res);
$this->assertGreaterThan(0, $res->rowCount());
$res = $this->mysql->getDbh()->query("select * from groups where name = 'coders'");
$this->assertNotEquals(false, $res);
$this->assertGreaterThan(0, $res->rowCount());
}
public function testGetSingleColumnPrimaryKey()
{
$this->assertEquals(['id'], $this->mysql->getPrimaryKey('order'));
}
public function testGetCompositePrimaryKey()
{
$this->assertEquals(['group_id', 'id'], $this->mysql->getPrimaryKey('composite_pk'));
}
public function testGetEmptyArrayIfTableHasNoPrimaryKey()
{
$this->assertEquals([], $this->mysql->getPrimaryKey('no_pk'));
}
public function testGetPrimaryColumnOfTableUsingReservedWordAsTableName()
{
$this->assertEquals('id', $this->mysql->getPrimaryColumn('order'));
}
public function testGetPrimaryColumnThrowsExceptionIfTableHasCompositePrimaryKey()
{
$this->setExpectedException(
'\Exception',
'getPrimaryColumn method does not support composite primary keys, use getPrimaryKey instead'
);
$this->mysql->getPrimaryColumn('composite_pk');
}
public function testDeleteFromTableUsingReservedWordAsTableName()
{
$this->mysql->deleteQuery('order', 1);
$res = $this->mysql->getDbh()->query("select id from `order` where id = 1");
$this->assertEquals(0, $res->rowCount());
}
public function testDeleteFromTableUsingReservedWordAsPrimaryKey()
{
$this->mysql->deleteQuery('table_with_reserved_primary_key', 1, 'unique');
$res = $this->mysql->getDbh()->query("select name from `table_with_reserved_primary_key` where `unique` = 1");
$this->assertEquals(0, $res->rowCount());
}
public function testSelectWithBooleanParam()
{
$res = $this->mysql->executeQuery("select `id` from `users` where `is_active` = ?", [false]);
$this->assertEquals(1, $res->rowCount());
}
public function testInsertIntoBitField()
{
if (getenv('WERCKER_ROOT')) {
$this->markTestSkipped('Disabled on Wercker CI');
}
$res = $this->mysql->executeQuery(
"insert into `users`(`id`,`name`,`email`,`is_active`,`created_at`) values (?,?,?,?,?)",
[5,'insert.test','insert.test@mail.ua',false,'2012-02-01 21:17:47']
);
$this->assertEquals(1, $res->rowCount());
}
/**
* THis will fail if MariaDb is used
*/
public function testLoadThrowsExceptionWhenDumpFileContainsSyntaxError()
{
$sql = "INSERT INTO `users` (`name`) VALS('')";
$expectedMessage = 'You have an error in your SQL syntax; ' .
'check the manual that corresponds to your MySQL server version for the right syntax to use near ' .
"'VALS('')' at line 1\nSQL query being executed: \n" . $sql;
$this->setExpectedException('Codeception\Exception\ModuleException', $expectedMessage);
$this->mysql->load([$sql]);
}
}