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,
],
],
];
}
}