Skip to content

Firefly III: a personal finances manager

Expand some test helpers.

James Colea93da2a91282
diff --git a/tests/Api/Models/Account/StoreControllerTest.php b/tests/Api/Models/Account/StoreControllerTest.php
index 31fd59fcf4..21c0be7f9a 100644
--- a/tests/Api/Models/Account/StoreControllerTest.php
+++ b/tests/Api/Models/Account/StoreControllerTest.php
@@ -25,6 +25,8 @@ namespace Tests\Api\Models\Account;
use Faker\Factory;
use Laravel\Passport\Passport;
use Log;
+use Tests\Objects\Field;
+use Tests\Objects\FieldSet;
use Tests\Objects\TestConfiguration;
use Tests\Objects\TestMandatoryField;
use Tests\Objects\TestMandatoryFieldSet;
@@ -246,11 +248,11 @@ class StoreControllerTest extends TestCase
$config = new TestConfiguration;
// add a set of mandatory fields:
- $mandatoryFieldSet = new TestMandatoryFieldSet();
+ $mandatoryFieldSet = new FieldSet();
$mandatoryFieldSet->title = 'default_asset_account';
// name
- $mandatoryField = new TestMandatoryField;
+ $mandatoryField = new Field;
$mandatoryField->title = 'name';
$mandatoryField->fieldTitle = 'name';
$mandatoryField->fieldPosition = ''; // root
@@ -258,10 +260,10 @@ class StoreControllerTest extends TestCase
$mandatoryField->expectedReturnType = 'equal'; // or 'callback'
$mandatoryField->expectedReturn = null; // or the callback
$mandatoryField->ignorableFields = [];
- $mandatoryFieldSet->addMandatoryField($mandatoryField);
+ $mandatoryFieldSet->addField($mandatoryField);
// type
- $mandatoryField = new TestMandatoryField;
+ $mandatoryField = new Field;
$mandatoryField->title = 'type';
$mandatoryField->fieldTitle = 'type';
$mandatoryField->fieldPosition = ''; // root
@@ -269,10 +271,10 @@ class StoreControllerTest extends TestCase
$mandatoryField->expectedReturnType = 'equal'; // or 'callback'
$mandatoryField->expectedReturn = null; // or the callback
$mandatoryField->ignorableFields = []; // something like transactions/0/currency_code
- $mandatoryFieldSet->addMandatoryField($mandatoryField);
+ $mandatoryFieldSet->addField($mandatoryField);
// role
- $mandatoryField = new TestMandatoryField;
+ $mandatoryField = new Field;
$mandatoryField->title = 'role';
$mandatoryField->fieldTitle = 'account_role';
$mandatoryField->fieldPosition = ''; // root
@@ -280,19 +282,41 @@ class StoreControllerTest extends TestCase
$mandatoryField->expectedReturnType = 'equal'; // or 'callback'
$mandatoryField->expectedReturn = null; // or the callback
$mandatoryField->ignorableFields = []; // something like transactions/0/currency_code
- $mandatoryFieldSet->addMandatoryField($mandatoryField);
+ $mandatoryFieldSet->addField($mandatoryField);
+ $config->mandatoryFieldSet = $mandatoryFieldSet;
+ unset($mandatoryField);
+ // $mandatoryField = new TestMandatoryField;
+ // $mandatoryField->title = 'transaction_type';
+ // $mandatoryField->fieldTitle = 'type';
+ // $mandatoryField->fieldPosition = 'transactions/0'; // not root!
+ // $mandatoryField->fieldType = 'random-transactionType'; // refers to a generator or something?
+ // $mandatoryField->expectedReturnType = 'equal'; // or 'callback'
+ // $mandatoryField->expectedReturn = null; // or the callback
+ // $mandatoryField->ignorableFields = [];
+ // $mandatoryFieldSet->addMandatoryField($mandatoryField);
-// $mandatoryField = new TestMandatoryField;
-// $mandatoryField->title = 'transaction_type';
-// $mandatoryField->fieldTitle = 'type';
-// $mandatoryField->fieldPosition = 'transactions/0'; // not root!
-// $mandatoryField->fieldType = 'random-transactionType'; // refers to a generator or something?
-// $mandatoryField->expectedReturnType = 'equal'; // or 'callback'
-// $mandatoryField->expectedReturn = null; // or the callback
-// $mandatoryField->ignorableFields = [];
-// $mandatoryFieldSet->addMandatoryField($mandatoryField);
+ $optionalFieldSet = new FieldSet;
+ $optionalField = new Field;
+ $optionalField->title = 'active';
+ $optionalField->fieldTitle = 'active';
+ $optionalField->fieldPosition = '';
+ $optionalField->fieldType = 'boolean'; // refers to a generator or something?
+ $optionalField->expectedReturnType = 'equal'; // or 'callback'
+ $optionalField->expectedReturn = null; // or the callback
+ $optionalField->ignorableFields = []; // something like transactions/0/currency_code
+ $optionalFieldSet->addField($optionalField, 'active');
- $config->mandatoryFieldSet = $mandatoryFieldSet;
+ $optionalField = new Field;
+ $optionalField->title = 'iban';
+ $optionalField->fieldTitle = 'iban';
+ $optionalField->fieldPosition = '';
+ $optionalField->fieldType = 'iban'; // refers to a generator or something?
+ $optionalField->expectedReturnType = 'equal'; // or 'callback'
+ $optionalField->expectedReturn = null; // or the callback
+ $optionalField->ignorableFields = []; // something like transactions/0/currency_code
+ $optionalFieldSet->addField($optionalField, 'iban');
+
+ $config->optionalFieldSet = $optionalFieldSet;
// generate submissions
$arr = $config->generateSubmission();
@@ -300,7 +324,7 @@ class StoreControllerTest extends TestCase
exit;
// generate expected returns.
- $set = [
+ $set = [
// set for withdrawal, copy this for
// other transaction types etc.
// make a CLASS!!
diff --git a/tests/Objects/Field.php b/tests/Objects/Field.php
new file mode 100644
index 0000000000..de223d4fc1
--- /dev/null
+++ b/tests/Objects/Field.php
@@ -0,0 +1,21 @@
+<?php
+
+
+namespace Tests\Objects;
+
+use Closure;
+
+/**
+ * Class Field
+ */
+class Field
+{
+ public ?Closure $expectedReturn;
+ public string $expectedReturnType;
+ public string $fieldPosition;
+ public string $fieldTitle;
+ public string $fieldType;
+ public ?array $ignorableFields;
+ public string $title;
+
+}
\ No newline at end of file
diff --git a/tests/Objects/FieldSet.php b/tests/Objects/FieldSet.php
new file mode 100644
index 0000000000..302a8930ae
--- /dev/null
+++ b/tests/Objects/FieldSet.php
@@ -0,0 +1,36 @@
+<?php
+
+
+namespace Tests\Objects;
+
+/**
+ * Class FieldSet
+ */
+class FieldSet
+{
+ public ?array $fields;
+ public string $title;
+
+ /**
+ * FieldSet constructor.
+ */
+ public function __construct()
+ {
+ $this->fields = [];
+ }
+
+ /**
+ * @param Field $field
+ * @param string|null $key
+ */
+ public function addField(Field $field, ?string $key = null): void
+ {
+ if (null === $key) {
+ $this->fields[] = $field;
+
+ return;
+ }
+ $this->fields[$key] = $field;
+ }
+
+}
\ No newline at end of file
diff --git a/tests/Objects/IgnorableField.php b/tests/Objects/IgnorableField.php
deleted file mode 100644
index 0e42a79ed2..0000000000
--- a/tests/Objects/IgnorableField.php
+++ /dev/null
@@ -1,12 +0,0 @@
-<?php
-
-
-namespace Tests\Objects;
-
-
-class IgnorableField
-{
- public string $fieldTitle;
- public string $fieldPosition;
-
-}
\ No newline at end of file
diff --git a/tests/Objects/TestConfiguration.php b/tests/Objects/TestConfiguration.php
index 13e316884b..cd6b3216c6 100644
--- a/tests/Objects/TestConfiguration.php
+++ b/tests/Objects/TestConfiguration.php
@@ -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;
+ }
+
}
\ No newline at end of file
diff --git a/tests/Objects/TestMandatoryField.php b/tests/Objects/TestMandatoryField.php
deleted file mode 100644
index 006c5e2b87..0000000000
--- a/tests/Objects/TestMandatoryField.php
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-
-
-namespace Tests\Objects;
-use Closure;
-
-/**
- * Class TestMandatoryField
- */
-class TestMandatoryField
-{
- public string $title;
- public string $fieldTitle;
- public string $fieldPosition;
- public string $fieldType;
- public string $expectedReturnType;
- public ?Closure $expectedReturn;
- public ?array $ignorableFields;
-
-}
\ No newline at end of file
diff --git a/tests/Objects/TestMandatoryFieldSet.php b/tests/Objects/TestMandatoryFieldSet.php
deleted file mode 100644
index 025124fc48..0000000000
--- a/tests/Objects/TestMandatoryFieldSet.php
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-
-
-namespace Tests\Objects;
-
-
-class TestMandatoryFieldSet
-{
- public string $title;
- public ?array $mandatoryFields;
-
- /**
- * TestMandatoryFieldSet constructor.
- */
- public function __construct()
- {
- $this->mandatoryFields = [];
- }
-
- /**
- * @param TestMandatoryField $field
- */
- public function addMandatoryField(TestMandatoryField $field)
- {
- $this->mandatoryFields[] = $field;
- }
-
-}
\ No newline at end of file
diff --git a/tests/Objects/TestObject.php b/tests/Objects/TestObject.php
deleted file mode 100644
index 3d38479aa2..0000000000
--- a/tests/Objects/TestObject.php
+++ /dev/null
@@ -1,16 +0,0 @@
-<?php
-
-
-namespace Tests\Objects;
-
-/**
- * Class TestObject
- */
-class TestObject
-{
- public function __construct()
- {
-
- }
-
-}
\ No newline at end of file