1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Core\Test\Integration\App; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Handler\MockHandler; |
6
|
|
|
use GuzzleHttp\Promise\Promise; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @internal |
13
|
|
|
*/ |
14
|
|
|
class TestAppServer |
15
|
|
|
{ |
16
|
|
|
final public const TEST_SETUP_SECRET = 's3cr3t'; |
17
|
|
|
final public const CONFIRMATION_URL = 'https://my-app.com/confirm'; |
18
|
|
|
final public const APP_SECRET = 'dont_tell'; |
19
|
|
|
|
20
|
|
|
private ?RequestInterface $registrationRequest = null; |
21
|
|
|
|
22
|
|
|
private ?RequestInterface $confirmationRequest = null; |
23
|
|
|
|
24
|
|
|
public function __construct(private readonly MockHandler $inner) |
25
|
|
|
{ |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array<mixed> $options |
30
|
|
|
*/ |
31
|
|
|
public function __invoke(RequestInterface $request, array $options): Promise |
32
|
|
|
{ |
33
|
|
|
if ($this->inner->count() > 0) { |
34
|
|
|
return \call_user_func($this->inner, $request, $options); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($this->isRegistration($request)) { |
38
|
|
|
$this->registrationRequest = $request; |
39
|
|
|
$promise = new Promise(); |
40
|
|
|
$promise->resolve(new Response(200, [], $this->buildAppResponse($request))); |
41
|
|
|
|
42
|
|
|
return $promise; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($this->isRegistrationConfirmation($request)) { |
46
|
|
|
$this->confirmationRequest = $request; |
47
|
|
|
$promise = new Promise(); |
48
|
|
|
$promise->resolve(new Response(200)); |
49
|
|
|
|
50
|
|
|
return $promise; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return \call_user_func($this->inner, $request, $options); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function didRegister(): bool |
57
|
|
|
{ |
58
|
|
|
return $this->registrationRequest !== null && $this->confirmationRequest !== null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function reset(): void |
62
|
|
|
{ |
63
|
|
|
$this->registrationRequest = null; |
64
|
|
|
$this->confirmationRequest = null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function buildAppResponse(RequestInterface $request): string |
68
|
|
|
{ |
69
|
|
|
$shopUrl = $this->getQueryParameter($request, 'shop-url'); |
70
|
|
|
$appname = $this->getAppname($request); |
71
|
|
|
$shopId = $this->getQueryParameter($request, 'shop-id'); |
72
|
|
|
|
73
|
|
|
$proof = \hash_hmac('sha256', $shopId . $shopUrl . $appname, self::TEST_SETUP_SECRET); |
74
|
|
|
|
75
|
|
|
return (string) \json_encode(['proof' => $proof, 'secret' => self::APP_SECRET, 'confirmation_url' => self::CONFIRMATION_URL], \JSON_THROW_ON_ERROR); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function isRegistration(RequestInterface $request): bool |
79
|
|
|
{ |
80
|
|
|
$path = $request->getUri()->getPath(); |
81
|
|
|
$pathElems = \explode('/', $path); |
82
|
|
|
|
83
|
|
|
return ($pathElems[2] ?? '') === 'registration'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function isRegistrationConfirmation(RequestInterface $request): bool |
87
|
|
|
{ |
88
|
|
|
return ((string) $request->getUri()) === self::CONFIRMATION_URL; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function getQueryParameter(RequestInterface $request, string $param): string |
92
|
|
|
{ |
93
|
|
|
$query = []; |
94
|
|
|
\parse_str($request->getUri()->getQuery(), $query); |
95
|
|
|
|
96
|
|
|
TestCase::assertIsString($query[$param]); |
97
|
|
|
|
98
|
|
|
return $query[$param]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function getAppname(RequestInterface $request): string |
102
|
|
|
{ |
103
|
|
|
$path = $request->getUri()->getPath(); |
104
|
|
|
$pathElems = \explode('/', $path); |
105
|
|
|
|
106
|
|
|
return $pathElems[1] ?? ''; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|