Test Spreadsheet Features Reliably
Test values, workbook structure, security, integrations, and large-file behavior without brittle binary comparisons.
Updated
validateXlsx()assertValidXlsx()inspect()environmentCheck()What you will learn
- Test workbook meaning instead of raw ZIP bytes.
- Use temporary directories and deterministic fixtures.
- Build a CI matrix for optional extensions and database drivers.
Test round-trip values#
$path = sys_get_temp_dir() . '/mnb-test-' . bin2hex(random_bytes(6)) . '.xlsx';
MnbExcel::fromArray([
['SKU' => 'A-1', 'Amount' => 12.50],
])->withHeader()->save($path);
$rows = MnbExcel::read($path)
->withHeaderRow(1)
->toArray();
assert($rows[0]['SKU'] === 'A-1');
assert((float) $rows[0]['Amount'] === 12.50);
unlink($path);Validate the XLSX package#
MnbExcel::assertValidXlsx($path, [
'strict' => true,
]);
$inspection = MnbExcel::inspect($path);Test encryption as behavior#
MnbExcel::encryptXlsx($plain, $encrypted, 'TestPassword!');
assert(MnbExcel::isEncryptedXlsx($encrypted));
$rows = MnbExcel::read($encrypted, [
'password' => 'TestPassword!',
])->toArray();
try {
MnbExcel::read($encrypted, ['password' => 'wrong'])->toArray();
throw new RuntimeException('Wrong password unexpectedly succeeded.');
} catch (Throwable) {
// Expected failure.
}Use a capability matrix in CI#
| Job | Purpose |
|---|---|
| PHP 8.1 minimum | Protect the declared platform floor. |
| Current PHP | Catch new deprecations and behavior changes. |
| ZIP/XMLReader enabled | Exercise native streaming paths. |
| Portable fallback | Exercise pure-PHP compatibility paths. |
| MySQL/PostgreSQL/SQLite driver | Exercise SQL imports and PDO queues/schedules. |
| LibreOffice smoke test | Open and resave representative generated workbooks. |
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue