TransformEntryFunctionTest.php
5.25 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
<?php
include_once(dirname(dirname(__FILE__)) . '/CFDBPermittedFunctions.php');
include_once(dirname(dirname(__FILE__)) . '/CFDBQueryResultIteratorFactory.php');
include_once(dirname(dirname(__FILE__)) . '/ExportToJson.php');
include_once('MockQueryResultIterator.php');
include_once('WP_Mock_Functions.php');
include_once('WPDB_Mock.php');
class TransformEntryFunctionTest extends PHPUnit_Framework_TestCase {
public function tearDown() {
CFDBQueryResultIteratorFactory::getInstance()->clearMock();
$wpdb = null;
try {
ob_flush();
ob_end_clean();
} catch (Exception $e) {
}
}
public function setUp() {
date_default_timezone_set('America/New_York');
$str = file_get_contents('TransformEntryFunctionTest.json');
$data = json_decode($str, true);
$mock = new MockQueryResultIterator($data);
CFDBQueryResultIteratorFactory::getInstance()->setQueryResultsIteratorMock($mock);
global $wpdb;
$wpdb = new WPDB_Mock;
$fields = array();
foreach (array_keys($data[0]) as $key) {
$fields[] = (object)array('field_name' => $key);
}
$wpdb->getResultReturnVal = $fields;
}
public function testAddNewFieldInTransformEntry() {
$options = array();
$options['trans'] = 'sumToNewField';
$exp = new ExportToJson();
ob_start();
$exp->export('form', $options);
$text = ob_get_contents();
$stuff = json_decode($text);
$idx = 0;
$this->assertTrue(is_array($stuff));
$this->assertEquals('1', $stuff[$idx]->a);
$this->assertEquals('2', $stuff[$idx]->b);
$this->assertEquals('3', $stuff[$idx]->c);
++$idx;
$this->assertEquals('20', $stuff[$idx]->a);
$this->assertEquals('30', $stuff[$idx]->b);
$this->assertEquals('50', $stuff[$idx]->c);
}
public function testAddNewFieldRemoveOldInTransformEntry() {
$options = array();
$options['trans'] = 'sumToNewFieldRemoveB';
$exp = new ExportToJson();
ob_start();
$exp->export('form', $options);
$text = ob_get_contents();
$stuff = json_decode($text);
$idx = 0;
$this->assertTrue(is_array($stuff));
$this->assertEquals('1', $stuff[$idx]->a);
$this->assertFalse(isset($stuff[0]->b));
$this->assertEquals('3', $stuff[$idx]->c);
++$idx;
$this->assertEquals('20', $stuff[$idx]->a);
$this->assertFalse(isset($stuff[0]->b));
$this->assertEquals('50', $stuff[$idx]->c);
}
public function testAddAllNewsFieldInTransformEntry() {
$options = array();
$options['trans'] = 'allNewFields';
$exp = new ExportToJson();
ob_start();
$exp->export('form', $options);
$text = ob_get_contents();
$stuff = json_decode($text);
$this->assertTrue(is_array($stuff));
$this->assertEquals('100', $stuff[0]->c);
$this->assertEquals('200', $stuff[0]->d);
$this->assertFalse(isset($stuff[0]->a));
$this->assertFalse(isset($stuff[0]->b));
}
public function testSometimesAddNewFieldInTransformEntry() {
$options = array();
$options['trans'] = 'sometimesNewFields';
$exp = new ExportToJson();
ob_start();
$exp->export('form', $options);
$text = ob_get_contents();
$stuff = json_decode($text);
$idx = 0;
$this->assertTrue(is_array($stuff));
$this->assertEquals('1', $stuff[$idx]->a);
$this->assertEquals('2', $stuff[$idx]->b);
$this->assertEquals('3', $stuff[$idx]->c);
++$idx;
$this->assertEquals('20', $stuff[$idx]->a);
$this->assertEquals('30', $stuff[$idx]->b);
$this->assertEquals('', $stuff[$idx]->c);
}
public function testSometimesAddNewFieldInTransformEntry2() {
$options = array();
$options['trans'] = 'sometimesNewFields2';
$exp = new ExportToJson();
ob_start();
$exp->export('form', $options);
$text = ob_get_contents();
$stuff = json_decode($text);
$idx = 0;
$this->assertTrue(is_array($stuff));
$this->assertEquals('1', $stuff[$idx]->a);
$this->assertEquals('2', $stuff[$idx]->b);
// todo: should be first following line, not second
//$this->assertEquals('', $stuff[$idx]->c);
$this->assertFalse(isset($stuff[$idx]->c));
++$idx;
$this->assertEquals('20', $stuff[$idx]->a);
$this->assertEquals('30', $stuff[$idx]->b);
// todo: should be first following line, not second
// $this->assertEquals('50', $stuff[$idx]->c);
$this->assertFalse(isset($stuff[$idx]->c));
}
}
function sumToNewField(&$entry) {
$entry['c'] = $entry['a'] + $entry['b'];
}
function sumToNewFieldRemoveB(&$entry) {
$entry['c'] = $entry['a'] + $entry['b'];
unset($entry['b']);
}
function allNewFields(&$entry) {
return array('c' => '100', 'd' => '200');
}
function sometimesNewFields(&$entry) {
if ($entry['a'] == '1') {
$entry['c'] = $entry['a'] + $entry['b'];
}
}
function sometimesNewFields2(&$entry) {
if ($entry['a'] == '20') {
$entry['c'] = $entry['a'] + $entry['b'];
}
}