mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Expand some test helpers.
This commit is contained in:
parent
df86c89acc
commit
a93da2a912
@ -25,6 +25,8 @@ namespace Tests\Api\Models\Account;
|
|||||||
use Faker\Factory;
|
use Faker\Factory;
|
||||||
use Laravel\Passport\Passport;
|
use Laravel\Passport\Passport;
|
||||||
use Log;
|
use Log;
|
||||||
|
use Tests\Objects\Field;
|
||||||
|
use Tests\Objects\FieldSet;
|
||||||
use Tests\Objects\TestConfiguration;
|
use Tests\Objects\TestConfiguration;
|
||||||
use Tests\Objects\TestMandatoryField;
|
use Tests\Objects\TestMandatoryField;
|
||||||
use Tests\Objects\TestMandatoryFieldSet;
|
use Tests\Objects\TestMandatoryFieldSet;
|
||||||
@ -246,11 +248,11 @@ class StoreControllerTest extends TestCase
|
|||||||
$config = new TestConfiguration;
|
$config = new TestConfiguration;
|
||||||
|
|
||||||
// add a set of mandatory fields:
|
// add a set of mandatory fields:
|
||||||
$mandatoryFieldSet = new TestMandatoryFieldSet();
|
$mandatoryFieldSet = new FieldSet();
|
||||||
$mandatoryFieldSet->title = 'default_asset_account';
|
$mandatoryFieldSet->title = 'default_asset_account';
|
||||||
|
|
||||||
// name
|
// name
|
||||||
$mandatoryField = new TestMandatoryField;
|
$mandatoryField = new Field;
|
||||||
$mandatoryField->title = 'name';
|
$mandatoryField->title = 'name';
|
||||||
$mandatoryField->fieldTitle = 'name';
|
$mandatoryField->fieldTitle = 'name';
|
||||||
$mandatoryField->fieldPosition = ''; // root
|
$mandatoryField->fieldPosition = ''; // root
|
||||||
@ -258,10 +260,10 @@ class StoreControllerTest extends TestCase
|
|||||||
$mandatoryField->expectedReturnType = 'equal'; // or 'callback'
|
$mandatoryField->expectedReturnType = 'equal'; // or 'callback'
|
||||||
$mandatoryField->expectedReturn = null; // or the callback
|
$mandatoryField->expectedReturn = null; // or the callback
|
||||||
$mandatoryField->ignorableFields = [];
|
$mandatoryField->ignorableFields = [];
|
||||||
$mandatoryFieldSet->addMandatoryField($mandatoryField);
|
$mandatoryFieldSet->addField($mandatoryField);
|
||||||
|
|
||||||
// type
|
// type
|
||||||
$mandatoryField = new TestMandatoryField;
|
$mandatoryField = new Field;
|
||||||
$mandatoryField->title = 'type';
|
$mandatoryField->title = 'type';
|
||||||
$mandatoryField->fieldTitle = 'type';
|
$mandatoryField->fieldTitle = 'type';
|
||||||
$mandatoryField->fieldPosition = ''; // root
|
$mandatoryField->fieldPosition = ''; // root
|
||||||
@ -269,10 +271,10 @@ class StoreControllerTest extends TestCase
|
|||||||
$mandatoryField->expectedReturnType = 'equal'; // or 'callback'
|
$mandatoryField->expectedReturnType = 'equal'; // or 'callback'
|
||||||
$mandatoryField->expectedReturn = null; // or the callback
|
$mandatoryField->expectedReturn = null; // or the callback
|
||||||
$mandatoryField->ignorableFields = []; // something like transactions/0/currency_code
|
$mandatoryField->ignorableFields = []; // something like transactions/0/currency_code
|
||||||
$mandatoryFieldSet->addMandatoryField($mandatoryField);
|
$mandatoryFieldSet->addField($mandatoryField);
|
||||||
|
|
||||||
// role
|
// role
|
||||||
$mandatoryField = new TestMandatoryField;
|
$mandatoryField = new Field;
|
||||||
$mandatoryField->title = 'role';
|
$mandatoryField->title = 'role';
|
||||||
$mandatoryField->fieldTitle = 'account_role';
|
$mandatoryField->fieldTitle = 'account_role';
|
||||||
$mandatoryField->fieldPosition = ''; // root
|
$mandatoryField->fieldPosition = ''; // root
|
||||||
@ -280,19 +282,41 @@ class StoreControllerTest extends TestCase
|
|||||||
$mandatoryField->expectedReturnType = 'equal'; // or 'callback'
|
$mandatoryField->expectedReturnType = 'equal'; // or 'callback'
|
||||||
$mandatoryField->expectedReturn = null; // or the callback
|
$mandatoryField->expectedReturn = null; // or the callback
|
||||||
$mandatoryField->ignorableFields = []; // something like transactions/0/currency_code
|
$mandatoryField->ignorableFields = []; // something like transactions/0/currency_code
|
||||||
$mandatoryFieldSet->addMandatoryField($mandatoryField);
|
$mandatoryFieldSet->addField($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;
|
$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);
|
||||||
|
|
||||||
|
$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');
|
||||||
|
|
||||||
|
$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
|
// generate submissions
|
||||||
$arr = $config->generateSubmission();
|
$arr = $config->generateSubmission();
|
||||||
@ -300,7 +324,7 @@ class StoreControllerTest extends TestCase
|
|||||||
exit;
|
exit;
|
||||||
// generate expected returns.
|
// generate expected returns.
|
||||||
|
|
||||||
$set = [
|
$set = [
|
||||||
// set for withdrawal, copy this for
|
// set for withdrawal, copy this for
|
||||||
// other transaction types etc.
|
// other transaction types etc.
|
||||||
// make a CLASS!!
|
// make a CLASS!!
|
||||||
|
21
tests/Objects/Field.php
Normal file
21
tests/Objects/Field.php
Normal file
@ -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;
|
||||||
|
|
||||||
|
}
|
36
tests/Objects/FieldSet.php
Normal file
36
tests/Objects/FieldSet.php
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace Tests\Objects;
|
|
||||||
|
|
||||||
|
|
||||||
class IgnorableField
|
|
||||||
{
|
|
||||||
public string $fieldTitle;
|
|
||||||
public string $fieldPosition;
|
|
||||||
|
|
||||||
}
|
|
@ -11,8 +11,9 @@ use RuntimeException;
|
|||||||
*/
|
*/
|
||||||
class TestConfiguration
|
class TestConfiguration
|
||||||
{
|
{
|
||||||
public TestMandatoryFieldSet $mandatoryFieldSet;
|
public FieldSet $mandatoryFieldSet;
|
||||||
private array $submission;
|
public FieldSet $optionalFieldSet;
|
||||||
|
private array $submission;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TestConfiguration constructor.
|
* TestConfiguration constructor.
|
||||||
@ -22,37 +23,70 @@ class TestConfiguration
|
|||||||
$this->submission = [];
|
$this->submission = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function generateSubmission(): array
|
public function generateSubmission(): array
|
||||||
{
|
{
|
||||||
|
// first generate standard submission:
|
||||||
$this->submission = [];
|
$this->submission = [];
|
||||||
/** @var TestMandatoryField $field */
|
$standard = [];
|
||||||
foreach ($this->mandatoryFieldSet->mandatoryFields as $field) {
|
/** @var Field $field */
|
||||||
$this->parseField($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;
|
return $this->submission;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function parseField(TestMandatoryField $field)
|
/**
|
||||||
|
* @param $k
|
||||||
|
* @param $xs
|
||||||
|
*
|
||||||
|
* @return array|array[]
|
||||||
|
*/
|
||||||
|
private function combinationsOf($k, $xs): array
|
||||||
{
|
{
|
||||||
if ('' === $field->fieldPosition) {
|
if ($k === 0) {
|
||||||
$this->submission[$field->fieldTitle] = $this->generateFieldValue($field->fieldType);
|
return [[]];
|
||||||
}
|
}
|
||||||
if ('' !== $field->fieldPosition) {
|
if (count($xs) === 0) {
|
||||||
$positions = explode('/', $field->fieldPosition);
|
return [];
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
$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
|
* @param string $type
|
||||||
*
|
*
|
||||||
* @return mixed|string
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function generateFieldValue(string $type)
|
private function generateFieldValue(string $type)
|
||||||
{
|
{
|
||||||
@ -68,7 +102,35 @@ class TestConfiguration
|
|||||||
return $faker->randomElement(['defaultAsset', 'savingsAsset']);
|
return $faker->randomElement(['defaultAsset', 'savingsAsset']);
|
||||||
case 'random-transactionType':
|
case 'random-transactionType':
|
||||||
return $faker->randomElement(['withdrawal', 'deposit', 'transfer']);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -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;
|
|
||||||
|
|
||||||
}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace Tests\Objects;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class TestObject
|
|
||||||
*/
|
|
||||||
class TestObject
|
|
||||||
{
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user