Skip to content

🎥 Make videos programmatically with React

chores: refactor test avoiding reflection

Helder Lucas7e7d15973ff9
diff --git a/packages/lambda-php/src/PHPClient.php b/packages/lambda-php/src/PHPClient.php
index 50da8a5ca1..83d4438582 100644
--- a/packages/lambda-php/src/PHPClient.php
+++ b/packages/lambda-php/src/PHPClient.php
@@ -65,7 +65,7 @@ class PHPClient
/**
* Generate a SHA256 hash for the payload
*/
- private function generateHash(string $payload): string
+ protected function generateHash(string $payload): string
{
return hash('sha256', $payload);
}
@@ -73,7 +73,7 @@ class PHPClient
/**
* Generate a random hash for bucket operations
*/
- private function generateRandomHash(): string
+ protected function generateRandomHash(): string
{
$alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
@@ -83,7 +83,7 @@ class PHPClient
/**
* Generate a bucket name following Remotion conventions
*/
- private function makeBucketName(): string
+ protected function makeBucketName(): string
{
$regionNoDashes = str_replace('-', '', $this->region);
$randomSuffix = $this->generateRandomHash();
@@ -94,7 +94,7 @@ class PHPClient
/**
* Generate S3 key for input props
*/
- private function inputPropsKey(string $hash): string
+ protected function inputPropsKey(string $hash): string
{
return "input-props/{$hash}.json";
}
@@ -209,7 +209,7 @@ class PHPClient
/**
* Determine if payload needs to be uploaded to S3
*/
- private function needsUpload(int $payloadSize, string $renderType): bool
+ protected function needsUpload(int $payloadSize, string $renderType): bool
{
// Constants based on AWS Lambda limits with margin for other payload data
$margin = 5_000 + 1_024; // 5KB margin + 1KB for webhook data
@@ -236,9 +236,6 @@ class PHPClient
/**
* Normalize payload to ensure valid JSON object is returned
- *
- * @param string|null $payload The JSON payload to normalize
- * @return string Valid JSON string, defaulting to empty object if invalid
*/
private function ensureValidPayload(?string $payload): string
{
@@ -364,7 +361,7 @@ class PHPClient
/**
* Serialize inputProps to a format compatible with Lambda
*/
- private function serializeInputProps($inputProps, string $region, string $type, ?string $userSpecifiedBucketName): array
+ protected function serializeInputProps($inputProps, string $region, string $type, ?string $userSpecifiedBucketName): array
{
try {
$payload = json_encode($inputProps, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
diff --git a/packages/lambda-php/tests/LargePayloadTest.php b/packages/lambda-php/tests/LargePayloadTest.php
index f5bc92abf3..4f8899d57c 100644
--- a/packages/lambda-php/tests/LargePayloadTest.php
+++ b/packages/lambda-php/tests/LargePayloadTest.php
@@ -2,18 +2,14 @@
namespace Remotion\LambdaPhp\Tests;
+use Exception;
use PHPUnit\Framework\TestCase;
use Remotion\LambdaPhp\PHPClient;
-use ReflectionClass;
-use ReflectionMethod;
+use Remotion\LambdaPhp\RenderParams;
-/**
- * Test large payload compression and S3 upload functionality
- */
class LargePayloadTest extends TestCase
{
private PHPClient $client;
- private ReflectionClass $reflectionClass;
protected function setUp(): void
{
@@ -24,138 +20,35 @@ class LargePayloadTest extends TestCase
null,
false
);
- $this->reflectionClass = new ReflectionClass(PHPClient::class);
}
- /**
- * Helper method to call private methods
- */
- private function callPrivateMethod(string $methodName, array $args = [])
+ public function testSmallPayloadUsesInlineFormat(): void
{
- $method = $this->reflectionClass->getMethod($methodName);
- $method->setAccessible(true);
- return $method->invokeArgs($this->client, $args);
- }
-
- /**
- * Test that small payloads use the payload format
- */
- public function testSmallPayloadUsesPayloadFormat(): void
- {
- $smallProps = ['message' => 'small'];
- $result = $this->callPrivateMethod('serializeInputProps', [
- $smallProps,
- 'us-east-1',
- 'video-or-audio',
- null
- ]);
-
- $this->assertEquals('payload', $result['type']);
- $this->assertArrayHasKey('payload', $result);
- $this->assertArrayNotHasKey('hash', $result);
- $this->assertArrayNotHasKey('bucketName', $result);
- }
-
- /**
- * Test the logic for determining when upload is needed
- */
- public function testNeedsUploadLogic(): void
- {
- // Small payload should not need upload
- $this->assertFalse($this->callPrivateMethod('needsUpload', [1000, 'video-or-audio']));
- $this->assertFalse($this->callPrivateMethod('needsUpload', [1000, 'still']));
-
- // Large payload for video should need upload
- $this->assertTrue($this->callPrivateMethod('needsUpload', [200000, 'video-or-audio']));
+ $params = new RenderParams(data: ['message' => 'small']);
+ $params->setComposition('react-svg');
- // Large payload for still should need upload (over 5MB)
- $this->assertTrue($this->callPrivateMethod('needsUpload', [5000000, 'still']));
+ $result = $this->client->constructInternals($params);
- // Medium payload for still should not need upload
- $this->assertFalse($this->callPrivateMethod('needsUpload', [1000000, 'still']));
+ $this->assertEquals('payload', $result['inputProps']['type']);
}
- /**
- * Test that hash generation is consistent
- */
- public function testHashGeneration(): void
+ public function testLargePayloadWouldUseBucketUrl(): void
{
- $payload = '{"test": "data"}';
- $hash1 = $this->callPrivateMethod('generateHash', [$payload]);
- $hash2 = $this->callPrivateMethod('generateHash', [$payload]);
-
- $this->assertEquals($hash1, $hash2);
- $this->assertIsString($hash1);
- $this->assertEquals(64, strlen($hash1)); // SHA256 hex string length
- }
-
- /**
- * Test S3 key generation for input props
- */
- public function testInputPropsKeyGeneration(): void
- {
- $hashValue = 'test123';
- $key = $this->callPrivateMethod('inputPropsKey', [$hashValue]);
- $this->assertEquals('input-props/test123.json', $key);
- }
+ $largeData = [];
+ foreach (range(1, 5000) as $item) {
+ $largeData[$item] = [
+ 'id' => $item,
+ 'description' => str_repeat('x', 100),
+ ];
+ }
- /**
- * Test bucket name generation following JS SDK conventions
- */
- public function testBucketNameGeneration(): void
- {
- $bucketName = $this->callPrivateMethod('makeBucketName', []);
-
- // Should start with remotionlambda- prefix
- $this->assertStringStartsWith('remotionlambda-', $bucketName);
-
- // Should contain region without dashes
- $expectedRegion = str_replace('-', '', 'us-east-1');
- $this->assertStringContainsString($expectedRegion, $bucketName);
-
- // Should be in format: remotionlambda-{region-no-dashes}-{random-hash}
- $parts = explode('-', $bucketName);
- $this->assertCount(3, $parts); // remotionlambda, region, hash
- $this->assertEquals('remotionlambda', $parts[0]);
- $this->assertEquals($expectedRegion, $parts[1]);
- $this->assertEquals(10, strlen($parts[2])); // random hash should be 10 chars
- }
-
- /**
- * Test random hash generation
- */
- public function testRandomHashGeneration(): void
- {
- $hash1 = $this->callPrivateMethod('generateRandomHash', []);
- $hash2 = $this->callPrivateMethod('generateRandomHash', []);
-
- // Should be 10 characters long
- $this->assertEquals(10, strlen($hash1));
- $this->assertEquals(10, strlen($hash2));
-
- // Should be different (extremely unlikely to be the same)
- $this->assertNotEquals($hash1, $hash2);
-
- // Should only contain lowercase alphanumeric characters
- $this->assertMatchesRegularExpression('/^[a-z0-9]{10}$/', $hash1);
- $this->assertMatchesRegularExpression('/^[a-z0-9]{10}$/', $hash2);
- }
-
- /**
- * Test payload size calculation matches expected thresholds
- */
- public function testPayloadSizeThresholds(): void
- {
- $margin = 5000 + 1024;
- $maxStillInlineSize = 5000000 - $margin;
- $maxVideoInlineSize = 200000 - $margin;
+ $params = new RenderParams(data: $largeData);
+ $params->setComposition('react-svg');
- // Test video threshold
- $this->assertFalse($this->callPrivateMethod('needsUpload', [$maxVideoInlineSize - 1, 'video-or-audio']));
- $this->assertTrue($this->callPrivateMethod('needsUpload', [$maxVideoInlineSize + 1, 'video-or-audio']));
+ // When a large payload is detected, it will attempt to upload to S3
+ // Without AWS credentials, this will fail, proving the S3 path is triggered
+ $this->expectException(Exception::class);
- // Test still threshold
- $this->assertFalse($this->callPrivateMethod('needsUpload', [$maxStillInlineSize - 1, 'still']));
- $this->assertTrue($this->callPrivateMethod('needsUpload', [$maxStillInlineSize + 1, 'still']));
+ $result = $this->client->constructInternals($params);
}
}