-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility to call custom endpoints
- Loading branch information
Showing
9 changed files
with
265 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Fschmtt\Keycloak\Http\Criteria; | ||
use Fschmtt\Keycloak\Http\CustomCommand; | ||
use Fschmtt\Keycloak\Http\CustomQuery; | ||
use Fschmtt\Keycloak\Http\Method; | ||
use Fschmtt\Keycloak\Keycloak; | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$keycloak = new Keycloak( | ||
baseUrl: $_SERVER['KEYCLOAK_BASE_URL'] ?? 'http://keycloak:8080', | ||
username: 'admin', | ||
password: 'admin', | ||
); | ||
|
||
$custom = $keycloak->custom(); | ||
|
||
$response = $custom->query(new CustomQuery( | ||
'/admin/custom/endpoint', | ||
new Criteria([ | ||
'key' => 'value', | ||
'bool' => true, | ||
]) | ||
)); | ||
|
||
$custom->command(new CustomCommand( | ||
'/admin/custom/endpoint', | ||
Method::POST, | ||
[ | ||
'key' => 'value', | ||
'bool' => true, | ||
] | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fschmtt\Keycloak\Http; | ||
|
||
use Fschmtt\Keycloak\Collection\Collection; | ||
use Fschmtt\Keycloak\Representation\Representation; | ||
|
||
class CustomCommand extends Command | ||
{ | ||
/** | ||
* @param Representation|Collection|array<mixed>|null $payload | ||
*/ | ||
public function __construct( | ||
string $path, | ||
Method $method, | ||
Representation|Collection|array|null $payload = null, | ||
) { | ||
parent::__construct($path, $method, [], $payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fschmtt\Keycloak\Http; | ||
|
||
class CustomQuery extends Query | ||
{ | ||
public function __construct( | ||
string $path, | ||
?Criteria $criteria = null, | ||
string $returnType = 'array', | ||
) { | ||
parent::__construct($path, $returnType, [], $criteria); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fschmtt\Keycloak\Resource; | ||
|
||
use Fschmtt\Keycloak\Http\Client; | ||
use Fschmtt\Keycloak\Http\CommandExecutor; | ||
use Fschmtt\Keycloak\Http\CustomCommand; | ||
use Fschmtt\Keycloak\Http\CustomQuery; | ||
use Fschmtt\Keycloak\Http\QueryExecutor; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class Custom extends Resource | ||
{ | ||
public function __construct( | ||
CommandExecutor $commandExecutor, | ||
QueryExecutor $queryExecutor, | ||
private readonly Client $client, | ||
) { | ||
parent::__construct($commandExecutor, $queryExecutor); | ||
} | ||
|
||
public function query(CustomQuery $query): mixed | ||
{ | ||
return $this->queryExecutor->executeQuery($query); | ||
} | ||
|
||
public function command(CustomCommand $command): void | ||
{ | ||
$this->commandExecutor->executeCommand($command); | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function request(string $method, string $path, array $options = []): ResponseInterface | ||
{ | ||
return $this->client->request($method, $path, $options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fschmtt\Keycloak\Test\Unit\Resource; | ||
|
||
use Fschmtt\Keycloak\Http\Client; | ||
use Fschmtt\Keycloak\Http\CommandExecutor; | ||
use Fschmtt\Keycloak\Http\Criteria; | ||
use Fschmtt\Keycloak\Http\CustomCommand; | ||
use Fschmtt\Keycloak\Http\CustomQuery; | ||
use Fschmtt\Keycloak\Http\Method; | ||
use Fschmtt\Keycloak\Http\QueryExecutor; | ||
use Fschmtt\Keycloak\Resource\Custom; | ||
use GuzzleHttp\Psr7\Response; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(Custom::class)] | ||
class CustomTest extends TestCase | ||
{ | ||
public function testRunsCustomQuery(): void | ||
{ | ||
$queryExecutor = $this->createMock(QueryExecutor::class); | ||
$queryExecutor->expects(static::once()) | ||
->method('executeQuery') | ||
->with(new CustomQuery( | ||
'/admin/my/custom-endpoint', | ||
new Criteria([ | ||
'foo' => 'bar', | ||
]), | ||
)) | ||
->willReturn([ | ||
'return' => 'value', | ||
]); | ||
|
||
$custom = new Custom( | ||
$this->createMock(CommandExecutor::class), | ||
$queryExecutor, | ||
$this->createMock(Client::class), | ||
); | ||
|
||
$result = $custom->query(new CustomQuery( | ||
'/admin/my/custom-endpoint', | ||
new Criteria([ | ||
'foo' => 'bar', | ||
]), | ||
)); | ||
|
||
static::assertSame([ | ||
'return' => 'value', | ||
], $result); | ||
} | ||
|
||
public function testRunsCustomCommand(): void | ||
{ | ||
$commandExecutor = $this->createMock(CommandExecutor::class); | ||
$commandExecutor->expects(static::once()) | ||
->method('executeCommand') | ||
->with(new CustomCommand( | ||
'/admin/my/custom-endpoint', | ||
Method::POST, | ||
[ | ||
'foo' => 'bar', | ||
], | ||
)); | ||
|
||
$custom = new Custom( | ||
$commandExecutor, | ||
$this->createMock(QueryExecutor::class), | ||
$this->createMock(Client::class), | ||
); | ||
|
||
$custom->command(new CustomCommand( | ||
'/admin/my/custom-endpoint', | ||
Method::POST, | ||
[ | ||
'foo' => 'bar', | ||
], | ||
)); | ||
} | ||
|
||
public function testRunsCustomRequest(): void | ||
{ | ||
$client = $this->createMock(Client::class); | ||
$client->expects(static::once()) | ||
->method('request') | ||
->with( | ||
'POST', | ||
'/admin/my/custom-endpoint', | ||
[ | ||
'headers' => [ | ||
'Custom-Header' => 'custom-value', | ||
], | ||
], | ||
) | ||
->willReturn(new Response( | ||
200, | ||
[], | ||
json_encode([ | ||
'return' => 'value', | ||
]), | ||
)); | ||
|
||
$custom = new Custom( | ||
$this->createMock(CommandExecutor::class), | ||
$this->createMock(QueryExecutor::class), | ||
$client, | ||
); | ||
|
||
$response = $custom->request( | ||
'POST', | ||
'/admin/my/custom-endpoint', | ||
[ | ||
'headers' => [ | ||
'Custom-Header' => 'custom-value', | ||
], | ||
] | ||
); | ||
|
||
static::assertSame(200, $response->getStatusCode()); | ||
} | ||
} |