Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/Shop/Http/Responses/ShopSetupResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@

use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

class ShopSetupResponse implements Responsable
{
/**
* @param string|null $authorizationUrl URL to external authorization service.
* @param bool $showConfiguration Instruction to the client of the response to display configuration form
*/
public function __construct(
private readonly ?string $authorizationUrl = null,
private readonly bool $showConfiguration = false,
) {
}

public function toResponse($request): SymfonyResponse
public function toResponse($request): JsonResponse
{
if (!$this->authorizationUrl) {
return new Response('', SymfonyResponse::HTTP_NO_CONTENT);
$responseData = [
'show_configuration' => $this->showConfiguration,
];

if ($this->authorizationUrl) {
$responseData['authorization_url'] = $this->authorizationUrl;
}

return new JsonResponse([
'data' => [
'authorization_url' => $this->authorizationUrl,
],
]);
return new JsonResponse(['data' => $responseData]);
}
}
67 changes: 67 additions & 0 deletions tests/Shops/ShopSetupResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Shops;

use MyParcelCom\Wms\Shop\Http\Responses\ShopSetupResponse;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

use function PHPUnit\Framework\assertSame;
use Illuminate\Http\Request;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

class ShopSetupResponseTest extends TestCase
{
use MockeryPHPUnitIntegration;

#[DataProvider('setupProvider')]
public function test_to_response(
?string $authorizationUrl,
bool $showConfiguration,
array $responseData,
): void {
assertSame(
$responseData,
new ShopSetupResponse($authorizationUrl, $showConfiguration)
->toResponse(Mockery::mock(Request::class))
->getData(true),
);
}

public static function setupProvider(): array
{
return [
[
'authorizationUrl' => null,
'showConfiguration' => false,
'responseData' => [
'data' => [
'show_configuration' => false,
],
],
],
[
'authorizationUrl' => null,
'showConfiguration' => true,
'responseData' => [
'data' => [
'show_configuration' => true,
],
],
],
[
'authorizationUrl' => 'http://localhost',
'showConfiguration' => false,
'responseData' => [
'data' => [
'show_configuration' => false,
'authorization_url' => 'http://localhost',
],
],
],
];
Comment on lines 36 to 65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return [
[
'authorizationUrl' => null,
'followUpWithConfiguration' => false,
'responseData' => [
'data' => [
'show_configuration' => false,
],
],
],
[
'authorizationUrl' => null,
'followUpWithConfiguration' => true,
'responseData' => [
'data' => [
'show_configuration' => true,
],
],
],
[
'authorizationUrl' => 'http://localhost',
'followUpWithConfiguration' => false,
'responseData' => [
'data' => [
'show_configuration' => false,
'authorization_url' => 'http://localhost',
],
],
],
];
return [
[
'authorizationUrl' => null,
'showConfiguration' => false,
'responseData' => [
'data' => [
'show_configuration' => false,
],
],
],
[
'authorizationUrl' => null,
'showConfiguration' => true,
'responseData' => [
'data' => [
'show_configuration' => true,
],
],
],
[
'authorizationUrl' => 'http://localhost',
'showConfiguration' => false,
'responseData' => [
'data' => [
'show_configuration' => false,
'authorization_url' => 'http://localhost',
],
],
],
];

}
}