-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNetCommonsSaveTest.php
More file actions
219 lines (191 loc) · 6.12 KB
/
NetCommonsSaveTest.php
File metadata and controls
219 lines (191 loc) · 6.12 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* NetCommonsSaveTest
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('NetCommonsModelTestCase', 'NetCommons.TestSuite');
/**
* NetCommonsSaveTest
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\TestSuite
* @codeCoverageIgnore
*/
abstract class NetCommonsSaveTest extends NetCommonsModelTestCase {
/**
* Model name
*
* @var array
*/
protected $_modelName = '';
/**
* Method name
*
* @var array
*/
protected $_methodName = '';
/**
* Key Alias
*
* @var array
*/
protected $_keyAlias = '';
/**
* Saveのテスト
*
* @param array $data 登録データ
* @dataProvider dataProviderSave
* @return void
*/
public function testSave($data) {
$model = $this->_modelName;
$method = $this->_methodName;
if (! $this->_keyAlias) {
$this->_keyAlias = $this->$model->alias;
}
if (array_key_exists('block_id', $data[$this->$model->alias]) && ! isset($data['Block'])) {
$this->Block = ClassRegistry::init('Blocks.Block');
$data['Block'] = $this->Block->find('first', [
'recursive' => -1,
'conditions' => ['id' => $data[$this->$model->alias]['block_id']]
]);
}
$created = !isset($data[$this->$model->alias]['id']);
//チェック用データ取得
if (! $created) {
$before = $this->$model->find('first', array(
'recursive' => -1,
'conditions' => array('id' => $data[$this->$model->alias]['id']),
));
} else {
$max = $this->$model->find('first', array(
'recursive' => -1,
'fields' => array('id'),
'order' => array('id' => 'desc'),
));
$before[$this->$model->alias] = array();
}
//テスト実行
$result = $this->$model->$method($data);
// バリデーションエラー時、出力
if (!empty($this->$model->validationErrors)) {
var_export($this->$model->validationErrors);
}
$this->assertNotEmpty($result);
//idのチェック
if (isset($data[$this->$model->alias]['id'])) {
$id = $data[$this->$model->alias]['id'];
} elseif ($max) {
$id = (string)($max[$this->$model->alias]['id'] + 1);
} else {
$id = $this->$model->getLastInsertID();
}
//チェック
$actual = $this->_getActual($id, $created);
$expected = $this->_getExpected($id, $data, $before, $created);
$this->assertEquals($expected, $actual);
}
/**
* 結果データ取得
*
* @param int $id ID
* @param bool $created 作成かどうか
* @return array
*/
protected function _getActual($id, $created) {
$model = $this->_modelName;
//登録データ取得
$actual = $this->$model->find('first', array(
'recursive' => -1,
'conditions' => array('id' => $id),
));
if ($created) {
$actual[$this->$model->alias] = Hash::remove($actual[$this->$model->alias], 'created');
$actual[$this->$model->alias] = Hash::remove($actual[$this->$model->alias], 'created_user');
$actual[$this->$model->alias] = Hash::remove($actual[$this->$model->alias], 'modified');
$actual[$this->$model->alias] = Hash::remove($actual[$this->$model->alias], 'modified_user');
} else {
$actual[$this->$model->alias] = Hash::remove($actual[$this->$model->alias], 'modified');
$actual[$this->$model->alias] = Hash::remove($actual[$this->$model->alias], 'modified_user');
}
return $actual;
}
/**
* 期待値の取得
*
* @param int $id ID
* @param array $data 登録データ
* @param array $before 登録前データ
* @param bool $created 作成かどうか
* @return array
*/
protected function _getExpected($id, $data, $before, $created) {
$model = $this->_modelName;
if ($created && $this->$model->hasField('key')) {
$data[$this->$model->alias]['key'] = OriginalKeyBehavior::generateKey(
$this->_keyAlias, $this->$model->useDbConfig
);
}
$expected[$this->$model->alias] = Hash::merge(
$before[$this->$model->alias],
$data[$this->$model->alias]
);
$expected[$this->$model->alias]['id'] = $id;
$expected[$this->$model->alias] = Hash::remove($expected[$this->$model->alias], 'modified');
$expected[$this->$model->alias] = Hash::remove($expected[$this->$model->alias], 'modified_user');
if ($this->$model->hasField('is_origin') && ! isset($data[$this->$model->alias]['is_origin'])) {
$expected[$this->$model->alias]['is_origin'] = true;
$expected[$this->$model->alias]['is_translation'] = false;
if ($this->$model->hasField('is_original_copy')) {
$expected[$this->$model->alias]['is_original_copy'] = false;
}
}
if ($this->$model->hasField('language_id') &&
! isset($data[$this->$model->alias]['language_id'])) {
$expected[$this->$model->alias]['language_id'] = '2';
}
return $expected;
}
/**
* SaveのExceptionErrorテスト
*
* @param array $data 登録データ
* @param string $mockModel Mockのモデル
* @param string $mockMethod Mockのメソッド
* @dataProvider dataProviderSaveOnExceptionError
* @return void
*/
public function testSaveOnExceptionError($data, $mockModel, $mockMethod) {
$model = $this->_modelName;
$method = $this->_methodName;
$this->_mockForReturnFalse($model, $mockModel, $mockMethod);
$this->setExpectedException('InternalErrorException');
$this->$model->$method($data);
// バリデーションエラー時、出力。
// 例外を想定しているのにバリデーションエラーが出来るのは、他クラスの変更が影響している事が多いため、
// エラー解決のヒントとして表示
if (!empty($this->$model->validationErrors)) {
var_export($this->$model->validationErrors);
}
}
/**
* SaveのValidationErrorテスト
*
* @param array $data 登録データ
* @param string $mockModel Mockのモデル
* @param string $mockMethod Mockのメソッド
* @dataProvider dataProviderSaveOnValidationError
* @return void
*/
public function testSaveOnValidationError($data, $mockModel, $mockMethod = 'validates') {
$model = $this->_modelName;
$method = $this->_methodName;
$this->_mockForReturnFalse($model, $mockModel, $mockMethod);
$result = $this->$model->$method($data);
$this->assertFalse($result);
}
}