Files
firefly-iii/tests/Objects/TestConfiguration.php

461 lines
19 KiB
PHP
Raw Normal View History

2021-03-16 14:34:11 +01:00
<?php
namespace Tests\Objects;
use Faker\Factory;
use RuntimeException;
/**
* Class TestConfiguration
*/
class TestConfiguration
{
2021-03-20 15:28:44 +01:00
protected const MAX_ITERATIONS = 3;
public const SHOW_DEBUG = false;
public array $ignores;
2021-03-19 06:12:28 +01:00
public array $mandatoryFieldSets;
public array $optionalFieldSets;
2021-03-20 15:28:44 +01:00
public array $parameters;
private array $expected;
2021-03-19 06:12:28 +01:00
private array $submission;
2021-03-16 14:34:11 +01:00
/**
* TestConfiguration constructor.
*/
public function __construct()
{
2021-03-19 06:12:28 +01:00
$this->submission = [];
$this->mandatoryFieldSets = [];
$this->optionalFieldSets = [];
$this->ignores = [];
2021-03-20 07:02:06 +01:00
$this->parameters = [];
2021-03-20 15:28:44 +01:00
$this->expected = [];
2021-03-19 06:12:28 +01:00
}
/**
* @param FieldSet $set
*/
public function addMandatoryFieldSet(FieldSet $set)
{
$this->mandatoryFieldSets[] = $set;
}
public function addOptionalFieldSet(string $key, FieldSet $set)
{
$this->optionalFieldSets[$key] = $set;
2021-03-16 14:34:11 +01:00
}
2021-03-20 15:28:44 +01:00
public function generateAll(): array
2021-03-19 06:12:28 +01:00
{
2021-03-20 15:28:44 +01:00
$this->debugMsg('Now in generateAll()');
// generate submissions
$array = $this->generateSubmissions();
//$expected = $this->generateExpected($array);
$parameters = $this->parameters;
$ignored = $this->ignores;
$expected = $this->expected;
// now create a combination for each submission and associated data:
$final = [];
foreach ($array as $index => $submission) {
$final[] = [[
'submission' => $submission,
'expected' => $expected[$index] ?? $submission,
'ignore' => $ignored[$index] ?? [],
'parameters' => $parameters[$index] ?? [],
]];
2021-03-19 06:12:28 +01:00
}
2021-03-20 15:28:44 +01:00
return $final;
2021-03-19 06:12:28 +01:00
}
2021-03-20 15:39:49 +01:00
/**
* @param string $message
*/
private function debugMsg(string $message): void
{
if (true === self::SHOW_DEBUG) {
echo sprintf("%s\n", $message);
}
}
2021-03-19 06:12:28 +01:00
/**
* @return array
*/
2021-03-20 15:28:44 +01:00
private function generateSubmissions(): array
2021-03-19 06:12:28 +01:00
{
2021-03-20 15:28:44 +01:00
$this->debugMsg('Now in generateSubmissions()');
2021-03-19 06:12:28 +01:00
// first generate standard submissions:
$this->submission = [];
// loop each standard submission:
2021-03-20 15:28:44 +01:00
$this->debugMsg(sprintf('There are %d mandatory field sets', count($this->mandatoryFieldSets)));
2021-03-19 06:12:28 +01:00
/** @var FieldSet $set */
foreach ($this->mandatoryFieldSets as $set) {
$this->submission[] = $this->toArray($set);
// expand the standard submission with extra sets from the optional field set.
$setCount = count($this->optionalFieldSets);
2021-03-20 15:39:49 +01:00
$this->debugMsg('Just created a standard set');
2021-03-19 06:12:28 +01:00
if (0 !== $setCount) {
$keys = array_keys($this->optionalFieldSets);
2021-03-20 15:39:49 +01:00
$this->debugMsg(sprintf(' keys to consider are: %s', join(', ', $keys)));
2021-03-19 06:12:28 +01:00
$maxCount = count($keys) > self::MAX_ITERATIONS ? self::MAX_ITERATIONS : count($keys);
for ($i = 1; $i <= $maxCount; $i++) {
$combinationSets = $this->combinationsOf($i, $keys);
2021-03-20 15:39:49 +01:00
$this->debugMsg(sprintf(' will create %d extra sets.', count($combinationSets)));
2021-03-19 06:12:28 +01:00
foreach ($combinationSets as $ii => $combinationSet) {
2021-03-20 15:39:49 +01:00
$this->debugMsg(sprintf('Set %d/%d will consist of:', ($ii + 1), count($combinationSets)));
2021-03-19 06:12:28 +01:00
// the custom set is born!
2021-03-20 08:00:58 +01:00
$customFields = [];
$custom = $this->toArray($set);
2021-03-20 15:39:49 +01:00
$this->debugMsg(' refreshed!');
$this->debugMsg(sprintf(' %s', json_encode($custom)));
2021-03-19 06:12:28 +01:00
foreach ($combinationSet as $combination) {
2021-03-20 15:39:49 +01:00
$this->debugMsg(sprintf(' %s', $combination));
2021-03-19 06:12:28 +01:00
// here we start adding stuff to a copy of the standard submission.
/** @var FieldSet $customSet */
$customSet = $this->optionalFieldSets[$combination] ?? false;
2021-03-20 15:39:49 +01:00
$this->debugMsg(sprintf(' there are %d field(s) in this custom set', count(array_keys($customSet->fields))));
2021-03-19 06:12:28 +01:00
// loop each field in this custom set and add them, nothing more.
/** @var Field $field */
foreach ($customSet->fields as $field) {
2021-03-20 15:39:49 +01:00
$this->debugMsg(sprintf(' added field %s from custom set %s', $field->fieldTitle, $combination));
2021-03-20 08:00:58 +01:00
$custom = $this->parseField($custom, $field);
$customFields[] = $field;
2021-03-20 07:02:06 +01:00
}
}
$this->submission[] = $custom;
2021-03-20 08:00:58 +01:00
// at this point we can update the ignorable fields because we know the position
// of the submission in the array
$index = count($this->submission) - 1;
$this->updateIgnorables($index, $customFields);
2021-03-20 15:28:44 +01:00
$this->updateExpected($index, $customFields);
2021-03-20 07:02:06 +01:00
}
}
}
}
2021-03-20 15:28:44 +01:00
$totalCount = 0;
2021-03-20 07:02:06 +01:00
// no mandatory sets? Loop the optional sets:
if (0 === count($this->mandatoryFieldSets)) {
// expand the standard submission with extra sets from the optional field set.
$setCount = count($this->optionalFieldSets);
2021-03-20 15:28:44 +01:00
$this->debugMsg(sprintf('there are %d optional field sets', $setCount));
2021-03-20 07:02:06 +01:00
if (0 !== $setCount) {
$keys = array_keys($this->optionalFieldSets);
2021-03-20 15:28:44 +01:00
$this->debugMsg(sprintf(' keys to consider are: %s', join(', ', $keys)));
2021-03-20 07:02:06 +01:00
$maxCount = count($keys) > self::MAX_ITERATIONS ? self::MAX_ITERATIONS : count($keys);
2021-03-20 15:28:44 +01:00
$this->debugMsg(sprintf(' max count is %d', $maxCount));
2021-03-20 07:02:06 +01:00
for ($i = 1; $i <= $maxCount; $i++) {
$combinationSets = $this->combinationsOf($i, $keys);
2021-03-20 15:28:44 +01:00
$this->debugMsg(sprintf(' will create %d extra sets.', count($combinationSets)));
2021-03-20 07:02:06 +01:00
foreach ($combinationSets as $ii => $combinationSet) {
2021-03-20 15:28:44 +01:00
$totalCount++;
$this->debugMsg(sprintf(' Set #%d will consist of:', $totalCount));
2021-03-20 07:02:06 +01:00
// the custom set is born!
2021-03-20 15:28:44 +01:00
$custom = [];
$expected = [];
2021-03-20 07:02:06 +01:00
foreach ($combinationSet as $combination) {
2021-03-20 15:28:44 +01:00
$this->debugMsg(sprintf(' %s', $combination));
2021-03-20 07:02:06 +01:00
// here we start adding stuff to a copy of the standard submission.
/** @var FieldSet $customSet */
$customSet = $this->optionalFieldSets[$combination] ?? false;
2021-03-20 15:39:49 +01:00
$this->debugMsg(sprintf(sprintf(' there are %d field(s) in this custom set', count(array_keys($customSet->fields)))));
2021-03-20 07:02:06 +01:00
// loop each field in this custom set and add them, nothing more.
/** @var Field $field */
foreach ($customSet->fields as $field) {
2021-03-20 15:28:44 +01:00
$this->debugMsg(sprintf(' added field %s from custom set %s', $field->fieldTitle, $combination));
$custom = $this->parseField($custom, $field);
$expected = $this->parseExpected($expected, $field, $custom);
2021-03-20 07:02:06 +01:00
// for each field, add the ignores to the current index (+1!) of
// ignores.
2021-03-20 15:28:44 +01:00
$count = count($this->submission);
2021-03-19 06:12:28 +01:00
if (null !== $field->ignorableFields && count($field->ignorableFields) > 0) {
$currentIgnoreSet = $this->ignores[$count] ?? [];
2021-03-20 15:28:44 +01:00
$this->ignores[$count] = array_values(array_unique(array_values(array_merge($currentIgnoreSet, $field->ignorableFields))));
2021-03-19 06:12:28 +01:00
}
2021-03-20 15:28:44 +01:00
$this->expected[$count] = $expected;
2021-03-19 06:12:28 +01:00
}
2021-03-20 07:02:06 +01:00
$count = count($this->submission);
$this->parameters[$count] = $customSet->parameters ?? [];
2021-03-19 06:12:28 +01:00
}
2021-03-20 15:28:44 +01:00
$count = count($this->submission);
2021-03-19 06:12:28 +01:00
$this->submission[] = $custom;
2021-03-20 15:28:44 +01:00
$this->debugMsg(sprintf(' Created set #%d', $totalCount));
$this->debugMsg(sprintf(' Will submit: %s', json_encode($custom)));
$this->debugMsg(sprintf(' Will ignore: %s', json_encode($this->ignores[$count] ?? [])));
$this->debugMsg(sprintf(' Will expect: %s', json_encode($this->expected[$count] ?? [])));
2021-03-19 06:12:28 +01:00
}
}
}
2021-03-16 17:20:41 +01:00
}
2021-03-20 15:28:44 +01:00
$this->debugMsg('Done!');
2021-03-19 06:12:28 +01:00
return $this->submission;
}
2021-03-20 08:00:58 +01:00
/**
2021-03-20 15:28:44 +01:00
* @param FieldSet $set
2021-03-20 07:02:06 +01:00
*
* @return array
*/
2021-03-20 15:28:44 +01:00
private function toArray(FieldSet $set): array
2021-03-20 07:02:06 +01:00
{
2021-03-20 15:28:44 +01:00
$ignore = [];
$result = [];
$expected = [];
/** @var Field $field */
foreach ($set->fields as $field) {
// this is what we will submit:
$result = $this->parseField($result, $field);
$expected = $this->parseExpected($expected, $field, $result);
// this is what we will ignore:
$newIgnore = array_unique($ignore + $field->ignorableFields);
$ignore = $newIgnore;
$this->debugMsg(sprintf('Merged! ignores %s + %s = %s', json_encode($ignore), json_encode($field->ignorableFields), json_encode($newIgnore)));
2021-03-20 07:02:06 +01:00
}
2021-03-20 15:28:44 +01:00
$this->ignores[] = array_values($ignore);
$this->expected[] = $expected;
$this->parameters[] = $set->parameters ?? [];
2021-03-20 07:02:06 +01:00
2021-03-20 15:28:44 +01:00
return $result;
2021-03-20 07:02:06 +01:00
}
2021-03-19 06:12:28 +01:00
/**
* @param array $current
* @param Field $field
*
* @return array
*/
private function parseField(array $current, Field $field): array
{
// fieldTitle indicates the position:
$positions = explode('/', $field->fieldTitle);
$count = count($positions);
if (1 === $count) {
$current[$field->fieldTitle] = $this->generateFieldValue($field->fieldType);
return $current;
2021-03-16 14:34:11 +01:00
}
2021-03-19 06:12:28 +01:00
if (3 === $count) {
$root = $positions[0];
$count = (int)$positions[1];
$final = $positions[2];
$current[$root] = array_key_exists($root, $current) ? $current[$root] : [];
$current[$root][$count] = array_key_exists($count, $current[$root]) ? $current[$root][$count] : [];
$current[$root][$count][$final] = $this->generateFieldValue($final);
2021-03-16 17:20:41 +01:00
2021-03-19 06:12:28 +01:00
return $current;
}
throw new RuntimeException(sprintf('Did not expect count %d from fieldTitle "%s".', $count, $field->fieldTitle));
2021-03-16 14:34:11 +01:00
}
/**
* @param string $type
*
2021-03-16 17:20:41 +01:00
* @return mixed
2021-03-16 14:34:11 +01:00
*/
private function generateFieldValue(string $type)
{
$faker = Factory::create();
switch ($type) {
default:
throw new RuntimeException(sprintf('Cannot handle field "%s"', $type));
case 'uuid':
return $faker->uuid;
case 'static-asset':
return 'asset';
2021-03-19 06:12:28 +01:00
case 'static-expense':
return 'expense';
case 'static-liabilities':
return 'liabilities';
case 'static-ccAsset':
return 'ccAsset';
case 'static-monthlyFull':
return 'monthlyFull';
2021-03-20 07:21:13 +01:00
case 'static-one':
return 1;
case 'static-journal-type':
return 'TransactionJournal';
2021-03-19 06:12:28 +01:00
case 'random-liability-type':
return $faker->randomElement(['loan', 'debt', 'mortgage']);
2021-03-20 07:21:13 +01:00
case 'random-journal-id':
2021-03-20 08:00:58 +01:00
return $faker->numberBetween(1, 25);
2021-03-19 06:12:28 +01:00
case 'random-amount':
return number_format($faker->randomFloat(2, 10, 100), 2);
case 'random-percentage':
return $faker->randomFloat(2, 1, 99);
case 'random-interest-period':
return $faker->randomElement(['daily', 'monthly', 'yearly']);
2021-03-20 15:28:44 +01:00
case 'random-bill-repeat-freq':
return $faker->randomElement(['half-year', 'weekly', 'monthly', 'yearly']);
2021-03-19 06:12:28 +01:00
case 'random-past-date':
return $faker->dateTimeBetween('-3 years', '-1 years')->format('Y-m-d');
2021-03-20 08:00:58 +01:00
case 'random-date-two-year':
return $faker->dateTimeBetween('-2 years', '-1 years')->format('Y-m-d');
case 'random-date-one-year':
return $faker->dateTimeBetween('-1 years', 'now')->format('Y-m-d');
2021-03-16 14:34:11 +01:00
case 'random-asset-accountRole':
2021-03-19 06:12:28 +01:00
return $faker->randomElement(['defaultAsset', 'savingAsset']);
2021-03-16 14:34:11 +01:00
case 'random-transactionType':
return $faker->randomElement(['withdrawal', 'deposit', 'transfer']);
2021-03-16 17:20:41 +01:00
case 'boolean':
return $faker->boolean;
case 'iban':
2021-03-19 06:12:28 +01:00
case 'account_number':
2021-03-16 17:20:41 +01:00
return $faker->iban();
2021-03-19 06:12:28 +01:00
case 'bic':
return $faker->swiftBicNumber;
case 'random-currency-id':
return $faker->numberBetween(1, 10);
case 'random-currency-code':
return $faker->randomElement(['EUR', 'USD', 'HUF', 'GBP']);
case 'order':
return $faker->numberBetween(1, 5);
case 'latitude':
return $faker->latitude;
case 'longitude':
return $faker->longitude;
case 'random-zoom_level':
return $faker->numberBetween(1, 12);
2021-03-20 07:02:06 +01:00
case 'null':
return null;
2021-03-20 07:21:13 +01:00
case 'random-attachment-type':
2021-03-20 08:00:58 +01:00
return $faker->randomElement(['Account', 'Budget', 'Bill', 'TransactionJournal', 'PiggyBank', 'Tag',]);
2021-03-20 15:28:44 +01:00
case 'random-amount-min':
return number_format($faker->randomFloat(2, 10, 50), 2);
case 'random-amount-max':
return number_format($faker->randomFloat(2, 50, 100), 2);
case 'random-skip':
return $faker->numberBetween(0, 4);
case 'random-og-id':
return $faker->numberBetween(1, 2);
case 'random-auto-type':
return $faker->randomElement(['rollover', 'reset']);
case 'random-auto-period':
return $faker->randomElement(['weekly', 'monthly', 'yearly']);
case 'static-auto-none':
return 'none';
}
}
/**
* @param array $expected
* @param Field $field
* @param array $result
*
* @return array
*/
private function parseExpected(array $expected, Field $field, array $result): array
{
// fieldTitle indicates the position:
$positions = explode('/', $field->fieldTitle);
$count = count($positions);
if (1 === $count && null === $field->expectedReturn) {
$expected[$field->fieldTitle] = $result[$field->fieldTitle];
$this->debugMsg(sprintf(' Expected result of field "%s" = "%s"', $field->fieldTitle, $expected[$field->fieldTitle]));
return $expected;
}
if (1 === $count && null !== $field->expectedReturn) {
// call the closure!
$expected[$field->fieldTitle] = ($field->expectedReturn)($result[$field->fieldTitle]);
return $expected;
2021-03-16 14:34:11 +01:00
}
2021-03-20 15:28:44 +01:00
if (3 === $count) {
$root = $positions[0];
$count = (int)$positions[1];
$final = $positions[2];
$expected[$root] = array_key_exists($root, $expected) ? $expected[$root] : [];
$expected[$root][$count] = array_key_exists($count, $expected[$root]) ? $expected[$root][$count] : [];
$expected[$root][$count][$final] = null;
if (null === $field->expectedReturn) {
$expected[$root][$count][$final] = $result[$root][$count][$final] ?? false;
}
if (null !== $field->expectedReturn) {
$expected[$root][$count][$final] = ($field->expectedReturn)($result[$root][$count][$final] ?? false);
}
return $expected;
}
throw new RuntimeException(sprintf('Did not expect count %d from fieldTitle "%s".', $count, $field->fieldTitle));
2021-03-16 14:34:11 +01:00
}
2021-03-16 17:20:41 +01:00
/**
2021-03-19 06:12:28 +01:00
* @param $k
* @param $xs
2021-03-16 17:20:41 +01:00
*
2021-03-19 06:12:28 +01:00
* @return array|array[]
2021-03-16 17:20:41 +01:00
*/
2021-03-19 06:12:28 +01:00
private function combinationsOf($k, $xs): array
2021-03-16 17:20:41 +01:00
{
2021-03-19 06:12:28 +01:00
if ($k === 0) {
return [[]];
}
if (count($xs) === 0) {
return [];
}
$x = $xs[0];
$xs1 = array_slice($xs, 1, count($xs) - 1);
$res1 = $this->combinationsOf($k - 1, $xs1);
for ($i = 0; $i < count($res1); $i++) {
array_splice($res1[$i], 0, 0, $x);
2021-03-16 17:20:41 +01:00
}
2021-03-19 06:12:28 +01:00
$res2 = $this->combinationsOf($k, $xs1);
return array_merge($res1, $res2);
}
2021-03-20 15:28:44 +01:00
/**
* @param int $index
* @param array $customFields
*/
function updateIgnorables(int $index, array $customFields): void
{
if (count($customFields) > 0) {
/** @var Field $field */
foreach ($customFields as $field) {
if (0 !== count($field->ignorableFields)) {
$this->ignores[$index] = array_values(array_unique(array_values(array_merge($this->ignores[$index], $field->ignorableFields))));
}
}
}
}
/**
* @param int $index
* @param array $customFields
*/
function updateExpected(int $index, array $customFields): void
{
if (count($customFields) > 0) {
/** @var Field $field */
foreach ($customFields as $field) {
if (null === $field->expectedReturn) {
$this->expected[$index][$field->fieldTitle] = $this->submission[$index][$field->fieldTitle];
}
if (null !== $field->expectedReturn) {
$this->expected[$index][$field->fieldTitle] = ($field->expectedReturn)($this->submission[$index][$field->fieldTitle]);
}
}
}
}
2021-03-19 06:12:28 +01:00
/**
* @param FieldSet $optionalFieldSet
*/
public function setOptionalFieldSet(FieldSet $optionalFieldSet): void
{
$this->optionalFieldSet = $optionalFieldSet;
}
2021-03-16 14:34:11 +01:00
}