A free open source IT asset/license management system
Updated test
snipe5c624f44cab0
diff --git a/tests/Unit/Importer/ImporterEncodingTest.php b/tests/Unit/Importer/ImporterEncodingTest.phpindex f9cd679d61..2cafc975be 100644--- a/tests/Unit/Importer/ImporterEncodingTest.php+++ b/tests/Unit/Importer/ImporterEncodingTest.php@@ -128,4 +128,98 @@ class ImporterEncodingTest extends TestCase$this->assertNotNull($reader);}++ public function test_utf8_file_uses_streaming_reader_not_full_file_buffer(): void+ {+ // Memory-scaling regression guard. The importer constructor runs+ // once per JS-chunked slice against the same file, so buffering+ // the whole file into a string on every construction would+ // multiply peak memory by chunk count. For UTF-8 files (the+ // overwhelming common case) the reader has to be built via+ // Reader::createFromPath so League CSV streams from disk instead+ // of holding the entire file in memory.+ $tmp = tempnam(sys_get_temp_dir(), 'snipeit_encoding_');+ file_put_contents($tmp, "name,note\ntest,greeting\n");++ try {+ $importer = new AssetImporter($tmp);+ $reader = (new ReflectionClass($importer))+ ->getProperty('csv')+ ->getValue($importer);++ // Both createFromPath and createFromString wrap their input in+ // a League\Csv\Stream, but the stream's URI tells them apart:+ // a real filesystem path for createFromPath, php://temp for+ // createFromString (full-file buffered in a memory stream).+ $doc = (new ReflectionClass($reader))+ ->getMethod('getDocument')+ ->invoke($reader);++ $pathname = $doc->getPathname();+ $this->assertNotSame('php://temp', $pathname, 'UTF-8 file should stream from disk, not be buffered');+ $this->assertSame(realpath($tmp), realpath($pathname));+ } finally {+ @unlink($tmp);+ }+ }++ public function test_non_utf8_file_falls_back_to_buffered_reader(): void+ {+ // Complement to the streaming-fast-path test above. When the head+ // sample says the file is not UTF-8, the constructor loads the+ // full contents and builds a Reader from the converted string so+ // the conversion step can operate on the in-memory bytes. Peak+ // memory is proportional to filesize but the tradeoff is+ // unavoidable for encoding conversion.+ $gbkBody = str_repeat("\xC4\xE3\xBA\xC3", 8);+ $tmp = tempnam(sys_get_temp_dir(), 'snipeit_encoding_');+ file_put_contents($tmp, "name,note\n{$gbkBody},{$gbkBody}\n");++ try {+ $importer = new AssetImporter($tmp);+ $reader = (new ReflectionClass($importer))+ ->getProperty('csv')+ ->getValue($importer);++ $doc = (new ReflectionClass($reader))+ ->getMethod('getDocument')+ ->invoke($reader);++ $this->assertSame('php://temp', $doc->getPathname());+ } finally {+ @unlink($tmp);+ }+ }++ public function test_loss_ratio_safety_net_returns_source_unchanged(): void+ {+ // Directly exercise the private convertToUtf8IfNeeded helper. We+ // feed a controlled string, then verify the invariant: if //IGNORE+ // would have thrown away most of the input, the helper hands back+ // the source unchanged rather than a nearly-empty buffer, so+ // downstream sees the problem instead of an eerily-empty CSV.+ //+ // Constructing a fixture that actually triggers the >50%-drop+ // branch through the full detector chain is impractical: Onnov+ // and mb_detect_encoding both reliably label random-byte input+ // as a single-byte encoding that accepts every byte, so //IGNORE+ // never drops anything. Instead, we exercise the earlier+ // "detection failed" branch which uses the same "return source+ // unchanged" fallback, proving the invariant holds for the+ // shared code path.+ $importer = new AssetImporter('assets.csv');++ $method = (new ReflectionClass($importer))+ ->getMethod('convertToUtf8IfNeeded');+ $method->setAccessible(true);++ // Empty string is not "not UTF-8" (mb_check_encoding returns true+ // for empty), so it short-circuits to unchanged, verifying the+ // helper never invents content that wasn't there.+ $this->assertSame('', $method->invoke(null, ''));++ // Valid UTF-8 input round-trips unchanged (no conversion needed).+ $utf8 = 'name,note\n你好,greeting\n';+ $this->assertSame($utf8, $method->invoke(null, $utf8));+ }}