Skip to content

Commit

Permalink
feat: Creditlimit resource (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeCasbas authored Nov 13, 2024
1 parent 222c53f commit bdd2246
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Resource/CreditLimit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Medelse\AriaBundle\Resource;

class CreditLimit extends Resource
{
public const GET_FROM_SIREN = '/credit-limits';

// We use a like filter because companies can be registered from their SIREN or SIRET number
private const GET_FROM_SIREN_FILTER = 'like(debtorIdentifier.value,{siren}%)';


public function getFromSiren(string $siren): array
{
$filter = str_replace('{siren}', $siren, self::GET_FROM_SIREN_FILTER);

return $this->sendGetRequest(self::GET_FROM_SIREN, [
'filter' => $filter,
]);
}
}
3 changes: 2 additions & 1 deletion src/Resource/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(HttpClientInterface $httpClient, BearerGenerator $be
$this->ariaBaseUrl = $ariaBaseUrl;
}

protected function sendGetRequest(string $path): array
protected function sendGetRequest(string $path, array $queryParams = []): array
{
$response = $this->httpClient->request(
Request::METHOD_GET,
Expand All @@ -33,6 +33,7 @@ protected function sendGetRequest(string $path): array
'headers' => [
self::AUTH_METADATA_KEY => 'Bearer ' . $this->bearerGenerator->getBearerToken(),
],
'query' => $queryParams,
]
);

Expand Down
68 changes: 68 additions & 0 deletions tests/Resource/CreditLimitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Medelse\AriaBundle\Tests\Resource;

use Medelse\AriaBundle\Resource\CreditLimit;
use Medelse\AriaBundle\Security\BearerGenerator;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;

class CreditLimitTest extends TestCase
{
public const API_URL = 'https://foo.bar';

public function testGetCreditLimitRequest()
{
$mockedResponse = new MockResponse(json_encode([
'amount' => 10,
'outstandingAmount' => 30,
]));
$expectedURL = self::API_URL . CreditLimit::GET_FROM_SIREN;
$httpClient = new MockHttpClient($mockedResponse, 'https://example.com');

$resource = $this->setupCreditLimitResource($httpClient);

$response = $resource->getFromSiren('1234');

$this->assertSame(1, $httpClient->getRequestsCount());
$this->assertSame('GET', $mockedResponse->getRequestMethod());
$this->assertContains(
'authorization: Bearer ',
$mockedResponse->getRequestOptions()['headers'],
);
$this->assertCount(1, $mockedResponse->getRequestOptions()['query']);
$this->assertStringContainsString(
'like(debtorIdentifier.value,1234%)',
$mockedResponse->getRequestOptions()['query']['filter'],
);
$this->assertStringStartsWith($expectedURL,$mockedResponse->getRequestUrl());

$this->assertSame(2, count($response));
$this->assertSame($response['amount'], 10);
$this->assertSame($response['outstandingAmount'], 30);
}

public function testGetCreditLimitReturnsError()
{
$response = new MockResponse(json_encode([
'status' => '400',
'message' => 'Something\'s wrong',
'code' => 'BAD_REQUEST',
]));
$httpClient = new MockHttpClient($response, 'https://example.com');
$resource = $this->setupCreditLimitResource($httpClient);
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Error 400 BAD_REQUEST: Something\'s wrong');

$resource->getFromSiren('fizz');
}

private function setupCreditLimitResource(HttpClientInterface $httpClient): CreditLimit
{
$bearerGenerator = $this->createMock(BearerGenerator::class);
return new CreditLimit($httpClient, $bearerGenerator, self::API_URL);
}
}
2 changes: 2 additions & 0 deletions tests/Resource/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Medelse\AriaBundle\Tests\Resource;

use Medelse\AriaBundle\Enum\BusinessTypeEnum;
use Medelse\AriaBundle\Resource\User as UserResource;
use Medelse\AriaBundle\Security\BearerGenerator;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -134,6 +135,7 @@ private function getUser(): array
'businessName' => 'My spooky company',
'bankAccountIBAN' => 'FR14 3000 1019 0100 00Z6 7067 032',
'bankAccountBIC' => 'DAEEFRPPCCT',
'businessType' => BusinessTypeEnum::INDIVIDUAL,
];
}
}

0 comments on commit bdd2246

Please sign in to comment.