Skip to content

Commit 017af86

Browse files
committedMay 7, 2023
Adds basic SpreadsheetML integration test
1 parent 5b6cb26 commit 017af86

File tree

7 files changed

+67
-7
lines changed

7 files changed

+67
-7
lines changed
 

‎.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ jobs:
2929
run: composer install
3030

3131
- name: Run tests
32-
run: make test
32+
run: make lint test

‎README.md

+12
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ Whether to disable the leading Byte Order Mark for the given encoding from being
325325

326326
### Class: \Quorum\Exporter\Engines\SpreadsheetMLEngine
327327

328+
---
329+
330+
#### Method: SpreadsheetMLEngine->setCreatedTime
331+
332+
```php
333+
function setCreatedTime(?int $createdTime) : void
334+
```
335+
336+
##### Parameters:
337+
338+
- ***int*** | ***null*** `$createdTime` - The timestamp to use for the created time. If null, the current time will be used.
339+
328340
### Class: \Quorum\Exporter\Exceptions\ExportException
329341

330342
### Class: \Quorum\Exporter\Exceptions\InvalidDataTypeException

‎phpunit.xml.dist

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
<directory>test</directory>
66
</testsuite>
77
</testsuites>
8-
<logging>
9-
<log type="coverage-clover" target="clover.xml"/>
10-
</logging>
8+
<coverage>
9+
<include>
10+
<directory suffix=".php">./src</directory>
11+
</include>
12+
</coverage>
1113
</phpunit>

‎src/Engines/SpreadsheetMLEngine.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class SpreadsheetMLEngine implements EngineInterface {
1111

1212
protected $autoIndex = 1;
1313

14+
protected ?int $createdTime = null;
15+
1416
public function processSheet( DataSheet $sheet ) : void {
1517
$outputStream = fopen("php://temp", "r+");
1618
foreach( $sheet as $dataRow ) {
@@ -89,12 +91,12 @@ protected function generateBaseXmlDocument() : string {
8991
$workbook->setAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
9092
$workbook->setAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
9193
$workbook->setAttribute('xmlns:ss', 'urn:schemas-microsoft-com:office:spreadsheet');
92-
$workbook->setAttribute('xmlns:html', 'http://www.w3.org/TR/REC-html40');
94+
// $workbook->setAttribute('xmlns:html', 'http://www.w3.org/TR/REC-html40');
9395
$doc->appendChild($workbook);
9496

9597
$documentProperties = $doc->createElement('DocumentProperties');
9698
$documentProperties->setAttribute('xmlns', 'urn:schemas-microsoft-com:office:office');
97-
$documentProperties->appendChild($doc->createElement('Created', date('c')));
99+
$documentProperties->appendChild($doc->createElement('Created', date('c', $this->createdTime ?: time())));
98100
$workbook->appendChild($documentProperties);
99101

100102
$styles = $doc->createElement('Styles');
@@ -152,4 +154,11 @@ protected function generateBaseXmlDocument() : string {
152154
return $doc->saveXML();
153155
}
154156

157+
/**
158+
* @param int|null $createdTime The timestamp to use for the created time. If null, the current time will be used.
159+
*/
160+
public function setCreatedTime( ?int $createdTime ) : void {
161+
$this->createdTime = $createdTime;
162+
}
163+
155164
}

‎test/Integration/CsvIntegrationTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace Integration;
44

5+
use PHPUnit\Framework\TestCase;
56
use Quorum\Exporter\DataExport;
67
use Quorum\Exporter\DataSheet;
78
use Quorum\Exporter\Engines\CsvEngine;
89

9-
class CsvIntegrationTest extends \PHPUnit\Framework\TestCase {
10+
class CsvIntegrationTest extends TestCase {
1011

1112
/**
1213
* @dataProvider encodingProvider
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Integration;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Quorum\Exporter\DataExport;
7+
use Quorum\Exporter\DataSheet;
8+
use Quorum\Exporter\Engines\SpreadsheetMLEngine;
9+
10+
class SpreadsheetMLTest extends TestCase {
11+
12+
public function test_SpreadsheetML() : void {
13+
$engine = new SpreadsheetMLEngine();
14+
$engine->setCreatedTime(518395400);
15+
$export = new DataExport($engine);
16+
$sheet = new DataSheet();
17+
$export->addSheet($sheet);
18+
19+
$temp = tmpfile();
20+
$meta = stream_get_meta_data($temp);
21+
$tmpFile = $meta['uri'];
22+
23+
$sheet->addRow([ 'test one', '日本語', 'test two' ]);
24+
$export->export($temp);
25+
26+
$file = __DIR__ . '/fixtures/basic-spreadsheet-ml.xml';
27+
28+
$this->assertXmlFileEqualsXmlFile($file, $tmpFile);
29+
30+
fclose($temp);
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0"?>
2+
<?mso-application progid="Excel.Sheet"?>
3+
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"><DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Created>1986-06-05T22:43:20+00:00</Created></DocumentProperties><Styles><Style ss:ID="Default" ss:Name="Normal"><Alignment ss:Vertical="Bottom"/></Style><Style ss:ID="s21"><Font ss:Bold="1"/></Style><Style ss:ID="s22"><Alignment ss:Vertical="Bottom" ss:WrapText="1"/></Style></Styles><Worksheet ss:Name="Sheet1"><Table><Row><Cell><Data ss:Type="String">test one</Data></Cell><Cell><Data ss:Type="String">日本語</Data></Cell><Cell><Data ss:Type="String">test two</Data></Cell></Row></Table></Worksheet></Workbook>

0 commit comments

Comments
 (0)
Please sign in to comment.