103 lines
3.7 KiB
PHP
103 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Business;
|
|
|
|
use App\Business\XlsxSheetReader;
|
|
use PHPUnit\Framework\TestCase;
|
|
use ZipArchive;
|
|
|
|
class XlsxSheetReaderTest extends TestCase
|
|
{
|
|
public function test_it_reads_worksheets_with_prefixed_xml_elements(): void
|
|
{
|
|
$path = tempnam(sys_get_temp_dir(), 'xlsx-prefixed') . '.xlsx';
|
|
$zip = new ZipArchive();
|
|
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
|
|
$zip->addFromString('xl/worksheets/sheet1.xml', <<<'XML'
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
|
<x:sheetData>
|
|
<x:row r="1">
|
|
<x:c r="A1" t="inlineStr"><x:is><x:t>店铺</x:t></x:is></x:c>
|
|
<x:c r="B1" t="inlineStr"><x:is><x:t>商品编码</x:t></x:is></x:c>
|
|
</x:row>
|
|
<x:row r="2">
|
|
<x:c r="A2" t="inlineStr"><x:is><x:t>天猫-测试店</x:t></x:is></x:c>
|
|
<x:c r="B2" t="inlineStr"><x:is><x:t>SKU-1</x:t></x:is></x:c>
|
|
</x:row>
|
|
</x:sheetData>
|
|
</x:worksheet>
|
|
XML);
|
|
$zip->close();
|
|
|
|
$rows = iterator_to_array((new XlsxSheetReader())->rows($path, ['店铺', '商品编码']));
|
|
|
|
$this->assertSame([['店铺' => '天猫-测试店', '商品编码' => 'SKU-1']], $rows);
|
|
|
|
@unlink($path);
|
|
}
|
|
|
|
public function test_it_can_select_a_named_worksheet(): void
|
|
{
|
|
$path = tempnam(sys_get_temp_dir(), 'xlsx-named-sheet') . '.xlsx';
|
|
$zip = new ZipArchive();
|
|
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
|
|
$zip->addFromString('xl/workbook.xml', <<<'XML'
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
|
|
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
|
|
<sheets>
|
|
<sheet name="汇总" sheetId="1" r:id="rId1"/>
|
|
<sheet name="发货明细表" sheetId="2" r:id="rId2"/>
|
|
</sheets>
|
|
</workbook>
|
|
XML);
|
|
$zip->addFromString('xl/_rels/workbook.xml.rels', <<<'XML'
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
|
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
|
|
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml"/>
|
|
</Relationships>
|
|
XML);
|
|
$zip->addFromString('xl/worksheets/sheet1.xml', <<<'XML'
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
|
<sheetData>
|
|
<row r="1"><c r="A1" t="inlineStr"><is><t>汇总</t></is></c></row>
|
|
<row r="2"><c r="A2" t="inlineStr"><is><t>不是明细</t></is></c></row>
|
|
</sheetData>
|
|
</worksheet>
|
|
XML);
|
|
$zip->addFromString('xl/worksheets/sheet2.xml', <<<'XML'
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
|
<sheetData>
|
|
<row r="1">
|
|
<c r="A1" t="inlineStr"><is><t>店铺</t></is></c>
|
|
<c r="B1" t="inlineStr"><is><t>商品编码</t></is></c>
|
|
</row>
|
|
<row r="2">
|
|
<c r="A2" t="inlineStr"><is><t>天猫-HX旗舰店101</t></is></c>
|
|
<c r="B2" t="inlineStr"><is><t>SKU-NAMED</t></is></c>
|
|
</row>
|
|
</sheetData>
|
|
</worksheet>
|
|
XML);
|
|
$zip->close();
|
|
|
|
$rows = iterator_to_array((new XlsxSheetReader())->rows(
|
|
$path,
|
|
['店铺', '商品编码'],
|
|
[],
|
|
'发货明细表',
|
|
));
|
|
|
|
$this->assertSame(
|
|
[['店铺' => '天猫-HX旗舰店101', '商品编码' => 'SKU-NAMED']],
|
|
$rows,
|
|
);
|
|
|
|
@unlink($path);
|
|
}
|
|
}
|