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:
@@ -19,6 +19,8 @@ use Faker\Factory;
|
||||
*/
|
||||
final class ActivityFixtures implements TestFixture
|
||||
{
|
||||
use FixturesTrait;
|
||||
|
||||
private int $amount = 0;
|
||||
private bool $isGlobal = false;
|
||||
private ?bool $isVisible = null;
|
||||
@@ -127,20 +129,4 @@ final class ActivityFixtures implements TestFixture
|
||||
|
||||
return $created;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return array<int|string, Project>
|
||||
*/
|
||||
private function getAllProjects(ObjectManager $manager): array
|
||||
{
|
||||
$all = [];
|
||||
/** @var Project[] $entries */
|
||||
$entries = $manager->getRepository(Project::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,6 @@ final class CustomerFixtures implements TestFixture
|
||||
* Will be called prior to persisting the object.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @return CustomerFixtures
|
||||
*/
|
||||
public function setCallback(callable $callback): CustomerFixtures
|
||||
{
|
||||
@@ -63,7 +62,6 @@ final class CustomerFixtures implements TestFixture
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return Customer[]
|
||||
*/
|
||||
public function load(ObjectManager $manager): array
|
||||
|
||||
91
tests/DataFixtures/FixturesTrait.php
Normal file
91
tests/DataFixtures/FixturesTrait.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?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 App\Tests\DataFixtures;
|
||||
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\Project;
|
||||
use App\Entity\User;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
|
||||
trait FixturesTrait
|
||||
{
|
||||
/**
|
||||
* @return array<int, User>
|
||||
*/
|
||||
private function getAllUsers(ObjectManager $manager): array
|
||||
{
|
||||
$all = [];
|
||||
/** @var User[] $entries */
|
||||
$entries = $manager->getRepository(User::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
if ($temp->getId() === null) {
|
||||
continue;
|
||||
}
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, Customer>
|
||||
*/
|
||||
private function getAllCustomers(ObjectManager $manager): array
|
||||
{
|
||||
$all = [];
|
||||
/** @var Customer[] $entries */
|
||||
$entries = $manager->getRepository(Customer::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
if ($temp->getId() === null) {
|
||||
continue;
|
||||
}
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, Project>
|
||||
*/
|
||||
private function getAllProjects(ObjectManager $manager): array
|
||||
{
|
||||
$all = [];
|
||||
/** @var Project[] $entries */
|
||||
$entries = $manager->getRepository(Project::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
if ($temp->getId() === null) {
|
||||
continue;
|
||||
}
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, Activity>
|
||||
*/
|
||||
private function getAllActivities(ObjectManager $manager): array
|
||||
{
|
||||
$all = [];
|
||||
/** @var Activity[] $entries */
|
||||
$entries = $manager->getRepository(Activity::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
if ($temp->getId() === null) {
|
||||
continue;
|
||||
}
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
}
|
||||
76
tests/DataFixtures/InvoiceFixtures.php
Normal file
76
tests/DataFixtures/InvoiceFixtures.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?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 App\Tests\DataFixtures;
|
||||
|
||||
use App\Entity\Invoice;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Faker\Factory;
|
||||
|
||||
/**
|
||||
* Defines the sample data to load in during controller tests.
|
||||
*/
|
||||
class InvoiceFixtures implements TestFixture
|
||||
{
|
||||
use FixturesTrait;
|
||||
|
||||
private int $amount = 50;
|
||||
private array $status = [Invoice::STATUS_CANCELED, Invoice::STATUS_NEW, Invoice::STATUS_PAID, Invoice::STATUS_PENDING];
|
||||
|
||||
public function setAmount(int $amount = 50): void
|
||||
{
|
||||
$this->amount = $amount;
|
||||
}
|
||||
|
||||
public function setStatus(array $status): void
|
||||
{
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Invoice[]
|
||||
*/
|
||||
public function load(ObjectManager $manager): array
|
||||
{
|
||||
$created = [];
|
||||
|
||||
$faker = Factory::create();
|
||||
|
||||
$customers = $this->getAllCustomers($manager);
|
||||
$users = $this->getAllUsers($manager);
|
||||
|
||||
for ($i = 0; $i < $this->amount; $i++) {
|
||||
$total = $faker->randomFloat(2, 50, 5000);
|
||||
$vat = $faker->randomFloat(2, 0, 23) / 10;
|
||||
$tax = $total * $vat;
|
||||
|
||||
$invoice = new Invoice();
|
||||
$invoice->setStatus($this->status[array_rand($this->status)]);
|
||||
$invoice->setTotal($total);
|
||||
$invoice->setVat($vat);
|
||||
$invoice->setTax($tax);
|
||||
$invoice->setCustomer($customers[array_rand($customers)]);
|
||||
|
||||
$invoice->setInvoiceNumber($i . '_ ' . $faker->text(10));
|
||||
$invoice->setCreatedAt($faker->dateTimeBetween('-1 year', 'now'));
|
||||
$invoice->setUser($users[array_rand($users)]);
|
||||
$invoice->setDueDays($faker->randomNumber(2));
|
||||
$invoice->setFilename($faker->text(30));
|
||||
$invoice->setComment($faker->text(300));
|
||||
$invoice->setCurrency($faker->currencyCode());
|
||||
|
||||
$manager->persist($invoice);
|
||||
$created[] = $invoice;
|
||||
}
|
||||
|
||||
$manager->flush();
|
||||
|
||||
return $created;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ use Faker\Factory;
|
||||
class InvoiceTemplateFixtures implements TestFixture
|
||||
{
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return InvoiceTemplate[]
|
||||
*/
|
||||
public function load(ObjectManager $manager): array
|
||||
|
||||
@@ -19,6 +19,8 @@ use Faker\Factory;
|
||||
*/
|
||||
final class ProjectFixtures implements TestFixture
|
||||
{
|
||||
use FixturesTrait;
|
||||
|
||||
private int $amount = 0;
|
||||
private ?bool $isVisible = null;
|
||||
/**
|
||||
@@ -114,20 +116,4 @@ final class ProjectFixtures implements TestFixture
|
||||
|
||||
return $created;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return array<int|string, Customer>
|
||||
*/
|
||||
private function getAllCustomers(ObjectManager $manager): array
|
||||
{
|
||||
$all = [];
|
||||
/** @var Customer[] $entries */
|
||||
$entries = $manager->getRepository(Customer::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
namespace App\Tests\DataFixtures;
|
||||
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\Team;
|
||||
use App\Entity\User;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
@@ -19,22 +18,15 @@ use Doctrine\Persistence\ObjectManager;
|
||||
*/
|
||||
final class TeamFixtures implements TestFixture
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $amount = 0;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $addCustomer = true;
|
||||
use FixturesTrait;
|
||||
|
||||
private int $amount = 0;
|
||||
private bool $addCustomer = true;
|
||||
/**
|
||||
* @var User[]
|
||||
*/
|
||||
private $skipUser = [];
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $addUser = true;
|
||||
private array $skipUser = [];
|
||||
private bool $addUser = true;
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
@@ -44,7 +36,6 @@ final class TeamFixtures implements TestFixture
|
||||
* Will be called prior to persisting the object.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @return TeamFixtures
|
||||
*/
|
||||
public function setCallback(callable $callback): TeamFixtures
|
||||
{
|
||||
@@ -87,7 +78,6 @@ final class TeamFixtures implements TestFixture
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return Team[]
|
||||
*/
|
||||
public function load(ObjectManager $manager): array
|
||||
@@ -135,36 +125,4 @@ final class TeamFixtures implements TestFixture
|
||||
|
||||
return $created;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return array<int|string, Customer>
|
||||
*/
|
||||
private function getAllCustomers(ObjectManager $manager)
|
||||
{
|
||||
$all = [];
|
||||
/** @var Customer[] $entries */
|
||||
$entries = $manager->getRepository(Customer::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return array<int|string, User>
|
||||
*/
|
||||
private function getAllUsers(ObjectManager $manager): array
|
||||
{
|
||||
$all = [];
|
||||
/** @var User[] $entries */
|
||||
$entries = $manager->getRepository(User::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ use Faker\Factory;
|
||||
*/
|
||||
final class TimesheetFixtures implements TestFixture
|
||||
{
|
||||
use FixturesTrait;
|
||||
|
||||
private int $running = 0;
|
||||
/**
|
||||
* @var Activity[]
|
||||
@@ -307,54 +309,6 @@ final class TimesheetFixtures implements TestFixture
|
||||
return $start;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return array<int|string, Activity>
|
||||
*/
|
||||
private function getAllActivities(ObjectManager $manager): array
|
||||
{
|
||||
$all = [];
|
||||
/** @var Activity[] $entries */
|
||||
$entries = $manager->getRepository(Activity::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return array<int|string, Project>
|
||||
*/
|
||||
private function getAllProjects(ObjectManager $manager): array
|
||||
{
|
||||
$all = [];
|
||||
/** @var Project[] $entries */
|
||||
$entries = $manager->getRepository(Project::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @return array<int|string, User>
|
||||
*/
|
||||
private function getAllUsers(ObjectManager $manager): array
|
||||
{
|
||||
$all = [];
|
||||
/** @var User[] $entries */
|
||||
$entries = $manager->getRepository(User::class)->findAll();
|
||||
foreach ($entries as $temp) {
|
||||
$all[$temp->getId()] = $temp;
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $start
|
||||
* @param array<Tag> $tagArray
|
||||
|
||||
Reference in New Issue
Block a user