ImportControllerTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Controllers;
  4. use App\Controllers\ImportController;
  5. use App\Tests\TestCase;
  6. use ReflectionMethod;
  7. /**
  8. * Phase 20 — pure-static guards on ImportController. Mirrors the
  9. * UserControllerTest pattern: exercise the bits that do not need PDO or
  10. * session wiring.
  11. */
  12. final class ImportControllerTest extends TestCase
  13. {
  14. public function testLooksLikeXlsxAcceptsRealZipHeader(): void
  15. {
  16. $tmp = tempnam(sys_get_temp_dir(), 'sptest');
  17. file_put_contents($tmp, "PK\x03\x04rest of the file");
  18. try {
  19. $this->assertTrue(self::call('looksLikeXlsx', 'workbook.xlsx', $tmp));
  20. $this->assertFalse(self::call('looksLikeXlsx', 'workbook.txt', $tmp), 'wrong extension');
  21. } finally {
  22. unlink($tmp);
  23. }
  24. }
  25. public function testLooksLikeXlsxRejectsNonZip(): void
  26. {
  27. $tmp = tempnam(sys_get_temp_dir(), 'sptest');
  28. file_put_contents($tmp, 'not a zip');
  29. try {
  30. $this->assertFalse(self::call('looksLikeXlsx', 'workbook.xlsx', $tmp));
  31. } finally {
  32. unlink($tmp);
  33. }
  34. }
  35. public function testLooksLikeXlsxRejectsTooShortFile(): void
  36. {
  37. $tmp = tempnam(sys_get_temp_dir(), 'sptest');
  38. file_put_contents($tmp, 'PK');
  39. try {
  40. $this->assertFalse(self::call('looksLikeXlsx', 'workbook.xlsx', $tmp));
  41. } finally {
  42. unlink($tmp);
  43. }
  44. }
  45. public function testUploadErrorCodeMapping(): void
  46. {
  47. $this->assertSame('too_big', self::call('uploadErrorCode', UPLOAD_ERR_INI_SIZE));
  48. $this->assertSame('too_big', self::call('uploadErrorCode', UPLOAD_ERR_FORM_SIZE));
  49. $this->assertSame('partial', self::call('uploadErrorCode', UPLOAD_ERR_PARTIAL));
  50. $this->assertSame('no_file', self::call('uploadErrorCode', UPLOAD_ERR_NO_FILE));
  51. $this->assertSame('server', self::call('uploadErrorCode', UPLOAD_ERR_NO_TMP_DIR));
  52. $this->assertSame('server', self::call('uploadErrorCode', UPLOAD_ERR_CANT_WRITE));
  53. $this->assertSame('unknown', self::call('uploadErrorCode', 9999));
  54. }
  55. /**
  56. * Reflectively call a private static helper on ImportController so we
  57. * don't need to expand its public surface for testability.
  58. */
  59. private static function call(string $method, mixed ...$args): mixed
  60. {
  61. $r = new ReflectionMethod(ImportController::class, $method);
  62. $r->setAccessible(true);
  63. return $r->invoke(null, ...$args);
  64. }
  65. }