forked from cakephp/codeception
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathORMTrait.php
More file actions
123 lines (96 loc) · 3.18 KB
/
Copy pathORMTrait.php
File metadata and controls
123 lines (96 loc) · 3.18 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
<?php
namespace Cake\Codeception\Helper;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
trait ORMTrait
{
public function seeAssociation($table, $name, $type = null, array $options = [])
{
if (!($table instanceof Table)) {
$table = $this->grabTable($table);
}
$association = $table->association($name);
if (empty($options) && empty($type)) {
$this->assertNotEmpty($association);
return;
}
if (!empty($type)) {
$this->assertEquals($type, $association->type());
}
foreach ($options as $key => $value) {
$this->assertEquals($association->{$key}(), $value);
}
}
public function dontSeeAssociation($table, $name, $type = null, array $options = [])
{
if (!($table instanceof Table)) {
$table = $this->grabTable($table);
}
$association = $table->association($name);
if (empty($options) && empty($type)) {
$this->assertEmpty($association);
return;
}
if (empty($options) && !empty($type)) {
$this->assertNotEquals($type, $association->type());
return;
}
foreach ($options as $key => $value) {
if ($association->{$key}() !== $value) {
$this->assertTrue(true);
return;
}
}
$this->assertNotEquals($type, $association->type());
$this->assertNotEquals($options, $options);
}
public function seeValidationErrors($entity, $data, array $expectedErrors = [])
{
$errors = $this->tryValidating($entity, $data);
foreach ($expectedErrors as $field => $error) {
if (is_numeric($field)) {
$field = $error;
$error = null;
}
$this->assertContains($field, array_keys($errors), json_encode($errors));
if (empty($error)) {
continue;
}
$error = (array)$error;
foreach ($error as $name => $message) {
if (is_numeric($name)) {
$name = $message;
$message = null;
}
$this->assertContains($name, array_keys($errors[$field]));
if (empty($message)) {
continue;
}
$this->assertEquals($message, $errors[$field][$name]);
}
}
}
public function dontSeeValidationErrors($entity, $data)
{
$errors = $this->tryValidating($entity, $data);
$this->assertEquals(0, count($errors), json_encode($errors));
}
public function grabTable($alias)
{
return TableRegistry::get($alias);
}
public function tryPatching($entity, $data)
{
if (!($entity instanceof Entity)) {
$table = $this->grabTable($entity);
$entity = $table->newEntity();
} else {
$table = $this->grabTable($entity->source());
}
return $table->patchEntity($entity, $data);
}
public function tryValidating($entity, $data)
{
return $this->tryPatching($entity, $data)->errors();
}
}