Unit Tests

This commit is contained in:
MarkBaker
2022-12-21 11:38:44 +01:00
parent 519d3a9f1e
commit 70e668a593
2 changed files with 35 additions and 0 deletions
@@ -2236,6 +2236,11 @@ class Worksheet implements IComparable
return $tableNames;
}
/**
* @param string $name the table name to search
*
* @return null|Table The table from the tables collection, or null if not found
*/
public function getTableByName(string $name): ?Table
{
$tableIndex = $this->getTableIndexByName($name);
@@ -2243,6 +2248,11 @@ class Worksheet implements IComparable
return ($tableIndex === null) ? null : $this->tableCollection[$tableIndex];
}
/**
* @param string $name the table name to search
*
* @return null|int The index of the located table in the tables collection, or null if not found
*/
protected function getTableIndexByName(string $name): ?int
{
$name = Shared\StringHelper::strToUpper($name);
@@ -4,8 +4,10 @@ namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
use Exception;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator;
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
@@ -506,4 +508,27 @@ class WorksheetTest extends TestCase
['I', true],
];
}
public function testGetTableNames(): void
{
$reader = new Xlsx();
$spreadsheet = $reader->load('tests/data/Worksheet/Table/TableFormulae.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$tables = $worksheet->getTableNames();
self::assertSame(['DeptSales'], $tables);
}
public function testGetTableByName(): void
{
$reader = new Xlsx();
$spreadsheet = $reader->load('tests/data/Worksheet/Table/TableFormulae.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$table = $worksheet->getTableByName('Non-existent Table');
self::assertNull($table);
$table = $worksheet->getTableByName('DeptSales');
self::assertInstanceOf(Table::class, $table);
}
}