Repository navigation
Expand file tree
/
Copy pathDbh.php
More file actions
107 lines (90 loc) · 3.41 KB
/
Copy pathDbh.php
File metadata and controls
107 lines (90 loc) · 3.41 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
<?php
namespace Codeception\Module;
/**
* This module replaces Db module for functional and unit testing, and requires PDO instance to be set.
* Be 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 \Codeception\Module implements \Codeception\Util\DbInterface
{
public static $dbh;
public function _before(\Codeception\TestCase $test) {
if (!self::$dbh) throw new \Codeception\Exception\ModuleConfig(__CLASS__,
"Transaction module requires PDO instance explicitly set.\n" .
"You can use your bootstrap file to assign the dbh:\n\n" .
'\Codeception\Module\Transaction::$dbh = $dbh');
self::$dbh->beginTransaction();
}
public function _after(\Codeception\TestCase $test) {
if (!self::$dbh) throw new \Codeception\Exception\ModuleConfig(__CLASS__,
"Transaction module requires PDO instance explicitly set.\n" .
"You can use your bootstrap file to assign the dbh:\n\n" .
'\Codeception\Module\Transaction::$dbh = $dbh');
self::$dbh->rollback();
}
public function seeInDatabase($table, $criteria = array())
{
$res = $this->proceedSeeInDatabase($table, "count(*)", $criteria);
\PHPUnit_Framework_Assert::assertGreaterThan(0, $res);
}
public function dontSeeInDatabase($table, $criteria = array())
{
$res = $this->proceedSeeInDatabase($table, "count(*)", $criteria);
\PHPUnit_Framework_Assert::assertLessThan(1, $res);
}
protected function proceedSeeInDatabase($table, $column, $criteria)
{
$query = "select %s from %s where %s";
$params = array();
foreach ($criteria as $k => $v) {
$params[] = "$k = ?";
}
$params = implode('AND ',$params);
$query = sprintf($query, $column, $table, $params);
$this->debugSection('Query',$query, $params);
$sth = self::$dbh->prepare($query);
$sth->execute(array_values($criteria));
return $sth->fetchColumn();
}
public function grabFromDatabase($table, $column, $criteria = array()) {
return $this->proceedSeeInDatabase($table, $column, $criteria);
}
}