Skip to content

Firefly III: a personal finances manager

Some test scripts.

James Coledf86c89acc09
diff --git a/tests/Api/Models/Account/StoreControllerTest.php b/tests/Api/Models/Account/StoreControllerTest.php
index 63b3ede760..31fd59fcf4 100644
--- a/tests/Api/Models/Account/StoreControllerTest.php
+++ b/tests/Api/Models/Account/StoreControllerTest.php
@@ -25,6 +25,9 @@ namespace Tests\Api\Models\Account;
use Faker\Factory;
use Laravel\Passport\Passport;
use Log;
+use Tests\Objects\TestConfiguration;
+use Tests\Objects\TestMandatoryField;
+use Tests\Objects\TestMandatoryFieldSet;
use Tests\TestCase;
use Tests\Traits\CollectsValues;
use Tests\Traits\RandomValues;
@@ -55,6 +58,8 @@ class StoreControllerTest extends TestCase
*/
public function testStore(array $submission): void
{
+ $this->someTestData();
+ exit;
if ([] === $submission) {
$this->markTestSkipped('Empty data provider');
}
@@ -235,7 +240,67 @@ class StoreControllerTest extends TestCase
public function someTestData(): void
{
- $set = [
+ // a basic test config set contains
+ // mandatory fields and X optional fields
+ // the optional fields will be rotated automatically.
+ $config = new TestConfiguration;
+
+ // add a set of mandatory fields:
+ $mandatoryFieldSet = new TestMandatoryFieldSet();
+ $mandatoryFieldSet->title = 'default_asset_account';
+
+ // name
+ $mandatoryField = new TestMandatoryField;
+ $mandatoryField->title = 'name';
+ $mandatoryField->fieldTitle = 'name';
+ $mandatoryField->fieldPosition = ''; // root
+ $mandatoryField->fieldType = 'uuid'; // refers to a generator or something?
+ $mandatoryField->expectedReturnType = 'equal'; // or 'callback'
+ $mandatoryField->expectedReturn = null; // or the callback
+ $mandatoryField->ignorableFields = [];
+ $mandatoryFieldSet->addMandatoryField($mandatoryField);
+
+ // type
+ $mandatoryField = new TestMandatoryField;
+ $mandatoryField->title = 'type';
+ $mandatoryField->fieldTitle = 'type';
+ $mandatoryField->fieldPosition = ''; // root
+ $mandatoryField->fieldType = 'static-asset'; // refers to a generator or something?
+ $mandatoryField->expectedReturnType = 'equal'; // or 'callback'
+ $mandatoryField->expectedReturn = null; // or the callback
+ $mandatoryField->ignorableFields = []; // something like transactions/0/currency_code
+ $mandatoryFieldSet->addMandatoryField($mandatoryField);
+
+ // role
+ $mandatoryField = new TestMandatoryField;
+ $mandatoryField->title = 'role';
+ $mandatoryField->fieldTitle = 'account_role';
+ $mandatoryField->fieldPosition = ''; // root
+ $mandatoryField->fieldType = 'random-asset-accountRole'; // refers to a generator or something?
+ $mandatoryField->expectedReturnType = 'equal'; // or 'callback'
+ $mandatoryField->expectedReturn = null; // or the callback
+ $mandatoryField->ignorableFields = []; // something like transactions/0/currency_code
+ $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);
+
+ $config->mandatoryFieldSet = $mandatoryFieldSet;
+
+ // generate submissions
+ $arr = $config->generateSubmission();
+ var_dump($arr);
+ exit;
+ // generate expected returns.
+
+ $set = [
// set for withdrawal, copy this for
// other transaction types etc.
// make a CLASS!!
diff --git a/tests/Objects/IgnorableField.php b/tests/Objects/IgnorableField.php
new file mode 100644
index 0000000000..0e42a79ed2
--- /dev/null
+++ b/tests/Objects/IgnorableField.php
@@ -0,0 +1,12 @@
+<?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
new file mode 100644
index 0000000000..13e316884b
--- /dev/null
+++ b/tests/Objects/TestConfiguration.php
@@ -0,0 +1,74 @@
+<?php
+
+
+namespace Tests\Objects;
+
+use Faker\Factory;
+use RuntimeException;
+
+/**
+ * Class TestConfiguration
+ */
+class TestConfiguration
+{
+ public TestMandatoryFieldSet $mandatoryFieldSet;
+ private array $submission;
+
+ /**
+ * TestConfiguration constructor.
+ */
+ public function __construct()
+ {
+ $this->submission = [];
+ }
+
+ public function generateSubmission(): array
+ {
+ $this->submission = [];
+ /** @var TestMandatoryField $field */
+ foreach ($this->mandatoryFieldSet->mandatoryFields as $field) {
+ $this->parseField($field);
+ }
+
+ return $this->submission;
+ }
+
+ private function parseField(TestMandatoryField $field)
+ {
+ if ('' === $field->fieldPosition) {
+ $this->submission[$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];
+ $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);
+ }
+ }
+
+ /**
+ * @param string $type
+ *
+ * @return mixed|string
+ */
+ 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';
+ case 'random-asset-accountRole':
+ return $faker->randomElement(['defaultAsset', 'savingsAsset']);
+ case 'random-transactionType':
+ return $faker->randomElement(['withdrawal', 'deposit', 'transfer']);
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/tests/Objects/TestMandatoryField.php b/tests/Objects/TestMandatoryField.php
new file mode 100644
index 0000000000..006c5e2b87
--- /dev/null
+++ b/tests/Objects/TestMandatoryField.php
@@ -0,0 +1,20 @@
+<?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
new file mode 100644
index 0000000000..025124fc48
--- /dev/null
+++ b/tests/Objects/TestMandatoryFieldSet.php
@@ -0,0 +1,28 @@
+<?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
new file mode 100644
index 0000000000..3d38479aa2
--- /dev/null
+++ b/tests/Objects/TestObject.php
@@ -0,0 +1,16 @@
+<?php
+
+
+namespace Tests\Objects;
+
+/**
+ * Class TestObject
+ */
+class TestObject
+{
+ public function __construct()
+ {
+
+ }
+
+}
\ No newline at end of file