Added API endpoints to fetch invoices (#5070)

* added serializer attributes for API usage
* new setters to fill invoice data from fixture
* re-usable fixture helper methods
* added API endpoints to fetch invoices
* adjust tests
This commit is contained in:
Kevin Papst
2024-09-22 16:17:45 +02:00
committed by GitHub
parent 1965c35b43
commit 8de54e1fa7
20 changed files with 532 additions and 155 deletions

View File

@@ -107,10 +107,10 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
return $client->request($method, $this->createUrl($url), $parameters, [], $server, $content);
}
protected function assertEntityNotFound(string $role, string $url, string $method = 'GET', ?string $message = null): void
protected function assertEntityNotFound(string $role, string $url): void
{
$client = $this->getClientForAuthenticatedUser($role);
$this->request($client, $url, $method);
$this->request($client, $url);
$this->assertApiException($client->getResponse(), [
'code' => Response::HTTP_NOT_FOUND,
'message' => 'Not Found'
@@ -272,6 +272,25 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
protected static function getExpectedResponseStructure(string $type): array
{
switch ($type) {
case 'Invoice':
case 'InvoiceCollection':
return [
'id' => 'int',
'comment' => '@string',
'createdAt' => 'datetime',
'currency' => 'string',
'customer' => ['result' => 'object', 'type' => '@Customer'],
'user' => ['result' => 'object', 'type' => '@User'],
'dueDays' => 'int',
'invoiceNumber' => 'string',
'metaFields' => 'array',
'paymentDate' => '@datetime',
'status' => 'string',
'tax' => 'float',
'total' => 'float',
'vat' => 'float',
];
case 'PageActionItem':
return [
'id' => 'string',

View File

@@ -245,7 +245,7 @@ class ActivityControllerTest extends APIControllerBaseTest
public function testNotFound(): void
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/activities/' . PHP_INT_MAX, 'GET', 'App\\Entity\\Activity object not found by the @ParamConverter annotation.');
$this->assertEntityNotFound(User::ROLE_USER, '/api/activities/' . PHP_INT_MAX);
}
public function testPostAction(): void

View File

@@ -38,7 +38,7 @@ class ApiDocControllerTest extends ControllerBaseTest
}
}
$expectedKeys = ['Actions', 'Activity', 'Default', 'Customer', 'Project', 'Tag', 'Team', 'Timesheet', 'User'];
$expectedKeys = ['Actions', 'Activity', 'Default', 'Customer', 'Project', 'Tag', 'Team', 'Timesheet', 'User', 'Invoice'];
$actual = array_keys($tags);
sort($actual);
@@ -70,6 +70,8 @@ class ApiDocControllerTest extends ControllerBaseTest
'/api/customers/{id}/meta',
'/api/customers/{id}/rates',
'/api/customers/{id}/rates/{rateId}',
'/api/invoices',
'/api/invoices/{id}',
'/api/projects',
'/api/projects/{id}',
'/api/projects/{id}/meta',

View File

@@ -190,7 +190,7 @@ class CustomerControllerTest extends APIControllerBaseTest
public function testNotFound(): void
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/customers/' . PHP_INT_MAX, 'GET', 'App\\Entity\\Customer object not found by the @ParamConverter annotation.');
$this->assertEntityNotFound(User::ROLE_USER, '/api/customers/' . PHP_INT_MAX);
}
public function testPostAction(): void

View File

@@ -0,0 +1,106 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace API;
use App\Entity\Invoice;
use App\Entity\User;
use App\Tests\API\APIControllerBaseTest;
use App\Tests\DataFixtures\InvoiceFixtures;
/**
* @group integration
*/
class InvoiceControllerTest extends APIControllerBaseTest
{
/**
* @return Invoice[]
*/
protected function importInvoiceFixtures(int $amount, ?array $status = null): array
{
$fixture = new InvoiceFixtures();
$fixture->setAmount($amount);
if (\is_array($status)) {
$fixture->setStatus($status);
}
return $this->importFixture($fixture);
}
public function testIsSecure(): void
{
$this->assertUrlIsSecured('/api/invoices');
}
public function testGetCollection(): void
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importInvoiceFixtures(10);
$this->assertAccessIsGranted($client, '/api/invoices');
$content = $client->getResponse()->getContent();
$this->assertIsString($content);
$result = json_decode($content, true);
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(10, \count($result));
self::assertApiResponseTypeStructure('InvoiceCollection', $result[0]);
}
public function testGetCollectionWithQuery(): void
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_TEAMLEAD);
$this->importInvoiceFixtures(5, [Invoice::STATUS_PENDING]);
$this->importInvoiceFixtures(2, [Invoice::STATUS_NEW]);
$this->importInvoiceFixtures(7, [Invoice::STATUS_PAID]);
$this->importInvoiceFixtures(1, [Invoice::STATUS_CANCELED]);
$query = ['order' => 'ASC', 'orderBy' => 'name', 'status' => [Invoice::STATUS_PAID]];
$this->assertAccessIsGranted($client, '/api/invoices', 'GET', $query);
$content = $client->getResponse()->getContent();
$this->assertIsString($content);
$result = json_decode($content, true);
$this->assertIsArray($result);
$this->assertNotEmpty($result);
$this->assertEquals(7, \count($result));
self::assertApiResponseTypeStructure('InvoiceCollection', $result[0]);
}
public function testGetEntityIsSecure(): void
{
$client = $this->getClientForAuthenticatedUser();
$invoices = $this->importInvoiceFixtures(1);
$this->assertApiAccessDenied($client, '/api/invoices/' . $invoices[0]->getId());
}
public function testGetEntity(): void
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);
$invoices = $this->importInvoiceFixtures(1);
$this->assertAccessIsGranted($client, '/api/invoices/' . $invoices[0]->getId());
$content = $client->getResponse()->getContent();
$this->assertIsString($content);
$result = json_decode($content, true);
$this->assertIsArray($result);
self::assertApiResponseTypeStructure('Invoice', $result);
}
public function testNotFound(): void
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/invoices/' . PHP_INT_MAX);
}
}

View File

@@ -326,7 +326,7 @@ class ProjectControllerTest extends APIControllerBaseTest
public function testNotFound(): void
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/projects/' . PHP_INT_MAX, 'GET', 'App\\Entity\\Project object not found by the @ParamConverter annotation.');
$this->assertEntityNotFound(User::ROLE_USER, '/api/projects/' . PHP_INT_MAX);
}
public function testPostAction(): void

View File

@@ -83,7 +83,7 @@ class TeamControllerTest extends APIControllerBaseTest
public function testNotFound(): void
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/teams/' . PHP_INT_MAX, 'GET', 'App\\Entity\\Team object not found by the @ParamConverter annotation.');
$this->assertEntityNotFound(User::ROLE_USER, '/api/teams/' . PHP_INT_MAX);
}
public function testDeleteActionWithUnknownTeam(): void

View File

@@ -448,7 +448,7 @@ class TimesheetControllerTest extends APIControllerBaseTest
public function testGetEntityNotFound(): void
{
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/' . PHP_INT_MAX, 'GET', 'App\\Entity\\Timesheet object not found by the @ParamConverter annotation.');
$this->assertEntityNotFound(User::ROLE_USER, '/api/timesheets/' . PHP_INT_MAX);
}
public function testPostAction(): void