forked from etsy/411
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelTest.php
More file actions
194 lines (167 loc) · 5.45 KB
/
Copy pathModelTest.php
File metadata and controls
194 lines (167 loc) · 5.45 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace FOO {
class TestModel extends Model {
public static $TABLE = 'tests';
public static $PKEY = 'test_id';
public static function generateSchema() {
return [
'bool' => [self::T_BOOL, null, false],
'num' => [self::T_NUM, null, 0],
'str' => [self::T_STR, null, ''],
'enum' => [self::T_ENUM, [1, 2], 1],
'arr' => [self::T_ARR, null, []],
'obj' => [self::T_OBJ, null, []],
];
}
protected function serialize(array $data) {
$data['bool'] = (bool)$data['bool'];
$data['arr'] = json_encode($data['arr']);
$data['obj'] = json_encode((object)$data['obj']);
return parent::serialize($data);
}
protected function deserialize(array $data) {
$data['bool'] = (bool)$data['bool'];
$data['arr'] = json_decode($data['arr']);
$data['obj'] = json_decode($data['obj'], true);
return parent::deserialize($data);
}
}
class TestModelFinder extends ModelFinder {
public static $MODEL = 'TestModel';
}
}
namespace {
class ModelTest extends TestCase {
public function setUp() {
parent::setUp();
FOO\DB::query('
CREATE TABLE `tests` (
`test_id` INTEGER PRIMARY KEY,
`site_id` INTEGER NOT NULL,
`bool` UNSIGNED INTEGER NOT NULL,
`num` UNSIGNED INTEGER NOT NULL,
`str` VARCHAR(255) NOT NULL,
`enum` UNSIGNED INTEGER NOT NULL,
`arr` TEXT NOT NULL,
`obj` TEXT NOT NULL,
`archived` UNSIGNED INTEGER NOT NULL,
`create_date` UNSIGNED INTEGER NOT NULL,
`update_date` UNSIGNED INTEGER NOT NULL
)
');
}
public function testStore() {
$model = new FOO\TestModel();
$this->assertTrue($model->store());
$model_ = FOO\TestModelFinder::getById($model['id']);
$this->assertTrue($model_->store());
}
public function testGetDefaults() {
$expected = [
'bool' => false,
'num' => 0,
'str' => '',
'enum' => 1,
'arr' => [],
'obj' => [],
'archived' => false,
'create_date' => 0,
'update_date' => 0,
];
$this->assertSame($expected, FOO\TestModel::getDefaults());
}
public function testValidateData() {
$model = new FOO\TestModel();
$model['bool'] = true;
$model['num'] = 2;
$model['str'] = 'stuff';
$model['enum'] = 2;
$model['arr'] = [1, 2];
$model['obj'] = ['a' => 'b'];
}
/**
* @expectedException UnexpectedValueException
*/
public function testValidateDataUnexpectedKey() {
$model = new FOO\TestModel();
$model['b'] = true;
$model->validate();
}
/**
* @expectedException FOO\ValidationException
*/
public function testValidateDataBadEnum() {
$model = new FOO\TestModel();
$model['enum'] = 99;
$model->validate();
}
/**
* @expectedException FOO\ValidationException
*/
public function testValidateDataBadArr() {
$model = new FOO\TestModel();
$model['arr'] = 'str';
$model->validate();
}
/**
* @expectedException FOO\ValidationException
*/
public function testValidateDataBadObj() {
$model = new FOO\TestModel();
$model['obj'] = 1;
$model->validate();
}
public function testIsNew() {
$model = new FOO\TestModel();
$this->assertTrue($model->isNew());
}
public function testToArray() {
$model = new FOO\TestModel();
$data = $model->toArray(['bool']);
$this->assertSame(['bool', 'id', 'archived', 'create_date', 'update_date'], array_keys($data));
}
public function testFinderHydrateModels() {
$raw = [
[
'id' => '1',
'bool' => '1',
'num' => '2',
'str' => 'stuff',
'enum' => '2',
'arr' => '[1, 2]',
'obj' => '{"a": "b"}',
'create_date' => '0',
'update_date' => '0',
'archived' => '0',
]
];
$expected = [
'id' => 1,
'bool' => 1,
'num' => 2,
'str' => 'stuff',
'enum' => 2,
'arr' => [1, 2],
'obj' => (object) ['a' => 'b'],
'create_date' => 0,
'update_date' => 0,
'archived' => 0,
];
$objs = FOO\TestModelFinder::hydrateModels($raw);
$this->assertCount(1, $objs);
$this->assertEquals($expected, $objs[0]->toArray());
}
public function testFinderGenerateWhere() {
$finder = 'FOO\\TestModelFinder';
$where = ['`site_id` = ?', '`archived` = ?'];
$vals = [0, 0];
$this->assertSame([$where, $vals], TestHelper::invokeMethod($finder, 'generateWhere', [[]]));
$where = ['`site_id` = ?', '(`create_date` > ? OR `update_date` > ?)'];
$vals = [0, 10, 10];
$this->assertSame([$where, $vals], TestHelper::invokeMethod($finder, 'generateWhere', [['time' => 10]]));
$where = ['`test_id` IN (?)', '`site_id` = ?', '`archived` = ?'];
$vals = [1, 0, 0];
$this->assertSame([$where, $vals], TestHelper::invokeMethod($finder, 'generateWhere', [['id' => 1]]));
}
}
}