Skip to content

GET-1975 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2023
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
2 changes: 0 additions & 2 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
TEST_USERNAME=
TEST_PASSWORD=
TEST_MERCHANT_ID=
TEST_WEGETFINANCING_URL=
TEST_WEGETFINANCING_URL_V3=
8 changes: 3 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@

class Client
{
protected AuthEntity $authEntity;

public function __construct(AuthEntity $authEntity)
{
$this->authEntity = $authEntity;
public function __construct(
protected AuthEntity $authEntity
) {
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

abstract class AbstractCommand implements CommandInterface
{
protected HttpClientInterface $httpClient;

public function __construct(HttpClientInterface $httpClient)
public function __construct(protected HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
}
1 change: 0 additions & 1 deletion src/Command/RequestNewLoanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
class RequestNewLoanCommand extends AbstractCommand
{
public const LOAN_REQUEST_VERB = 'POST';

public const LOAN_REQUEST_PATH = '/merchant/' . HttpClientInterface::MERCHANT_ID_REPLACE . '/requests';

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Command/UpdateShippingStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use WeGetFinancing\SDK\Entity\Request\AbstractRequestEntity;
use WeGetFinancing\SDK\Entity\Request\UpdateShippingStatusRequestEntity;
use WeGetFinancing\SDK\Entity\Response\ResponseEntity;
use WeGetFinancing\SDK\Exception\EntityValidationException;
use WeGetFinancing\SDK\Service\Http\HttpClientInterface;
use WeGetFinancing\SDK\Service\Http\V3\HttpClientV3;

class UpdateShippingStatusCommand extends AbstractCommand
Expand Down
16 changes: 3 additions & 13 deletions src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,18 @@

abstract class AbstractEntity implements EntityInterface
{
protected ValidatorInterface $validator;

protected NameConverterInterface $camelCaseToSnakeCase;

/**
* @param ValidatorInterface $validator
* @param NameConverterInterface $camelCaseToSnakeCase
* @param null|array<string, mixed> $data
* @throws EntityValidationException
*/
public function __construct(
ValidatorInterface $validator,
NameConverterInterface $camelCaseToSnakeCase,
protected ValidatorInterface $validator,
protected NameConverterInterface $camelCaseToSnakeCase,
array $data = null
) {
$this->validator = $validator;
$this->camelCaseToSnakeCase = $camelCaseToSnakeCase;

if (
true === is_null($data) ||
true === empty($data)
) {
if (true === is_null($data) || true === empty($data)) {
return;
}

Expand Down
13 changes: 3 additions & 10 deletions src/Entity/AuthEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ class AuthEntity extends AbstractEntity
*/
protected string $merchantId;

/**
* @Assert\Url(
* protocols = { "https" },
* message = "The value of url is not a valid URL."
* )
* @Assert\NotBlank(message = "The value of url should not be blank.")
*/
protected string $url;
protected bool $prod = false;

/**
* @SuppressWarnings(PHPMD.StaticAccess)
Expand Down Expand Up @@ -77,8 +70,8 @@ public function getMerchantId(): string
return $this->merchantId;
}

public function getUrl(): string
public function isProd(): bool
{
return $this->url;
return $this->prod;
}
}
36 changes: 25 additions & 11 deletions src/Service/Http/AbstractHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,39 @@

abstract class AbstractHttpClient implements HttpClientInterface
{
public AuthEntity $authEntity;

protected ClientInterface $httpClient;
public const URL_API_V1_PROD = 'https://api.wegetfinancing.com';
public const URL_API_V1_SANDBOX = 'https://api.sandbox.wegetfinancing.com';
public const URL_API_V3_PROD = 'https://apisrv.wegetfinancing.com';
public const URL_API_V3_SANDBOX = 'https://apisrv.sandbox.wegetfinancing.com';

/**
* @param AuthEntity $authEntity
* @param ClientInterface $httpClient
*/
public function __construct(
AuthEntity $authEntity,
ClientInterface $httpClient
public AuthEntity $authEntity,
protected ClientInterface $httpClient
) {
$this->authEntity = $authEntity;
$this->httpClient = $httpClient;
}

protected function getUrlFromPath(string $path): string
protected function getBaseUrlApiV1(): string
{
return (true === $this->authEntity->isProd()) ? self::URL_API_V1_PROD : self::URL_API_V1_SANDBOX;
}

protected function getBaseUrlApiV3(): string
{
return (true === $this->authEntity->isProd()) ? self::URL_API_V3_PROD : self::URL_API_V3_SANDBOX;
}

protected function getUrlApiV1FromPath(string $path): string
{
return $this->getBaseUrlApiV1() . $path;
}

protected function getUrlApiV3FromPath(string $path): string
{
return $this->authEntity->getUrl() . $path;
return $this->getBaseUrlApiV3() . $path;
}

protected function getMerchantIdPath(string $path): string
Expand All @@ -39,9 +53,9 @@ protected function getMerchantIdPath(string $path): string
);
}

protected function getUrlFromMerchantIdPath(string $path): string
protected function getUrlApiV1FromMerchantIdPath(string $path): string
{
return $this->getUrlFromPath(
return $this->getUrlApiV1FromPath(
$this->getMerchantIdPath($path)
);
}
Expand Down
6 changes: 1 addition & 5 deletions src/Service/Http/V1/HttpClientV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@
class HttpClientV1 extends AbstractHttpClient
{
public const DEFAULT_ERROR_ERROR = 'unknown-error';

public const DEFAULT_ERROR_MESSAGE = 'Impossible to decode response content.';

public const DEFAULT_ERROR_STAMP = '0x0';

public const DEFAULT_ERROR_TYPE = 'error';

public const HEADERS = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
Expand Down Expand Up @@ -51,7 +47,7 @@ public function request(string $verb, string $path, array $data): ResponseEntity
{
$response = $this->httpClient->request(
$verb,
$this->getUrlFromMerchantIdPath($path),
$this->getUrlApiV1FromMerchantIdPath($path),
[
'http_errors' => false,
'headers' => $this->getHeaders(),
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Http/V3/HttpClientV3.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function request(string $verb, string $path, array $data): ResponseEntity

$response = $this->httpClient->request(
$verb,
$this->getUrlFromPath($path),
$this->getUrlApiV3FromPath($path),
[
'http_errors' => false,
'headers' => $this->getAuthenticatedHeaders($token['access_token']),
Expand Down Expand Up @@ -97,7 +97,7 @@ public function getToken(): mixed
{
$response = $this->httpClient->request(
'POST',
$this->getUrlFromPath('/v3/auth'),
$this->getUrlApiV3FromPath('/v3/auth'),
[
'http_errors' => false,
'headers' => self::HEADERS,
Expand Down
48 changes: 25 additions & 23 deletions tests/Functional/Entity/AuthEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ final class AuthEntityTest extends TestCase
'username' => 'User',
'password' => 'password',
'merchantId' => '1234',
'url' => 'https://valid.url.com',
'prod' => false,
],
'expected' => [
'username' => 'User',
'password' => 'password',
'merchantId' => '1234',
'url' => 'https://valid.url.com',
'prod' => false,
],
];

Expand All @@ -32,13 +32,27 @@ final class AuthEntityTest extends TestCase
'username' => 'Username1234',
'password' => 'pass1234',
'merchant_id' => '5678',
'url' => 'https://api.sandbox.wegetfinancing.com',
'prod' => true,
],
'expected' => [
'username' => 'Username1234',
'password' => 'pass1234',
'merchantId' => '5678',
'url' => 'https://api.sandbox.wegetfinancing.com',
'prod' => true,
],
];

public const VALID_ITEM_3 = [
'entity' => [
'username' => 'Username1234',
'password' => 'pass1234',
'merchant_id' => '5678',
],
'expected' => [
'username' => 'Username1234',
'password' => 'pass1234',
'merchantId' => '5678',
'prod' => false,
],
];

Expand All @@ -47,7 +61,6 @@ final class AuthEntityTest extends TestCase
'username' => '',
'password' => '',
'merchantId' => '',
'url' => '',
],
'violations' => [
7 => [
Expand All @@ -57,7 +70,6 @@ final class AuthEntityTest extends TestCase
'The value of password should not be blank.',
'The value of merchant id is too short. It should have 2 characters or more.',
'The value of merchant id should not be blank.',
'The value of url should not be blank.',
],
8 => [
'The value of username is too short. It should have 2 characters or more.',
Expand All @@ -66,7 +78,6 @@ final class AuthEntityTest extends TestCase
'The value of password should not be blank.',
'The value of merchant id is too short. It should have 2 characters or more.',
'The value of merchant id should not be blank.',
'The value of url should not be blank.',
],
],
];
Expand All @@ -88,21 +99,11 @@ final class AuthEntityTest extends TestCase
'username' => 'u',
'password' => 'p',
'merchantId' => '1',
'url' => 'http://invalid.protocol.com',
'prod' => 'http://invalid.protocol.com',
],
'violations' => [
7 => [
'The value of username is too short. It should have 2 characters or more.',
'The value of password is too short. It should have 2 characters or more.',
'The value of merchant id is too short. It should have 2 characters or more.',
'The value of url is not a valid URL.',
],
8 => [
'The value of username is too short. It should have 2 characters or more.',
'The value of password is too short. It should have 2 characters or more.',
'The value of merchant id is too short. It should have 2 characters or more.',
'The value of url is not a valid URL.',
],
7 => [ 'Typed property WeGetFinancing\SDK\Entity\AuthEntity::$prod must be bool, string used' ],
8 => [ 'Cannot assign string to property WeGetFinancing\SDK\Entity\AuthEntity::$prod of type bool' ],
],
];

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

/**
* @return iterable<array<array<string, array<string, string>>>>
* @return iterable<array<array<string, array<string, bool|string>>>>
*/
public function getValidAuthEntityData(): iterable
{
yield [ self::VALID_ITEM_1 ];
yield [ self::VALID_ITEM_2 ];
yield [ self::VALID_ITEM_3 ];
}

/**
Expand Down Expand Up @@ -151,8 +153,8 @@ public function testMakeWithDataWillSucceedAndEntityWillWorkAsExpected(array $da
$this->sut->getMerchantId()
);
$this->assertEquals(
$data['expected']['url'],
$this->sut->getUrl()
$data['expected']['prod'],
$this->sut->isProd()
);
}

Expand Down
25 changes: 0 additions & 25 deletions tests/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@ public function setUp(): void
$username = getenv('TEST_USERNAME');
$password = getenv('TEST_PASSWORD');
$merchantId = getenv('TEST_MERCHANT_ID');
$url = getenv('TEST_WEGETFINANCING_URL');

$this->assertIsNotBool($username);
$this->assertIsNotBool($password);
$this->assertIsNotBool($merchantId);
$this->assertIsNotBool($url);

$auth = AuthEntity::make([
'username' => $username,
'password' => $password,
'merchantId' => $merchantId,
'url' => $url,
]);

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

$this->assertIsNotBool($username);
$this->assertIsNotBool($password);
$this->assertIsNotBool($merchantId);
$this->assertIsNotBool($url);

$auth = AuthEntity::make([
'username' => $username,
'password' => $password,
'merchantId' => $merchantId,
'url' => $url,
]);

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

$username = getenv('TEST_USERNAME');
$password = getenv('TEST_PASSWORD');
$merchantId = getenv('TEST_MERCHANT_ID');
$url = getenv('TEST_WEGETFINANCING_URL_V3');

$this->assertIsNotBool($username);
$this->assertIsNotBool($password);
$this->assertIsNotBool($merchantId);
$this->assertIsNotBool($url);

$auth = AuthEntity::make([
'username' => $username,
'password' => $password,
'merchantId' => $merchantId,
'url' => $url,
]);

$this->sut = new Client($auth);

$this->assertArrayHasKey('invId', $loanResponse->getData());

$data = UpdateShippingStatusRequestEntityTest::VALID_ITEM_1['entity'];
Expand Down