Repository navigation
Expand file tree
/
Copy pathDbTest.php
More file actions
118 lines (99 loc) · 4.84 KB
/
Copy pathDbTest.php
File metadata and controls
118 lines (99 loc) · 4.84 KB
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
<?php
class DbTest extends \PHPUnit_Framework_TestCase
{
protected static $config = [
'dsn' => 'sqlite:tests/data/dbtest.db',
'user' => 'root',
'password' => '',
'cleanup' => false
];
/**
* @var \Codeception\Module\Db
*/
protected static $module;
public static function setUpBeforeClass()
{
self::$module = new \Codeception\Module\Db(make_container());
self::$module->_setConfig(self::$config);
self::$module->_initialize();
$sqlite = self::$module->driver;
$sqlite->cleanup();
$sql = file_get_contents(\Codeception\Configuration::dataDir() . '/dumps/sqlite.sql');
$sql = preg_replace('%/\*(?:(?!\*/).)*\*/%s', "", $sql);
$sql = explode("\n", $sql);
$sqlite->load($sql);
}
public function testSeeInDatabase()
{
self::$module->seeInDatabase('users', ['name' => 'davert']);
}
public function testCountInDatabase()
{
self::$module->seeNumRecords(1, 'users', ['name' => 'davert']);
self::$module->seeNumRecords(0, 'users', ['name' => 'davert', 'email' => 'xxx@yyy.zz']);
self::$module->seeNumRecords(0, 'users', ['name' => 'user1']);
}
public function testDontSeeInDatabase()
{
self::$module->dontSeeInDatabase('users', ['name' => 'user1']);
}
public function testDontSeeInDatabaseWithEmptyTable()
{
self::$module->dontSeeInDatabase('empty_table');
}
public function testGrabFromDatabase()
{
$email = self::$module->grabFromDatabase('users', 'email', ['name' => 'davert']);
$this->assertEquals('davert@mail.ua', $email);
}
public function testHaveAndSeeInDatabase()
{
self::$module->_before(\Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface'));
$user_id = self::$module->haveInDatabase('users', ['name' => 'john', 'email' => 'john@jon.com']);
$group_id = self::$module->haveInDatabase('groups', ['name' => 'john', 'enabled' => false]);
$this->assertInternalType('integer', $user_id);
self::$module->seeInDatabase('users', ['name' => 'john', 'email' => 'john@jon.com']);
self::$module->dontSeeInDatabase('users', ['name' => 'john', 'email' => null]);
self::$module->_after(\Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface'));
self::$module->dontSeeInDatabase('users', ['name' => 'john']);
}
public function testHaveInDatabaseWithCompositePrimaryKey()
{
self::$module->_before(\Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface'));
$insertQuery = 'INSERT INTO composite_pk (group_id, id, status) VALUES(?, ?, ?)';
//this test checks that module does not delete columns by partial primary key
self::$module->driver->executeQuery($insertQuery, [1, 2, 'test']);
self::$module->driver->executeQuery($insertQuery, [2, 1, 'test2']);
$testData = ['id' => 2, 'group_id' => 2, 'status' => 'test3'];
self::$module->haveInDatabase('composite_pk', $testData);
self::$module->seeInDatabase('composite_pk', $testData);
self::$module->_after(\Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface'));
self::$module->dontSeeInDatabase('composite_pk', $testData);
self::$module->seeInDatabase('composite_pk', ['group_id' => 1, 'id' => 2, 'status' => 'test']);
self::$module->seeInDatabase('composite_pk', ['group_id' => 2, 'id' => 1, 'status' => 'test2']);
}
public function testHaveInDatabaseWithoutPrimaryKey()
{
self::$module->_before(\Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface'));
$testData = ['status' => 'test'];
self::$module->haveInDatabase('no_pk', $testData);
self::$module->seeInDatabase('no_pk', $testData);
self::$module->_after(\Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface'));
self::$module->dontSeeInDatabase('no_pk', $testData);
}
public function testReconnectOption()
{
$testCase1 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
$testCase2 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
self::$module->_reconfigure(['reconnect' => true]);
$this->assertNotNull(self::$module->driver, 'driver is null before test');
$this->assertNotNull(self::$module->dbh, 'dbh is null before test');
self::$module->_after($testCase1);
$this->assertNull(self::$module->driver, 'driver is not unset by _after');
$this->assertNull(self::$module->dbh, 'dbh is not unset by _after');
self::$module->_before($testCase2);
$this->assertNotNull(self::$module->driver, 'driver is not set by _before');
$this->assertNotNull(self::$module->dbh, 'dbh is not set by _before');
self::$module->_reconfigure(['reconnect' => false]);
}
}