-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWorkflowSaveTest.php
More file actions
147 lines (127 loc) · 4.09 KB
/
WorkflowSaveTest.php
File metadata and controls
147 lines (127 loc) · 4.09 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
<?php
/**
* WorkflowSaveTest
*
* @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('WorkflowComponent', 'Workflow.Controller/Component');
App::uses('NetCommonsSaveTest', 'NetCommons.TestSuite');
/**
* WorkflowSaveTest
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Workflow\TestSuite
*/
abstract class WorkflowSaveTest extends NetCommonsSaveTest {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
Current::$current['Block']['id'] = '2';
Current::$current['Room']['id'] = '2';
Current::writePermission('2', 'content_editable', true);
Current::writePermission('2', 'content_publishable', true);
}
/**
* Save(公開)のテスト
*
* @param array $data 登録データ
* @dataProvider dataProviderSave
* @return array 登録後のデータ
*/
public function testSave($data) {
$model = $this->_modelName;
$method = $this->_methodName;
$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']),
));
$saveData = Hash::remove($data, $this->$model->alias . '.id');
} else {
$saveData = $data;
$before[$this->$model->alias] = array();
}
//テスト実行
$result = $this->$model->$method($saveData);
$this->assertNotEmpty($result);
$id = $this->$model->getLastInsertID();
//is_latestのチェック
if (! $created) {
$after = $this->$model->find('first', array(
'recursive' => -1,
'conditions' => array('id' => $data[$this->$model->alias]['id']),
));
$this->assertEquals($after,
Hash::merge($before, array(
$this->$model->alias => array('is_latest' => false)
)
));
}
//更新のチェック
$actual = $this->_getActual($id, $created);
$expected = $this->_getExpected($id, $data, $before, $created);
$this->assertEquals($expected, $actual);
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;
$expected = parent::_getExpected($id, $data, $before, $created);
if ($created) {
$expected[$this->$model->alias]['key'] = OriginalKeyBehavior::generateKey(
$this->$model->name, $this->$model->useDbConfig
);
}
$expected[$this->$model->alias]['is_active'] = true;
$expected[$this->$model->alias]['is_latest'] = true;
return $expected;
}
/**
* Test to call WorkflowBehavior::beforeSave
*
* WorkflowBehaviorをモックに置き換えて登録処理を呼び出します。<br>
* WorkflowBehavior::beforeSaveが1回呼び出されることをテストします。<br>
* ##### 参考URL
* http://stackoverflow.com/questions/19833495/how-to-mock-a-cakephp-behavior-for-unit-testing]
*
* @param array $data 登録データ
* @dataProvider dataProviderSave
* @return void
* @throws CakeException Workflow.Workflowがロードされていないとエラー
*/
public function testCallWorkflowBehavior($data) {
$model = $this->_modelName;
$method = $this->_methodName;
if (! $this->$model->Behaviors->loaded('Workflow.Workflow')) {
$error = '"Workflow.Workflow" not loaded in ' . $this->$model->alias . '.';
throw new CakeException($error);
};
ClassRegistry::removeObject('WorkflowBehavior');
$workflowBehaviorMock = $this->getMock('WorkflowBehavior', ['beforeSave']);
ClassRegistry::addObject('WorkflowBehavior', $workflowBehaviorMock);
$this->$model->Behaviors->unload('Workflow');
$this->$model->Behaviors->load('Workflow', $this->$model->actsAs['Workflow.Workflow']);
$workflowBehaviorMock
->expects($this->once())
->method('beforeSave')
->will($this->returnValue(true));
$this->$model->$method($data);
}
}