new API endpoint to save invoice meta-fields (#5916)

This commit is contained in:
Kevin Papst
2026-04-25 18:14:10 +02:00
committed by GitHub
parent 087350ab72
commit 3eebb02ad3
39 changed files with 1427 additions and 659 deletions

View File

@@ -136,7 +136,7 @@ abstract class APIControllerBaseTestCase extends AbstractControllerBaseTestCase
]);
}
protected function assertEntityNotFoundForPatch(string $role, string $url, array $data): void
protected function assertEntityNotFoundForPatch(HttpKernelBrowser|string $role, string $url, array $data): void
{
$this->assertExceptionForPatchAction($role, $url, $data, [
'code' => Response::HTTP_NOT_FOUND,
@@ -152,17 +152,17 @@ abstract class APIControllerBaseTestCase extends AbstractControllerBaseTestCase
]);
}
protected function assertExceptionForDeleteAction(string $role, string $url, array $data, array $expectedErrors): void
protected function assertExceptionForDeleteAction(HttpKernelBrowser|string $role, string $url, array $data, array $expectedErrors): void
{
$this->assertExceptionForRole($role, $url, 'DELETE', $data, $expectedErrors);
}
protected function assertExceptionForPatchAction(string $role, string $url, array $data, array $expectedErrors): void
protected function assertExceptionForPatchAction(HttpKernelBrowser|string $role, string $url, array $data, array $expectedErrors): void
{
$this->assertExceptionForRole($role, $url, 'PATCH', $data, $expectedErrors);
}
protected function assertExceptionForPostAction(string $role, string $url, array $data, array $expectedErrors): void
protected function assertExceptionForPostAction(HttpKernelBrowser|string $role, string $url, array $data, array $expectedErrors): void
{
$this->assertExceptionForRole($role, $url, 'POST', $data, $expectedErrors);
}
@@ -180,9 +180,9 @@ abstract class APIControllerBaseTestCase extends AbstractControllerBaseTestCase
self::assertEquals($expectedErrors, json_decode($response->getContent(), true));
}
protected function assertExceptionForRole(string $role, string $url, string $method, array $data, array $expectedErrors): void
protected function assertExceptionForRole(HttpKernelBrowser|string $role, string $url, string $method, array $data, array $expectedErrors): void
{
$client = $this->getClientForAuthenticatedUser($role);
$client = ($role instanceof HttpKernelBrowser) ? $role : $this->getClientForAuthenticatedUser($role);
$this->assertExceptionForMethod($client, $url, $method, $data, $expectedErrors);
}
@@ -321,7 +321,7 @@ abstract class APIControllerBaseTestCase extends AbstractControllerBaseTestCase
'total' => 'float',
'vat' => 'float',
'overdue' => 'bool',
'metaFields' => ['result' => 'array', 'type' => 'CustomerMeta'],
'metaFields' => ['result' => 'array', 'type' => 'InvoiceMeta'],
];
case 'PageActionItem':
@@ -350,6 +350,7 @@ abstract class APIControllerBaseTestCase extends AbstractControllerBaseTestCase
'value' => '@string',
];
case 'InvoiceMeta':
case 'CustomerMeta':
case 'ProjectMeta':
case 'ActivityMeta':
@@ -630,7 +631,7 @@ abstract class APIControllerBaseTestCase extends AbstractControllerBaseTestCase
'number' => '@string',
'color' => '@string',
'color-safe' => 'string',
'metaFields' => ['result' => 'array', 'type' => 'ProjectMeta'], // since 2.45
'metaFields' => ['result' => 'array', 'type' => 'ActivityMeta'], // since 2.45
'comment' => '@string',
];
@@ -644,7 +645,7 @@ abstract class APIControllerBaseTestCase extends AbstractControllerBaseTestCase
'number' => '@string',
'color' => '@string',
'color-safe' => 'string',
'metaFields' => ['result' => 'array', 'type' => 'ProjectMeta'], // since 2.45
'metaFields' => ['result' => 'array', 'type' => 'ActivityMeta'], // since 2.45
'comment' => '@string',
];
@@ -659,7 +660,7 @@ abstract class APIControllerBaseTestCase extends AbstractControllerBaseTestCase
'number' => '@string',
'color' => '@string',
'color-safe' => 'string',
'metaFields' => ['result' => 'array', 'type' => 'ProjectMeta'],
'metaFields' => ['result' => 'array', 'type' => 'ActivityMeta'],
'comment' => '@string',
'parentTitle' => '@string',
'teams' => ['result' => 'array', 'type' => 'Team'],
@@ -676,7 +677,7 @@ abstract class APIControllerBaseTestCase extends AbstractControllerBaseTestCase
'number' => '@string',
'color' => '@string',
'color-safe' => 'string',
'metaFields' => ['result' => 'array', 'type' => 'ProjectMeta'],
'metaFields' => ['result' => 'array', 'type' => 'ActivityMeta'],
'comment' => '@string',
'parentTitle' => '@string',
'teams' => ['result' => 'array', 'type' => 'Team'],

View File

@@ -76,6 +76,7 @@ class ApiDocControllerTest extends AbstractControllerBaseTestCase
'/api/export/{id}',
'/api/invoices',
'/api/invoices/{id}',
'/api/invoices/{id}/custom-fields',
'/api/projects',
'/api/projects/{id}',
'/api/projects/{id}/meta',

View File

@@ -15,13 +15,17 @@ use App\Entity\Team;
use App\Entity\User;
use App\Repository\TeamRepository;
use App\Tests\DataFixtures\InvoiceFixtures;
use App\Tests\Mocks\InvoiceTestMetaFieldSubscriberMock;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Response;
#[Group('integration')]
class InvoiceControllerTest extends APIControllerBaseTestCase
{
/**
* @return Invoice[]
* @param int<1, 999> $amount
* @return non-empty-array<Invoice>
*/
protected function importInvoiceFixtures(int $amount, ?array $status = null): array
{
@@ -120,6 +124,8 @@ class InvoiceControllerTest extends APIControllerBaseTestCase
self::assertIsArray($result);
self::assertApiResponseTypeStructure('Invoice', $result);
self::assertArrayHasKey('metaFields', $result);
self::assertCount(0, $result['metaFields']);
}
public function testNotFound(): void
@@ -181,4 +187,95 @@ class InvoiceControllerTest extends APIControllerBaseTestCase
$this->request($client, '/api/invoices', 'GET', $query);
$this->assertApiResponseAccessDenied($client->getResponse());
}
// ------------------------------------- [META FIELDS] -------------------------------------
public function testUpdateInvoiceMetaFieldsThrowsNotFound(): void
{
$this->assertEntityNotFoundForPatch(User::ROLE_ADMIN, '/api/invoices/42/custom-fields', []);
}
public function testUpdateInvoiceMetaFieldsThrowsExceptionOnWrongStructure(): void
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$id = $this->importInvoiceFixtures(1)[0]->getId();
$this->assertExceptionForPatchAction($client, '/api/invoices/' . $id . '/custom-fields', ['name' => 'X', 'value' => 'X'], [
'code' => Response::HTTP_BAD_REQUEST,
'message' => 'Bad Request'
]);
}
public function testUpdateInvoiceMetaFieldsThrowsExceptionOnMissingName(): void
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$id = $this->importInvoiceFixtures(1)[0]->getId();
$this->assertExceptionForPatchAction($client, '/api/invoices/' . $id . '/custom-fields', [['value' => 'X']], [
'code' => Response::HTTP_BAD_REQUEST,
'message' => 'Bad Request'
]);
}
public function testUpdateInvoiceMetaFieldsThrowsExceptionOnMissingValue(): void
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$id = $this->importInvoiceFixtures(1)[0]->getId();
$this->assertExceptionForPatchAction($client, '/api/invoices/' . $id . '/custom-fields', [['name' => 'X']], [
'code' => Response::HTTP_BAD_REQUEST,
'message' => 'Bad Request'
]);
}
public function testUpdateInvoiceMetaFieldsThrowsExceptionOnMissingMetafield(): void
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$id = $this->importInvoiceFixtures(1)[0]->getId();
$this->assertExceptionForPatchAction($client, '/api/invoices/' . $id . '/custom-fields', [['name' => 'X', 'value' => 'Y']], [
'code' => Response::HTTP_NOT_FOUND,
'message' => 'Not Found'
]);
}
public function testUpdateInvoiceMetaFields(): void
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$invoices = $this->importInvoiceFixtures(1);
$id = $invoices[0]->getId();
/** @var EventDispatcher $dispatcher */
$dispatcher = static::getContainer()->get('event_dispatcher');
$dispatcher->addSubscriber(new InvoiceTestMetaFieldSubscriberMock());
$data = [
[
'name' => 'metatestmock',
'value' => 'another,testing,bar'
],
[
'name' => 'foobar',
'value' => 13081978
],
];
$this->request($client, '/api/invoices/' . $id . '/custom-fields', 'PATCH', [], (string) json_encode($data));
self::assertTrue($client->getResponse()->isSuccessful());
$content = $client->getResponse()->getContent();
self::assertIsString($content);
$result = json_decode($content, true);
self::assertIsArray($result);
self::assertApiResponseTypeStructure('Invoice', $result);
self::assertArrayHasKey('metaFields', $result);
// only visible should be returned
self::assertCount(1, $result['metaFields']);
self::assertEquals(['name' => 'metatestmock', 'value' => 'another,testing,bar'], $result['metaFields'][0]);
$em = $this->getEntityManager();
/** @var Invoice $invoice */
$invoice = $em->getRepository(Invoice::class)->find($id);
self::assertEquals('another,testing,bar', $invoice->getMetaField('metatestmock')?->getValue());
self::assertEquals(13081978, $invoice->getMetaField('foobar')?->getValue());
}
}