major refactor

- add request status to search instance API
- use JSend-type response in elixForms clients
- readjust namespaces
- add more unit tests (manual HTTP requests too)
This commit is contained in:
2026-07-24 14:56:25 +02:00
parent ef53ee8364
commit 33ff4d9577
11 changed files with 315 additions and 129 deletions
+17
View File
@@ -0,0 +1,17 @@
# Sample requests to try out authorization logic
###
# Authorization OK
@term=MA
GET http://localhost:8000/api/dipendenti/cerca?term={{term}}
Authorization: Basic elixforms_ws:password123
X-API-Key: myApiAccessToken
###
# Should return 401 Unauthorized
@term=MA
GET http://localhost:8000/api/dipendenti/cerca?term={{term}}
@@ -0,0 +1,26 @@
# Sample requests to trigger web service
###
# TAG not found
GET http://localhost:8000/elixforms/instances/search?moduleTag=ANTANI&fieldName=responsabileScientifico.codiceFiscale&fieldValue=MMM
###
# No data found (choose a TAG with very few instances...)
GET http://localhost:8000/elixforms/instances/search?moduleTag=RequestForm_EDILIZIA_RDA_MANUTENZIONE&fieldName=responsabileScientifico.codiceFiscale&fieldValue=MMM
###
# With one status
GET http://localhost:8000/elixforms/instances/search?requestStatuses=PROCESSED&moduleTag=RequestForm_EDILIZIA_RDA_MANUTENZIONE&fieldName=responsabileScientifico.codiceFiscale&fieldValue=MMM
###
# With two statuses (comma-separated)
GET http://localhost:8000/elixforms/instances/search?requestStatuses=PROCESSED,SUBMITTED&moduleTag=RequestForm_EDILIZIA_RDA_MANUTENZIONE&fieldName=responsabileScientifico.codiceFiscale&fieldValue=MMM
###
# With wrong status
GET http://localhost:8000/elixforms/instances/search?requestStatuses=UNKNOWN&moduleTag=RequestForm_EDILIZIA_RDA_MANUTENZIONE&fieldName=responsabileScientifico.codiceFiscale&fieldValue=MMM
+67 -16
View File
@@ -2,8 +2,7 @@
namespace Tests\Unit;
use Api\Core\HttpClient;
use ElixForms\Auth\ElixFormsApiClient;
use ElixForms\Auth\ElixFormsRequestStatus;
use ElixForms\Clients\ElixFormsApiClient;
use PHPUnit\Framework\TestCase;
final class ElixFormsApiClientTest extends TestCase
@@ -32,16 +31,17 @@ final class ElixFormsApiClientTest extends TestCase
};
$client = new ElixFormsApiClient('https://example.test', $httpClient);
$requests = $client->lookupByStatus('MODULO TEST', 'token', 'user');
$response = $client->lookupByStatus('MODULO TEST', 'token', 'user');
self::assertSame([['requestId' => 123]], $requests);
self::assertSame('success', $response['status']);
self::assertSame([['requestId' => 123]], $response['data']);
self::assertStringNotContainsString('requestStatus=', $httpClient->url);
self::assertStringContainsString('moduleTag=MODULO%20TEST', $httpClient->url);
self::assertSame('Bearer token', $httpClient->headers['Authorization']);
self::assertSame('user', $httpClient->headers['x-api-username']);
}
public function testLookupByStatusExpandsCombinedFlags(): void
public function testLookupByStatusExpandsCommaSeparatedStatuses(): void
{
$httpClient = new class extends HttpClient {
public string $url = '';
@@ -67,7 +67,7 @@ final class ElixFormsApiClientTest extends TestCase
'MODULO',
'token',
'user',
ElixFormsRequestStatus::IN_PROGRESS | ElixFormsRequestStatus::PROCESSED
'IN_PROGRESS, PROCESSED'
);
self::assertStringContainsString('requestStatus=IN_PROGRESS', $httpClient->url);
@@ -75,11 +75,39 @@ final class ElixFormsApiClientTest extends TestCase
self::assertStringContainsString('requestStatus=PROCESSED', $httpClient->url);
}
public function testRequestStatusRejectsUnknownFlags(): void
public function testLookupByStatusOmitsStatusesWhenTheValueIsEmpty(): void
{
$this->expectException(\InvalidArgumentException::class);
$httpClient = new class extends HttpClient {
public string $url = '';
ElixFormsRequestStatus::toApiValues(8);
public function get(string $url, array $headers = [])
{
$this->url = $url;
return [
'status' => 200,
'json' => ['value' => ['globalStatus' => 'OK', 'requests' => []]],
];
}
};
$client = new ElixFormsApiClient('https://example.test', $httpClient);
$client->lookupByStatus('MODULO', 'token', 'user', ' ');
self::assertStringNotContainsString('requestStatus=', $httpClient->url);
}
public function testLookupByStatusReturnsFailForAnUnknownStatus(): void
{
$httpClient = new HttpClient();
$client = new ElixFormsApiClient('https://example.test', $httpClient);
$response = $client->lookupByStatus('MODULO', 'token', 'user', 'UNKNOWN');
self::assertSame([
'status' => 'fail',
'data' => ['requestStatuses' => 'Lo stato elixForms richiesto non è valido.'],
], $response);
}
public function testGetExportTagsSupportsVendorJsonContentTypeResponses(): void
@@ -107,14 +135,15 @@ final class ElixFormsApiClientTest extends TestCase
};
$client = new ElixFormsApiClient('https://example.test/', $httpClient);
$tags = $client->getExportTags(42, 'MODULO', 'token', 'user');
$response = $client->getExportTags(42, 'MODULO', 'token', 'user');
self::assertSame('success', $response['status']);
self::assertSame(
[['name' => 'contratto.id', 'value' => 'ABC-123']],
$tags
$response['data']
);
self::assertSame(
'https://example.test/eF/services/api/request/42/view/_DEFAULT/exportTags/get/v1?moduleTag=MODULO&exportGroup=API',
'https://example.test/services/api/request/42/view/_DEFAULT/exportTags/get/v1?moduleTag=MODULO&exportGroup=API',
$httpClient->url
);
}
@@ -146,12 +175,34 @@ final class ElixFormsApiClientTest extends TestCase
self::assertStringContainsString('exportGroup=REPORT%20ORE', $httpClient->url);
}
public function testGetExportTagsRejectsAnEmptyExportGroup(): void
public function testGetExportTagsReturnsFailForAnEmptyExportGroup(): void
{
$this->expectException(\InvalidArgumentException::class);
$httpClient = new HttpClient();
$client = new ElixFormsApiClient('https://example.test', $httpClient);
$client->getExportTags(42, 'MODULO', 'token', 'user', ' ');
$response = $client->getExportTags(42, 'MODULO', 'token', 'user', ' ');
self::assertSame([
'status' => 'fail',
'data' => ['exportGroup' => 'deve essere una stringa non vuota.'],
], $response);
}
public function testLookupByStatusReturnsErrorForAnUnsuccessfulHttpResponse(): void
{
$httpClient = new class extends HttpClient {
public function get(string $url, array $headers = [])
{
return ['status' => 503];
}
};
$client = new ElixFormsApiClient('https://example.test', $httpClient);
$response = $client->lookupByStatus('MODULO', 'token', 'user');
self::assertSame([
'status' => 'error',
'message' => 'Errore durante LookupByStatus.',
'code' => 503,
], $response);
}
}
+17 -9
View File
@@ -5,8 +5,8 @@ use Api\Controllers\V1\ElixFormsController;
use Api\Core\Config;
use Api\Core\Request;
use Api\Core\Response;
use ElixForms\Auth\ElixFormsApiClient;
use ElixForms\Auth\ElixFormsAuthenticationClient;
use ElixForms\Clients\ElixFormsApiClient;
use ElixForms\Clients\ElixFormsAuthenticationClient;
use PHPUnit\Framework\TestCase;
final class ElixFormsControllerTest extends TestCase
@@ -18,9 +18,9 @@ final class ElixFormsControllerTest extends TestCase
{
}
public function login(string $username, string $password): string
public function login(string $username, string $password): array
{
return 'token';
return ['status' => 'success', 'data' => ['authToken' => 'token']];
}
};
$apiClient = new class extends ElixFormsApiClient {
@@ -32,14 +32,18 @@ final class ElixFormsControllerTest extends TestCase
string $moduleTag,
string $authToken,
string $username,
?int $status = null
?string $requestStatuses = null
): array
{
return [
if ($requestStatuses !== 'IN_PROGRESS,PROCESSED') {
throw new \RuntimeException('requestStatuses non inoltrato al client.');
}
return ['status' => 'success', 'data' => [
['idDomanda' => 10],
['requestId' => 20],
['idRequest' => 30],
];
]];
}
public function getExportTags(
@@ -69,7 +73,7 @@ final class ElixFormsControllerTest extends TestCase
],
];
return [$tags[$requestId]];
return ['status' => 'success', 'data' => [$tags[$requestId]]];
}
};
$config = new class extends Config {
@@ -85,6 +89,7 @@ final class ElixFormsControllerTest extends TestCase
'moduleTag' => 'MODULO',
'fieldName' => 'contratto.id',
'fieldValue' => 'abc-123',
'requestStatuses' => 'IN_PROGRESS,PROCESSED',
'exportGroup' => ' CUSTOM ',
];
}
@@ -97,7 +102,10 @@ final class ElixFormsControllerTest extends TestCase
self::fail('La risposta avrebbe dovuto interrompere il flusso del test.');
} catch (CapturedResponseException $exception) {
self::assertSame(200, $exception->status);
self::assertSame([['requestId' => 20]], $exception->payload);
self::assertSame([
'status' => 'success',
'data' => [['requestId' => 20]],
], $exception->payload);
}
}