temporalio / sdk-php

Temporal PHP SDK
https://php.preview.thundergun.io
MIT License
266 stars 45 forks source link

[Bug] Can't run the tests according to documentation #441

Open Dirst opened 4 months ago

Dirst commented 4 months ago

Hello, the tests are not working though I did everything according to readme.

I have symfony app.

My worker.test.php

require dirname(__DIR__) . '/vendor/autoload.php';

use App\ProductImportProcess\App\Activity\ProductImportFlowActivity;
use App\ProductImportProcess\App\Workflow\ProductImportWorkflow;
use RoadRunner\VersionChecker\Version\Installed;
use RoadRunner\VersionChecker\VersionChecker;
use Temporal\Testing\WorkerFactory;
use Temporal\Worker\Transport\RoadRunner;
use Temporal\Worker\Transport\RoadRunnerVersionChecker;

$factory = WorkerFactory::create();
$worker = $factory->newWorker();

/**
 * @TODO Сделать добавление автоматическим.
 */
$worker->registerWorkflowTypes(ProductImportWorkflow::class);
$worker->registerActivity(ProductImportFlowActivity::class);
//$worker->registerActivity(FetchProductTemplateActivity::class);
$factory->run(
    RoadRunner::create(
        versionChecker: new RoadRunnerVersionChecker(
            checker: new VersionChecker(
                installedVersion: new Installed(executablePath: 'rr')
            )
        )
    )
);

My tests/.rr.test.yaml

version: "3"

rpc:
    listen: tcp://127.0.0.1:6001

server:
    command: "php /srv/app/tests/worker.test.php"

kv:
    test:
        driver: memory
        config:
            interval: 10

My tests bootstrap.php

use Symfony\Component\Dotenv\Dotenv;
use Temporal\Testing\Environment;

require dirname(__DIR__) . '/vendor/autoload.php';

if (file_exists(dirname(__DIR__) . '/config/bootstrap.php')) {
    require dirname(__DIR__) . '/config/bootstrap.php';
} elseif (method_exists(Dotenv::class, 'bootEnv')) {
    (new Dotenv())->bootEnv(dirname(__DIR__) . '/.env');
}

if ($_SERVER['APP_DEBUG']) {
    umask(0);
}

$environment = Environment::create();

$environment->startTemporalTestServer();
$environment->startRoadRunner('tests/rr serve -c .rr.test.yaml -w tests');

register_shutdown_function(fn () => $environment->stop());

My test

namespace App\Tests\New\Functional\Temporal;

use App\ProductImportProcess\App\Workflow\ProductImportWorkflow;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
use Temporal\Client\GRPC\ServiceClient;
use Temporal\Client\WorkflowClient;
use Temporal\Testing\ActivityMocker;

class ProductImportWorkflowTest extends TestCase
{
    private WorkflowClient $workflowClient;
    private ActivityMocker $activityMocks;

    protected function setUp(): void
    {
        $this->workflowClient = new WorkflowClient(ServiceClient::create(getenv('TEMPORAL_ADDRESS')));
        $this->activityMocks = new ActivityMocker();

        parent::setUp();
    }

    protected function tearDown(): void
    {
        $this->activityMocks->clear();
        parent::tearDown();
    }

    public function testWorkflow(): void
    {
        $this->activityMocks->expectCompletion('ProductImportFlowActivity.assertCategoryAssigned', 'world');
        $workflow = $this->workflowClient->newWorkflowStub(ProductImportWorkflow::class);
        $run = $this->workflowClient->start($workflow, Uuid::uuid4());

        $this->assertSame('world', $run->getResult('string'));
    }
}

I run my test with php unit and getting this - it just hangs image

roxblnfk commented 4 months ago

There is an example https://github.com/wolfy-j/temporal-simple-test-example/

roxblnfk commented 4 months ago

My tests/.rr.test.yaml

There is no temporal section in config

Dirst commented 4 months ago

Thanks, this is working now, may it is worth to add this information in Readme for testing framework.