/
EntityTest.php
168 lines (127 loc) · 4.87 KB
/
EntityTest.php
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\tests\cases\data;
use lithium\data\Entity;
use lithium\data\Schema;
class EntityTest extends \lithium\test\Unit {
protected $_model = 'lithium\tests\mocks\data\MockPost';
public function testSchemaAccess() {
$fields = array('foo' => array('type' => 'string'));
$schema = new Schema(compact('fields'));
$entity = new Entity(compact('schema'));
$this->assertEqual($schema, $entity->schema());
}
public function testPropertyAccess() {
$entity = new Entity(array('model' => 'Foo', 'exists' => false));
$this->assertEqual('Foo', $entity->model());
$this->assertFalse($entity->exists());
$entity = new Entity(array('exists' => true));
$this->assertTrue($entity->exists());
$expected = array(
'exists' => true, 'data' => array(), 'update' => array(), 'increment' => array()
);
$this->assertEqual($expected, $entity->export());
}
public function testPropertyIssetEmpty() {
$entity = new Entity(array(
'model' => 'Foo',
'exists' => true,
'data' => array('test_field' => 'foo'),
'relationships' => array('test_relationship' => array('test_me' => 'bar'))
));
$this->assertEqual('foo', $entity->test_field);
$this->assertEqual(array('test_me' => 'bar'), $entity->test_relationship);
$this->assertTrue(isset($entity->test_field));
$this->assertTrue(isset($entity->test_relationship));
$this->assertFalse(empty($entity->test_field));
$this->assertFalse(empty($entity->test_relationship));
$this->assertTrue(empty($entity->test_invisible_field));
$this->assertTrue(empty($entity->test_invisible_relationship));
}
public function testIncrement() {
$entity = new Entity(array('data' => array('counter' => 0)));
$this->assertEqual(0, $entity->counter);
$entity->increment('counter');
$this->assertEqual(1, $entity->counter);
$entity->decrement('counter', 5);
$this->assertEqual(-4, $entity->counter);
$this->assertNull($entity->increment);
$entity->increment('foo');
$this->assertEqual(1, $entity->foo);
$this->assertFalse(isset($entity->bar));
$entity->bar = 'blah';
$entity->sync();
$this->expectException("/^Field 'bar' cannot be incremented.$/");
$entity->increment('bar');
}
public function testMethodDispatch() {
$model = $this->_model;
$data = array('foo' => true);
$entity = new Entity(compact('model', 'data'));
$this->assertTrue($entity->validates());
$model::instanceMethods(array('testInstanceMethod' => function($entity) {
return 'testInstanceMethod';
}));
$this->assertEqual('testInstanceMethod', $entity->testInstanceMethod($entity));
$this->expectException("/^Unhandled method call `foo`.$/");
$entity->foo();
}
public function testMethodDispatchWithNoModel() {
$data = array('foo' => true);
$entity = new Entity(compact('data'));
$this->expectException("/^No model bound to call `foo`.$/");
$entity->foo();
}
public function testErrors() {
$entity = new Entity();
$errors = array('foo' => 'Something bad happened.');
$this->assertEqual(array(), $entity->errors());
$entity->errors($errors);
$this->assertEqual($errors, $entity->errors());
$this->assertEqual('Something bad happened.', $entity->errors('foo'));
}
public function testConversion() {
$data = array('foo' => '!!', 'bar' => '??', 'baz' => '--');
$entity = new Entity(compact('data'));
$this->assertEqual($data, $entity->to('array'));
$this->assertEqual($data, $entity->data());
$this->assertEqual($entity, $entity->to('foo'));
}
public function testModified() {
$entity = new Entity();
$this->assertEqual(array(), $entity->modified());
$data = array('foo' => 'bar', 'baz' => 'dib');
$entity->set($data);
$this->assertEqual(array('foo' => true, 'baz' => true), $entity->modified());
$this->assertTrue($entity->modified('foo'));
$this->assertTrue($entity->modified('baz'));
$this->assertNull($entity->modified('ole'));
$subentity = new Entity();
$subentity->set($data);
$entity->set(array('ble' => $subentity));
$this->assertEqual(array('foo' => true, 'baz' => true, 'ble' => true), $entity->modified());
$this->assertTrue($entity->ble->modified('foo'));
$this->assertFalse($entity->ble->modified('iak'));
$this->assertEqual($entity->ble->modified(), array('foo' => true, 'baz' => true));
}
/**
* Tests that an entity can be cast to a string based on its bound model's meta data.
*/
public function testStringCasting() {
$model = $this->_model;
$old = $model::meta('title') ?: 'title';
$model::meta('title', 'firstName');
$object = new Entity(compact('model'));
$object->firstName = 'Bob';
$this->assertEqual('Bob', (string) $object);
$object->firstName = 'Rob';
$this->assertEqual('Rob', (string) $object);
$model::meta('title', $old);
}
}
?>