Expand API test to bill store.

This commit is contained in:
James Cole
2021-03-13 16:47:29 +01:00
parent bdb298740a
commit c9f7f877c0
15 changed files with 679 additions and 178 deletions

View File

@@ -61,7 +61,7 @@ class StoreControllerTest extends TestCase
}
// run account store with a minimal data set:
$route = 'api.v1.accounts.store';
$this->submitAndCompare($route, $submission);
$this->storeAndCompare($route, $submission);
}
/**
@@ -106,79 +106,81 @@ class StoreControllerTest extends TestCase
*/
private function optionalSets(): array
{
$faker = Factory::create();
$currencies = [
$faker = Factory::create();
$currencies = [
1 => 'EUR',
2 => 'HUF',
3 => 'GBP',
4 => 'UAH',
];
$rand = rand(1, 4);
$rand = rand(1, 4);
// rand
return [
'active' => [
'active' => [
'fields' => [
'active' => $faker->boolean,
],
],
// 'iban' => [
// 'fields' => [
// 'iban' => $faker->iban(),
// ],
// ],
// 'bic' => [
// 'fields' => [
// 'bic' => $faker->swiftBicNumber,
// ],
// ],
// 'account_number' => [
// 'fields' => [
// 'account_number' => $faker->iban(),
// ],
// ],
// 'ob' => [
// 'fields' => [
// 'opening_balance' => $this->getRandomAmount(),
// 'opening_balance_date' => $this->getRandomDateString(),
// ],
// ],
// 'virtual_balance' => [
// 'fields' => [
// 'virtual_balance' => $this->getRandomAmount(),
// ],
// ],
// 'currency_id' => [
// 'fields' => [
// 'currency_id' => $rand,
// ],
// ],
// 'currency_code' => [
// 'fields' => [
// 'currency_code' => $currencies[$rand],
// ],
// ],
// 'order' => [
// 'fields' => [
// 'order' => $faker->numberBetween(1, 5),
// ],
// ],
// 'include_net_worth' => [
// 'fields' => [
// 'include_net_worth' => $faker->boolean,
// ],
// ],
// 'notes' => [
// 'fields' => [
// 'notes' => join(' ', $faker->words(5)),
// ],
// ],
// 'location' => [
// 'fields' => [
// 'latitude' => $faker->latitude,
// 'longitude' => $faker->longitude,
// 'zoom_level' => $faker->numberBetween(1, 10),
// ],
// ],
'iban' => [
'fields' => [
'iban' => $faker->iban(),
],
],
'bic' => [
'fields' => [
'bic' => $faker->swiftBicNumber,
],
],
'account_number' => [
'fields' => [
'account_number' => $faker->iban(),
],
],
'ob' => [
'fields' => [
'opening_balance' => $this->getRandomAmount(),
'opening_balance_date' => $this->getRandomDateString(),
],
],
'virtual_balance' => [
'fields' => [
'virtual_balance' => $this->getRandomAmount(),
],
],
'currency_id' => [
'fields' => [
'currency_id' => $rand,
],
],
'currency_code' => [
'fields' => [
'currency_code' => $currencies[$rand],
],
],
'order' => [
'fields' => [
'order' => $faker->numberBetween(1, 5),
],
],
'include_net_worth' => [
'fields' => [
'include_net_worth' => $faker->boolean,
],
],
'notes' => [
'fields' => [
'notes' => join(' ', $faker->words(5)),
],
],
'location' => [
'fields' => [
'latitude' => $faker->latitude,
'longitude' => $faker->longitude,
'zoom_level' => $faker->numberBetween(1, 10),
],
],
];
}

View File

@@ -92,7 +92,7 @@ class UpdateControllerTest extends TestCase
$accountRoles = ['defaultAsset', 'sharedAsset', 'savingAsset'];
$accountRole = $accountRoles[rand(0, count($accountRoles) - 1)];
$liabilityRoles = ['loan', 'debt', 'asset'];
$liabilityRoles = ['loan', 'debt', 'mortgage'];
$liabilityRole = $liabilityRoles[rand(0, count($liabilityRoles) - 1)];
$interestPeriods = ['daily', 'monthly', 'yearly'];
@@ -179,7 +179,7 @@ class UpdateControllerTest extends TestCase
'notes' => [
'id' => 1,
'fields' => [
'notes' => ['test_value' => $faker->randomAscii],
'notes' => ['test_value' => join(' ', $faker->words(3))],
],
'extra_ignore' => [],
],

View File

@@ -61,7 +61,7 @@ class StoreControllerTest extends TestCase
}
// run account store with a minimal data set:
$route = 'api.v1.attachments.store';
$this->submitAndCompare($route, $submission);
$this->storeAndCompare($route, $submission);
}
/**
@@ -81,23 +81,7 @@ class StoreControllerTest extends TestCase
{
$minimalSets = $this->minimalSets();
$optionalSets = $this->optionalSets();
$regenConfig = [
// 'name' => function () {
// $faker = Factory::create();
//
// return $faker->name;
// },
// 'iban' => function () {
// $faker = Factory::create();
//
// return $faker->iban();
// },
// 'account_number' => function () {
// $faker = Factory::create();
//
// return $faker->iban();
// },
];
$regenConfig = [];
return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig);
}
@@ -122,7 +106,7 @@ class StoreControllerTest extends TestCase
return [
'default_file' => [
'fields' => [
'filename' => $faker->randomAscii,
'filename' => join(' ', $faker->words(3)),
'attachable_type' => $type,
'attachable_id' => '1',
],
@@ -139,7 +123,7 @@ class StoreControllerTest extends TestCase
return [
'title' => [
'fields' => [
'title' => $faker->randomAscii,
'title' => join(' ', $faker->words(3)),
],
],
'notes' => [

View File

@@ -0,0 +1,135 @@
<?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\AvailableBudget;
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.available_budgets.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 = [];
return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig);
}
/**
* @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],
],
],
];
}
}

View File

@@ -0,0 +1,139 @@
<?php
/*
* UpdateControllerTest.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\AvailableBudget;
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.available_budgets.update', [$submission['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 = ['EUR', 'GBP', 'USD', 'HUF'];
$currencyCode = $currencies[rand(0, count($currencies) - 1)];
$set = [
'currency_id' => [
'id' => 1,
'fields' => [
'currency_id' => ['test_value' => (string)$faker->numberBetween(1, 10)],
],
'extra_ignore' => ['currency_code', 'currency_symbol'],
],
'currency_code' => [
'id' => 1,
'fields' => [
'currency_code' => ['test_value' => $currencyCode],
],
'extra_ignore' => ['currency_id', 'currency_symbol'],
],
'amount' => [
'id' => 1,
'fields' => [
'amount' => ['test_value' => number_format($faker->randomFloat(2, 10, 100), 2)],
],
'extra_ignore' => [],
],
'start' => [
'id' => 1,
'fields' => [
'start' => ['test_value' => $faker->dateTimeBetween('-2 year', '-1 year')->format('Y-m-d')],
],
'extra_ignore' => [],
],
'end' => [
'id' => 1,
'fields' => [
'end' => ['test_value' => $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d')],
],
'extra_ignore' => [],
],
'both' => [
'id' => 1,
'fields' => [
'start' => ['test_value' => $faker->dateTimeBetween('-2 year', '-1 year')->format('Y-m-d')],
'end' => ['test_value' => $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d')],
],
'extra_ignore' => [],
],
];
return $set;
}
}

View File

@@ -0,0 +1,200 @@
<?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\Bill;
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.bills.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 = [
'name' => function () {
$faker = Factory::create();
return join(' ', $faker->words(5));
},
];
return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig);
}
/**
* @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' => join(',', $faker->words(5)),
'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' => join(' ', $faker->words(5)),
],
],
'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,
],
],
];
}
}

View File

@@ -135,7 +135,9 @@ trait TestHelpers
$response = $this->put($route, $submissionArray, ['Accept' => 'application/json']);
$responseString = $response->content();
$response->assertStatus(200);
$status = $response->getStatusCode();
$this->assertEquals($status, 200, sprintf("Submission: %s\nResponse: %s", json_encode($submissionArray), $responseString));
//$response->assertStatus(200);
$responseArray = json_decode($responseString, true, 512, JSON_THROW_ON_ERROR);
$responseAttributes = $responseArray['data']['attributes'] ?? [];
@@ -171,7 +173,7 @@ trait TestHelpers
// original has this key too:
if (array_key_exists($rKey, $originalAttributes)) {
// but we can ignore it!
if(in_array($rKey, $submission['extra_ignore'])) {
if (in_array($rKey, $submission['extra_ignore'])) {
continue;
}
// but it is different?
@@ -187,7 +189,6 @@ trait TestHelpers
);
$this->assertTrue(false, $message);
}
}
@@ -248,18 +249,27 @@ trait TestHelpers
* @param string $route
* @param array $submission
*/
protected function submitAndCompare(string $route, array $submission): void
protected function storeAndCompare(string $route, array $submission, ?array $ignore = null): void
{
$ignore = $ignore ?? [];
// submit!
$response = $this->post(route($route), $submission, ['Accept' => 'application/json']);
$responseBody = $response->content();
$responseJson = json_decode($responseBody, true);
$message = sprintf('Status code is %d and body is %s', $response->getStatusCode(), $responseBody);
$this->assertEquals($response->getStatusCode(), 200, $message);
$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:
foreach ($responseJson['data']['attributes'] as $returnName => $returnValue) {
if (in_array($returnName, $ignore)) {
Log::debug(sprintf('Ignore value of "%s".', $returnName));
continue;
}
if (array_key_exists($returnName, $submission)) {
// TODO still based on account routine:
if ($this->ignoreCombination($route, $submission['type'] ?? 'blank', $returnName)) {