mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-29 18:20:01 +00:00
Expand some test helpers.
This commit is contained in:
@@ -11,8 +11,9 @@ use RuntimeException;
|
||||
*/
|
||||
class TestConfiguration
|
||||
{
|
||||
public TestMandatoryFieldSet $mandatoryFieldSet;
|
||||
private array $submission;
|
||||
public FieldSet $mandatoryFieldSet;
|
||||
public FieldSet $optionalFieldSet;
|
||||
private array $submission;
|
||||
|
||||
/**
|
||||
* TestConfiguration constructor.
|
||||
@@ -22,37 +23,70 @@ class TestConfiguration
|
||||
$this->submission = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function generateSubmission(): array
|
||||
{
|
||||
// first generate standard submission:
|
||||
$this->submission = [];
|
||||
/** @var TestMandatoryField $field */
|
||||
foreach ($this->mandatoryFieldSet->mandatoryFields as $field) {
|
||||
$this->parseField($field);
|
||||
$standard = [];
|
||||
/** @var Field $field */
|
||||
foreach ($this->mandatoryFieldSet->fields as $field) {
|
||||
$standard = $this->parseField($standard, $field);
|
||||
}
|
||||
$this->submission[] = $standard;
|
||||
|
||||
// expand the standard submission with extra sets from the optional field set.
|
||||
$optionalCount = count($this->optionalFieldSet->fields);
|
||||
if (0 !== $optionalCount) {
|
||||
$keys = array_keys($this->optionalFieldSet->fields);
|
||||
for ($i = 1; $i <= count($keys); $i++) {
|
||||
$combinations = $this->combinationsOf($i, $keys);
|
||||
foreach ($combinations as $combination) {
|
||||
$custom = $standard;
|
||||
foreach ($combination as $key) {
|
||||
// add field $key to the standard submission.
|
||||
$custom = $this->parseField($custom, $this->optionalFieldSet->fields[$key]);
|
||||
}
|
||||
// add custom to $submission:
|
||||
$this->submission[] = $custom;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->submission;
|
||||
}
|
||||
|
||||
private function parseField(TestMandatoryField $field)
|
||||
/**
|
||||
* @param $k
|
||||
* @param $xs
|
||||
*
|
||||
* @return array|array[]
|
||||
*/
|
||||
private function combinationsOf($k, $xs): array
|
||||
{
|
||||
if ('' === $field->fieldPosition) {
|
||||
$this->submission[$field->fieldTitle] = $this->generateFieldValue($field->fieldType);
|
||||
if ($k === 0) {
|
||||
return [[]];
|
||||
}
|
||||
if ('' !== $field->fieldPosition) {
|
||||
$positions = explode('/', $field->fieldPosition);
|
||||
// since the "positions" array is almost 2 indexes deep at best, we can do some manual fiddling.
|
||||
$root = $positions[0];
|
||||
$count = (int)$positions[1];
|
||||
$this->submission[$root] = array_key_exists($root, $this->submission) ? $this->submission[$root] : [];
|
||||
$this->submission[$root][$count] = array_key_exists($count, $this->submission[$root]) ? $this->submission[$root][$count] : [];
|
||||
$this->submission[$root][$count][$field->fieldTitle] = $this->generateFieldValue($field->fieldType);
|
||||
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);
|
||||
}
|
||||
$res2 = $this->combinationsOf($k, $xs1);
|
||||
|
||||
return array_merge($res1, $res2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return mixed|string
|
||||
* @return mixed
|
||||
*/
|
||||
private function generateFieldValue(string $type)
|
||||
{
|
||||
@@ -68,7 +102,35 @@ class TestConfiguration
|
||||
return $faker->randomElement(['defaultAsset', 'savingsAsset']);
|
||||
case 'random-transactionType':
|
||||
return $faker->randomElement(['withdrawal', 'deposit', 'transfer']);
|
||||
case 'boolean':
|
||||
return $faker->boolean;
|
||||
case 'iban':
|
||||
return $faker->iban();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $current
|
||||
* @param Field $field
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function parseField(array $current, Field $field): array
|
||||
{
|
||||
if ('' === $field->fieldPosition) {
|
||||
$current[$field->fieldTitle] = $this->generateFieldValue($field->fieldType);
|
||||
}
|
||||
if ('' !== $field->fieldPosition) {
|
||||
$positions = explode('/', $field->fieldPosition);
|
||||
// since the "positions" array is almost 2 indexes deep at best, we can do some manual fiddling.
|
||||
$root = $positions[0];
|
||||
$count = (int)$positions[1];
|
||||
$current[$root] = array_key_exists($root, $current) ? $current[$root] : [];
|
||||
$current[$root][$count] = array_key_exists($count, $current[$root]) ? $current[$root][$count] : [];
|
||||
$current[$root][$count][$field->fieldTitle] = $this->generateFieldValue($field->fieldType);
|
||||
}
|
||||
|
||||
return $current;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user