@@ -12,23 +12,26 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
1212 /**
1313 * @return array
1414 */
15- public function dataProviderForExcelDateTest ()
15+ public function dataProviderForTestExcelDate ()
1616 {
1717 return [
18- [ CellValueFormatter::CELL_TYPE_NUMERIC , 42429 , '2016-02-29 00:00:00 ' ],
19- [ CellValueFormatter::CELL_TYPE_NUMERIC , '146098 ' , '2299-12-31 00:00:00 ' ],
20- [ CellValueFormatter::CELL_TYPE_NUMERIC , -700 , null ],
21- [ CellValueFormatter::CELL_TYPE_NUMERIC , 0 , null ],
22- [ CellValueFormatter::CELL_TYPE_NUMERIC , 0.5 , null ],
23- [ CellValueFormatter::CELL_TYPE_NUMERIC , 1 , '1900-01-01 00:00:00 ' ],
24- [ CellValueFormatter::CELL_TYPE_NUMERIC , 59.999988425926 , '1900-02-28 23:59:59 ' ],
25- [ CellValueFormatter::CELL_TYPE_NUMERIC , 60.458333333333 , '1900-02-28 11:00:00 ' ],
18+ [CellValueFormatter::CELL_TYPE_NUMERIC , 42429 , '2016-02-29 00:00:00 ' ],
19+ [CellValueFormatter::CELL_TYPE_NUMERIC , '146098 ' , '2299-12-31 00:00:00 ' ],
20+ [CellValueFormatter::CELL_TYPE_NUMERIC , -700 , null ],
21+ [CellValueFormatter::CELL_TYPE_NUMERIC , 0 , null ],
22+ [CellValueFormatter::CELL_TYPE_NUMERIC , 0.5 , null ],
23+ [CellValueFormatter::CELL_TYPE_NUMERIC , 1 , '1900-01-01 00:00:00 ' ],
24+ [CellValueFormatter::CELL_TYPE_NUMERIC , 59.999988425926 , '1900-02-28 23:59:59 ' ],
25+ [CellValueFormatter::CELL_TYPE_NUMERIC , 60.458333333333 , '1900-02-28 11:00:00 ' ],
2626 ];
2727 }
2828
2929 /**
30- * @dataProvider dataProviderForExcelDateTest
30+ * @dataProvider dataProviderForTestExcelDate
3131 *
32+ * @param string $cellType
33+ * @param int|float|string $nodeValue
34+ * @param string|null $expectedDateAsString
3235 * @return void
3336 */
3437 public function testExcelDate ($ cellType , $ nodeValue , $ expectedDateAsString )
@@ -39,16 +42,16 @@ public function testExcelDate($cellType, $nodeValue, $expectedDateAsString)
3942 ->expects ($ this ->atLeastOnce ())
4043 ->method ('item ' )
4144 ->with (0 )
42- ->will ($ this ->returnValue ((object )[ 'nodeValue ' => $ nodeValue ]));
45+ ->will ($ this ->returnValue ((object )['nodeValue ' => $ nodeValue ]));
4346
4447 $ nodeMock = $ this ->getMockBuilder ('DOMElement ' )->disableOriginalConstructor ()->getMock ();
4548
4649 $ nodeMock
4750 ->expects ($ this ->atLeastOnce ())
4851 ->method ('getAttribute ' )
4952 ->will ($ this ->returnValueMap ([
50- [ CellValueFormatter::XML_ATTRIBUTE_TYPE , $ cellType ],
51- [ CellValueFormatter::XML_ATTRIBUTE_STYLE_ID , 123 ],
53+ [CellValueFormatter::XML_ATTRIBUTE_TYPE , $ cellType ],
54+ [CellValueFormatter::XML_ATTRIBUTE_STYLE_ID , 123 ],
5255 ]));
5356
5457 $ nodeMock
@@ -57,17 +60,16 @@ public function testExcelDate($cellType, $nodeValue, $expectedDateAsString)
5760 ->with (CellValueFormatter::XML_NODE_VALUE )
5861 ->will ($ this ->returnValue ($ nodeListMock ));
5962
60- $ styleHelperMock = $ this ->getMockBuilder (__NAMESPACE__ . ' \StyleHelper ' )->disableOriginalConstructor ()->getMock ();
63+ $ styleHelperMock = $ this ->getMockBuilder (' Box\Spout\Reader\XLSX\Helper \StyleHelper ' )->disableOriginalConstructor ()->getMock ();
6164
6265 $ styleHelperMock
6366 ->expects ($ this ->once ())
6467 ->method ('shouldFormatNumericValueAsDate ' )
6568 ->with (123 )
6669 ->will ($ this ->returnValue (true ));
6770
68- $ instance = new CellValueFormatter (null , $ styleHelperMock );
69-
70- $ result = $ instance ->extractAndFormatNodeValue ($ nodeMock );
71+ $ formatter = new CellValueFormatter (null , $ styleHelperMock );
72+ $ result = $ formatter ->extractAndFormatNodeValue ($ nodeMock );
7173
7274 if ($ expectedDateAsString === null ) {
7375 $ this ->assertNull ($ result );
@@ -77,4 +79,48 @@ public function testExcelDate($cellType, $nodeValue, $expectedDateAsString)
7779 }
7880 }
7981
82+ /**
83+ * @return array
84+ */
85+ public function dataProviderForTestFormatNumericCellValueWithNumbers ()
86+ {
87+ return [
88+ [42 , 42 , 'integer ' ],
89+ [42.5 , 42.5 , 'double ' ],
90+ [-42 , -42 , 'integer ' ],
91+ [-42.5 , -42.5 , 'double ' ],
92+ ['42 ' , 42 , 'integer ' ],
93+ ['42.5 ' , 42.5 , 'double ' ],
94+ [865640023012945 , 865640023012945 , 'integer ' ],
95+ ['865640023012945 ' , 865640023012945 , 'integer ' ],
96+ [865640023012945.5 , 865640023012945.5 , 'double ' ],
97+ ['865640023012945.5 ' , 865640023012945.5 , 'double ' ],
98+ [PHP_INT_MAX , PHP_INT_MAX , 'integer ' ],
99+ [~PHP_INT_MAX + 1 , ~PHP_INT_MAX + 1 , 'integer ' ], // ~PHP_INT_MAX === PHP_INT_MIN, PHP_INT_MIN being PHP7+
100+ [PHP_INT_MAX + 1 , PHP_INT_MAX + 1 , 'double ' ],
101+ ];
102+ }
103+
104+ /**
105+ * @dataProvider dataProviderForTestFormatNumericCellValueWithNumbers
106+ *
107+ * @param int|float|string $value
108+ * @param int|float $expectedFormattedValue
109+ * @param string $expectedType
110+ * @return void
111+ */
112+ public function testFormatNumericCellValueWithNumbers ($ value , $ expectedFormattedValue , $ expectedType )
113+ {
114+ $ styleHelperMock = $ this ->getMockBuilder ('Box\Spout\Reader\XLSX\Helper\StyleHelper ' )->disableOriginalConstructor ()->getMock ();
115+ $ styleHelperMock
116+ ->expects ($ this ->once ())
117+ ->method ('shouldFormatNumericValueAsDate ' )
118+ ->will ($ this ->returnValue (false ));
119+
120+ $ formatter = new CellValueFormatter (null , $ styleHelperMock );
121+ $ formattedValue = \ReflectionHelper::callMethodOnObject ($ formatter , 'formatNumericCellValue ' , $ value , 0 );
122+
123+ $ this ->assertEquals ($ expectedFormattedValue , $ formattedValue );
124+ $ this ->assertEquals ($ expectedType , gettype ($ formattedValue ));
125+ }
80126}
0 commit comments