Skip to content

Commit

Permalink
feat(search-acts): update
Browse files Browse the repository at this point in the history
  • Loading branch information
PiLep committed Dec 29, 2023
1 parent fd8b24e commit 6da90de
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 0 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<!-- <testsuite name="DifferentialSearch">
<directory>./tests/features/DifferentialSearch/</directory>
</testsuite> -->
<testsuite name="SearchActsTest">
<directory>./tests/features/SearchActs/</directory>
</testsuite>
</testsuites>
<source>
<include>
Expand Down
14 changes: 14 additions & 0 deletions src/RNEClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ protected function requestApi(string $method, string $url, array $options = []):
return $data;
}

protected function requestFileApi(string $method, string $url, array $options = []): string
{
$data = [];
try {
$response = $this->client->request($method, $url, $options);
$data = $response->getBody()->getContents();
} catch (GuzzleException $e) {
$this->catchResponseErrors($e);
}
return $data;
}

/**
* Catch the response errors
*
Expand All @@ -194,6 +206,8 @@ protected function catchResponseErrors(GuzzleException $e): void
throw new \Exception('Bad credentials');
} elseif ($e->getCode() === 403) {
throw new \Exception('Forbidden');
} elseif ($e->getCode() === 404) {
throw new \Exception('Not found');
} elseif ($e->getCode() === 429) {
throw new \Exception('Too many requests');
} elseif ($e->getCode() === 500) {
Expand Down
59 changes: 59 additions & 0 deletions src/SearchActs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace RNEClient;

use GuzzleHttp\Exception\GuzzleException;

/**
* Class SearchActs
*
* @package RNEClient
*/
class SearchActs extends RNEClient implements SearchActsInterface
{
/**
* Search a company acts by its siren number
* Exact search only
*
* @param string $siren
*
* @throws GuzzleException
* @return array
*/
public function searchBySiren(string $siren): array
{
// error if the siren is not 9 length number
if (!preg_match('/^\d{9}$/', $siren)) {
throw new \Exception('Invalid input siren, please use a 9 length number.');
}
return $this->requestApi("get", "api/companies/{$siren}/attachments", ['headers' => $this->getAuthorizationHeaderArray()]);
}

/**
* Search a act metadata by its id
* Exact search only
*
* @param string $siren
*
* @throws GuzzleException
* @return array
*/
public function getMetadataById(string $id): array
{
return $this->requestApi("get", "api/actes/{$id}", ['headers' => $this->getAuthorizationHeaderArray()]);
}

/**
* Search a act file by its id
* Exact search only
*
* @param string $siren
*
* @throws GuzzleException
* @return resource
*/
public function getFileById(string $id): string
{
return $this->requestFileApi("get", "api/actes/{$id}/download", ['headers' => $this->getAuthorizationHeaderArray()]);
}
}
10 changes: 10 additions & 0 deletions src/SearchActsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace RNEClient;

interface SearchActsInterface
{
public function searchBySiren(string $siren): array;
public function getMetadataById(string $id): array;
public function getFileById(string $id): string;
}
49 changes: 49 additions & 0 deletions tests/features/SearchActs/GetFileByIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace RNEClient;


use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

class GetFileByIdTest extends TestCase
{
private SearchActs $RNEClient;

protected function setUp(): void
{
$this->RNEClient = new SearchActs();
}

public function testGetFileById(): void
{
// get from file
$fakeBinaryFile = 'fake_binary_file';

$mockHandler = new MockHandler([new Response(200, [], $fakeBinaryFile)]);
$handlerStack = HandlerStack::create($mockHandler);
$mockedClient = new Client(['handler' => $handlerStack]);

$this->RNEClient = new SearchActs('fake_token', $mockedClient);

$data = $this->RNEClient->getFileById('idnumberzzz');

$this->assertEquals($fakeBinaryFile, $data);
}

public function testGetFileByIdWithBadId(): void
{
$mockHandler = new MockHandler([new Response(404, [], '[]')]);
$handlerStack = HandlerStack::create($mockHandler);
$mockedClient = new Client(['handler' => $handlerStack]);

$this->RNEClient = new SearchActs('fake_token', $mockedClient);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('Not found');
$this->RNEClient->getFileById('bad_id');
}
}
37 changes: 37 additions & 0 deletions tests/features/SearchActs/GetMetadataByIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace RNEClient;


use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

class GetMetadataByIdTest extends TestCase
{
private SearchActs $RNEClient;

protected function setUp(): void
{
$this->RNEClient = new SearchActs();
}

public function testGetMetadataById(): void
{
// get from file
$fakeResponse = file_get_contents(__DIR__ . '/../../fixtures/SearchActs/getMetadataById.json');

$mockHandler = new MockHandler([new Response(200, [], $fakeResponse)]);
$handlerStack = HandlerStack::create($mockHandler);
$mockedClient = new Client(['handler' => $handlerStack]);

$this->RNEClient = new SearchActs('fake_token', $mockedClient);

$data = $this->RNEClient->getMetadataById('idnumberzzz');
$this->assertIsArray($data);
$this->assertEquals('idnumberzzz', $data['id']);
$this->assertEquals('NNNNNNNNN', $data['siren']);
}
}
50 changes: 50 additions & 0 deletions tests/features/SearchActs/SearchBySirenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace RNEClient;


use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

class SearchBySirenTest extends TestCase
{
private SearchActs $RNEClient;

protected function setUp(): void
{
$this->RNEClient = new SearchActs();
}

public function testSearchBySiren(): void
{
// get from file
$fakeResponse = file_get_contents(__DIR__ . '/../../fixtures/SearchActs/searchBySiren.json');

$mockHandler = new MockHandler([new Response(200, [], $fakeResponse)]);
$handlerStack = HandlerStack::create($mockHandler);
$mockedClient = new Client(['handler' => $handlerStack]);

$this->RNEClient = new SearchActs('fake_token', $mockedClient);

// Testez le comportement de recherche
$result = $this->RNEClient->searchBySiren('113277693');

$this->assertIsArray($result);
}

public function testSearchBySirenWithBadSiren(): void
{
$mockHandler = new MockHandler([new Response(200, [], '[]')]);
$handlerStack = HandlerStack::create($mockHandler);
$mockedClient = new Client(['handler' => $handlerStack]);

$this->RNEClient = new SearchActs('fake_token', $mockedClient);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('Invalid input siren, please use a 9 length number.');
$this->RNEClient->searchBySiren('1234567890');
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/SearchActs/getMetadataById.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "idnumberzzz",
"siren": "NNNNNNNNN",
"denomination": "Nom Société",
"dateDepot": "2022-12-25",
"typeRdd": [{
"typeActe": "Proces-verbal d'assemblee generale ordinaire"
}]
}
62 changes: 62 additions & 0 deletions tests/fixtures/SearchActs/searchBySiren.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"actes": [
{
"id": "6385db8b901325760b0c0d71",
"idOdrncs": 1409112,
"siren": "113277693",
"denomination": "VITALAC RUMINANT",
"dateDepot": "2021-08-02",
"nomDocument": "GU_ACTE_113277693_20221118_ENT21-000004478_1",
"nomDocumentOrigine": "GU_ACTE_113277693_20221118_ENT21-000004478_1",
"confidentiality": "Public",
"dateReceptionRne": "2022-11-18",
"tailleFichier": "56257",
"documentExtension": "PDF",
"nbPages": 1,
"natureArchive": "A",
"path": "/rne/Actes_PDF/2022/11/18/GU_ACTE_113277693_20221118_ENT21-000004478_1/",
"pathOrigine":
"/opendatarncs/public/Actes_PDF/flux/2022/11/18/GU_ACTE_113277693_20221118_ENT21-000004478_1/",
"typeDocument": "original électronique",
"numNat": "ENT21-000004478",
"numNatChrono": "1",
"valideParCfe": "oui",
"nomDocumentDeposant": "testPub.pdf",
"typeRdd": [
{
"typeActe": "Copie des statuts"
}
]
},
{
"id": "6385db8b901325760b0c0d73",
"idOdrncs": 1409113,
"siren": "113277693",
"denomination": "VITALAC RUMINANT",
"dateDepot": "2021-08-02",
"nomDocument": "GU_ACTE_113277693_20221118_ENT21-000004478_2",
"nomDocumentOrigine": "GU_ACTE_113277693_20221118_ENT21-000004478_2",
"confidentiality": "Public",
"dateReceptionRne": "2022-11-18",
"tailleFichier": "56257",
"documentExtension": "PDF",
"nbPages": 1,
"natureArchive": "A",
"path": "/rne/Actes_PDF/2022/11/18/GU_ACTE_113277693_20221118_ENT21-000004478_2/",
"pathOrigine":
"/opendatarncs/public/Actes_PDF/flux/2022/11/18/GU_ACTE_113277693_20221118_ENT21-000004478_2/",
"typeDocument": "original électronique",
"numNat": "ENT21-000004478",
"numNatChrono": "2",
"valideParCfe": "oui",
"nomDocumentDeposant": "testPubStats.pdf",
"typeRdd": [
{
"typeActe": "Copie des statuts"
}
]
}
],
"bilans": [],
"bilansSaisis": []
}

0 comments on commit 6da90de

Please sign in to comment.