6
6
use Box \Spout \Writer \Common \Creator \WriterEntityFactory ;
7
7
use Box \Spout \Writer \Common \Entity \Sheet ;
8
8
use Box \Spout \Writer \Exception \InvalidSheetNameException ;
9
+ use Box \Spout \Writer \Exception \WriterNotOpenedException ;
9
10
use Box \Spout \Writer \RowCreationHelper ;
10
11
use PHPUnit \Framework \TestCase ;
11
12
@@ -82,7 +83,7 @@ public function testSetSheetNameShouldThrowWhenNameIsAlreadyUsed()
82
83
*/
83
84
public function testSetSheetVisibilityShouldCreateSheetHidden ()
84
85
{
85
- $ fileName = 'test_set_visibility_should_create_sheet_hidden.xlsx ' ;
86
+ $ fileName = 'test_set_visibility_should_create_sheet_hidden.ods ' ;
86
87
$ this ->writeDataToHiddenSheet ($ fileName );
87
88
88
89
$ resourcePath = $ this ->getGeneratedResourcePath ($ fileName );
@@ -92,19 +93,60 @@ public function testSetSheetVisibilityShouldCreateSheetHidden()
92
93
$ this ->assertContains (' table:display="false" ' , $ xmlContents , 'The sheet visibility should have been changed to "hidden" ' );
93
94
}
94
95
95
- /**
96
- * @param string $fileName
97
- * @param string $sheetName
98
- * @return Sheet
99
- */
100
- private function writeDataAndReturnSheetWithCustomName ($ fileName , $ sheetName )
96
+ function testThrowsIfWorkbookIsNotInitialized ()
97
+ {
98
+ $ this ->expectException (WriterNotOpenedException::class);
99
+ $ writer = WriterEntityFactory::createODSWriter ();
100
+
101
+ $ writer ->addRow ($ this ->createRowFromValues ([]));
102
+ }
103
+
104
+ public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded ()
105
+ {
106
+ $ this ->expectException (WriterNotOpenedException::class);
107
+ $ writer = WriterEntityFactory::createXLSXWriter ();
108
+ $ writer ->setDefaultColumnWidth (10.0 );
109
+ }
110
+
111
+ public function testWritesDefaultCellSizesIfSet ()
112
+ {
113
+ $ fileName = 'test_writes_default_cell_sizes_if_set.ods ' ;
114
+ $ writer = $ this ->writerForFile ($ fileName );
115
+
116
+ $ writer ->setDefaultColumnWidth (100.0 );
117
+ $ writer ->setDefaultRowHeight (20.0 );
118
+ $ writer ->addRow ($ this ->createRowFromValues (['ods--11 ' , 'ods--12 ' ]));
119
+ $ writer ->close ();
120
+
121
+ $ resourcePath = $ this ->getGeneratedResourcePath ($ fileName );
122
+ $ pathToWorkbookFile = $ resourcePath . '#content.xml ' ;
123
+ $ xmlContents = file_get_contents ('zip:// ' . $ pathToWorkbookFile );
124
+
125
+ $ this ->assertContains (' style:column-width="100pt" ' , $ xmlContents , 'No default col width found in sheet ' );
126
+ $ this ->assertContains (' style:row-height="20pt" ' , $ xmlContents , 'No default row height found in sheet ' );
127
+ $ this ->assertContains (' style:use-optimal-row-height="false ' , $ xmlContents , 'No optimal row height override found in sheet ' );
128
+ }
129
+
130
+ private function writerForFile ($ fileName )
101
131
{
102
132
$ this ->createGeneratedFolderIfNeeded ($ fileName );
103
133
$ resourcePath = $ this ->getGeneratedResourcePath ($ fileName );
104
134
105
135
$ writer = WriterEntityFactory::createODSWriter ();
106
136
$ writer ->openToFile ($ resourcePath );
107
137
138
+ return $ writer ;
139
+ }
140
+
141
+ /**
142
+ * @param string $fileName
143
+ * @param string $sheetName
144
+ * @return void
145
+ */
146
+ private function writeDataAndReturnSheetWithCustomName ($ fileName , $ sheetName )
147
+ {
148
+ $ writer = $ this ->writerForFile ($ fileName );
149
+
108
150
$ sheet = $ writer ->getCurrentSheet ();
109
151
$ sheet ->setName ($ sheetName );
110
152
0 commit comments