|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Apify\Client\Tests\Unit; |
| 6 | + |
| 7 | +use Apify\Client\Internal\Compression; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +final class CompressionTest extends TestCase |
| 11 | +{ |
| 12 | + /** True when at least one compression codec is available in this PHP build. */ |
| 13 | + private static function hasCodec(): bool |
| 14 | + { |
| 15 | + return function_exists('brotli_compress') || function_exists('gzencode'); |
| 16 | + } |
| 17 | + |
| 18 | + public function testSmallBodyIsNotCompressed(): void |
| 19 | + { |
| 20 | + $body = str_repeat('a', Compression::MIN_COMPRESS_BYTES - 1); |
| 21 | + self::assertNull(Compression::maybeCompress($body)); |
| 22 | + } |
| 23 | + |
| 24 | + public function testBodyAtThresholdIsCompressed(): void |
| 25 | + { |
| 26 | + if (!self::hasCodec()) { |
| 27 | + self::markTestSkipped('no compression codec available in this PHP build'); |
| 28 | + } |
| 29 | + // A body of exactly MIN_COMPRESS_BYTES is compressed (the below-threshold case is covered by |
| 30 | + // testSmallBodyIsNotCompressed). |
| 31 | + $result = Compression::maybeCompress(str_repeat('a', Compression::MIN_COMPRESS_BYTES)); |
| 32 | + self::assertNotNull($result); |
| 33 | + [$encoding, $data] = $result; |
| 34 | + self::assertContains($encoding, ['br', 'gzip']); |
| 35 | + self::assertNotSame('', $data); |
| 36 | + } |
| 37 | + |
| 38 | + public function testCompressedBodyRoundTrips(): void |
| 39 | + { |
| 40 | + if (!self::hasCodec()) { |
| 41 | + self::markTestSkipped('no compression codec available in this PHP build'); |
| 42 | + } |
| 43 | + $original = json_encode(['items' => array_fill(0, 500, ['field' => 'value-with-some-length'])]); |
| 44 | + self::assertIsString($original); |
| 45 | + |
| 46 | + $result = Compression::maybeCompress($original); |
| 47 | + self::assertNotNull($result); |
| 48 | + [$encoding, $data] = $result; |
| 49 | + |
| 50 | + // Highly repetitive JSON must shrink. |
| 51 | + self::assertLessThan(strlen($original), strlen($data)); |
| 52 | + |
| 53 | + $decoded = self::decode($encoding, $data); |
| 54 | + self::assertSame($original, $decoded); |
| 55 | + } |
| 56 | + |
| 57 | + public function testPrefersBrotliWhenAvailableElseGzip(): void |
| 58 | + { |
| 59 | + if (!self::hasCodec()) { |
| 60 | + self::markTestSkipped('no compression codec available in this PHP build'); |
| 61 | + } |
| 62 | + $result = Compression::maybeCompress(str_repeat('payload-', 500)); |
| 63 | + self::assertNotNull($result); |
| 64 | + [$encoding] = $result; |
| 65 | + |
| 66 | + $expected = function_exists('brotli_compress') ? 'br' : 'gzip'; |
| 67 | + self::assertSame($expected, $encoding); |
| 68 | + } |
| 69 | + |
| 70 | + private static function decode(string $encoding, string $data): string |
| 71 | + { |
| 72 | + if ($encoding === 'br') { |
| 73 | + self::assertTrue(function_exists('brotli_uncompress'), 'brotli extension needed to decode'); |
| 74 | + // Called indirectly: the symbol only exists when the PECL brotli extension is loaded, |
| 75 | + // so a direct call would be an unresolved reference for static analysis. |
| 76 | + $brotliUncompress = 'brotli_uncompress'; |
| 77 | + $decoded = $brotliUncompress($data); |
| 78 | + } else { |
| 79 | + $decoded = gzdecode($data); |
| 80 | + } |
| 81 | + self::assertIsString($decoded); |
| 82 | + return $decoded; |
| 83 | + } |
| 84 | +} |
0 commit comments