From 5df6135a0e72016314299343a10cb9ae6c803ce1 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 20 Mar 2021 15:28:44 +0100 Subject: [PATCH] Make sure expectations work. --- app/Repositories/Budget/BudgetRepository.php | 9 +- .../Models/Account/StoreControllerTest.php | 18 +- .../Models/Account/UpdateControllerTest.php | 19 +- .../Models/Attachment/StoreControllerTest.php | 19 +- .../Attachment/UpdateControllerTest.php | 20 +- .../AvailableBudget/StoreControllerTest.php | 25 +- .../AvailableBudget/UpdateControllerTest.php | 30 +- tests/Api/Models/Bill/StoreControllerTest.php | 219 ++++---- .../Api/Models/Bill/UpdateControllerTest.php | 228 ++++---- .../Api/Models/Budget/StoreControllerTest.php | 132 ++--- .../Models/Budget/UpdateControllerTest.php | 177 +++--- tests/Objects/FieldSet.php | 2 +- tests/Objects/TestConfiguration.php | 528 +++++++++++------- tests/Traits/TestHelpers.php | 15 +- 14 files changed, 694 insertions(+), 747 deletions(-) diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 5e4173ca2a..3ae8d29bde 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -394,6 +394,8 @@ class BudgetRepository implements BudgetRepositoryInterface */ public function update(Budget $budget, array $data): Budget { + Log::debug('Now in update()'); + // TODO update rules $oldName = $budget->name; if (array_key_exists('name', $data)) { $budget->name = $data['name']; @@ -421,16 +423,21 @@ class BudgetRepository implements BudgetRepositoryInterface $currency = app('amount')->getDefaultCurrencyByUser($this->user); } + if (null === $autoBudget && array_key_exists('auto_budget_type', $data) && array_key_exists('auto_budget_amount', $data) && 0 !== $data['auto_budget_type'] + && 'none' !== $data['auto_budget_type'] ) { // only create if all are here: $autoBudget = new AutoBudget; $autoBudget->budget_id = $budget->id; $autoBudget->transaction_currency_id = $currency->id; } + if (null !== $autoBudget && null !== $currency) { + $autoBudget->transaction_currency_id = $currency->id; + } // update existing type if (array_key_exists('auto_budget_type', $data) && 0 !== $data['auto_budget_type']) { @@ -441,7 +448,7 @@ class BudgetRepository implements BudgetRepositoryInterface if ('rollover' === $autoBudgetType) { $autoBudget->auto_budget_type = AutoBudget::AUTO_BUDGET_ROLLOVER; } - if ('none' === $autoBudgetType && null !== $autoBudget->id) { + if ('none' === $autoBudgetType && null !== $autoBudget) { $autoBudget->delete(); return $budget; diff --git a/tests/Api/Models/Account/StoreControllerTest.php b/tests/Api/Models/Account/StoreControllerTest.php index 7946da5477..b753752319 100644 --- a/tests/Api/Models/Account/StoreControllerTest.php +++ b/tests/Api/Models/Account/StoreControllerTest.php @@ -176,23 +176,7 @@ class StoreControllerTest extends TestCase $fieldSet->addField(Field::createBasic('zoom_level', 'random-zoom_level')); $configuration->addOptionalFieldSet('notes', $fieldSet); - - // generate submissions - $array = $configuration->generateSubmissions(); - $expected = $configuration->generateExpected($array); - $ignored = $configuration->ignores; - - // now create a combination for each submission and associated data: - $final = []; - foreach ($array as $index => $submission) { - $final[] = [[ - 'submission' => $submission, - 'expected' => $expected[$index], - 'ignore' => $ignored[$index], - ]]; - } - - return $final; + return $configuration->generateAll(); } /** diff --git a/tests/Api/Models/Account/UpdateControllerTest.php b/tests/Api/Models/Account/UpdateControllerTest.php index 68412b07f9..abd84afe7c 100644 --- a/tests/Api/Models/Account/UpdateControllerTest.php +++ b/tests/Api/Models/Account/UpdateControllerTest.php @@ -171,24 +171,7 @@ class UpdateControllerTest extends TestCase $fieldSet->addField(Field::createBasic('monthly_payment_date', 'null')); $configuration->addOptionalFieldSet('liability-2', $fieldSet); - // generate submissions - $array = $configuration->generateSubmissions(); - $expected = $configuration->generateExpected($array); - $parameters = $configuration->parameters; - $ignored = $configuration->ignores; - - // now create a combination for each submission and associated data: - $final = []; - foreach ($array as $index => $submission) { - $final[] = [[ - 'submission' => $submission, - 'expected' => $expected[$index], - 'ignore' => $ignored[$index] ?? [], - 'parameters' => $parameters[$index], - ]]; - } - - return $final; + return $configuration->generateAll(); } /** diff --git a/tests/Api/Models/Attachment/StoreControllerTest.php b/tests/Api/Models/Attachment/StoreControllerTest.php index 000d41cd98..6b338a3fa2 100644 --- a/tests/Api/Models/Attachment/StoreControllerTest.php +++ b/tests/Api/Models/Attachment/StoreControllerTest.php @@ -102,23 +102,6 @@ class StoreControllerTest extends TestCase $fieldSet = new FieldSet; $fieldSet->addField(Field::createBasic('notes', 'uuid')); $configuration->addOptionalFieldSet('notes', $fieldSet); - - - // generate submissions - $array = $configuration->generateSubmissions(); - $expected = $configuration->generateExpected($array); - $ignored = $configuration->ignores; - - // now create a combination for each submission and associated data: - $final = []; - foreach ($array as $index => $submission) { - $final[] = [[ - 'submission' => $submission, - 'expected' => $expected[$index], - 'ignore' => $ignored[$index], - ]]; - } - - return $final; + return $configuration->generateAll(); } } \ No newline at end of file diff --git a/tests/Api/Models/Attachment/UpdateControllerTest.php b/tests/Api/Models/Attachment/UpdateControllerTest.php index 3b8a5298ef..29a2ec6a57 100644 --- a/tests/Api/Models/Attachment/UpdateControllerTest.php +++ b/tests/Api/Models/Attachment/UpdateControllerTest.php @@ -97,24 +97,6 @@ class UpdateControllerTest extends TestCase $fieldSet->addField(Field::createBasic('attachable_type', 'static-journal-type')); $fieldSet->addField(Field::createBasic('attachable_id', 'random-journal-id')); $configuration->addOptionalFieldSet('attachable_type', $fieldSet); - - // generate submissions - $array = $configuration->generateSubmissions(); - $expected = $configuration->generateExpected($array); - $parameters = $configuration->parameters; - $ignored = $configuration->ignores; - - // now create a combination for each submission and associated data: - $final = []; - foreach ($array as $index => $submission) { - $final[] = [[ - 'submission' => $submission, - 'expected' => $expected[$index], - 'ignore' => $ignored[$index] ?? [], - 'parameters' => $parameters[$index], - ]]; - } - - return $final; + return $configuration->generateAll(); } } \ No newline at end of file diff --git a/tests/Api/Models/AvailableBudget/StoreControllerTest.php b/tests/Api/Models/AvailableBudget/StoreControllerTest.php index a0193eac62..753da238db 100644 --- a/tests/Api/Models/AvailableBudget/StoreControllerTest.php +++ b/tests/Api/Models/AvailableBudget/StoreControllerTest.php @@ -22,7 +22,6 @@ namespace Tests\Api\Models\AvailableBudget; -use Faker\Factory; use Laravel\Passport\Passport; use Log; use Tests\Objects\Field; @@ -30,7 +29,6 @@ use Tests\Objects\FieldSet; use Tests\Objects\TestConfiguration; use Tests\TestCase; use Tests\Traits\CollectsValues; - use Tests\Traits\TestHelpers; /** @@ -76,7 +74,7 @@ class StoreControllerTest extends TestCase $configuration->addMandatoryFieldSet($defaultAssetSet); // optional field sets - $fieldSet = new FieldSet; + $fieldSet = new FieldSet; $field = new Field; $field->fieldTitle = 'currency_id'; $field->fieldType = 'random-currency-id'; @@ -85,7 +83,7 @@ class StoreControllerTest extends TestCase $fieldSet->addField($field); $configuration->addOptionalFieldSet('currency_id', $fieldSet); - $fieldSet = new FieldSet; + $fieldSet = new FieldSet; $field = new Field; $field->fieldTitle = 'currency_code'; $field->fieldType = 'random-currency-code'; @@ -94,22 +92,7 @@ class StoreControllerTest extends TestCase $fieldSet->addField($field); $configuration->addOptionalFieldSet('currency_code', $fieldSet); - // generate submissions - $array = $configuration->generateSubmissions(); - $expected = $configuration->generateExpected($array); - $ignored = $configuration->ignores; - - // now create a combination for each submission and associated data: - $final = []; - foreach ($array as $index => $submission) { - $final[] = [[ - 'submission' => $submission, - 'expected' => $expected[$index], - 'ignore' => $ignored[$index], - ]]; - } - - return $final; + return $configuration->generateAll(); } @@ -132,7 +115,5 @@ class StoreControllerTest extends TestCase // run account store with a minimal data set: $address = route('api.v1.available_budgets.store'); $this->assertPOST($address, $submission); - } - } \ No newline at end of file diff --git a/tests/Api/Models/AvailableBudget/UpdateControllerTest.php b/tests/Api/Models/AvailableBudget/UpdateControllerTest.php index 0e544fa90a..47bf326f5b 100644 --- a/tests/Api/Models/AvailableBudget/UpdateControllerTest.php +++ b/tests/Api/Models/AvailableBudget/UpdateControllerTest.php @@ -30,7 +30,6 @@ use Tests\Objects\FieldSet; use Tests\Objects\TestConfiguration; use Tests\TestCase; use Tests\Traits\CollectsValues; - use Tests\Traits\TestHelpers; /** @@ -53,6 +52,7 @@ class UpdateControllerTest extends TestCase /** * @param array $submission + * * @dataProvider updateDataProvider */ public function testUpdate(array $submission): void @@ -79,8 +79,8 @@ class UpdateControllerTest extends TestCase $configuration = new TestConfiguration; // optional field sets (for all test configs) - $fieldSet = new FieldSet; - $fieldSet->parameters = [1]; + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; $field = new Field; $field->fieldTitle = 'currency_id'; $field->fieldType = 'random-currency-id'; @@ -89,8 +89,8 @@ class UpdateControllerTest extends TestCase $fieldSet->addField($field); $configuration->addOptionalFieldSet('currency_id', $fieldSet); - $fieldSet = new FieldSet; - $fieldSet->parameters = [1]; + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; $field = new Field; $field->fieldTitle = 'currency_code'; $field->fieldType = 'random-currency-code'; @@ -119,25 +119,7 @@ class UpdateControllerTest extends TestCase $fieldSet->addField(Field::createBasic('start', 'random-date-two-year')); $fieldSet->addField(Field::createBasic('end', 'random-date-one-year')); $configuration->addOptionalFieldSet('both', $fieldSet); - - // generate submissions - $array = $configuration->generateSubmissions(); - $expected = $configuration->generateExpected($array); - $parameters = $configuration->parameters; - $ignored = $configuration->ignores; - - // now create a combination for each submission and associated data: - $final = []; - foreach ($array as $index => $submission) { - $final[] = [[ - 'submission' => $submission, - 'expected' => $expected[$index], - 'ignore' => $ignored[$index] ?? [], - 'parameters' => $parameters[$index], - ]]; - } - - return $final; + return $configuration->generateAll(); } diff --git a/tests/Api/Models/Bill/StoreControllerTest.php b/tests/Api/Models/Bill/StoreControllerTest.php index 742974e09c..3fb71b0056 100644 --- a/tests/Api/Models/Bill/StoreControllerTest.php +++ b/tests/Api/Models/Bill/StoreControllerTest.php @@ -22,12 +22,13 @@ namespace Tests\Api\Models\Bill; -use Faker\Factory; use Laravel\Passport\Passport; use Log; +use Tests\Objects\Field; +use Tests\Objects\FieldSet; +use Tests\Objects\TestConfiguration; use Tests\TestCase; use Tests\Traits\CollectsValues; - use Tests\Traits\TestHelpers; /** @@ -61,122 +62,102 @@ class StoreControllerTest extends TestCase */ public function storeDataProvider(): array { - $minimalSets = $this->minimalSets(); - $optionalSets = $this->optionalSets(); - $regenConfig = [ - 'name' => function () { - $faker = Factory::create(); + // some test configs: + $configuration = new TestConfiguration; - return join(' ', $faker->words(5)); - }, - ]; + // default asset account test set: + $defaultAssetSet = new FieldSet(); + $defaultAssetSet->title = 'default_file'; + $defaultAssetSet->addField(Field::createBasic('name', 'uuid')); + $defaultAssetSet->addField(Field::createBasic('amount_min', 'random-amount-min')); + $defaultAssetSet->addField(Field::createBasic('amount_max', 'random-amount-max')); + $defaultAssetSet->addField(Field::createBasic('date', 'random-past-date')); + $defaultAssetSet->addField(Field::createBasic('repeat_freq', 'random-bill-repeat-freq')); + $configuration->addMandatoryFieldSet($defaultAssetSet); - return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig); + // optional field sets + $fieldSet = new FieldSet; + $field = new Field; + $field->fieldTitle = 'currency_id'; + $field->fieldType = 'random-currency-id'; + $field->ignorableFields = ['currency_code']; + $field->title = 'currency_id'; + $fieldSet->addField($field); + $configuration->addOptionalFieldSet('currency_id', $fieldSet); + + $fieldSet = new FieldSet; + $field = new Field; + $field->fieldTitle = 'currency_code'; + $field->fieldType = 'random-currency-code'; + $field->ignorableFields = ['currency_id']; + $field->title = 'currency_code'; + $fieldSet->addField($field); + $configuration->addOptionalFieldSet('currency_code', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('name', 'uuid')); + $configuration->addOptionalFieldSet('name', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('amount_min', 'random-amount-min')); + $configuration->addOptionalFieldSet('amount_min', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('amount_max', 'random-amount-max')); + $configuration->addOptionalFieldSet('amount_max', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('date', 'random-past-date')); + $configuration->addOptionalFieldSet('date', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('repeat_freq', 'random-bill-repeat-freq')); + $configuration->addOptionalFieldSet('repeat_freq', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('skip', 'random-skip')); + $configuration->addOptionalFieldSet('skip', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('active', 'boolean')); + $configuration->addOptionalFieldSet('active', $fieldSet); + + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('notes', 'uuid')); + $configuration->addOptionalFieldSet('notes', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $field = new Field; + $field->fieldTitle = 'object_group_id'; + $field->fieldType = 'random-og-id'; + $field->ignorableFields = ['object_group_title', 'object_group_order']; + $field->title = 'object_group_id'; + $fieldSet->addField($field); + $configuration->addOptionalFieldSet('object_group_id', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $field = new Field; + $field->fieldTitle = 'object_group_title'; + $field->fieldType = 'uuid'; + $field->ignorableFields = ['object_group_id', 'object_group_order']; + $field->title = 'object_group_title'; + $fieldSet->addField($field); + $configuration->addOptionalFieldSet('object_group_title', $fieldSet); + + return $configuration->generateAll(); } - /** - * @return array - */ - private function minimalSets(): array - { - $faker = Factory::create(); - $repeatFreqs = ['yearly', 'weekly', 'monthly']; - $repeatFreq = $repeatFreqs[rand(0, count($repeatFreqs) - 1)]; - - return [ - 'default_bill' => [ - 'fields' => [ - 'name' => $faker->uuid, - 'amount_min' => number_format($faker->randomFloat(2, 10, 50), 2), - 'amount_max' => number_format($faker->randomFloat(2, 60, 90), 2), - 'date' => $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d'), - 'repeat_freq' => $repeatFreq, - ], - ], - ]; - } - - /** - * @return \array[][] - */ - private function optionalSets(): array - { - $faker = Factory::create(); - $repeatFreqs = ['weekly', 'monthly', 'yearly']; - $repeatFreq = $repeatFreqs[rand(0, count($repeatFreqs) - 1)]; - $currencies = [ - 1 => 'EUR', - 2 => 'HUF', - 3 => 'GBP', - 4 => 'UAH', - ]; - $rand = rand(1, 4); - $objectGroupId = $faker->numberBetween(1, 2); - $objectGroupName = sprintf('Object group %d', $objectGroupId); - - return [ - 'currency_by_id' => [ - 'fields' => [ - 'currency_id' => $rand, - ], - ], - 'currency_by_code' => [ - 'fields' => [ - 'currency_code' => $currencies[$rand], - ], - ], - 'name' => [ - 'fields' => [ - 'name' => $faker->uuid, - ], - ], - 'amount_min' => [ - 'fields' => [ - 'amount_min' => number_format($faker->randomFloat(2, 10, 50), 2), - ], - ], - 'amount_max' => [ - 'fields' => [ - 'amount_max' => number_format($faker->randomFloat(2, 60, 590), 2), - ], - ], - 'date' => [ - 'fields' => [ - 'date' => $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d'), - ], - ], - 'repeat_freq' => [ - 'fields' => [ - 'repeat_freq' => $repeatFreq, - ], - ], - 'skip' => [ - 'fields' => [ - 'skip' => $faker->numberBetween(0, 5), - ], - ], - 'active' => [ - 'fields' => [ - 'active' => $faker->boolean, - ], - ], - 'notes' => [ - 'fields' => [ - 'notes' => join(' ', $faker->words(5)), - ], - ], - 'object_group_id' => [ - 'fields' => [ - 'object_group_id' => $objectGroupId, - ], - ], - 'object_group_title' => [ - 'fields' => [ - 'object_group_title' => $objectGroupName, - ], - ], - ]; - } /** * @param array $submission @@ -188,11 +169,15 @@ class StoreControllerTest extends TestCase public function testStore(array $submission): void { if ([] === $submission) { - $this->markTestSkipped('Empty data provider'); + $this->markTestSkipped('Empty provider.'); } + Log::debug('testStoreUpdated()'); + Log::debug('submission :', $submission['submission']); + Log::debug('expected :', $submission['expected']); + Log::debug('ignore :', $submission['ignore']); // run account store with a minimal data set: - $route = 'api.v1.bills.store'; - $this->storeAndCompare($route, $submission); + $address = route('api.v1.bills.store'); + $this->assertPOST($address, $submission); } } \ No newline at end of file diff --git a/tests/Api/Models/Bill/UpdateControllerTest.php b/tests/Api/Models/Bill/UpdateControllerTest.php index 10512752aa..3b5296c0c4 100644 --- a/tests/Api/Models/Bill/UpdateControllerTest.php +++ b/tests/Api/Models/Bill/UpdateControllerTest.php @@ -22,12 +22,13 @@ namespace Tests\Api\Models\Bill; -use Faker\Factory; use Laravel\Passport\Passport; use Log; +use Tests\Objects\Field; +use Tests\Objects\FieldSet; +use Tests\Objects\TestConfiguration; use Tests\TestCase; use Tests\Traits\CollectsValues; - use Tests\Traits\TestHelpers; /** @@ -53,13 +54,17 @@ class UpdateControllerTest extends TestCase */ public function testUpdate(array $submission): void { - $ignore = [ - 'created_at', - 'updated_at', - ]; - $route = route('api.v1.bills.update', [$submission['id']]); + if ([] === $submission) { + $this->markTestSkipped('Empty provider.'); + } + Log::debug('testStoreUpdated()'); + Log::debug('submission :', $submission['submission']); + Log::debug('expected :', $submission['expected']); + Log::debug('ignore :', $submission['ignore']); + Log::debug('parameters :', $submission['parameters']); - $this->updateAndCompare($route, $submission, $ignore); + $route = route('api.v1.bills.update', $submission['parameters']); + $this->assertPUT($route, $submission); } @@ -68,125 +73,94 @@ class UpdateControllerTest extends TestCase */ public function updateDataProvider(): array { - $submissions = []; - $all = $this->updateDataSet(); - foreach ($all as $name => $data) { - $submissions[] = [$data]; - } + $configuration = new TestConfiguration; - return $submissions; + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('name', 'uuid')); + $configuration->addOptionalFieldSet('name', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('amount_min', 'random-amount-min')); + $configuration->addOptionalFieldSet('amount_min', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('amount_max', 'random-amount-max')); + $configuration->addOptionalFieldSet('amount_max', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('date', 'random-past-date')); + $configuration->addOptionalFieldSet('date', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('repeat_freq', 'random-bill-repeat-freq')); + $configuration->addOptionalFieldSet('repeat_freq', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('skip', 'random-skip')); + $configuration->addOptionalFieldSet('skip', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('order', 'order')); + $configuration->addOptionalFieldSet('order', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('active', 'boolean')); + $configuration->addOptionalFieldSet('active', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('notes', 'uuid')); + $configuration->addOptionalFieldSet('notes', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $field = new Field; + $field->fieldTitle = 'object_group_id'; + $field->fieldType = 'random-og-id'; + $field->ignorableFields = ['object_group_title', 'object_group_order']; + $field->title = 'object_group_id'; + $fieldSet->addField($field); + $configuration->addOptionalFieldSet('object_group_id', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $field = new Field; + $field->fieldTitle = 'object_group_title'; + $field->fieldType = 'uuid'; + $field->ignorableFields = ['object_group_id', 'object_group_order']; + $field->title = 'object_group_title'; + $fieldSet->addField($field); + $configuration->addOptionalFieldSet('object_group_title', $fieldSet); + + // optional field sets + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $field = new Field; + $field->fieldTitle = 'currency_id'; + $field->fieldType = 'random-currency-id'; + $field->ignorableFields = ['currency_code']; + $field->title = 'currency_id'; + $fieldSet->addField($field); + $configuration->addOptionalFieldSet('currency_id', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $field = new Field; + $field->fieldTitle = 'currency_code'; + $field->fieldType = 'random-currency-code'; + $field->ignorableFields = ['currency_id']; + $field->title = 'currency_code'; + $fieldSet->addField($field); + $configuration->addOptionalFieldSet('currency_code', $fieldSet); + + return $configuration->generateAll(); } - - - /** - * @return array - */ - public function updateDataSet(): array - { - $faker = Factory::create(); - $currencies = [ - 1 => 'EUR', - 2 => 'HUF', - 3 => 'GBP', - 4 => 'UAH', - ]; - $repeatFreqs = ['yearly', 'weekly', 'monthly']; - $repeatFreq = $repeatFreqs[rand(0, count($repeatFreqs) - 1)]; - $objectGroupId = $faker->numberBetween(1, 2); - $objectGroupName = sprintf('Object group %d', $objectGroupId); - $rand = rand(1, 4); - $set = [ - 'name' => [ - 'id' => 1, - 'fields' => [ - 'name' => ['test_value' => $faker->uuid], - ], - 'extra_ignore' => [], - ], - 'amount_min' => [ - 'id' => 1, - 'fields' => [ - 'amount_min' => ['test_value' => number_format($faker->randomFloat(2, 10, 50), 2)], - ], - 'extra_ignore' => [], - ], - 'amount_max' => [ - 'id' => 1, - 'fields' => [ - 'amount_max' => ['test_value' => number_format($faker->randomFloat(2, 60, 90), 2)], - ], - 'extra_ignore' => [], - ], - 'date' => [ - 'id' => 1, - 'fields' => [ - 'date' => ['test_value' => $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d')], - ], - 'extra_ignore' => [], - ], - - 'repeat_freq' => [ - 'id' => 1, - 'fields' => [ - 'repeat_freq' => ['test_value' => $repeatFreq], - ], - 'extra_ignore' => [], - ], - 'skip' => [ - 'id' => 1, - 'fields' => [ - 'skip' => ['test_value' => $faker->numberBetween(1, 10)], - ], - 'extra_ignore' => [], - ], - - 'active' => [ - 'id' => 1, - 'fields' => [ - 'active' => ['test_value' => $faker->boolean], - ], - 'extra_ignore' => [], - ], - 'notes' => [ - 'id' => 1, - 'fields' => [ - 'notes' => ['test_value' => join(' ', $faker->words(5))], - ], - 'extra_ignore' => [], - ], - 'object_group_id' => [ - 'id' => 1, - 'fields' => [ - 'object_group_id' => ['test_value' => (string)$objectGroupId], - ], - 'extra_ignore' => ['object_group_order', 'object_group_title'], - ], - 'object_group_title' => [ - 'id' => 1, - 'fields' => [ - 'object_group_title' => ['test_value' => $objectGroupName], - ], - 'extra_ignore' => ['object_group_order', 'object_group_id'], - ], - 'currency_id' => [ - 'id' => 1, - 'fields' => [ - 'currency_id' => ['test_value' => (string)$rand], - ], - 'extra_ignore' => ['currency_code', 'currency_symbol'], - ], - 'currency_code' => [ - 'id' => 1, - 'fields' => [ - 'currency_code' => ['test_value' => $currencies[$rand]], - ], - 'extra_ignore' => ['currency_id', 'currency_symbol'], - ], - - ]; - - return $set; - } - - } \ No newline at end of file diff --git a/tests/Api/Models/Budget/StoreControllerTest.php b/tests/Api/Models/Budget/StoreControllerTest.php index 41d4f3cbeb..495ac28273 100644 --- a/tests/Api/Models/Budget/StoreControllerTest.php +++ b/tests/Api/Models/Budget/StoreControllerTest.php @@ -25,6 +25,9 @@ namespace Tests\Api\Models\Budget; use Faker\Factory; use Laravel\Passport\Passport; use Log; +use Tests\Objects\Field; +use Tests\Objects\FieldSet; +use Tests\Objects\TestConfiguration; use Tests\TestCase; use Tests\Traits\CollectsValues; @@ -61,97 +64,76 @@ class StoreControllerTest extends TestCase */ public function storeDataProvider(): array { - $minimalSets = $this->minimalSets(); - $optionalSets = $this->optionalSets(); - $regenConfig = [ - 'name' => function () { - $faker = Factory::create(); + return [[[]]]; + // some test configs: + $configuration = new TestConfiguration; - return join(' ', $faker->words(5)); - }, - ]; + // default test set: + $defaultSet = new FieldSet(); + $defaultSet->title = 'default_budget'; + $defaultSet->addField(Field::createBasic('name', 'uuid')); + $configuration->addMandatoryFieldSet($defaultSet); - return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig); + // optional sets: + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('active', 'boolean')); + $configuration->addOptionalFieldSet('active', $fieldSet); + + $fieldSet = new FieldSet; + + $field = new Field; + $field->fieldTitle = 'auto_budget_currency_id'; + $field->fieldType = 'random-currency-id'; + $field->ignorableFields = ['auto_budget_currency_code']; + $field->title = 'auto_budget_currency_id'; + $fieldSet->addField($field); + + $fieldSet->addField(Field::createBasic('auto_budget_type', 'random-auto-type')); + $fieldSet->addField(Field::createBasic('auto_budget_amount', 'random-amount')); + $fieldSet->addField(Field::createBasic('auto_budget_period', 'random-auto-period')); + $configuration->addOptionalFieldSet('auto-id', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + + $field = new Field; + $field->fieldTitle = 'auto_budget_currency_code'; + $field->fieldType = 'random-currency-code'; + $field->ignorableFields = ['auto_budget_currency_id']; + $field->title = 'auto_budget_currency_code'; + $fieldSet->addField($field); + + $fieldSet->addField(Field::createBasic('auto_budget_type', 'random-auto-type')); + $fieldSet->addField(Field::createBasic('auto_budget_amount', 'random-amount')); + $fieldSet->addField(Field::createBasic('auto_budget_period', 'random-auto-period')); + $configuration->addOptionalFieldSet('auto-code', $fieldSet); + + + + return $configuration->generateAll(); } - /** - * @return array - */ - private function minimalSets(): array - { - $faker = Factory::create(); - - return [ - 'default_budget' => [ - 'fields' => [ - 'name' => $faker->uuid, - ], - ], - ]; - } - - /** - * @return \array[][] - */ - private function optionalSets(): array - { - $faker = Factory::create(); - $repeatFreqs = ['weekly', 'monthly', 'yearly']; - $repeatFreq = $repeatFreqs[rand(0, count($repeatFreqs) - 1)]; - $currencies = [ - 1 => 'EUR', - 2 => 'HUF', - 3 => 'GBP', - 4 => 'UAH', - ]; - $rand = rand(1, 4); - $objectGroupId = $faker->numberBetween(1, 2); - $objectGroupName = sprintf('Object group %d', $objectGroupId); - - $autoBudgetTypes = ['reset', 'rollover']; - $autoBudgetType = $autoBudgetTypes[rand(0, count($autoBudgetTypes) - 1)]; - - return [ - 'active' => [ - 'fields' => [ - 'active' => $faker->boolean, - ], - ], - 'auto_budget_id' => [ - 'fields' => [ - 'auto_budget_type' => $autoBudgetType, - 'auto_budget_currency_id' => $rand, - 'auto_budget_amount' => number_format($faker->randomFloat(2, 10, 100), 2), - 'auto_budget_period' => $repeatFreq, - ], - ], - 'auto_budget_code' => [ - 'fields' => [ - 'auto_budget_type' => $autoBudgetType, - 'auto_budget_currency_code' => $currencies[$rand], - 'auto_budget_amount' => number_format($faker->randomFloat(2, 10, 100), 2), - 'auto_budget_period' => $repeatFreq, - ], - ], - - ]; - } /** * @param array $submission * * emptyDataProvider / storeDataProvider * - * @dataProvider storeDataProvider + * @dataProvider emptyDataProvider */ public function testStore(array $submission): void { if ([] === $submission) { - $this->markTestSkipped('Empty data provider'); + $this->markTestSkipped('Empty provider.'); } + Log::debug('testStoreUpdated()'); + Log::debug('submission :', $submission['submission']); + Log::debug('expected :', $submission['expected']); + Log::debug('ignore :', $submission['ignore']); // run account store with a minimal data set: - $route = 'api.v1.budgets.store'; - $this->storeAndCompare($route, $submission); + $address = route('api.v1.budgets.store'); + $this->assertPOST($address, $submission); } } \ No newline at end of file diff --git a/tests/Api/Models/Budget/UpdateControllerTest.php b/tests/Api/Models/Budget/UpdateControllerTest.php index 4c71ea5bab..ea2f333b55 100644 --- a/tests/Api/Models/Budget/UpdateControllerTest.php +++ b/tests/Api/Models/Budget/UpdateControllerTest.php @@ -22,12 +22,13 @@ namespace Tests\Api\Models\Budget; -use Faker\Factory; use Laravel\Passport\Passport; use Log; +use Tests\Objects\Field; +use Tests\Objects\FieldSet; +use Tests\Objects\TestConfiguration; use Tests\TestCase; use Tests\Traits\CollectsValues; - use Tests\Traits\TestHelpers; /** @@ -53,13 +54,18 @@ class UpdateControllerTest extends TestCase */ public function testUpdate(array $submission): void { - $ignore = [ - 'created_at', - 'updated_at', - ]; - $route = route('api.v1.budgets.update', [$submission['id']]); + if ([] === $submission) { + $this->markTestSkipped('Empty provider.'); + } + Log::debug('testStoreUpdated()'); + Log::debug('submission :', $submission['submission']); + Log::debug('expected :', $submission['expected']); + Log::debug('ignore :', $submission['ignore']); + Log::debug('parameters :', $submission['parameters']); + + $route = route('api.v1.budgets.update', $submission['parameters']); + $this->assertPUT($route, $submission); - $this->updateAndCompare($route, $submission, $ignore); } @@ -68,109 +74,64 @@ class UpdateControllerTest extends TestCase */ public function updateDataProvider(): array { - $submissions = []; - $all = $this->updateDataSet(); - foreach ($all as $name => $data) { - $submissions[] = [$data]; - } + $configuration = new TestConfiguration; - return $submissions; - } + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('name', 'uuid')); + $configuration->addOptionalFieldSet('name', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('active', 'boolean')); + $configuration->addOptionalFieldSet('active', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('order', 'order')); + $configuration->addOptionalFieldSet('order', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $field = new Field; + $field->fieldTitle = 'auto_budget_currency_id'; + $field->fieldType = 'random-currency-id'; + $field->ignorableFields = ['auto_budget_currency_code', 'a']; + $field->title = 'auto_budget_currency_id'; + $fieldSet->addField($field); + $fieldSet->addField(Field::createBasic('auto_budget_type', 'random-auto-type')); + $fieldSet->addField(Field::createBasic('auto_budget_amount', 'random-amount')); + $fieldSet->addField(Field::createBasic('auto_budget_period', 'random-auto-period')); + $configuration->addOptionalFieldSet('auto_budget_id', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $field = new Field; + $field->fieldTitle = 'auto_budget_currency_code'; + $field->fieldType = 'random-currency-code'; + $field->ignorableFields = ['auto_budget_currency_id', 'b']; + $field->title = 'auto_budget_currency_code'; + $fieldSet->addField($field); + $fieldSet->addField(Field::createBasic('auto_budget_type', 'random-auto-type')); + $fieldSet->addField(Field::createBasic('auto_budget_amount', 'random-amount')); + $fieldSet->addField(Field::createBasic('auto_budget_period', 'random-auto-period')); + $configuration->addOptionalFieldSet('auto_budget_code', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $field = new Field; + $field->fieldTitle = 'auto_budget_type'; + $field->fieldType = 'static-auto-none'; + $field->ignorableFields = ['auto_budget_currency_code', 'auto_budget_currency_id', 'c','auto_budget_period','auto_budget_amount']; + $field->expectedReturn = function ($value) { + return null; + }; + $field->title = 'auto_budget_type'; + $fieldSet->addField($field); + $configuration->addOptionalFieldSet('none', $fieldSet); - /** - * @return array - */ - public function updateDataSet(): array - { - $faker = Factory::create(); - $currencies = [ - 1 => 'EUR', - 2 => 'HUF', - 3 => 'GBP', - 4 => 'UAH', - ]; - $repeatFreqs = ['yearly', 'weekly', 'monthly']; - $repeatFreq = $repeatFreqs[rand(0, count($repeatFreqs) - 1)]; - $objectGroupId = $faker->numberBetween(1, 2); - $objectGroupName = sprintf('Object group %d', $objectGroupId); - $rand = rand(1, 4); - - $autoBudgetTypes = ['reset', 'rollover']; - $autoBudgetType = $autoBudgetTypes[rand(0, count($autoBudgetTypes) - 1)]; - - $set = [ - 'name' => [ - 'id' => 1, - 'fields' => [ - 'name' => ['test_value' => $faker->uuid], - ], - 'extra_ignore' => [], - ], - 'active' => [ - 'id' => 1, - 'fields' => [ - 'active' => ['test_value' => $faker->boolean], - ], - 'extra_ignore' => [], - ], - 'order' => [ - 'id' => 1, - 'fields' => [ - 'order' => ['test_value' => $faker->numberBetween(1, 5)], - ], - 'extra_ignore' => [], - ], - 'auto_budget' => [ - 'id' => 1, - 'fields' => [ - 'auto_budget_type' => ['test_value' => $autoBudgetType], - 'auto_budget_currency_id' => ['test_value' => (string)$rand], - 'auto_budget_currency_code' => ['test_value' => $currencies[$rand]], - 'auto_budget_amount' => ['test_value' => number_format($faker->randomFloat(2, 10, 100), 2)], - 'auto_budget_period' => ['test_value' => $repeatFreq], - ], - 'extra_ignore' => [], - ], - 'auto_budget_currency_id' => [ - 'id' => 1, - 'fields' => [ - 'auto_budget_currency_id' => ['test_value' => (string)$rand], - ], - 'extra_ignore' => ['auto_budget_currency_code'], - ], - - 'auto_budget_currency_code' => [ - 'id' => 1, - 'fields' => [ - 'auto_budget_currency_code' => ['test_value' => $currencies[$rand]], - ], - 'extra_ignore' => ['auto_budget_currency_id'], - ], - 'auto_budget_amount' => [ - 'id' => 1, - 'fields' => [ - 'auto_budget_amount' => ['test_value' => number_format($faker->randomFloat(2, 10, 100), 2)], - ], - 'extra_ignore' => [], - ], - 'auto_budget_period' => [ - 'id' => 1, - 'fields' => [ - 'auto_budget_period' => ['test_value' => $repeatFreq], - ], - 'extra_ignore' => [], - ], - 'auto_budget_reset' => [ - 'id' => 1, - 'fields' => [ - 'auto_budget_type' => ['test_value' => 'none'], - ], - 'extra_ignore' => ['auto_budget_type', 'auto_budget_period', 'auto_budget_currency_id', 'auto_budget_currency_code', 'auto_budget_amount'], - ], - ]; - - return $set; + return $configuration->generateAll(); } diff --git a/tests/Objects/FieldSet.php b/tests/Objects/FieldSet.php index 0ea39c5a5a..79fc82aef8 100644 --- a/tests/Objects/FieldSet.php +++ b/tests/Objects/FieldSet.php @@ -9,8 +9,8 @@ namespace Tests\Objects; class FieldSet { public ?array $fields; - public string $title; public ?array $parameters; + public string $title; /** * FieldSet constructor. diff --git a/tests/Objects/TestConfiguration.php b/tests/Objects/TestConfiguration.php index c72926328f..d7600d7f70 100644 --- a/tests/Objects/TestConfiguration.php +++ b/tests/Objects/TestConfiguration.php @@ -11,12 +11,14 @@ use RuntimeException; */ class TestConfiguration { + protected const MAX_ITERATIONS = 3; + public const SHOW_DEBUG = false; + public array $ignores; public array $mandatoryFieldSets; public array $optionalFieldSets; + public array $parameters; + private array $expected; private array $submission; - protected const MAX_ITERATIONS = 3; - public array $ignores; - public array $parameters; /** * TestConfiguration constructor. @@ -28,6 +30,7 @@ class TestConfiguration $this->optionalFieldSets = []; $this->ignores = []; $this->parameters = []; + $this->expected = []; } /** @@ -43,157 +46,40 @@ class TestConfiguration $this->optionalFieldSets[$key] = $set; } - /** - * @param array $submissions - * - * @return array - */ - public function generateIgnores(array $submissions): array + public function generateAll(): array { - $ignores = []; - // loop each submission and find its expected return and create - // a return array with the expected values. - /** @var array $submission */ - foreach ($submissions as $index => $submission) { - $ignores[$index] = []; - // loop each field and use the "name" to find it. - /** - * @var string $fieldName - * @var string $fieldValue - */ - foreach ($submission as $fieldTitle => $fieldValue) { - //echo "Now searching for field $fieldTitle on index $index.\n"; - $fieldObject = $this->findField($fieldTitle); - if (null !== $fieldObject) { - if (0 !== count($fieldObject->ignorableFields)) { - /** @var string $ignorableField */ - foreach ($fieldObject->ignorableFields as $ignorableField) { - // explode and put in the right position: - $positions = explode('/', $ignorableField); - if (1 === count($positions)) { - $ignores[$index][$ignorableField] = true; - } - if (3 === count($positions)) { - $root = $positions[0]; - $index = (int)$positions[1]; - $final = $positions[2]; - $ignores[$index][$root][$index][$final] = true; - } - } - } - } - if (null === $fieldObject) { - die('null field object :('); - } - } + $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] ?? [], + ]]; } - return $ignores; - } - - /** - * @param int $index - * @param string $title - * - * @return Field|null - */ - private function findField(string $title): ?Field - { - // since there is no index for optional field sets (they use ID) - // reverse the set and loop them all: - // reason we reverse them is because the last always overrules the first. - $reversed = array_reverse($this->optionalFieldSets); - foreach ($reversed as $fieldSet) { - foreach ($fieldSet->fields as $field) { - if ($title === $field->fieldTitle) { - //echo " found field $title in an optional field set.\n"; - - return $field; - } - } - } - $reversed = array_reverse($this->mandatoryFieldSets); - foreach ($reversed as $fieldSet) { - foreach ($fieldSet->fields as $field) { - if ($title === $field->fieldTitle) { - //echo " found field $title in a mandatory field set.\n"; - - return $field; - } - } - } - - - return null; - } - - /** - * @param array $submissions - * - * @return array - */ - public function generateExpected(array $submissions): array - { - $returns = []; - // loop each submission and find its expected return and create - // a return array with the expected values. - /** @var array $submission */ - foreach ($submissions as $index => $submission) { - $returns[$index] = []; - // loop each field and use the "name" to find it. - /** - * @var string $fieldName - * @var string $fieldValue - */ - foreach ($submission as $fieldTitle => $fieldValue) { - //echo "Now searching for field $fieldTitle on index $index.\n"; - $fieldObject = $this->findField($fieldTitle); - if (null !== $fieldObject) { - if (null === $fieldObject->expectedReturn) { - $returns[$index][$fieldTitle] = $submissions[$index][$fieldTitle]; - } - if (null !== $fieldObject->expectedReturn) { - die('cannot handle closure'); - } - } - if (null === $fieldObject) { - die('null field object :('); - } - } - } - - return $returns; - } - - /** - * @param FieldSet $set - * - * @return array - */ - private function toArray(FieldSet $set): array - { - $ignore = []; - $result = []; - /** @var Field $field */ - foreach ($set->fields as $field) { - $result = $this->parseField($result, $field); - $ignore = array_unique($ignore + $field->ignorableFields); - } - $this->ignores[] = $ignore; - $this->parameters[] = $set->parameters ?? []; - - return $result; + return $final; } /** * @return array */ - public function generateSubmissions(): array + private function generateSubmissions(): array { + $this->debugMsg('Now in generateSubmissions()'); // first generate standard submissions: $this->submission = []; - // loop each standard submission: + $this->debugMsg(sprintf('There are %d mandatory field sets', count($this->mandatoryFieldSets))); /** @var FieldSet $set */ foreach ($this->mandatoryFieldSets as $set) { $this->submission[] = $this->toArray($set); @@ -234,31 +120,34 @@ class TestConfiguration // of the submission in the array $index = count($this->submission) - 1; $this->updateIgnorables($index, $customFields); + $this->updateExpected($index, $customFields); } } } } + $totalCount = 0; // 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); - // echo "there are " . $setCount . " optional sets\n"; + $this->debugMsg(sprintf('there are %d optional field sets', $setCount)); if (0 !== $setCount) { $keys = array_keys($this->optionalFieldSets); - // echo " keys to consider are: " . join(', ', $keys) . "\n"; + $this->debugMsg(sprintf(' keys to consider are: %s', join(', ', $keys))); $maxCount = count($keys) > self::MAX_ITERATIONS ? self::MAX_ITERATIONS : count($keys); - // echo " max count is " . $maxCount . "\n"; + $this->debugMsg(sprintf(' max count is %d', $maxCount)); for ($i = 1; $i <= $maxCount; $i++) { $combinationSets = $this->combinationsOf($i, $keys); - // echo " will create " . count($combinationSets) . " extra sets.\n"; + $this->debugMsg(sprintf(' will create %d extra sets.', count($combinationSets))); foreach ($combinationSets as $ii => $combinationSet) { - // echo " Set " . ($ii + 1) . "/" . count($combinationSets) . " will consist of:\n"; + $totalCount++; + $this->debugMsg(sprintf(' Set #%d will consist of:', $totalCount)); // the custom set is born! - //$custom = $this->toArray($set); - $custom = []; + $custom = []; + $expected = []; foreach ($combinationSet as $combination) { - // echo " $combination\n"; + $this->debugMsg(sprintf(' %s', $combination)); // here we start adding stuff to a copy of the standard submission. /** @var FieldSet $customSet */ $customSet = $this->optionalFieldSets[$combination] ?? false; @@ -266,74 +155,63 @@ class TestConfiguration // loop each field in this custom set and add them, nothing more. /** @var Field $field */ foreach ($customSet->fields as $field) { - // echo " added field " . $field->fieldTitle . " from custom set " . $combination . "\n"; - $custom = $this->parseField($custom, $field); + $this->debugMsg(sprintf(' added field %s from custom set %s', $field->fieldTitle, $combination)); + $custom = $this->parseField($custom, $field); + $expected = $this->parseExpected($expected, $field, $custom); // for each field, add the ignores to the current index (+1!) of // ignores. + $count = count($this->submission); if (null !== $field->ignorableFields && count($field->ignorableFields) > 0) { - $count = count($this->submission); $currentIgnoreSet = $this->ignores[$count] ?? []; - $this->ignores[$count] = array_unique(array_values(array_merge($currentIgnoreSet, $field->ignorableFields))); + $this->ignores[$count] = array_values(array_unique(array_values(array_merge($currentIgnoreSet, $field->ignorableFields)))); } + $this->expected[$count] = $expected; } $count = count($this->submission); $this->parameters[$count] = $customSet->parameters ?? []; } + $count = count($this->submission); $this->submission[] = $custom; + $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] ?? []))); } } } } + $this->debugMsg('Done!'); return $this->submission; } /** - * @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_unique(array_values(array_merge($this->ignores[$index], $field->ignorableFields))); - } - } - } - } - - /** - * @param array $submissions + * @param FieldSet $set * * @return array */ - public function generateParameters(array $submissions): array + private function toArray(FieldSet $set): array { - $params = []; - $index = 0; - /** @var array $submission */ - foreach ($submissions as $submission) { - // last one defined param - $submission = array_reverse($submission); - foreach ($submission as $key => $value) { - if (array_key_exists($key, $this->optionalFieldSets)) { - /** @var FieldSet $fieldSet */ - $fieldSet = $this->optionalFieldSets[$key]; - if (null !== $fieldSet->parameters) { - $params[$index] = $fieldSet->parameters; - continue; - } - if (null !== $fieldSet->parameters) { - $params[$index] = []; - } - } - } - $index++; - } + $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); - return $params; + // 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))); + + } + $this->ignores[] = array_values($ignore); + $this->expected[] = $expected; + $this->parameters[] = $set->parameters ?? []; + + return $result; } /** @@ -403,6 +281,8 @@ class TestConfiguration return $faker->randomFloat(2, 1, 99); case 'random-interest-period': return $faker->randomElement(['daily', 'monthly', 'yearly']); + case 'random-bill-repeat-freq': + return $faker->randomElement(['half-year', 'weekly', 'monthly', 'yearly']); case 'random-past-date': return $faker->dateTimeBetween('-3 years', '-1 years')->format('Y-m-d'); case 'random-date-two-year': @@ -436,9 +316,69 @@ class TestConfiguration return null; case 'random-attachment-type': return $faker->randomElement(['Account', 'Budget', 'Bill', 'TransactionJournal', 'PiggyBank', 'Tag',]); + 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; + } + + 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)); + } + /** * @param $k * @param $xs @@ -464,6 +404,122 @@ class TestConfiguration return array_merge($res1, $res2); } + /** + * @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]); + } + + } + } + } + + /** + * @param array $submissions + * + * @return array + */ + public function generateIgnores(array $submissions): array + { + $ignores = []; + // loop each submission and find its expected return and create + // a return array with the expected values. + /** @var array $submission */ + foreach ($submissions as $index => $submission) { + $ignores[$index] = []; + // loop each field and use the "name" to find it. + /** + * @var string $fieldName + * @var string $fieldValue + */ + foreach ($submission as $fieldTitle => $fieldValue) { + //echo "Now searching for field $fieldTitle on index $index.\n"; + $fieldObject = $this->findField($fieldTitle); + if (null !== $fieldObject) { + if (0 !== count($fieldObject->ignorableFields)) { + /** @var string $ignorableField */ + foreach ($fieldObject->ignorableFields as $ignorableField) { + // explode and put in the right position: + $positions = explode('/', $ignorableField); + if (1 === count($positions)) { + $ignores[$index][$ignorableField] = true; + } + if (3 === count($positions)) { + $root = $positions[0]; + $index = (int)$positions[1]; + $final = $positions[2]; + $ignores[$index][$root][$index][$final] = true; + } + } + } + } + if (null === $fieldObject) { + die('null field object :('); + } + } + } + + return $ignores; + } + + /** + * @param array $submissions + * + * @return array + */ + public function generateParameters(array $submissions): array + { + $params = []; + $index = 0; + /** @var array $submission */ + foreach ($submissions as $submission) { + // last one defined param + $submission = array_reverse($submission); + foreach ($submission as $key => $value) { + if (array_key_exists($key, $this->optionalFieldSets)) { + /** @var FieldSet $fieldSet */ + $fieldSet = $this->optionalFieldSets[$key]; + if (null !== $fieldSet->parameters) { + $params[$index] = $fieldSet->parameters; + continue; + } + if (null !== $fieldSet->parameters) { + $params[$index] = []; + } + } + } + $index++; + } + + return $params; + } + /** * @param FieldSet $optionalFieldSet */ @@ -472,6 +528,90 @@ class TestConfiguration $this->optionalFieldSet = $optionalFieldSet; } + /** + * @param string $message + */ + private function debugMsg(string $message): void + { + if (true === self::SHOW_DEBUG) { + echo sprintf("%s\n", $message); + } + } + + /** + * @param array $submissions + * + * @return array + */ + private function generateExpected(array $submissions): array + { + $returns = []; + // loop each submission and find its expected return and create + // a return array with the expected values. + /** @var array $submission */ + foreach ($submissions as $index => $submission) { + $returns[$index] = []; + // loop each field and use the "name" to find it. + /** + * @var string $fieldName + * @var string $fieldValue + */ + foreach ($submission as $fieldTitle => $fieldValue) { + //echo "Now searching for field $fieldTitle on index $index.\n"; + $fieldObject = $this->findField($fieldTitle); + if (null !== $fieldObject) { + if (null === $fieldObject->expectedReturn) { + $returns[$index][$fieldTitle] = $submissions[$index][$fieldTitle]; + } + if (null !== $fieldObject->expectedReturn) { + $returns[$index][$fieldTitle] = 'hi there';//($fieldObject->expectedReturn)($submissions[$index][$fieldTitle]); + } + } + if (null === $fieldObject) { + die('null field object :('); + } + } + } + + return $returns; + } + + /** + * @param int $index + * @param string $title + * + * @return Field|null + */ + private function findField(string $title): ?Field + { + // since there is no index for optional field sets (they use ID) + // reverse the set and loop them all: + // reason we reverse them is because the last always overrules the first. + $reversed = array_reverse($this->optionalFieldSets); + foreach ($reversed as $fieldSet) { + foreach ($fieldSet->fields as $field) { + if ($title === $field->fieldTitle) { + //echo " found field $title in an optional field set.\n"; + + return $field; + } + } + } + $reversed = array_reverse($this->mandatoryFieldSets); + foreach ($reversed as $fieldSet) { + foreach ($fieldSet->fields as $field) { + if ($title === $field->fieldTitle) { + //echo " found field $title in a mandatory field set.\n"; + + return $field; + } + } + } + + + return null; + } + /** * @param array $existing * @param array $config diff --git a/tests/Traits/TestHelpers.php b/tests/Traits/TestHelpers.php index 239b3ff662..a4ddef38cd 100644 --- a/tests/Traits/TestHelpers.php +++ b/tests/Traits/TestHelpers.php @@ -96,6 +96,7 @@ trait TestHelpers { $submission = $content['submission']; $ignore = $content['ignore']; + $expected = $content['expected']; $response = $this->put($route, $submission, ['Accept' => 'application/json']); $responseBody = $response->content(); $responseJson = json_decode($responseBody, true); @@ -105,21 +106,22 @@ trait TestHelpers // get return and compare each field $responseAttributes = $responseJson['data']['attributes']; - $this->comparePUTArray($route, $submission, $responseAttributes, $ignore); + $this->comparePUTArray($route, $submission, $responseAttributes, $expected, $ignore); } /** * @param string $url * @param array $submission * @param array $response + * @param array $expected * @param array $ignore */ - private function comparePUTArray(string $url, array $submission, array $response, array $ignore): void + private function comparePUTArray(string $url, array $submission, array $response, array $expected, array $ignore): void { foreach ($response as $key => $value) { if (is_array($value) && array_key_exists($key, $submission) && is_array($submission[$key])) { - $this->comparePUTArray($url, $submission[$key], $value, $ignore[$key] ?? []); + $this->comparePUTArray($url, $submission[$key], $value, $expected[$key], $ignore[$key] ?? []); } if (isset($submission[$key])) { @@ -128,17 +130,18 @@ trait TestHelpers } if (!in_array($key, $ignore, true)) { $message = sprintf( - "Field '%s' with value %s is expected to be %s.\nSubmitted:\n%s\nIgnored: %s\nReturned\n%s\nURL: %s", + "Field '%s' with value %s is expected to be %s.\nSubmitted: %s\nIgnored: %s\nExpected: %s\nReturned: %s\nURL: %s", $key, var_export($value, true), - var_export($submission[$key], true), + var_export($expected[$key], true), json_encode($submission), json_encode($ignore), + json_encode($expected), json_encode($response), $url ); - $this->assertEquals($value, $submission[$key], $message); + $this->assertEquals($value, $expected[$key], $message); } } }