Expand test cases.

This commit is contained in:
James Cole 2021-03-13 19:47:19 +01:00
parent 1f50f65bb7
commit 25bc0b0b78
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
10 changed files with 315 additions and 96 deletions

View File

@ -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);

View File

@ -35,29 +35,9 @@
</include>
</coverage>
<testsuites>
<testsuite name="ApiAccount">
<directory suffix="Test.php">./tests/Api/Models/Account</directory>
<testsuite name="Api">
<directory suffix="Test.php">./tests/Api/Models</directory>
</testsuite>
<testsuite name="ApiAttachment">
<directory suffix="Test.php">./tests/Api/Models/Attachment</directory>
</testsuite>
<testsuite name="ApiAb">
<directory suffix="Test.php">./tests/Api/Models/AvailableBudget</directory>
</testsuite>
<testsuite name="ApiBill">
<directory suffix="Test.php">./tests/Api/Models/Bill</directory>
</testsuite>
<!--
<testsuite name="Api">
<directory suffix="Test.php">./tests/Api</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
-->
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>

View File

@ -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',

View File

@ -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' => [

View File

@ -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
{

View File

@ -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
{

View File

@ -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)),
],

View File

@ -0,0 +1,163 @@
<?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\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),
],
],
];
}
}

View File

@ -0,0 +1,118 @@
<?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\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;
}
}

View File

@ -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 <code>%s</code> to <code>%s</code> but shouldnt be.', $uKey, $currentProperties[$uKey], $uValue)
// );
// } else {
// $log->warning(
// sprintf('Field %s is updated from <code>(array)</code> to <code>(array)</code> 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: