Skip to content

Commit

Permalink
Merge pull request #468: add ProtoBinary decoder in default set
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jul 2, 2024
2 parents b290246 + ebb28cc commit 8da341d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/DataConverter/DataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static function createDefault(): DataConverterInterface
new NullConverter(),
new BinaryConverter(),
new ProtoJsonConverter(),
new ProtoConverter(),
new JsonConverter()
);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/DataConverter/DataConverterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
use PHPUnit\Framework\Attributes\DataProvider;
use Temporal\DataConverter\DataConverter;
use Temporal\DataConverter\DataConverterInterface;
use Temporal\DataConverter\EncodingKeys;
use Temporal\DataConverter\ProtoConverter;
use Temporal\DataConverter\ProtoJsonConverter;
use Temporal\DataConverter\Type;
use Temporal\Exception\DataConverterException;
use Temporal\Tests\Proto\Test;
use Temporal\Tests\Unit\AbstractUnit;

/**
Expand Down Expand Up @@ -177,4 +181,28 @@ public function testNullableTypeCoercion(string $type, $value): void

$this->assertNull($converter->fromPayload($payload, $type));
}

/**
* DataConverter may decode protobuf binary messages
*/
public function testProtobufBinaryDecoding(): void
{
$message = (new Test())->setValue('foo');
$payload = (new ProtoConverter())->toPayload($message);

$decoded = DataConverter::createDefault()->fromPayload($payload, Test::class);

self::assertEquals($message, $decoded);
}

/**
* DataConverter decodes Protobuf messages using ProtoJson converter
*/
public function testProtobufEncodingToJson(): void
{
$message = (new Test())->setValue('foo');
$payload = DataConverter::createDefault()->toPayload($message);

self::assertSame(EncodingKeys::METADATA_ENCODING_PROTOBUF_JSON, $payload->getMetadata()['encoding']);
}
}
9 changes: 4 additions & 5 deletions tests/Unit/Schedule/Action/StartWorkflowActionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,21 @@ public function testWithWorkflowTypeObject(): void
public function testWithWorkflowId(): void
{
$init = StartWorkflowAction::new('TestWorkflow');
$initId = $init->workflowId;
$new = $init->withWorkflowId('workflow-id');

$this->assertNotSame($init, $new, 'immutable method clones object');
$this->assertSame('', $init->workflowId, 'init value was not changed');
$this->assertSame($initId, $init->workflowId, 'init value was not changed');
$this->assertSame('workflow-id', $new->workflowId);
}

public function testWithEmptyWorkflowId(): void
{
$init = StartWorkflowAction::new('TestWorkflow')->withWorkflowId('test-id');

$new = $init->withWorkflowId('');
$this->expectException(\InvalidArgumentException::class);

$this->assertNotSame($init, $new, 'immutable method clones object');
$this->assertSame('test-id', $init->workflowId, 'init value was not changed');
$this->assertSame('', $new->workflowId);
$init->withWorkflowId('');
}

public function testWithTaskQueue(): void
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/Schedule/ScheduleClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ public function testCreateSchedule(): void
$this->assertSame('default', $testContext->request->getNamespace());
$this->assertSame('test-id', $testContext->request->getScheduleId());
$this->assertSame('test-id', $result->getID());
$this->assertSame(
'workflow-id',
$testContext->request->getSchedule()->getAction()->getStartWorkflow()->getWorkflowId(),
);
}

public function testCreateScheduleWithoutWorkflowId(): void
{
$testContext = new class {
public CreateScheduleRequest $request;
};
// Prepare mocks
$clientMock = $this->createMock(ServiceClientInterface::class);
$clientMock->expects($this->once())
->method('CreateSchedule')
->with($this->callback(fn(CreateScheduleRequest $request) => $testContext->request = $request or true))
->willReturn((new CreateScheduleResponse())->setConflictToken('test-conflict-token'));
$scheduleClient = $this->createScheduleClient(
client: $clientMock,
);
$result = $scheduleClient->createSchedule(
Schedule::new()->withAction(
StartWorkflowAction::new('PingSite')
),
scheduleId: 'test-id',
);

$this->assertTrue(isset($testContext->request));
$this->assertNotEmpty($testContext->request->getSchedule()->getAction()->getStartWorkflow()->getWorkflowId());
}

public function testListSchedules(): void
Expand Down

0 comments on commit 8da341d

Please sign in to comment.