diff --git a/.github/workflows/test-phpunit.yml b/.github/workflows/test-phpunit.yml index 94ffde1..db17e8c 100644 --- a/.github/workflows/test-phpunit.yml +++ b/.github/workflows/test-phpunit.yml @@ -68,7 +68,7 @@ jobs: fi - name: Test with PHPUnit - run: vendor/bin/phpunit tests/ --coverage-text --coverage-clover build/logs/clover.xml --coverage-php build/cov/coverage.cov --testsuite main + run: vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml --coverage-php build/cov/coverage.cov --testsuite main env: TERM: xterm-256color TACHYCARDIA_MONITOR_GA: enabled diff --git a/.gitignore b/.gitignore index b29f218..4f1868a 100644 --- a/.gitignore +++ b/.gitignore @@ -54,6 +54,8 @@ tests/coverage* # Don't save phpunit under version control. phpunit +tests/_support/result/* +!tests/_support/result/.gitkeep #------------------------- # Composer diff --git a/tests/SecureTest.php b/tests/SecureTest.php index c979868..b9ded25 100644 --- a/tests/SecureTest.php +++ b/tests/SecureTest.php @@ -11,7 +11,9 @@ namespace Tests; +use DateTimeImmutable; use PHPDevsr\Spreadsheet\Secure; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -24,20 +26,79 @@ final class SecureTest extends TestCase */ private static string $folderSupport = ''; + /** + * Result of Test + */ + private static string $folderSupportResult = ''; + protected function setUp(): void { - self::$folderSupport = './tests/_support/'; + self::$folderSupport = './tests/_support/'; + self::$folderSupportResult = self::$folderSupport . 'result/'; + + if ($handlePath = opendir(self::$folderSupportResult)) { + $date = new DateTimeImmutable(); + + while (false !== ($file = readdir($handlePath))) { + if ($file === '.' || $file === '..' || $file === '.gitkeep') { + continue; + } - if (file_exists(self::$folderSupport . 'bb.xlsx')) { - unlink(self::$folderSupport . 'bb.xlsx'); + $fileLastModified = filemtime(self::$folderSupportResult . $file); + + if ($date->getTimestamp() > $fileLastModified && is_file(self::$folderSupportResult . $file)) { + unlink(self::$folderSupportResult . $file); + } + } + + closedir($handlePath); } } - public static function testEncryptor(): void + public static function dataProviderExcel(): iterable + { + yield from [ + 'Excel 2024' => [ + 'checkFiles' => 'excel2024.xlsx', + 'expectedFiles' => 'excel2024_result.xlsx', + ], + 'Excel 2024 - Strict' => [ + 'checkFiles' => 'excel2024strict.xlsx', + 'expectedFiles' => 'excel2024strict_result.xlsx', + ], + 'Excel Macro' => [ + 'checkFiles' => 'excelmacro.xlsm', + 'expectedFiles' => 'excelmacro_result.xlsm', + ], + 'Excel Binary' => [ + 'checkFiles' => 'excelbinary.xlsb', + 'expectedFiles' => 'excelbinary_result.xlsb', + ], + 'Excel 97-2003' => [ + 'checkFiles' => 'excel97.xls', + 'expectedFiles' => 'excel97_result.xls', + ], + 'Excel 95' => [ + 'checkFiles' => 'excel95.xls', + 'expectedFiles' => 'excel95_result.xls', + ], + 'CSV UTF-8' => [ + 'checkFiles' => 'csvutf8.csv', + 'expectedFiles' => 'csvutf8_result.csv', + ], + 'CSV Unknown' => [ + 'checkFiles' => 'csvunknown.csv', + 'expectedFiles' => 'csvunknown_result.csv', + ], + ]; + } + + #[DataProvider('dataProviderExcel')] + public static function testEncryptor(string $checkFiles = '', string $expectedFiles = ''): void { - (new Secure())->setFile(self::$folderSupport . 'Book1.xlsx')->setPassword('111')->output(self::$folderSupport . 'bb.xlsx'); + (new Secure())->setFile(self::$folderSupport . $checkFiles)->setPassword('111')->output(self::$folderSupportResult . $expectedFiles); - self::assertFileExists(self::$folderSupport . 'bb.xlsx'); + self::assertFileExists(self::$folderSupportResult . $expectedFiles); } public static function testEncryptorWithBinaryData(): void diff --git a/tests/_support/.gitkeep b/tests/_support/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/_support/csvunknown.csv b/tests/_support/csvunknown.csv new file mode 100644 index 0000000..fc05e53 --- /dev/null +++ b/tests/_support/csvunknown.csv @@ -0,0 +1,20 @@ +1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20 +2;2;;;;;;;;;;;;;;;;;; +3;;3;;;;;;;;;;;;;;;;; +4;;;4;;;;;;;;;;;;;;;; +5;;;;5;;;;;;;;;;;;;;; +6;;;;;6;;;;;;;;;;;;;; +7;;;;;;7;;;;;;;;;;;;; +8;;;;;;;8;;;;;;;;;;;; +9;;;;;;;;9;;;;;;;;;;; +10;;;;;;;;;10;;;;;;;;;; +11;;;;;;;;;;11;;;;;;;;; +12;;;;;;;;;;;12;;;;;;;; +13;;;;;;;;;;;;13;;;;;;; +14;;;;;;;;;;;;;14;;;;;; +15;;;;;;;;;;;;;;15;;;;; +16;;;;;;;;;;;;;;;16;;;; +17;;;;;;;;;;;;;;;;17;;; +18;;;;;;;;;;;;;;;;;18;; +19;;;;;;;;;;;;;;;;;;19; +20;;;;;;;;;;;;;;;;;;;20 diff --git a/tests/_support/csvutf8.csv b/tests/_support/csvutf8.csv new file mode 100644 index 0000000..c62fade --- /dev/null +++ b/tests/_support/csvutf8.csv @@ -0,0 +1,20 @@ +1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20 +2;2;;;;;;;;;;;;;;;;;; +3;;3;;;;;;;;;;;;;;;;; +4;;;4;;;;;;;;;;;;;;;; +5;;;;5;;;;;;;;;;;;;;; +6;;;;;6;;;;;;;;;;;;;; +7;;;;;;7;;;;;;;;;;;;; +8;;;;;;;8;;;;;;;;;;;; +9;;;;;;;;9;;;;;;;;;;; +10;;;;;;;;;10;;;;;;;;;; +11;;;;;;;;;;11;;;;;;;;; +12;;;;;;;;;;;12;;;;;;;; +13;;;;;;;;;;;;13;;;;;;; +14;;;;;;;;;;;;;14;;;;;; +15;;;;;;;;;;;;;;15;;;;; +16;;;;;;;;;;;;;;;16;;;; +17;;;;;;;;;;;;;;;;17;;; +18;;;;;;;;;;;;;;;;;18;; +19;;;;;;;;;;;;;;;;;;19; +20;;;;;;;;;;;;;;;;;;;20 diff --git a/tests/_support/excel2024.xlsx b/tests/_support/excel2024.xlsx new file mode 100644 index 0000000..eb0d023 Binary files /dev/null and b/tests/_support/excel2024.xlsx differ diff --git a/tests/_support/excel2024strict.xlsx b/tests/_support/excel2024strict.xlsx new file mode 100644 index 0000000..9a31da5 Binary files /dev/null and b/tests/_support/excel2024strict.xlsx differ diff --git a/tests/_support/excel95.xls b/tests/_support/excel95.xls new file mode 100644 index 0000000..fcf601c Binary files /dev/null and b/tests/_support/excel95.xls differ diff --git a/tests/_support/excel97.xls b/tests/_support/excel97.xls new file mode 100644 index 0000000..a2631a7 Binary files /dev/null and b/tests/_support/excel97.xls differ diff --git a/tests/_support/excelbinary.xlsb b/tests/_support/excelbinary.xlsb new file mode 100644 index 0000000..767147e Binary files /dev/null and b/tests/_support/excelbinary.xlsb differ diff --git a/tests/_support/excelmacro.xlsm b/tests/_support/excelmacro.xlsm new file mode 100644 index 0000000..5139255 Binary files /dev/null and b/tests/_support/excelmacro.xlsm differ