Fix tests for transaction storage.

This commit is contained in:
James Cole
2021-03-14 16:08:49 +01:00
parent 288052905e
commit beece4dcbb
14 changed files with 1067 additions and 167 deletions

View File

@@ -0,0 +1,205 @@
<?php
/*
* StoreControllerTest.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Tests\Api\Models\Recurrence;
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
*
* emptyDataProvider / storeDataProvider
*
* @dataProvider storeDataProvider
*/
public function testStore(array $submission): void
{
if ([] === $submission) {
$this->markTestSkipped('Empty data provider');
}
$route = 'api.v1.recurrences.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 = [
'title' => function () {
$faker = Factory::create();
return $faker->uuid;
},
];
return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig);
}
/**
* @return array
*/
private function minimalSets(): array
{
$faker = Factory::create();
// three sets:
$combis = [
['withdrawal', 1, 8],
['deposit', 9, 1],
['transfer', 1, 2],
];
$types = [
['daily', ''],
['weekly', (string)$faker->numberBetween(1, 7)],
['ndom', (string)$faker->numberBetween(1, 4) . ',' . $faker->numberBetween(1, 7)],
['monthly', (string)$faker->numberBetween(1, 31)],
['yearly', $faker->date()],
];
$set = [];
foreach ($combis as $combi) {
foreach ($types as $type) {
$set[] = [
'parameters' => [],
'fields' => [
'type' => $combi[0],
'title' => $faker->uuid,
'first_date' => $faker->date(),
'repeat_until' => $faker->date(),
'repetitions' => [
[
'type' => $type[0],
'moment' => $type[1],
],
],
'transactions' => [
[
'description' => $faker->uuid,
'amount' => number_format($faker->randomFloat(2, 10, 100), 2),
'source_id' => $combi[1],
'destination_id' => $combi[2],
],
],
],
];
}
}
return $set;
}
/**
* @return \array[][]
*/
private function optionalSets(): array
{
$faker = Factory::create();
return [
'description' => [
'fields' => [
'description' => $faker->uuid,
],
],
'nr_of_repetitions' => [
'fields' => [
'nr_of_repetitions' => $faker->numberBetween(1, 2),
],
'remove_fields' => ['repeat_until'],
],
'apply_rules' => [
'fields' => [
'apply_rules' => $faker->boolean,
],
],
'active' => [
'fields' => [
'active' => $faker->boolean,
],
],
'notes' => [
'fields' => [
'notes' => $faker->uuid,
],
],
'repetitions_skip' => [
'fields' => [
'repetitions' => [
// first entry, set field:
[
'skip' => $faker->numberBetween(1,3),
],
],
],
],
'repetitions_weekend' => [
'fields' => [
'repetitions' => [
// first entry, set field:
[
'weekend' => $faker->numberBetween(1,4),
],
],
],
]
];
}
}

View File

@@ -0,0 +1,237 @@
<?php
/*
* StoreControllerTest.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Tests\Api\Models\Rule;
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
*
* emptyDataProvider / storeDataProvider
*
* @dataProvider storeDataProvider
*/
public function testStore(array $submission): void
{
if ([] === $submission) {
$this->markTestSkipped('Empty data provider');
}
$route = 'api.v1.rules.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 = [
'title' => function () {
$faker = Factory::create();
return $faker->uuid;
},
];
return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig);
}
/**
* @return array
*/
private function minimalSets(): array
{
$faker = Factory::create();
// - title
// - rule_group_id
// - trigger
// - triggers
// - actions
$set = [
'default_by_id' => [
'parameters' => [],
'fields' => [
'title' => $faker->uuid,
'rule_group_id' => (string)$faker->randomElement([1, 2]),
'trigger' => $faker->randomElement(['store-journal', 'update-journal']),
'triggers' => [
[
'type' => $faker->randomElement(['from_account_starts', 'from_account_is', 'description_ends', 'description_is']),
'value' => $faker->uuid,
],
],
'actions' => [
[
'type' => $faker->randomElement(['set_category', 'add_tag', 'set_description']),
'value' => $faker->uuid,
],
],
],
],
'default_by_title' => [
'parameters' => [],
'fields' => [
'title' => $faker->uuid,
'rule_group_title' => sprintf('Rule group %d', $faker->randomElement([1, 2])),
'trigger' => $faker->randomElement(['store-journal', 'update-journal']),
'triggers' => [
[
'type' => $faker->randomElement(['from_account_starts', 'from_account_is', 'description_ends', 'description_is']),
'value' => $faker->uuid,
],
],
'actions' => [
[
'type' => $faker->randomElement(['set_category', 'add_tag', 'set_description']),
'value' => $faker->uuid,
],
],
],
],
];
// leave it like this for now.
return $set;
}
/**
* @return \array[][]
*/
private function optionalSets(): array
{
$faker = Factory::create();
return [
'order' => [
'fields' => [
'order' => $faker->numberBetween(1, 2),
],
],
'active' => [
'fields' => [
'active' => $faker->boolean,
],
],
'strict' => [
'fields' => [
'strict' => $faker->boolean,
],
],
'stop_processing' => [
'fields' => [
'stop_processing' => $faker->boolean,
],
],
'triggers_order' => [
'fields' => [
'triggers' => [
// first entry, set field:
[
'order' => 1,
],
],
],
],
'triggers_active' => [
'fields' => [
'triggers' => [
// first entry, set field:
[
'active' => false,
],
],
],
],
'triggers_not_active' => [
'fields' => [
'triggers' => [
// first entry, set field:
[
'active' => true,
],
],
],
],
'triggers_processing' => [
'fields' => [
'triggers' => [
// first entry, set field:
[
'stop_processing' => true,
],
],
],
],
'triggers_not_processing' => [
'fields' => [
'triggers' => [
// first entry, set field:
[
'stop_processing' => false,
],
],
],
],
];
}
}

View File

@@ -0,0 +1,167 @@
<?php
/*
* StoreControllerTest.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Tests\Api\Models\Transaction;
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
*
* emptyDataProvider / storeDataProvider
*
* @dataProvider storeDataProvider
*/
public function testStore(array $submission): void
{
if ([] === $submission) {
$this->markTestSkipped('Empty data provider');
}
$route = 'api.v1.transactions.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 = [
'title' => function () {
$faker = Factory::create();
return $faker->uuid;
},
'transactions' => [
[
'description' => function () {
$faker = Factory::create();
return $faker->uuid;
},
],
],
];
return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig);
}
/**
* @return array
*/
private function minimalSets(): array
{
$faker = Factory::create();
// 3 sets:
$combis = [
['withdrawal', 1, 8],
['deposit', 9, 1],
['transfer', 1, 2],
];
$set = [];
foreach ($combis as $combi) {
$set[] = [
'parameters' => [],
'fields' => [
// not even required but OK.
'error_if_duplicate_hash' => $faker->boolean,
'transactions' => [
[
'type' => $combi[0],
'date' => $faker->dateTime(null, 'Europe/Amsterdam')->format(\DateTimeInterface::RFC3339),
'amount' => number_format($faker->randomFloat(2, 10, 100), 12),
'description' => $faker->uuid,
'source_id' => $combi[1],
'destination_id' => $combi[2],
],
],
],
];
}
return $set;
}
/**
* @return \array[][]
*/
private function optionalSets(): array
{
$faker = Factory::create();
return [
'title' => [
'fields' => [
'title' => $faker->uuid,
],
],
'order' => [
'fields' => [
'order' => $faker->numberBetween(1, 2),
],
],
'active' => [
'fields' => [
'active' => $faker->boolean,
],
],
];
}
}

View File

@@ -43,13 +43,13 @@ trait TestHelpers
{
$submissions = [];
/**
* @var string $name
* @var string $i
* @var array $set
*/
foreach ($minimalSets as $name => $set) {
foreach ($minimalSets as $i => $set) {
$body = [];
foreach ($set['fields'] as $field => $value) {
$body[$field] = $value;
foreach ($set['fields'] as $ii => $value) {
$body[$ii] = $value;
}
// minimal set is part of all submissions:
$submissions[] = [[
@@ -62,16 +62,32 @@ trait TestHelpers
$optionalSets = $startOptionalSets;
$keys = array_keys($optionalSets);
$count = count($keys) > self::MAX_ITERATIONS ? self::MAX_ITERATIONS : count($keys);
for ($i = 1; $i <= $count; $i++) {
$combinations = $this->combinationsOf($i, $keys);
for ($iii = 1; $iii <= $count; $iii++) {
$combinations = $this->combinationsOf($iii, $keys);
// expand body with N extra fields:
foreach ($combinations as $extraFields) {
foreach ($combinations as $iv => $extraFields) {
$second = $body;
$ignore = $set['ignore'] ?? []; // unused atm.
foreach ($extraFields as $extraField) {
foreach ($extraFields as $v => $extraField) {
// now loop optional sets on $extraField and add whatever the config is:
foreach ($optionalSets[$extraField]['fields'] as $newField => $newValue) {
$second[$newField] = $newValue;
foreach ($optionalSets[$extraField]['fields'] as $vi => $newValue) {
// if the newValue is an array, we must merge it with whatever may
// or may not already be there. Its the optional field for one of the
// (maybe existing?) fields:
if (is_array($newValue) && array_key_exists($vi, $second) && is_array($second[$vi])) {
// loop $second[$vi] and merge it with whatever is in $newValue[$someIndex]
foreach ($second[$vi] as $vii => $iiValue) {
$second[$vi][$vii] = $iiValue + $newValue[$vii];
}
}
if (!is_array($newValue)) {
$second[$vi] = $newValue;
}
}
if (array_key_exists('remove_fields', $optionalSets[$extraField])) {
foreach ($optionalSets[$extraField]['remove_fields'] as $removed) {
unset($second[$removed]);
}
}
}
@@ -113,9 +129,20 @@ trait TestHelpers
*/
protected function regenerateValues($set, $opts): array
{
foreach ($opts as $key => $func) {
if (array_key_exists($key, $set)) {
$set[$key] = $func();
foreach ($opts as $i => $func) {
if (array_key_exists($i, $set)) {
if(!is_array($set[$i])) {
$set[$i] = $func();
}
if(is_array($set[$i])) {
foreach($set[$i] as $ii => $lines) {
foreach($lines as $iii => $value) {
if(isset($opts[$i][$ii][$iii])) {
$set[$i][$ii][$iii] = $opts[$i][$ii][$iii]();
}
}
}
}
}
}
@@ -132,7 +159,7 @@ trait TestHelpers
{
// get original values:
$response = $this->get($route, ['Accept' => 'application/json']);
$status = $response->getStatusCode();
$status = $response->getStatusCode();
$this->assertEquals($status, 200, sprintf(sprintf('%s failed with 404.', $route)));
$response->assertStatus(200);
$originalString = $response->content();
@@ -238,13 +265,49 @@ trait TestHelpers
if ($this->ignoreCombination($route, $submission['type'] ?? 'blank', $returnName)) {
continue;
}
// check if is array, if so we need something smart:
if (is_array($returnValue) && is_array($submission[$returnName])) {
$this->compareArray($returnName, $submission[$returnName], $returnValue);
}
if (!is_array($returnValue) && !is_array($submission[$returnName])) {
$message = sprintf(
"Main: Return value '%s' of key '%s' does not match submitted value '%s'.\n%s\n%s", $returnValue, $returnName, $submission[$returnName],
json_encode($submission), $responseBody
);
$this->assertEquals($returnValue, $submission[$returnName], $message);
}
$message = sprintf(
"Return value '%s' of key '%s' does not match submitted value '%s'.\n%s\n%s", $returnValue, $returnName, $submission[$returnName],
json_encode($submission), $responseBody
);
$this->assertEquals($returnValue, $submission[$returnName], $message);
}
}
}
/**
* @param string $key
* @param array $original
* @param array $returned
*/
protected function compareArray(string $key, array $original, array $returned)
{
$ignore = ['id', 'created_at', 'updated_at'];
foreach ($returned as $objectKey => $object) {
// each object is a transaction, a rule trigger, a rule action, whatever.
// assume the original also contains this key:
if (!array_key_exists($objectKey, $original)) {
$message = sprintf('Sub: Original array "%s" does not have returned key %d.', $key, $objectKey);
$this->assertTrue(false, $message);
}
foreach ($object as $returnKey => $returnValue) {
if (in_array($returnKey, $ignore, true)) {
continue;
}
if (array_key_exists($returnKey, $original[$objectKey])) {
$message = sprintf(
'Sub: sub-array "%s" returned value %s does not match sent X value %s.',
$key, var_export($returnValue, true), var_export($original[$objectKey][$returnKey], true)
);
$this->assertEquals($original[$objectKey][$returnKey], $returnValue, $message);
}
}
}
}