mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-25 17:38:15 +00:00
07271c83aa
* Rename Two Test Files When I run unit tests only for Reader/Xlsx, phpunit is issuing a deprecation message because the names of 2 files have an extra dot in them and thus don't match the class name in the file. I do not see these warnings when I run the entire test suite. * Remove Phpstan Annotations It was a bit difficult to handle a cast from mixed to string. * Fix Same Phpstan Problem in One Other Test This is the only other test case that tries to cast mixed to string.
93 lines
3.1 KiB
PHP
93 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
class NamespaceIssue2109bTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private static $testbook = 'tests/data/Reader/XLSX/issue2109b.xlsx';
|
|
|
|
public function testPreliminaries(): void
|
|
{
|
|
$file = 'zip://';
|
|
$file .= self::$testbook;
|
|
$file .= '#xl/workbook.xml';
|
|
$data = file_get_contents($file);
|
|
// confirm that file contains expected namespaced xml tag
|
|
if ($data === false) {
|
|
self::fail('Unable to read file');
|
|
} else {
|
|
self::assertStringContainsString('<x:workbook ', $data);
|
|
}
|
|
}
|
|
|
|
public function testInfo(): void
|
|
{
|
|
$reader = new Xlsx();
|
|
$workSheetInfo = $reader->listWorkSheetInfo(self::$testbook);
|
|
$info0 = $workSheetInfo[0];
|
|
self::assertEquals('Sheet1', $info0['worksheetName']);
|
|
self::assertEquals('AF', $info0['lastColumnLetter']);
|
|
self::assertEquals(31, $info0['lastColumnIndex']);
|
|
self::assertEquals(4, $info0['totalRows']);
|
|
self::assertEquals(32, $info0['totalColumns']);
|
|
}
|
|
|
|
public function testSheetNames(): void
|
|
{
|
|
$reader = new Xlsx();
|
|
$worksheetNames = $reader->listWorksheetNames(self::$testbook);
|
|
self::assertEquals(['Sheet1'], $worksheetNames);
|
|
}
|
|
|
|
public function testActive(): void
|
|
{
|
|
$reader = new Xlsx();
|
|
$spreadsheet = $reader->load(self::$testbook);
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
self::assertSame('Sheet1', $sheet->getTitle());
|
|
self::assertNull($sheet->getFreezePane());
|
|
self::assertNull($sheet->getTopLeftCell());
|
|
self::assertSame('A1', $sheet->getSelectedCells());
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
private static function getCellValue(Worksheet $sheet, string $cell): string
|
|
{
|
|
$result = $sheet->getCell($cell)->getValue();
|
|
if (is_scalar($result) || (is_object($result) && method_exists($result, '__toString'))) {
|
|
return (string) $result;
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
public function testLoadXlsx(): void
|
|
{
|
|
$reader = new Xlsx();
|
|
$spreadsheet = $reader->load(self::$testbook);
|
|
$sheet = $spreadsheet->getSheet(0);
|
|
self::assertEquals('Sheet1', $sheet->getTitle());
|
|
$expectedArray = [
|
|
'A1' => 'Channel Name = Cartoon Network RSE',
|
|
'B2' => 'Event ID',
|
|
'C3' => '2021-05-17 03:00',
|
|
'F4' => 'The Internet',
|
|
'AF3' => '902476',
|
|
'AF4' => '902477',
|
|
'J2' => 'Episode Synopsis',
|
|
'J3' => 'Gumball and Darwin\'s reputation is challenged and they really couldn\'t care less...',
|
|
'J4' => 'Gumball accidentally uploads a video of himself and wants it gone.',
|
|
];
|
|
foreach ($expectedArray as $key => $value) {
|
|
self::assertSame($value, self::getCellValue($sheet, $key), "error in cell $key");
|
|
}
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|