4
4
5
5
namespace PhpOffice \PhpSpreadsheetTests \Cell ;
6
6
7
+ use DateTime ;
7
8
use PhpOffice \PhpSpreadsheet \Cell \Cell ;
8
9
use PhpOffice \PhpSpreadsheet \Cell \DataType ;
9
10
use PhpOffice \PhpSpreadsheet \Cell \IValueBinder ;
10
11
use PhpOffice \PhpSpreadsheet \Cell \StringValueBinder ;
12
+ use PhpOffice \PhpSpreadsheet \RichText \RichText ;
11
13
use PhpOffice \PhpSpreadsheet \Spreadsheet ;
12
14
use PHPUnit \Framework \TestCase ;
13
15
@@ -31,15 +33,24 @@ public function testStringValueBinderIgnoredErrorsDefault(): void
31
33
Cell::setValueBinder ($ valueBinder );
32
34
$ spreadsheet = new Spreadsheet ();
33
35
$ sheet = $ spreadsheet ->getActiveSheet ();
36
+ $ richText = new RichText ();
37
+ $ richText ->createTextRun ('6 ' );
38
+ $ richText2 = new RichText ();
39
+ $ richText2 ->createTextRun ('a ' );
34
40
$ sheet ->fromArray ([
35
41
[1 , 'x ' , 3.2 ],
36
42
['y ' , -5 , 'z ' ],
43
+ [new DateTime (), $ richText , $ richText2 ],
44
+ [new StringableObject ('a ' ), new StringableObject (2 ), 'z ' ],
37
45
]);
38
46
$ ignoredCells = [];
39
47
foreach ($ sheet ->getRowIterator () as $ row ) {
40
48
foreach ($ row ->getCellIterator () as $ cell ) {
41
49
$ coordinate = $ cell ->getCoordinate ();
42
- self ::assertSame (DataType::TYPE_STRING , $ cell ->getDataType (), "not string for cell $ coordinate " );
50
+ $ dataType = $ cell ->getDataType ();
51
+ if ($ dataType !== DataType::TYPE_INLINE ) {
52
+ self ::assertSame (DataType::TYPE_STRING , $ dataType , "not string for cell $ coordinate " );
53
+ }
43
54
if ($ cell ->getIgnoredErrors ()->getNumberStoredAsText ()) {
44
55
$ ignoredCells [] = $ coordinate ;
45
56
}
@@ -56,21 +67,30 @@ public function testStringValueBinderIgnoredErrorsTrue(): void
56
67
Cell::setValueBinder ($ valueBinder );
57
68
$ spreadsheet = new Spreadsheet ();
58
69
$ sheet = $ spreadsheet ->getActiveSheet ();
70
+ $ richText = new RichText ();
71
+ $ richText ->createTextRun ('6 ' );
72
+ $ richText2 = new RichText ();
73
+ $ richText2 ->createTextRun ('a ' );
59
74
$ sheet ->fromArray ([
60
75
[1 , 'x ' , 3.2 ],
61
76
['y ' , -5 , 'z ' ],
77
+ [new DateTime (), $ richText , $ richText2 ],
78
+ [new StringableObject ('a ' ), new StringableObject (2 ), 'z ' ],
62
79
]);
63
80
$ ignoredCells = [];
64
81
foreach ($ sheet ->getRowIterator () as $ row ) {
65
82
foreach ($ row ->getCellIterator () as $ cell ) {
66
83
$ coordinate = $ cell ->getCoordinate ();
67
- self ::assertSame (DataType::TYPE_STRING , $ cell ->getDataType (), "not string for cell $ coordinate " );
84
+ $ dataType = $ cell ->getDataType ();
85
+ if ($ dataType !== DataType::TYPE_INLINE ) {
86
+ self ::assertSame (DataType::TYPE_STRING , $ dataType , "not string for cell $ coordinate " );
87
+ }
68
88
if ($ cell ->getIgnoredErrors ()->getNumberStoredAsText ()) {
69
89
$ ignoredCells [] = $ coordinate ;
70
90
}
71
91
}
72
92
}
73
- self ::assertSame (['A1 ' , 'C1 ' , 'B2 ' ], $ ignoredCells );
93
+ self ::assertSame (['A1 ' , 'C1 ' , 'B2 ' , ' B3 ' , ' B4 ' ], $ ignoredCells );
74
94
$ spreadsheet ->disconnectWorksheets ();
75
95
}
76
96
@@ -82,22 +102,28 @@ public function testStringValueBinderPreserveNumeric(): void
82
102
Cell::setValueBinder ($ valueBinder );
83
103
$ spreadsheet = new Spreadsheet ();
84
104
$ sheet = $ spreadsheet ->getActiveSheet ();
105
+ $ richText = new RichText ();
106
+ $ richText ->createTextRun ('6 ' );
107
+ $ richText2 = new RichText ();
108
+ $ richText2 ->createTextRun ('a ' );
85
109
$ sheet ->fromArray ([
86
110
[1 , 'x ' , 3.2 ],
87
111
['y ' , -5 , 'z ' ],
112
+ [new DateTime (), $ richText , $ richText2 ],
113
+ [new StringableObject ('a ' ), new StringableObject (2 ), 'z ' ],
88
114
]);
89
115
$ ignoredCells = [];
90
116
foreach ($ sheet ->getRowIterator () as $ row ) {
91
117
foreach ($ row ->getCellIterator () as $ cell ) {
92
118
$ coordinate = $ cell ->getCoordinate ();
93
- $ expected = is_numeric ( $ cell ->getValue ()) ? DataType::TYPE_NUMERIC : DataType::TYPE_STRING ;
119
+ $ expected = ( is_int ( $ cell ->getValue ()) || is_float ( $ cell -> getValue ())) ? DataType::TYPE_NUMERIC : (( $ cell -> getValue () instanceof RichText) ? DataType::TYPE_INLINE : DataType:: TYPE_STRING ) ;
94
120
self ::assertSame ($ expected , $ cell ->getDataType (), "wrong type for cell $ coordinate " );
95
121
if ($ cell ->getIgnoredErrors ()->getNumberStoredAsText ()) {
96
122
$ ignoredCells [] = $ coordinate ;
97
123
}
98
124
}
99
125
}
100
- self ::assertSame ([], $ ignoredCells );
126
+ self ::assertSame ([' B3 ' , ' B4 ' ], $ ignoredCells );
101
127
$ spreadsheet ->disconnectWorksheets ();
102
128
}
103
129
}
0 commit comments