VOOZH about

URL: https://deepwiki.com/rudderlabs/rudder-php-sdk/5.1-test-suite-structure

⇱ Test Suite Structure | rudderlabs/rudder-php-sdk | DeepWiki


Loading...
Menu

Test Suite Structure

The test/ directory contains a comprehensive suite of unit and integration tests designed to validate the SDK's core logic and its various transport consumers. The suite utilizes PHPUnit for test execution, donatj/MockWebServer for HTTP interception, and vlucas/phpdotenv for environment configuration.

Test Configuration and Environment

The test environment is orchestrated through a central PHPUnit configuration and environment variables.

PHPUnit Configuration (phpunit.xml)

The phpunit.xml file defines the testing environment, including:

  • Bootstrap: Loads vendor/autoload.php to initialize the class autoloader phpunit.xml6
  • Test Suites: Defines the analytics-php suite which targets the test directory phpunit.xml30-34
  • Coverage: Configured to process files in the lib directory and output Clover reports to build/logs/clover.xml phpunit.xml22-29
  • Error Handling: Converts errors, notices, and warnings to exceptions phpunit.xml8-10

Environment Loading (.env)

Tests rely on a .env file located at the project root for sensitive or configurable data like the WRITE_KEY. Each test class typically loads this in its setUpBeforeClass method using Dotenv\Dotenv test/AnalyticsTest.php18-23 test/ConsumerSocketTest.php17-23

Mock Web Server

To avoid hitting production RudderStack endpoints during testing, the suite uses donatj\MockWebServer.

Data Flow: Test Environment Setup


Sources: test/AnalyticsTest.php18-44 phpunit.xml1-41


Test Classes and Coverage

The test suite is partitioned by the specific component or consumer being validated.

AnalyticsTest

Validates the Rudder static facade. It ensures that calls to Rudder::track, Rudder::identify, etc., are correctly delegated and that input validation (e.g., requiring userId or anonymousId in group calls) functions as expected test/AnalyticsTest.php47-101

Consumer Tests

Each consumer implementation has a corresponding test file to verify its specific transport mechanics:

Test ClassTarget ComponentKey Validations
ConsumerLibCurlTestlib/Consumer/LibCurl.phpSynchronous curl execution, request compression test/ConsumerLibCurlTest.php38-213
ConsumerForkCurlTestlib/Consumer/ForkCurl.phpAsync "fire-and-forget" behavior, shell execution safety test/ConsumerForkCurlTest.php37-209
ConsumerSocketTestlib/Consumer/Socket.phpRaw TCP socket connection, timeout handling, and payload size enforcement test/ConsumerSocketTest.php35-241
ConsumerFileTestlib/Consumer/File.phpJSON line-delimited logging and integration with the SendBatchFromFile.php utility test/ConsumerFileTest.php56-244

Key Test Utilities

Code Entity Mapping: Consumer Testing


Sources: test/ConsumerLibCurlTest.php43-52 test/ConsumerSocketTest.php29-32 test/ConsumerFileTest.php61-70


Specialized Test Scenarios

The suite covers several edge cases and error conditions beyond standard event tracking.

Error Handling and Debug Mode

Tests verify that the SDK behaves differently depending on the debug flag:

Large Payload Handling

ConsumerSocketTest and ConsumerLibCurlTest include scenarios where payloads exceed the maximum allowed size (e.g., 32KB). These tests ensure the SDK returns false and refuses to enqueue items that would break transport limits test/ConsumerSocketTest.php216-240 test/ConsumerLibCurlTest.php215-244

Request Compression

Tests for LibCurl and ForkCurl consumers specifically validate the compress_request option. They ensure that when enabled, the transport layer correctly handles Gzip compression of the JSON batch test/ConsumerLibCurlTest.php190-213 test/ConsumerForkCurlTest.php186-209

Sequence: Socket Consumer Error Test


Sources: test/ConsumerSocketTest.php216-240 README.md51