Skip to content

Commit

Permalink
feat: allow setting content type on command
Browse files Browse the repository at this point in the history
  • Loading branch information
fschmtt committed Jan 17, 2025
1 parent ba4f7e3 commit 8b8e017
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 39 deletions.
20 changes: 6 additions & 14 deletions src/Http/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,14 @@ public function __construct(
/** @var Representation|Collection|array<mixed>|null */
private readonly Representation|Collection|array|null $payload = null,
private readonly ?Criteria $criteria = null,
private readonly ?ContentType $contentType = null,
private readonly ContentType $contentType = ContentType::JSON,
) {}

public function getMethod(): Method
{
return $this->method;
}

public function getContentType(): ContentType
{
if ($this->contentType !== null) {
return $this->contentType;
}

if (is_array($this->payload)) {
return ContentType::FORM_DATA;
}

return ContentType::JSON;
}

public function getPath(): string
{
$placeholders = array_map(
Expand Down Expand Up @@ -75,4 +62,9 @@ public function getQuery(): string

return '?' . http_build_query($this->criteria->jsonSerialize());
}

public function getContentType(): ContentType
{
return $this->contentType;

Check warning on line 68 in src/Http/Command.php

View check run for this annotation

Codecov / codecov/patch

src/Http/Command.php#L68

Added line #L68 was not covered by tests
}
}
24 changes: 8 additions & 16 deletions src/Http/CommandExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,15 @@ public function __construct(

public function executeCommand(Command $command): void
{
$contentType = $command->getContentType();
$payload = $command->getPayload();

$options = [
'headers' => [
'Content-Type' => $contentType->value,
$options = match ($command->getContentType()) {
ContentType::JSON => [
'body' => $this->serializer->serialize($command->getPayload()),
'headers' => [
'Content-Type' => $command->getContentType()->value,
]
],
];

switch ($contentType) {
case ContentType::JSON:
$options['body'] = $this->serializer->serialize($payload);
break;
case ContentType::FORM_DATA:
$options['form_params'] = $payload;
break;
}
ContentType::FORM_PARAMS => ['form_params' => $command->getPayload()],
};

$this->client->request(
$command->getMethod()->value,
Expand Down
2 changes: 1 addition & 1 deletion src/Http/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
enum ContentType: string
{
case JSON = 'application/json';
case FORM_DATA = 'application/x-www-form-urlencoded';
case FORM_PARAMS = 'application/x-www-form-urlencoded';
}
2 changes: 2 additions & 0 deletions src/Resource/Organizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Fschmtt\Keycloak\Collection\OrganizationCollection;
use Fschmtt\Keycloak\Http\Command;
use Fschmtt\Keycloak\Http\ContentType;
use Fschmtt\Keycloak\Http\Criteria;
use Fschmtt\Keycloak\Http\Method;
use Fschmtt\Keycloak\Http\Query;
Expand Down Expand Up @@ -71,6 +72,7 @@ public function inviteUser(string $realm, string $id, string $email, string $fir
'firstName' => $firstName,
'lastName' => $lastName,
],
contentType: ContentType::FORM_PARAMS,
),
);
}
Expand Down
1 change: 0 additions & 1 deletion src/Resource/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ public function executeActionsEmail(string $realm, string $userId, ?array $actio
],
$actions,
$criteria,
ContentType::JSON,
),
);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Integration/Resource/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@ public function testGetUserCredentials(): void
static::assertNull($user);
}

public function testExecuteActionsEmail(): void
{
$users = $this->getKeycloak()->users();
$username = Uuid::uuid4()->toString();

$users->create('master', new User(
username: $username,
));

$user = $this->searchUserByUsername($username);
static::assertInstanceOf(User::class, $user);

$users->executeActionsEmail('master', $user->getId(), ['UPDATE_PASSWORD']);

$users->delete('master', $user->getId());

$user = $this->searchUserByUsername($username);
static::assertNull($user);
}

private function searchUserByUsername(string $username, string $realm = 'master'): ?User
{
/** @var User|null $user */
Expand Down
11 changes: 5 additions & 6 deletions tests/Unit/Http/CommandExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Fschmtt\Keycloak\Http\Client;
use Fschmtt\Keycloak\Http\Command;
use Fschmtt\Keycloak\Http\CommandExecutor;
use Fschmtt\Keycloak\Http\ContentType;
use Fschmtt\Keycloak\Http\Method;
use Fschmtt\Keycloak\Json\JsonEncoder;
use Fschmtt\Keycloak\Serializer\Serializer;
Expand All @@ -30,7 +31,7 @@ public function testCallsClientWithoutBodyIfCommandHasNoRepresentation(): void
'body' => null,
'headers' => [
'Content-Type' => 'application/json',
],
]
],
);

Expand All @@ -43,7 +44,7 @@ public function testCallsClientWithoutBodyIfCommandHasNoRepresentation(): void
);
}

public function testCallsClientWithBodyIfCommandHasRepresentation(): void
public function testCallsClientWithJsonIfCommandHasRepresentation(): void
{
$command = new Command(
'/path/to/resource',
Expand Down Expand Up @@ -101,13 +102,14 @@ public function testCallsClientWithBodyIfCommandHasCollection(): void
$executor->executeCommand($command);
}

public function testCallsClientWithFormParamsIfCommandHasArrayPayload(): void
public function testCallsClientWithFormParamsIfCommandFormParamContentType(): void
{
$command = new Command(
'/path/to/resource',
Method::PUT,
[],
$payload = ['UPDATE_PASSWORD', 'VERIFY_EMAIL'],
contentType: ContentType::FORM_PARAMS,
);

$client = $this->createMock(Client::class);
Expand All @@ -118,9 +120,6 @@ public function testCallsClientWithFormParamsIfCommandHasArrayPayload(): void
'/path/to/resource',
[
'form_params' => $payload,
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
],
);

Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Resource/OrganizationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Fschmtt\Keycloak\Collection\OrganizationCollection;
use Fschmtt\Keycloak\Http\Command;
use Fschmtt\Keycloak\Http\CommandExecutor;
use Fschmtt\Keycloak\Http\ContentType;
use Fschmtt\Keycloak\Http\Method;
use Fschmtt\Keycloak\Http\Query;
use Fschmtt\Keycloak\Http\QueryExecutor;
Expand Down Expand Up @@ -148,6 +149,7 @@ public function testInviteUser(): void
'firstName' => 'first name',
'lastName' => 'last name',
],
contentType: ContentType::FORM_PARAMS,
);

$commandExecutor = $this->createMock(CommandExecutor::class);
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/Resource/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ public function testExecuteActionsEmail(): void
'realm' => 'test-realm',
'userId' => 'test-user-id',
],
contentType: ContentType::JSON,
);

$commandExecutor = $this->createMock(CommandExecutor::class);
Expand Down

0 comments on commit 8b8e017

Please sign in to comment.