forked from cakephp/codeception
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbTrait.php
More file actions
133 lines (116 loc) · 3.59 KB
/
Copy pathDbTrait.php
File metadata and controls
133 lines (116 loc) · 3.59 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
130
131
132
133
<?php
namespace Cake\Codeception\Helper;
use Cake\ORM\TableRegistry;
trait DbTrait
{
protected $insertedRecords = [];
/**
* Cleans up inserted records.
*
* @param string $model Model alias.
* @return void
*/
public function cleanUpInsertedRecords($model = null)
{
$records = $this->insertedRecords;
if (empty($records)) {
return;
}
if (!empty($model) && !empty($records[$model])) {
$this->deleteAllRecords($model, $records[$model]);
return;
}
foreach ($records as $model => $data) {
$this->deleteAllRecords($model, $data);
}
}
/**
* Delete inserted records with constraints disabled.
*
* @param string $model Model alias.
* @param array $data Delete target conditions.
* @return void
*/
protected function deleteAllRecords($model, $data)
{
$table = TableRegistry::get($model);
$table->connection()
->disableConstraints(function () use ($table, $data) {
$table->deleteAll($data);
});
}
/**
* Inserts record into the database.
*
* @param string $model Model alias.
* @param Cake\ORM\Entity|array $data Entity data.
* @return string Record's primary key.
*/
public function haveRecord($model, $data = [])
{
$table = TableRegistry::get($model);
if (!($data instanceof Entity)) {
$data = $table->newEntity($data, ['validate' => false]);
}
if (!$table->save($data, ['checkRules' => false])) {
$this->fail('Could not insert record into table ' . $model);
}
if (empty($this->insertedRecords[$model])) {
$this->insertedRecords[$model] = [];
}
$this->insertedRecords[$model][$table->primaryKey() . ' IN'][] = $data->{$table->primaryKey()};
return $data->{$table->primaryKey()};
}
/**
* Checks that record exists in database.
*
* @param string $model Model alias.
* @param array $conditions Conditions to find the record.
*/
public function seeRecord($model, $conditions = [])
{
$record = $this->findRecord($model, $conditions);
$this->debugSection($model, json_encode($record));
if (!$record) {
$this->fail("Couldn't find $model with " . json_encode($conditions));
}
}
/**
* Checks that record does not exist in database.
*
* @param string $model Model alias.
* @param array $conditions Conditions to find the record.
*/
public function dontSeeRecord($model, $conditions = [])
{
$record = $this->findRecord($model, $conditions);
$this->debugSection($model, json_encode($conditions));
if ($record) {
$this->fail("Unexpectedly managed to find $model with " . json_encode($conditions));
}
}
/**
* Retrieves record from database.
*
* @param string $model Model alias.
* @param array $conditions Conditions to find the record.
* @return \Cake\ORM\Entity Record.
*/
public function grabRecord($model, $conditions = [])
{
return $this->findRecord($model, $conditions);
}
/**
* Wraps the ORM finder query used by the other methods.
*
* @param string $model Model alias.
* @param array $conditions Conditions to find the record.
*/
protected function findRecord($model, $conditions = [])
{
return TableRegistry::get($model)
->find()
->where($conditions)
->first();
}
}