forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetCommonsSaveTest.php
More file actions
165 lines (143 loc) · 4.58 KB
/
NetCommonsSaveTest.php
File metadata and controls
165 lines (143 loc) · 4.58 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
<?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
*/
class NetCommonsSaveTest extends NetCommonsModelTestCase {
/**
* Model name
*
* @var array
*/
protected $_modelName = '';
/**
* Method name
*
* @var array
*/
protected $_methodName = '';
/**
* Saveのテスト
*
* @param array $data 登録データ
* @dataProvider dataProviderSave
* @return void
*/
public function testSave($data) {
$model = $this->_modelName;
$method = $this->_methodName;
//チェック用データ取得
if (isset($data[$this->$model->alias]['id'])) {
$before = $this->$model->find('first', array(
'recursive' => -1,
'conditions' => array('id' => $data[$this->$model->alias]['id']),
));
}
//テスト実行
$result = $this->$model->$method($data);
$this->assertNotEmpty($result);
//idのチェック
if (isset($data[$this->$model->alias]['id'])) {
$id = $data[$this->$model->alias]['id'];
} else {
$id = $this->$model->getLastInsertID();
}
//登録データ取得
$actual = $this->$model->find('first', array(
'recursive' => -1,
'conditions' => array('id' => $id),
));
if (isset($data[$this->$model->alias]['id'])) {
$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], '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');
if ($this->$model->hasField('key')) {
$data[$this->$model->alias]['key'] = OriginalKeyBehavior::generateKey($this->$model->name, $this->$model->useDbConfig);
}
$before[$this->$model->alias] = array();
}
$expected[$this->$model->alias] = Hash::merge(
$before[$this->$model->alias],
$data[$this->$model->alias],
array(
'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');
$this->assertEquals($expected, $actual);
}
/**
* 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);
}
/**
* 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);
}
/**
* Validatesのテスト
*
* @param array $data 登録データ
* @param string $field フィールド名
* @param string $value セットする値
* @param string $message エラーメッセージ
* @param array $overwrite 上書きするデータ
* @dataProvider dataProviderValidationError
* @return void
*/
public function testValidationError($data, $field, $value, $message, $overwrite = array()) {
$model = $this->_modelName;
if (is_null($value)) {
unset($data[$model][$field]);
} else {
$data[$model][$field] = $value;
}
$data = Hash::merge($data, $overwrite);
//validate処理実行
$this->$model->set($data);
$result = $this->$model->validates();
$this->assertFalse($result);
$this->assertEquals($this->$model->validationErrors[$field][0], $message);
}
}