Repository navigation
Expand file tree
/
Copy pathDbh.php
More file actions
129 lines (113 loc) · 3.87 KB
/
Copy pathDbh.php
File metadata and controls
129 lines (113 loc) · 3.87 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
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Codeception\Module;
use Codeception\Lib\Notification;
use Codeception\Module as CodeceptionModule;
use Codeception\Lib\Interfaces\Db as DbInterface;
use Codeception\Exception\ModuleConfigException;
use Codeception\TestCase;
/**
* This module replaces Db module for functional and unit testing, and requires PDO instance to be set.
* By default it will cover all database queries into transaction and rollback it afterwards.
* The database should support nested transactions, in order to make cleanup work as expected.
*
* Pass PDO instance to this module from within your bootstrap file.
*
* In _bootstrap.php:
*
* ``` php
* <?php
* \Codeception\Module\Dbh::$dbh = $dbh;
* ?>
* ```
*
* This will make all queries in this connection run withing transaction and rolled back afterwards.
*
* Note, that you can't use this module with MySQL. Or perhaps you don't use transactions in your project, then it's ok.
* Otherwise consider using ORMs like Doctrine, that emulate nested transactions, or switch to Db module.
*
* ## Status
*
* * Maintainer: **davert**
* * Stability: **stable**
* * Contact: codecept@davert.mail.ua
*
* This module despite of it's stability may act unstable because of transactions issue. If test fails with fatal error and transaction is not finished, it may affect other transactions.
*
* *Please review the code of non-stable modules and provide patches if you have issues.*
*
* ### Configuration
*
* * cleanup: true - enable cleanups by covering all queries inside transaction.
*
* ### Example
*
* modules:
* enabled: [Dbh]
* config:
* Dbh:
* cleanup: false
*
*/
class Dbh extends CodeceptionModule implements DbInterface
{
public static $dbh;
public function _initialize()
{
Notification::deprecate("Module Dbh is deprecated and will be removed in 2.2");
}
public function _before(TestCase $test)
{
if (!self::$dbh) {
throw new ModuleConfigException(
__CLASS__,
"Transaction module requires PDO instance explicitly set.\n"
. "You can use your bootstrap file to assign the dbh:\n\n"
. '\Codeception\Module\Dbh::$dbh = $dbh'
);
}
if (!self::$dbh->inTransaction()) {
self::$dbh->beginTransaction();
}
}
public function _after(TestCase $test)
{
if (!self::$dbh) {
throw new ModuleConfigException(
__CLASS__,
"Transaction module requires PDO instance explicitly set.\n"
. "You can use your bootstrap file to assign the dbh:\n\n"
. '\Codeception\Module\Dbh::$dbh = $dbh'
);
}
if (self::$dbh->inTransaction()) {
self::$dbh->rollback();
}
}
public function seeInDatabase($table, $criteria = [])
{
$res = $this->proceedSeeInDatabase($table, "count(*)", $criteria);
\PHPUnit_Framework_Assert::assertGreaterThan(0, $res);
}
public function dontSeeInDatabase($table, $criteria = [])
{
$res = $this->proceedSeeInDatabase($table, "count(*)", $criteria);
\PHPUnit_Framework_Assert::assertLessThan(1, $res);
}
protected function proceedSeeInDatabase($table, $column, $criteria)
{
$params = [];
foreach ($criteria as $k => $v) {
$params[] = "$k = ?";
}
$sparams = implode('AND ', $params);
$query = sprintf('select %s from %s where %s', $column, $table, $sparams);
$this->debugSection('Query', $query, $sparams);
$sth = self::$dbh->prepare($query);
$sth->execute(array_values($criteria));
return $sth->fetchColumn();
}
public function grabFromDatabase($table, $column, $criteria = [])
{
return $this->proceedSeeInDatabase($table, $column, $criteria);
}
}