1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class FunctionsTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); |
16
|
|
|
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function tearDown() |
20
|
|
|
{ |
21
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); |
22
|
|
|
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testCompatibilityMode() |
26
|
|
|
{ |
27
|
|
|
$result = Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC); |
28
|
|
|
// Test for a true response for success |
29
|
|
|
$this->assertTrue($result); |
30
|
|
|
// Test that mode has been changed |
31
|
|
|
$this->assertEquals(Functions::COMPATIBILITY_GNUMERIC, Functions::getCompatibilityMode()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testInvalidCompatibilityMode() |
35
|
|
|
{ |
36
|
|
|
$result = Functions::setCompatibilityMode('INVALIDMODE'); |
37
|
|
|
// Test for a false response for failure |
38
|
|
|
$this->assertFalse($result); |
39
|
|
|
// Test that mode has not been changed |
40
|
|
|
$this->assertEquals(Functions::COMPATIBILITY_EXCEL, Functions::getCompatibilityMode()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testReturnDateType() |
44
|
|
|
{ |
45
|
|
|
$result = Functions::setReturnDateType(Functions::RETURNDATE_PHP_OBJECT); |
46
|
|
|
// Test for a true response for success |
47
|
|
|
$this->assertTrue($result); |
48
|
|
|
// Test that mode has been changed |
49
|
|
|
$this->assertEquals(Functions::RETURNDATE_PHP_OBJECT, Functions::getReturnDateType()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testInvalidReturnDateType() |
53
|
|
|
{ |
54
|
|
|
$result = Functions::setReturnDateType('INVALIDTYPE'); |
55
|
|
|
// Test for a false response for failure |
56
|
|
|
$this->assertFalse($result); |
57
|
|
|
// Test that mode has not been changed |
58
|
|
|
$this->assertEquals(Functions::RETURNDATE_EXCEL, Functions::getReturnDateType()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testDUMMY() |
62
|
|
|
{ |
63
|
|
|
$result = Functions::DUMMY(); |
64
|
|
|
self::assertEquals('#Not Yet Implemented', $result); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testDIV0() |
68
|
|
|
{ |
69
|
|
|
$result = Functions::DIV0(); |
70
|
|
|
self::assertEquals('#DIV/0!', $result); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testNA() |
74
|
|
|
{ |
75
|
|
|
$result = Functions::NA(); |
76
|
|
|
self::assertEquals('#N/A', $result); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testNAN() |
80
|
|
|
{ |
81
|
|
|
$result = Functions::NAN(); |
82
|
|
|
self::assertEquals('#NUM!', $result); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testNAME() |
86
|
|
|
{ |
87
|
|
|
$result = Functions::NAME(); |
88
|
|
|
self::assertEquals('#NAME?', $result); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testREF() |
92
|
|
|
{ |
93
|
|
|
$result = Functions::REF(); |
94
|
|
|
self::assertEquals('#REF!', $result); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testNULL() |
98
|
|
|
{ |
99
|
|
|
$result = Functions::null(); |
100
|
|
|
self::assertEquals('#NULL!', $result); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testVALUE() |
104
|
|
|
{ |
105
|
|
|
$result = Functions::VALUE(); |
106
|
|
|
self::assertEquals('#VALUE!', $result); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @dataProvider providerIsBlank |
111
|
|
|
* |
112
|
|
|
* @param mixed $expectedResult |
113
|
|
|
*/ |
114
|
|
|
public function testIsBlank($expectedResult, ...$args) |
115
|
|
|
{ |
116
|
|
|
$result = Functions::isBlank(...$args); |
|
|
|
|
117
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function providerIsBlank() |
121
|
|
|
{ |
122
|
|
|
return require 'data/Calculation/Functions/IS_BLANK.php'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @dataProvider providerIsErr |
127
|
|
|
* |
128
|
|
|
* @param mixed $expectedResult |
129
|
|
|
*/ |
130
|
|
|
public function testIsErr($expectedResult, ...$args) |
131
|
|
|
{ |
132
|
|
|
$result = Functions::isErr(...$args); |
|
|
|
|
133
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function providerIsErr() |
137
|
|
|
{ |
138
|
|
|
return require 'data/Calculation/Functions/IS_ERR.php'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @dataProvider providerIsError |
143
|
|
|
* |
144
|
|
|
* @param mixed $expectedResult |
145
|
|
|
*/ |
146
|
|
|
public function testIsError($expectedResult, ...$args) |
147
|
|
|
{ |
148
|
|
|
$result = Functions::isError(...$args); |
|
|
|
|
149
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function providerIsError() |
153
|
|
|
{ |
154
|
|
|
return require 'data/Calculation/Functions/IS_ERROR.php'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @dataProvider providerErrorType |
159
|
|
|
* |
160
|
|
|
* @param mixed $expectedResult |
161
|
|
|
*/ |
162
|
|
|
public function testErrorType($expectedResult, ...$args) |
163
|
|
|
{ |
164
|
|
|
$result = Functions::errorType(...$args); |
|
|
|
|
165
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function providerErrorType() |
169
|
|
|
{ |
170
|
|
|
return require 'data/Calculation/Functions/ERROR_TYPE.php'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @dataProvider providerIsLogical |
175
|
|
|
* |
176
|
|
|
* @param mixed $expectedResult |
177
|
|
|
*/ |
178
|
|
|
public function testIsLogical($expectedResult, ...$args) |
179
|
|
|
{ |
180
|
|
|
$result = Functions::isLogical(...$args); |
|
|
|
|
181
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function providerIsLogical() |
185
|
|
|
{ |
186
|
|
|
return require 'data/Calculation/Functions/IS_LOGICAL.php'; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @dataProvider providerIsNa |
191
|
|
|
* |
192
|
|
|
* @param mixed $expectedResult |
193
|
|
|
*/ |
194
|
|
|
public function testIsNa($expectedResult, ...$args) |
195
|
|
|
{ |
196
|
|
|
$result = Functions::isNa(...$args); |
|
|
|
|
197
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function providerIsNa() |
201
|
|
|
{ |
202
|
|
|
return require 'data/Calculation/Functions/IS_NA.php'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @dataProvider providerIsNumber |
207
|
|
|
* |
208
|
|
|
* @param mixed $expectedResult |
209
|
|
|
*/ |
210
|
|
|
public function testIsNumber($expectedResult, ...$args) |
211
|
|
|
{ |
212
|
|
|
$result = Functions::isNumber(...$args); |
|
|
|
|
213
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function providerIsNumber() |
217
|
|
|
{ |
218
|
|
|
return require 'data/Calculation/Functions/IS_NUMBER.php'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @dataProvider providerIsText |
223
|
|
|
* |
224
|
|
|
* @param mixed $expectedResult |
225
|
|
|
*/ |
226
|
|
|
public function testIsText($expectedResult, ...$args) |
227
|
|
|
{ |
228
|
|
|
$result = Functions::isText(...$args); |
|
|
|
|
229
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function providerIsText() |
233
|
|
|
{ |
234
|
|
|
return require 'data/Calculation/Functions/IS_TEXT.php'; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @dataProvider providerIsNonText |
239
|
|
|
* |
240
|
|
|
* @param mixed $expectedResult |
241
|
|
|
*/ |
242
|
|
|
public function testIsNonText($expectedResult, ...$args) |
243
|
|
|
{ |
244
|
|
|
$result = Functions::isNonText(...$args); |
|
|
|
|
245
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function providerIsNonText() |
249
|
|
|
{ |
250
|
|
|
return require 'data/Calculation/Functions/IS_NONTEXT.php'; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @dataProvider providerIsEven |
255
|
|
|
* |
256
|
|
|
* @param mixed $expectedResult |
257
|
|
|
*/ |
258
|
|
|
public function testIsEven($expectedResult, ...$args) |
259
|
|
|
{ |
260
|
|
|
$result = Functions::isEven(...$args); |
|
|
|
|
261
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function providerIsEven() |
265
|
|
|
{ |
266
|
|
|
return require 'data/Calculation/Functions/IS_EVEN.php'; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @dataProvider providerIsOdd |
271
|
|
|
* |
272
|
|
|
* @param mixed $expectedResult |
273
|
|
|
*/ |
274
|
|
|
public function testIsOdd($expectedResult, ...$args) |
275
|
|
|
{ |
276
|
|
|
$result = Functions::isOdd(...$args); |
|
|
|
|
277
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function providerIsOdd() |
281
|
|
|
{ |
282
|
|
|
return require 'data/Calculation/Functions/IS_ODD.php'; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @dataProvider providerTYPE |
287
|
|
|
* |
288
|
|
|
* @param mixed $expectedResult |
289
|
|
|
*/ |
290
|
|
|
public function testTYPE($expectedResult, ...$args) |
291
|
|
|
{ |
292
|
|
|
$result = Functions::TYPE(...$args); |
|
|
|
|
293
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
public function providerTYPE() |
297
|
|
|
{ |
298
|
|
|
return require 'data/Calculation/Functions/TYPE.php'; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @dataProvider providerN |
303
|
|
|
* |
304
|
|
|
* @param mixed $expectedResult |
305
|
|
|
*/ |
306
|
|
|
public function testN($expectedResult, ...$args) |
307
|
|
|
{ |
308
|
|
|
$result = Functions::n(...$args); |
|
|
|
|
309
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function providerN() |
313
|
|
|
{ |
314
|
|
|
return require 'data/Calculation/Functions/N.php'; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @dataProvider providerIsFormula |
319
|
|
|
* |
320
|
|
|
* @param mixed $expectedResult |
321
|
|
|
* @param mixed $reference Reference to the cell we wish to test |
322
|
|
|
* @param mixed $value Value of the cell we wish to test |
323
|
|
|
*/ |
324
|
|
|
public function testIsFormula($expectedResult, $reference, $value = 'undefined') |
325
|
|
|
{ |
326
|
|
|
$ourCell = null; |
327
|
|
|
if ($value !== 'undefined') { |
328
|
|
|
$remoteCell = $this->getMockBuilder(Cell::class) |
329
|
|
|
->disableOriginalConstructor() |
330
|
|
|
->getMock(); |
331
|
|
|
$remoteCell->method('isFormula') |
332
|
|
|
->will($this->returnValue(substr($value, 0, 1) == '=')); |
333
|
|
|
|
334
|
|
|
$remoteSheet = $this->getMockBuilder(Worksheet::class) |
335
|
|
|
->disableOriginalConstructor() |
336
|
|
|
->getMock(); |
337
|
|
|
$remoteSheet->method('getCell') |
338
|
|
|
->will($this->returnValue($remoteCell)); |
339
|
|
|
|
340
|
|
|
$workbook = $this->getMockBuilder(Spreadsheet::class) |
341
|
|
|
->disableOriginalConstructor() |
342
|
|
|
->getMock(); |
343
|
|
|
$workbook->method('getSheetByName') |
344
|
|
|
->will($this->returnValue($remoteSheet)); |
345
|
|
|
|
346
|
|
|
$sheet = $this->getMockBuilder(Worksheet::class) |
347
|
|
|
->disableOriginalConstructor() |
348
|
|
|
->getMock(); |
349
|
|
|
$sheet->method('getCell') |
350
|
|
|
->will($this->returnValue($remoteCell)); |
351
|
|
|
$sheet->method('getParent') |
352
|
|
|
->will($this->returnValue($workbook)); |
353
|
|
|
|
354
|
|
|
$ourCell = $this->getMockBuilder(Cell::class) |
355
|
|
|
->disableOriginalConstructor() |
356
|
|
|
->getMock(); |
357
|
|
|
$ourCell->method('getWorksheet') |
358
|
|
|
->will($this->returnValue($sheet)); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
$result = Functions::isFormula($reference, $ourCell); |
362
|
|
|
self::assertEquals($expectedResult, $result, null, 1E-8); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function providerIsFormula() |
366
|
|
|
{ |
367
|
|
|
return require 'data/Calculation/Functions/ISFORMULA.php'; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @dataProvider providerIfCondition |
372
|
|
|
* |
373
|
|
|
* @param mixed $expectedResult |
374
|
|
|
*/ |
375
|
|
|
public function testIfCondition($expectedResult, ...$args) |
376
|
|
|
{ |
377
|
|
|
$result = Functions::ifCondition(...$args); |
|
|
|
|
378
|
|
|
self::assertEquals($expectedResult, $result); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
public function providerIfCondition() |
382
|
|
|
{ |
383
|
|
|
return require 'data/Calculation/Functions/IF_CONDITION.php'; |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|