Skip to content

Commit eeee239

Browse files
authored
Merge pull request #5 from WeGetFinancing/GET-1975
GET-1975
2 parents b2e2f12 + 9dd0e11 commit eeee239

File tree

12 files changed

+63
-103
lines changed

12 files changed

+63
-103
lines changed

.env.dist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
TEST_USERNAME=
22
TEST_PASSWORD=
33
TEST_MERCHANT_ID=
4-
TEST_WEGETFINANCING_URL=
5-
TEST_WEGETFINANCING_URL_V3=

src/Client.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414

1515
class Client
1616
{
17-
protected AuthEntity $authEntity;
18-
19-
public function __construct(AuthEntity $authEntity)
20-
{
21-
$this->authEntity = $authEntity;
17+
public function __construct(
18+
protected AuthEntity $authEntity
19+
) {
2220
}
2321

2422
/**

src/Command/AbstractCommand.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88

99
abstract class AbstractCommand implements CommandInterface
1010
{
11-
protected HttpClientInterface $httpClient;
12-
13-
public function __construct(HttpClientInterface $httpClient)
11+
public function __construct(protected HttpClientInterface $httpClient)
1412
{
15-
$this->httpClient = $httpClient;
1613
}
1714
}

src/Command/RequestNewLoanCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
class RequestNewLoanCommand extends AbstractCommand
1717
{
1818
public const LOAN_REQUEST_VERB = 'POST';
19-
2019
public const LOAN_REQUEST_PATH = '/merchant/' . HttpClientInterface::MERCHANT_ID_REPLACE . '/requests';
2120

2221
/**

src/Command/UpdateShippingStatusCommand.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use WeGetFinancing\SDK\Entity\Request\AbstractRequestEntity;
99
use WeGetFinancing\SDK\Entity\Request\UpdateShippingStatusRequestEntity;
1010
use WeGetFinancing\SDK\Entity\Response\ResponseEntity;
11-
use WeGetFinancing\SDK\Exception\EntityValidationException;
12-
use WeGetFinancing\SDK\Service\Http\HttpClientInterface;
1311
use WeGetFinancing\SDK\Service\Http\V3\HttpClientV3;
1412

1513
class UpdateShippingStatusCommand extends AbstractCommand

src/Entity/AbstractEntity.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,18 @@
1212

1313
abstract class AbstractEntity implements EntityInterface
1414
{
15-
protected ValidatorInterface $validator;
16-
17-
protected NameConverterInterface $camelCaseToSnakeCase;
18-
1915
/**
2016
* @param ValidatorInterface $validator
2117
* @param NameConverterInterface $camelCaseToSnakeCase
2218
* @param null|array<string, mixed> $data
2319
* @throws EntityValidationException
2420
*/
2521
public function __construct(
26-
ValidatorInterface $validator,
27-
NameConverterInterface $camelCaseToSnakeCase,
22+
protected ValidatorInterface $validator,
23+
protected NameConverterInterface $camelCaseToSnakeCase,
2824
array $data = null
2925
) {
30-
$this->validator = $validator;
31-
$this->camelCaseToSnakeCase = $camelCaseToSnakeCase;
32-
33-
if (
34-
true === is_null($data) ||
35-
true === empty($data)
36-
) {
26+
if (true === is_null($data) || true === empty($data)) {
3727
return;
3828
}
3929

src/Entity/AuthEntity.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,7 @@ class AuthEntity extends AbstractEntity
3737
*/
3838
protected string $merchantId;
3939

40-
/**
41-
* @Assert\Url(
42-
* protocols = { "https" },
43-
* message = "The value of url is not a valid URL."
44-
* )
45-
* @Assert\NotBlank(message = "The value of url should not be blank.")
46-
*/
47-
protected string $url;
40+
protected bool $prod = false;
4841

4942
/**
5043
* @SuppressWarnings(PHPMD.StaticAccess)
@@ -77,8 +70,8 @@ public function getMerchantId(): string
7770
return $this->merchantId;
7871
}
7972

80-
public function getUrl(): string
73+
public function isProd(): bool
8174
{
82-
return $this->url;
75+
return $this->prod;
8376
}
8477
}

src/Service/Http/AbstractHttpClient.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,39 @@
99

1010
abstract class AbstractHttpClient implements HttpClientInterface
1111
{
12-
public AuthEntity $authEntity;
13-
14-
protected ClientInterface $httpClient;
12+
public const URL_API_V1_PROD = 'https://api.wegetfinancing.com';
13+
public const URL_API_V1_SANDBOX = 'https://api.sandbox.wegetfinancing.com';
14+
public const URL_API_V3_PROD = 'https://apisrv.wegetfinancing.com';
15+
public const URL_API_V3_SANDBOX = 'https://apisrv.sandbox.wegetfinancing.com';
1516

1617
/**
1718
* @param AuthEntity $authEntity
1819
* @param ClientInterface $httpClient
1920
*/
2021
public function __construct(
21-
AuthEntity $authEntity,
22-
ClientInterface $httpClient
22+
public AuthEntity $authEntity,
23+
protected ClientInterface $httpClient
2324
) {
24-
$this->authEntity = $authEntity;
25-
$this->httpClient = $httpClient;
2625
}
2726

28-
protected function getUrlFromPath(string $path): string
27+
protected function getBaseUrlApiV1(): string
28+
{
29+
return (true === $this->authEntity->isProd()) ? self::URL_API_V1_PROD : self::URL_API_V1_SANDBOX;
30+
}
31+
32+
protected function getBaseUrlApiV3(): string
33+
{
34+
return (true === $this->authEntity->isProd()) ? self::URL_API_V3_PROD : self::URL_API_V3_SANDBOX;
35+
}
36+
37+
protected function getUrlApiV1FromPath(string $path): string
38+
{
39+
return $this->getBaseUrlApiV1() . $path;
40+
}
41+
42+
protected function getUrlApiV3FromPath(string $path): string
2943
{
30-
return $this->authEntity->getUrl() . $path;
44+
return $this->getBaseUrlApiV3() . $path;
3145
}
3246

3347
protected function getMerchantIdPath(string $path): string
@@ -39,9 +53,9 @@ protected function getMerchantIdPath(string $path): string
3953
);
4054
}
4155

42-
protected function getUrlFromMerchantIdPath(string $path): string
56+
protected function getUrlApiV1FromMerchantIdPath(string $path): string
4357
{
44-
return $this->getUrlFromPath(
58+
return $this->getUrlApiV1FromPath(
4559
$this->getMerchantIdPath($path)
4660
);
4761
}

src/Service/Http/V1/HttpClientV1.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@
1414
class HttpClientV1 extends AbstractHttpClient
1515
{
1616
public const DEFAULT_ERROR_ERROR = 'unknown-error';
17-
1817
public const DEFAULT_ERROR_MESSAGE = 'Impossible to decode response content.';
19-
2018
public const DEFAULT_ERROR_STAMP = '0x0';
21-
2219
public const DEFAULT_ERROR_TYPE = 'error';
23-
2420
public const HEADERS = [
2521
'Content-Type' => 'application/json',
2622
'Accept' => 'application/json',
@@ -51,7 +47,7 @@ public function request(string $verb, string $path, array $data): ResponseEntity
5147
{
5248
$response = $this->httpClient->request(
5349
$verb,
54-
$this->getUrlFromMerchantIdPath($path),
50+
$this->getUrlApiV1FromMerchantIdPath($path),
5551
[
5652
'http_errors' => false,
5753
'headers' => $this->getHeaders(),

src/Service/Http/V3/HttpClientV3.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function request(string $verb, string $path, array $data): ResponseEntity
4848

4949
$response = $this->httpClient->request(
5050
$verb,
51-
$this->getUrlFromPath($path),
51+
$this->getUrlApiV3FromPath($path),
5252
[
5353
'http_errors' => false,
5454
'headers' => $this->getAuthenticatedHeaders($token['access_token']),
@@ -97,7 +97,7 @@ public function getToken(): mixed
9797
{
9898
$response = $this->httpClient->request(
9999
'POST',
100-
$this->getUrlFromPath('/v3/auth'),
100+
$this->getUrlApiV3FromPath('/v3/auth'),
101101
[
102102
'http_errors' => false,
103103
'headers' => self::HEADERS,

tests/Functional/Entity/AuthEntityTest.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ final class AuthEntityTest extends TestCase
1717
'username' => 'User',
1818
'password' => 'password',
1919
'merchantId' => '1234',
20-
'url' => 'https://valid.url.com',
20+
'prod' => false,
2121
],
2222
'expected' => [
2323
'username' => 'User',
2424
'password' => 'password',
2525
'merchantId' => '1234',
26-
'url' => 'https://valid.url.com',
26+
'prod' => false,
2727
],
2828
];
2929

@@ -32,13 +32,27 @@ final class AuthEntityTest extends TestCase
3232
'username' => 'Username1234',
3333
'password' => 'pass1234',
3434
'merchant_id' => '5678',
35-
'url' => 'https://api.sandbox.wegetfinancing.com',
35+
'prod' => true,
3636
],
3737
'expected' => [
3838
'username' => 'Username1234',
3939
'password' => 'pass1234',
4040
'merchantId' => '5678',
41-
'url' => 'https://api.sandbox.wegetfinancing.com',
41+
'prod' => true,
42+
],
43+
];
44+
45+
public const VALID_ITEM_3 = [
46+
'entity' => [
47+
'username' => 'Username1234',
48+
'password' => 'pass1234',
49+
'merchant_id' => '5678',
50+
],
51+
'expected' => [
52+
'username' => 'Username1234',
53+
'password' => 'pass1234',
54+
'merchantId' => '5678',
55+
'prod' => false,
4256
],
4357
];
4458

@@ -47,7 +61,6 @@ final class AuthEntityTest extends TestCase
4761
'username' => '',
4862
'password' => '',
4963
'merchantId' => '',
50-
'url' => '',
5164
],
5265
'violations' => [
5366
7 => [
@@ -57,7 +70,6 @@ final class AuthEntityTest extends TestCase
5770
'The value of password should not be blank.',
5871
'The value of merchant id is too short. It should have 2 characters or more.',
5972
'The value of merchant id should not be blank.',
60-
'The value of url should not be blank.',
6173
],
6274
8 => [
6375
'The value of username is too short. It should have 2 characters or more.',
@@ -66,7 +78,6 @@ final class AuthEntityTest extends TestCase
6678
'The value of password should not be blank.',
6779
'The value of merchant id is too short. It should have 2 characters or more.',
6880
'The value of merchant id should not be blank.',
69-
'The value of url should not be blank.',
7081
],
7182
],
7283
];
@@ -88,21 +99,11 @@ final class AuthEntityTest extends TestCase
8899
'username' => 'u',
89100
'password' => 'p',
90101
'merchantId' => '1',
91-
'url' => 'http://invalid.protocol.com',
102+
'prod' => 'http://invalid.protocol.com',
92103
],
93104
'violations' => [
94-
7 => [
95-
'The value of username is too short. It should have 2 characters or more.',
96-
'The value of password is too short. It should have 2 characters or more.',
97-
'The value of merchant id is too short. It should have 2 characters or more.',
98-
'The value of url is not a valid URL.',
99-
],
100-
8 => [
101-
'The value of username is too short. It should have 2 characters or more.',
102-
'The value of password is too short. It should have 2 characters or more.',
103-
'The value of merchant id is too short. It should have 2 characters or more.',
104-
'The value of url is not a valid URL.',
105-
],
105+
7 => [ 'Typed property WeGetFinancing\SDK\Entity\AuthEntity::$prod must be bool, string used' ],
106+
8 => [ 'Cannot assign string to property WeGetFinancing\SDK\Entity\AuthEntity::$prod of type bool' ],
106107
],
107108
];
108109

@@ -118,12 +119,13 @@ public function testMakeWithoutDataWillWorkAsExpected(): void
118119
}
119120

120121
/**
121-
* @return iterable<array<array<string, array<string, string>>>>
122+
* @return iterable<array<array<string, array<string, bool|string>>>>
122123
*/
123124
public function getValidAuthEntityData(): iterable
124125
{
125126
yield [ self::VALID_ITEM_1 ];
126127
yield [ self::VALID_ITEM_2 ];
128+
yield [ self::VALID_ITEM_3 ];
127129
}
128130

129131
/**
@@ -151,8 +153,8 @@ public function testMakeWithDataWillSucceedAndEntityWillWorkAsExpected(array $da
151153
$this->sut->getMerchantId()
152154
);
153155
$this->assertEquals(
154-
$data['expected']['url'],
155-
$this->sut->getUrl()
156+
$data['expected']['prod'],
157+
$this->sut->isProd()
156158
);
157159
}
158160

tests/Integration/ClientTest.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,15 @@ public function setUp(): void
2424
$username = getenv('TEST_USERNAME');
2525
$password = getenv('TEST_PASSWORD');
2626
$merchantId = getenv('TEST_MERCHANT_ID');
27-
$url = getenv('TEST_WEGETFINANCING_URL');
2827

2928
$this->assertIsNotBool($username);
3029
$this->assertIsNotBool($password);
3130
$this->assertIsNotBool($merchantId);
32-
$this->assertIsNotBool($url);
3331

3432
$auth = AuthEntity::make([
3533
'username' => $username,
3634
'password' => $password,
3735
'merchantId' => $merchantId,
38-
'url' => $url,
3936
]);
4037

4138
$this->sut = new Client($auth);
@@ -61,18 +58,15 @@ public function testUnsuccessfullyV3Auth(): void
6158
$username = getenv('TEST_USERNAME');
6259
$password = 'wrong_password';
6360
$merchantId = getenv('TEST_MERCHANT_ID');
64-
$url = getenv('TEST_WEGETFINANCING_URL_V3');
6561

6662
$this->assertIsNotBool($username);
6763
$this->assertIsNotBool($password);
6864
$this->assertIsNotBool($merchantId);
69-
$this->assertIsNotBool($url);
7065

7166
$auth = AuthEntity::make([
7267
'username' => $username,
7368
'password' => $password,
7469
'merchantId' => $merchantId,
75-
'url' => $url,
7670
]);
7771

7872
$this->sut = new Client($auth);
@@ -98,25 +92,6 @@ public function testSuccessfullyUpdateShippingStatus(): void
9892
$loanResponse = $this->sut->requestNewLoan($loanRequest);
9993
$this->assertTrue($loanResponse->getIsSuccess());
10094

101-
$username = getenv('TEST_USERNAME');
102-
$password = getenv('TEST_PASSWORD');
103-
$merchantId = getenv('TEST_MERCHANT_ID');
104-
$url = getenv('TEST_WEGETFINANCING_URL_V3');
105-
106-
$this->assertIsNotBool($username);
107-
$this->assertIsNotBool($password);
108-
$this->assertIsNotBool($merchantId);
109-
$this->assertIsNotBool($url);
110-
111-
$auth = AuthEntity::make([
112-
'username' => $username,
113-
'password' => $password,
114-
'merchantId' => $merchantId,
115-
'url' => $url,
116-
]);
117-
118-
$this->sut = new Client($auth);
119-
12095
$this->assertArrayHasKey('invId', $loanResponse->getData());
12196

12297
$data = UpdateShippingStatusRequestEntityTest::VALID_ITEM_1['entity'];

0 commit comments

Comments
 (0)