From 186cbed77761af1c797dc2226dbde0229b5e61bd Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 20 Mar 2021 08:00:58 +0100 Subject: [PATCH] Refactor available budget test --- .../Models/Attachment/StoreControllerTest.php | 1 + .../Attachment/UpdateControllerTest.php | 44 ------- .../AvailableBudget/StoreControllerTest.php | 107 +++++++++--------- .../AvailableBudget/UpdateControllerTest.php | 85 ++++++++++++-- tests/Objects/TestConfiguration.php | 43 ++++--- 5 files changed, 161 insertions(+), 119 deletions(-) diff --git a/tests/Api/Models/Attachment/StoreControllerTest.php b/tests/Api/Models/Attachment/StoreControllerTest.php index 2e2f3d51e9..000d41cd98 100644 --- a/tests/Api/Models/Attachment/StoreControllerTest.php +++ b/tests/Api/Models/Attachment/StoreControllerTest.php @@ -94,6 +94,7 @@ class StoreControllerTest extends TestCase $defaultAssetSet->addField(Field::createBasic('attachable_id', 'static-one')); $configuration->addMandatoryFieldSet($defaultAssetSet); + // optional field sets $fieldSet = new FieldSet; $fieldSet->addField(Field::createBasic('title', 'uuid')); $configuration->addOptionalFieldSet('title', $fieldSet); diff --git a/tests/Api/Models/Attachment/UpdateControllerTest.php b/tests/Api/Models/Attachment/UpdateControllerTest.php index bb52dbaae3..3b8a5298ef 100644 --- a/tests/Api/Models/Attachment/UpdateControllerTest.php +++ b/tests/Api/Models/Attachment/UpdateControllerTest.php @@ -117,48 +117,4 @@ class UpdateControllerTest extends TestCase return $final; } - - - /** - * @return array - */ - public function updateDataSet(): array - { - $faker = Factory::create(); - $set = [ - 'filename' => [ - 'id' => 1, - 'fields' => [ - 'filename' => ['test_value' => $faker->text(64)], - ], - 'extra_ignore' => [], - ], - 'title' => [ - 'id' => 1, - 'fields' => [ - 'title' => ['test_value' => $faker->uuid], - ], - 'extra_ignore' => [], - ], - 'notes' => [ - 'id' => 1, - 'fields' => [ - 'notes' => ['test_value' => join(' ', $faker->words(5))], - ], - 'extra_ignore' => [], - ], - 'model' => [ - 'id' => 1, - 'fields' => [ - 'attachable_type' => ['test_value' => 'TransactionJournal'], - 'attachable_id' => ['test_value' => (string)2], - ], - 'extra_ignore' => [], - ], - ]; - - return $set; - } - - } \ No newline at end of file diff --git a/tests/Api/Models/AvailableBudget/StoreControllerTest.php b/tests/Api/Models/AvailableBudget/StoreControllerTest.php index 8cae7c9f70..a0193eac62 100644 --- a/tests/Api/Models/AvailableBudget/StoreControllerTest.php +++ b/tests/Api/Models/AvailableBudget/StoreControllerTest.php @@ -25,6 +25,9 @@ namespace Tests\Api\Models\AvailableBudget; 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,57 +64,54 @@ class StoreControllerTest extends TestCase */ public function storeDataProvider(): array { - $minimalSets = $this->minimalSets(); - $optionalSets = $this->optionalSets(); - $regenConfig = []; + // some test configs: + $configuration = new TestConfiguration; - return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig); + // default asset account test set: + $defaultAssetSet = new FieldSet(); + $defaultAssetSet->title = 'default_file'; + $defaultAssetSet->addField(Field::createBasic('amount', 'random-amount')); + $defaultAssetSet->addField(Field::createBasic('start', 'random-date-two-year')); + $defaultAssetSet->addField(Field::createBasic('end', 'random-date-one-year')); + $configuration->addMandatoryFieldSet($defaultAssetSet); + + // 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); + + // 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 array - */ - private function minimalSets(): array - { - $faker = Factory::create(); - - return [ - 'default_ab' => [ - 'fields' => [ - 'amount' => number_format($faker->randomFloat(2, 10, 100), 2), - 'start' => $faker->dateTimeBetween('-2 year', '-1 year')->format('Y-m-d'), - 'end' => $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d'), - ], - ], - ]; - } - - /** - * @return \array[][] - */ - private function optionalSets(): array - { - $currencies = [ - 1 => 'EUR', - 2 => 'HUF', - 3 => 'GBP', - 4 => 'UAH', - ]; - $rand = rand(1, 4); - - return [ - 'currency_by_id' => [ - 'fields' => [ - 'currency_id' => $rand, - ], - ], - 'currency_by_code' => [ - 'fields' => [ - 'currency_code' => $currencies[$rand], - ], - ], - ]; - } /** * @param array $submission @@ -123,11 +123,16 @@ 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.available_budgets.store'; - $this->storeAndCompare($route, $submission); + $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 0c8e10ef15..0e544fa90a 100644 --- a/tests/Api/Models/AvailableBudget/UpdateControllerTest.php +++ b/tests/Api/Models/AvailableBudget/UpdateControllerTest.php @@ -25,6 +25,9 @@ namespace Tests\Api\Models\AvailableBudget; 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; @@ -49,17 +52,22 @@ class UpdateControllerTest extends TestCase /** + * @param array $submission * @dataProvider updateDataProvider */ public function testUpdate(array $submission): void { - $ignore = [ - 'created_at', - 'updated_at', - ]; - $route = route('api.v1.available_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']); - $this->updateAndCompare($route, $submission, $ignore); + $route = route('api.v1.available_budgets.update', $submission['parameters']); + $this->assertPUT($route, $submission); } @@ -68,13 +76,68 @@ class UpdateControllerTest extends TestCase */ public function updateDataProvider(): array { - $submissions = []; - $all = $this->updateDataSet(); - foreach ($all as $name => $data) { - $submissions[] = [$data]; + $configuration = new TestConfiguration; + + // optional field sets (for all test configs) + $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_id', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('amount', 'random-amount')); + $configuration->addOptionalFieldSet('amount', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('start', 'random-date-two-year')); + $configuration->addOptionalFieldSet('start', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $fieldSet->addField(Field::createBasic('end', 'random-date-one-year')); + $configuration->addOptionalFieldSet('end', $fieldSet); + + $fieldSet = new FieldSet; + $fieldSet->parameters = [1]; + $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 $submissions; + return $final; } diff --git a/tests/Objects/TestConfiguration.php b/tests/Objects/TestConfiguration.php index 56c6f86e40..c72926328f 100644 --- a/tests/Objects/TestConfiguration.php +++ b/tests/Objects/TestConfiguration.php @@ -198,7 +198,6 @@ class TestConfiguration foreach ($this->mandatoryFieldSets as $set) { $this->submission[] = $this->toArray($set); - // expand the standard submission with extra sets from the optional field set. $setCount = count($this->optionalFieldSets); //echo "Just created a standard set\n"; @@ -212,8 +211,8 @@ class TestConfiguration foreach ($combinationSets as $ii => $combinationSet) { //echo " Set " . ($ii + 1) . "/" . count($combinationSets) . " will consist of:\n"; // the custom set is born! - - $custom = $this->toArray($set); + $customFields = []; + $custom = $this->toArray($set); // echo " refreshed!\n"; // echo " " . json_encode($custom) . "\n"; foreach ($combinationSet as $combination) { @@ -226,17 +225,15 @@ class TestConfiguration /** @var Field $field */ foreach ($customSet->fields as $field) { //echo " added field ".$field->fieldTitle." from custom set ".$combination."\n"; - $custom = $this->parseField($custom, $field); - // for each field, add the ignores to the current index (+1!) of - // ignores. - 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))); - } + $custom = $this->parseField($custom, $field); + $customFields[] = $field; } } $this->submission[] = $custom; + // at this point we can update the ignorable fields because we know the position + // of the submission in the array + $index = count($this->submission) - 1; + $this->updateIgnorables($index, $customFields); } } } @@ -291,6 +288,22 @@ class TestConfiguration 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 * @@ -383,7 +396,7 @@ class TestConfiguration case 'random-liability-type': return $faker->randomElement(['loan', 'debt', 'mortgage']); case 'random-journal-id': - return $faker->numberBetween(1,25); + return $faker->numberBetween(1, 25); case 'random-amount': return number_format($faker->randomFloat(2, 10, 100), 2); case 'random-percentage': @@ -392,6 +405,10 @@ class TestConfiguration return $faker->randomElement(['daily', 'monthly', 'yearly']); case 'random-past-date': return $faker->dateTimeBetween('-3 years', '-1 years')->format('Y-m-d'); + case 'random-date-two-year': + return $faker->dateTimeBetween('-2 years', '-1 years')->format('Y-m-d'); + case 'random-date-one-year': + return $faker->dateTimeBetween('-1 years', 'now')->format('Y-m-d'); case 'random-asset-accountRole': return $faker->randomElement(['defaultAsset', 'savingAsset']); case 'random-transactionType': @@ -418,7 +435,7 @@ class TestConfiguration case 'null': return null; case 'random-attachment-type': - return $faker->randomElement(['Account', 'Budget', 'Bill', 'TransactionJournal', 'PiggyBank','Tag',]); + return $faker->randomElement(['Account', 'Budget', 'Bill', 'TransactionJournal', 'PiggyBank', 'Tag',]); } }