From 25bc0b0b78a4e4b711afd50ee8111fcb9249ed71 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 13 Mar 2021 19:47:19 +0100 Subject: [PATCH] Expand test cases. --- .../AvailableBudget/StoreController.php | 12 +- phpunit.xml | 24 +-- .../Models/Account/StoreControllerTest.php | 7 +- .../Models/Attachment/StoreControllerTest.php | 10 +- .../AvailableBudget/StoreControllerTest.php | 4 +- tests/Api/Models/Bill/StoreControllerTest.php | 4 +- .../Api/Models/Budget/StoreControllerTest.php | 5 +- .../BudgetLimit/StoreControllerTest.php | 163 ++++++++++++++++++ .../BudgetLimit/UpdateControllerTest.php | 118 +++++++++++++ tests/Traits/TestHelpers.php | 64 +------ 10 files changed, 315 insertions(+), 96 deletions(-) create mode 100644 tests/Api/Models/BudgetLimit/StoreControllerTest.php create mode 100644 tests/Api/Models/BudgetLimit/UpdateControllerTest.php diff --git a/app/Api/V1/Controllers/Models/AvailableBudget/StoreController.php b/app/Api/V1/Controllers/Models/AvailableBudget/StoreController.php index e4f57915ee..72f533a722 100644 --- a/app/Api/V1/Controllers/Models/AvailableBudget/StoreController.php +++ b/app/Api/V1/Controllers/Models/AvailableBudget/StoreController.php @@ -73,14 +73,18 @@ class StoreController extends Controller // currency is not mandatory: if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) { - $factory = app(TransactionCurrencyFactory::class); - $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); + $factory = app(TransactionCurrencyFactory::class); + $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); $data['currency_id'] = $currency->id; unset($data['currency_code']); } + if (!array_key_exists('currency_id', $data)) { + $currency = app('amount')->getDefaultCurrencyByUser(auth()->user()); + $data['currency_id'] = $currency->id; + } - $availableBudget = $this->abRepository->store($data); - $manager = $this->getManager(); + $availableBudget = $this->abRepository->store($data); + $manager = $this->getManager(); /** @var AvailableBudgetTransformer $transformer */ $transformer = app(AvailableBudgetTransformer::class); diff --git a/phpunit.xml b/phpunit.xml index 06b5c1ada3..09f8267509 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -35,29 +35,9 @@ - - ./tests/Api/Models/Account + + ./tests/Api/Models - - ./tests/Api/Models/Attachment - - - ./tests/Api/Models/AvailableBudget - - - ./tests/Api/Models/Bill - - - diff --git a/tests/Api/Models/Account/StoreControllerTest.php b/tests/Api/Models/Account/StoreControllerTest.php index 12bf912a59..f38e21b42c 100644 --- a/tests/Api/Models/Account/StoreControllerTest.php +++ b/tests/Api/Models/Account/StoreControllerTest.php @@ -50,9 +50,9 @@ class StoreControllerTest extends TestCase /** * @param array $submission * - * @ data Provider storeDataProvider + * @dataProvider storeDataProvider * - * @dataProvider emptyDataProvider + * @ data Provider emptyDataProvider */ public function testStore(array $submission): void { @@ -193,6 +193,7 @@ class StoreControllerTest extends TestCase return [ 'asset' => [ + 'parameters' => [], 'fields' => [ 'name' => $faker->name . join(' ', $faker->words(2)), 'type' => 'asset', @@ -200,12 +201,14 @@ class StoreControllerTest extends TestCase ], ], 'expense' => [ + 'parameters' => [], 'fields' => [ 'name' => $faker->name, 'type' => 'expense', ], ], 'liability' => [ + 'parameters' => [], 'fields' => [ 'name' => $faker->name, 'type' => 'liabilities', diff --git a/tests/Api/Models/Attachment/StoreControllerTest.php b/tests/Api/Models/Attachment/StoreControllerTest.php index af50e72899..332a36a382 100644 --- a/tests/Api/Models/Attachment/StoreControllerTest.php +++ b/tests/Api/Models/Attachment/StoreControllerTest.php @@ -51,8 +51,8 @@ class StoreControllerTest extends TestCase /** * @param array $submission * - * @dataProvider storeDataProvider - * @ data Provider emptyDataProvider + * @ data Provider storeDataProvider + * @dataProvider emptyDataProvider */ public function testStore(array $submission): void { @@ -105,7 +105,8 @@ class StoreControllerTest extends TestCase return [ 'default_file' => [ - 'fields' => [ + 'parameters' => [], + 'fields' => [ 'filename' => join(' ', $faker->words(3)), 'attachable_type' => $type, 'attachable_id' => '1', @@ -119,7 +120,8 @@ class StoreControllerTest extends TestCase */ private function optionalSets(): array { - $faker = Factory::create(); + $faker = Factory::create(); + return [ 'title' => [ 'fields' => [ diff --git a/tests/Api/Models/AvailableBudget/StoreControllerTest.php b/tests/Api/Models/AvailableBudget/StoreControllerTest.php index 188bdc5082..66c4032bc1 100644 --- a/tests/Api/Models/AvailableBudget/StoreControllerTest.php +++ b/tests/Api/Models/AvailableBudget/StoreControllerTest.php @@ -51,8 +51,8 @@ class StoreControllerTest extends TestCase /** * @param array $submission * - * @ data Provider storeDataProvider - * @dataProvider emptyDataProvider + * @dataProvider storeDataProvider + * @ data Provider emptyDataProvider */ public function testStore(array $submission): void { diff --git a/tests/Api/Models/Bill/StoreControllerTest.php b/tests/Api/Models/Bill/StoreControllerTest.php index 195d1d8282..92da32a0d3 100644 --- a/tests/Api/Models/Bill/StoreControllerTest.php +++ b/tests/Api/Models/Bill/StoreControllerTest.php @@ -51,8 +51,8 @@ class StoreControllerTest extends TestCase /** * @param array $submission * - * @ data Provider storeDataProvider - * @dataProvider emptyDataProvider + * @dataProvider storeDataProvider + * @ data Provider emptyDataProvider */ public function testStore(array $submission): void { diff --git a/tests/Api/Models/Budget/StoreControllerTest.php b/tests/Api/Models/Budget/StoreControllerTest.php index 9fe231b572..5705a01008 100644 --- a/tests/Api/Models/Budget/StoreControllerTest.php +++ b/tests/Api/Models/Budget/StoreControllerTest.php @@ -98,11 +98,8 @@ class StoreControllerTest extends TestCase private function minimalSets(): array { $faker = Factory::create(); - $repeatFreqs = ['yearly', 'weekly', 'monthly']; - $repeatFreq = $repeatFreqs[rand(0, count($repeatFreqs) - 1)]; - return [ - 'default_bill' => [ + 'default_budget' => [ 'fields' => [ 'name' => join(',', $faker->words(5)), ], diff --git a/tests/Api/Models/BudgetLimit/StoreControllerTest.php b/tests/Api/Models/BudgetLimit/StoreControllerTest.php new file mode 100644 index 0000000000..23160ee61e --- /dev/null +++ b/tests/Api/Models/BudgetLimit/StoreControllerTest.php @@ -0,0 +1,163 @@ +. + */ + +namespace Tests\Api\Models\BudgetLimit; + + +use Faker\Factory; +use Laravel\Passport\Passport; +use Log; +use Tests\TestCase; +use Tests\Traits\CollectsValues; +use Tests\Traits\RandomValues; +use Tests\Traits\TestHelpers; + +/** + * Class StoreControllerTest + */ +class StoreControllerTest extends TestCase +{ + use RandomValues, TestHelpers, CollectsValues; + + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Passport::actingAs($this->user()); + Log::info(sprintf('Now in %s.', get_class($this))); + } + + + /** + * @param array $submission + * + * @dataProvider storeDataProvider + * @ data Provider emptyDataProvider + */ + public function testStore(array $submission): void + { + if ([] === $submission) { + $this->markTestSkipped('Empty data provider'); + } + // run account store with a minimal data set: + $route = 'api.v1.budgets.limits.store'; + $this->storeAndCompare($route, $submission); + } + + /** + * @return array + */ + public function emptyDataProvider(): array + { + return [[[]]]; + + } + + /** + * @return array + */ + public function storeDataProvider(): array + { + $minimalSets = $this->minimalSets(); + $optionalSets = $this->optionalSets(); + $regenConfig = [ + 'start' => function () { + $faker = Factory::create(); + + return $faker->dateTimeBetween('-2 year', '-1 year')->format('Y-m-d'); + }, + 'end' => function () { + $faker = Factory::create(); + + return $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d'); + }, + ]; + + return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig); + } + + /** + * @return array + */ + private function minimalSets(): array + { + $faker = Factory::create(); + + return [ + 'default_bl' => [ + 'parameters' => [1], + 'fields' => [ + 'start' => $faker->dateTimeBetween('-2 year', '-1 year')->format('Y-m-d'), + 'end' => $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d'), + 'amount' => number_format($faker->randomFloat(2, 10, 100), 2), + ], + ], + ]; + } + + + /** + * @return \array[][] + */ + private function optionalSets(): array + { + $faker = Factory::create(); + $currencies = [ + 1 => 'EUR', + 2 => 'HUF', + 3 => 'GBP', + 4 => 'UAH', + ]; + $rand = rand(1, 4); + + return [ + 'currency_id' => [ + 'fields' => [ + 'currency_id' => $rand, + ], + ], + 'currency_code' => [ + 'fields' => [ + 'currency_code' => $currencies[$rand], + ], + ], + 'start' => [ + 'fields' => [ + 'start' => $faker->dateTimeBetween('-2 year', '-1 year')->format('Y-m-d'), + ], + ], + 'end' => [ + 'fields' => [ + 'end' => $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d'), + ], + ], + 'amount' => [ + 'fields' => [ + 'amount' => number_format($faker->randomFloat(2, 10, 100), 2), + ], + ], + + ]; + } + +} \ No newline at end of file diff --git a/tests/Api/Models/BudgetLimit/UpdateControllerTest.php b/tests/Api/Models/BudgetLimit/UpdateControllerTest.php new file mode 100644 index 0000000000..1872483008 --- /dev/null +++ b/tests/Api/Models/BudgetLimit/UpdateControllerTest.php @@ -0,0 +1,118 @@ +. + */ + +namespace Tests\Api\Models\BudgetLimit; + + +use Faker\Factory; +use Laravel\Passport\Passport; +use Log; +use Tests\TestCase; +use Tests\Traits\CollectsValues; +use Tests\Traits\RandomValues; +use Tests\Traits\TestHelpers; + +/** + * Class UpdateControllerTest + */ +class UpdateControllerTest extends TestCase +{ + use RandomValues, TestHelpers, CollectsValues; + + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Passport::actingAs($this->user()); + Log::info(sprintf('Now in %s.', get_class($this))); + } + + + /** + * @dataProvider updateDataProvider + */ + public function testUpdate(array $submission): void + { + $ignore = [ + 'created_at', + 'updated_at', + ]; + $route = route('api.v1.budgets.limits.update', [$submission['id'], $submission['bl_id']]); + + $this->updateAndCompare($route, $submission, $ignore); + } + + + /** + * @return array + */ + public function updateDataProvider(): array + { + $submissions = []; + $all = $this->updateDataSet(); + foreach ($all as $name => $data) { + $submissions[] = [$data]; + } + + return $submissions; + } + + + /** + * @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, + 'bl_id' => 1, + 'fields' => [ + 'amount' => ['test_value' => number_format($faker->randomFloat(2,10,100), 2)], + ], + 'extra_ignore' => [], + ], + + ]; + + return $set; + } + + +} \ No newline at end of file diff --git a/tests/Traits/TestHelpers.php b/tests/Traits/TestHelpers.php index b05e4c1297..7592216d7d 100644 --- a/tests/Traits/TestHelpers.php +++ b/tests/Traits/TestHelpers.php @@ -48,12 +48,11 @@ trait TestHelpers $body[$field] = $value; } // minimal set is part of all submissions: - $submissions[] = [$body]; + $submissions[] = [['fields' => $body, 'parameters' => $set['parameters'] ?? []]]; // then loop and add fields: $optionalSets = $startOptionalSets; $keys = array_keys($optionalSets); - $submissions = []; for ($i = 1; $i <= count($keys); $i++) { $combinations = $this->combinationsOf($i, $keys); // expand body with N extra fields: @@ -67,12 +66,11 @@ trait TestHelpers } $second = $this->regenerateValues($second, $regenConfig); - $submissions[] = [$second]; + $submissions[] = [['fields' => $second, 'parameters' => $set['parameters'] ?? []]]; } } unset($second); } - return $submissions; } @@ -195,71 +193,25 @@ trait TestHelpers continue; } - - // if (!compareResult($uValue, $currentProperties[$uKey]) && !in_array($uKey, $fieldsToUpdate)) { - // if (!is_array($currentProperties[$uKey]) && !is_array($uValue)) { - // $log->warning( - // sprintf('Field %s is updated from %s to %s but shouldnt be.', $uKey, $currentProperties[$uKey], $uValue) - // ); - // } else { - // $log->warning( - // sprintf('Field %s is updated from (array) to (array) but shouldnt be.', $uKey) - // ); - // } - // $log->debug(json_encode($currentProperties)); - // $log->debug(json_encode($updatedAttributes)); - // } - // - // if (in_array($uKey, $fieldsToUpdate) && compareResult($uValue, $testBody[$uKey])) { - // $log->debug(sprintf('Field %s is updated and this is OK.', $uKey)); - // } - // if (in_array($uKey, $fieldsToUpdate) && !compareResult($uValue, $testBody[$uKey])) { - // if (!is_array($uValue) && !is_array($testBody[$uKey])) { - // $log->warning(sprintf('Field "%s" is different: %s but must be %s!', $uKey, var_export($uValue, true), var_export($testBody[$uKey], true))); - // $log->debug(json_encode($currentProperties)); - // $log->debug(json_encode($updatedAttributes)); - // } else { - // $log->warning(sprintf('Field "%s" is different!', $uKey)); - // $log->debug(json_encode(filterArray($currentProperties))); - // $log->debug(json_encode(filterArray($updatedAttributes))); - // } - // - // } } - - - // // OLD - // - // - // $updatedResponseBody = json_decode($updateResponse->getBody(), true, 512, JSON_THROW_ON_ERROR); - // $updatedAttributes = $updatedResponseBody['data']['attributes']; - // if (array_key_exists('key', $endpoint) && array_key_exists('level', $endpoint)) { - // $key = $endpoint['key']; - // $level = $endpoint['level']; - // $updatedAttributes = $updatedResponseBody['data']['attributes'][$key][$level]; - // } - // - // // END OLD - // - // var_dump($submissionJson); - // exit; } /** * @param string $route - * @param array $submission + * @param array $content */ - protected function storeAndCompare(string $route, array $submission, ?array $ignore = null): void + protected function storeAndCompare(string $route, array $content, ?array $ignore = null): void { - $ignore = $ignore ?? []; + $ignore = $ignore ?? []; + $submission = $content['fields']; + $parameters = $content['parameters']; // submit! - $response = $this->post(route($route), $submission, ['Accept' => 'application/json']); + $response = $this->post(route($route, $parameters), $submission, ['Accept' => 'application/json']); $responseBody = $response->content(); $responseJson = json_decode($responseBody, true); $status = $response->getStatusCode(); $this->assertEquals($status, 200, sprintf("Submission: %s\nResponse: %s", json_encode($submission), $responseBody)); - $response->assertHeader('Content-Type', 'application/vnd.api+json'); // compare results: