PHP SDK developer's guide - Testing
The Testing section of the Temporal Application development guide describes the frameworks that facilitate Workflow and integration testing.
In the context of Temporal, you can create these types of automated tests:
- End-to-end: Running a Temporal Server and Worker with all its Workflows and Activities; starting and interacting with Workflows from a Client.
- Integration: Anything between end-to-end and unit testing.
- Running Activities with mocked Context and other SDK imports (and usually network requests).
- Running Workers with mock Activities, and using a Client to start Workflows.
- Running Workflows with mocked SDK imports.
- Unit: Running a piece of Workflow or Activity code (a function or method) and mocking any code it calls.
We generally recommend writing the majority of your tests as integration tests.
Because the test server supports skipping time, use the test server for both end-to-end and integration tests with Workers.
Test Activities
An Activity can be tested with a mock Activity environment, which provides a way to mock the Activity context, listen to Heartbeats, and cancel the Activity. This behavior allows you to test the Activity in isolation by calling it directly, without needing to create a Worker to run the Activity.
Test Workflows
Mock Activities
Mock the Activity invocation when unit testing your Workflows.
When integration testing Workflows with a Worker, you can mock Activities by providing mock Activity implementations to the Worker.
RoadRunner config
To mock an Activity in PHP, use RoadRunner Key-Value storage and add the following lines to your tests/.rr.test.yaml
file.
# tests/.rr.test.yaml
kv:
test:
driver: memory
config:
interval: 10
If you want to be able to mock Activities, use WorkerFactory
from the Temporal\Testing
Namespace
in your PHP Worker:
// worker.test.php
use Temporal\Testing\WorkerFactory;
$factory = WorkerFactory::create();
$worker = $factory->newWorker();
$worker->registerWorkflowTypes(MyWorkflow::class);
$worker->registerActivity(MyActivity::class);
$factory->run();
Then, in your tests to mock an Activity, use theActivityMocker
class.
Assume we have the following Activity:
#[ActivityInterface(prefix: "SimpleActivity.")]
interface SimpleActivityInterface
{
#[ActivityMethod('doSomething')]
public function doSomething(string $input): string;
To mock it in the test, you can do this:
final class SimpleWorkflowTestCase extends TestCase
{
private WorkflowClient $workflowClient;
private ActivityMocker $activityMocks;
protected function setUp(): void
{
$this->workflowClient = new WorkflowClient(ServiceClient::create('localhost:7233'));
$this->activityMocks = new ActivityMocker();
parent::setUp();
}
protected function tearDown(): void
{
$this->activityMocks->clear();
parent::tearDown();
}
public function testWorkflowReturnsUpperCasedInput(): void
{
$this->activityMocks->expectCompletion('SimpleActivity.doSomething', 'world');
$workflow = $this->workflowClient->newWorkflowStub(SimpleWorkflow::class);
$run = $this->workflowClient->start($workflow, 'hello');
$this->assertSame('world', $run->getResult('string'));
}
}
In the preceding test case, we do the following:
- Instantiate
ActivityMocker
in thesetUp()
method of the test. - Clear the cache after each test in
tearDown()
. - Mock an Activity call to return a string
world
.
To mock a failure, use the expectFailure()
method:
$this->activityMocks->expectFailure('SimpleActivity.echo', new \LogicException('something went wrong'));
Skip time
Some long-running Workflows can persist for months or even years. Implementing the test framework allows your Workflow code to skip time and complete your tests in seconds rather than the Workflow's specified amount.
For example, if you have a Workflow sleep for a day, or have an Activity failure with a long retry interval, you don't need to wait the entire length of the sleep period to test whether the sleep function works. Instead, test the logic that happens after the sleep by skipping forward in time and complete your tests in a timely manner.
Skipping time is not relevant to unit testing Workflow code, because in that case you’re mocking functions that take time, like sleep and Activity calls.
The test framework included in most SDKs is an in-memory implementation of Temporal Server that supports skipping time.
Time is a global property of an instance of TestWorkflowEnvironment
: skipping time (either automatically or manually) applies to all currently running tests.
If you need different time behaviors for different tests, run your tests in a series or with separate instances of the test server.
For example, you could run all tests with automatic time skipping in parallel, and then all tests with manual time skipping in series, and then all tests without time skipping in parallel.
Setting up
Learn to set up the time-skipping test framework in the SDK of your choice.
- In the
tests
folder, createbootstrap.php
with the following contents:
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Temporal\Testing\Environment;
$environment = Environment::create();
$environment->start();
register_shutdown_function(fn () => $environment->stop());
If you don't want to run the test server with all of your tests, you can add a condition to start a test only if the RUN_TEMPORAL_TEST_SERVER
environment variable is present:
if (getenv('RUN_TEMPORAL_TEST_SERVER') !== false) {
$environment = Environment::create();
$environment->start('./rr serve -c .rr.silent.yaml -w tests');
register_shutdown_function(fn() => $environment->stop());
}
- Add
bootstrap.php
and theTEMPORAL_ADDRESS
environment variable tophpunit.xml
:
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="tests/bootstrap.php"
>
<php>
<env name="TEMPORAL_ADDRESS" value="127.0.0.1:7233" />
</php>
</phpunit>
- Add the test server executable to
.gitignore
:
temporal-test-server