Update test code.

This commit is contained in:
James Cole
2021-03-20 07:21:13 +01:00
parent 836f0ecf3f
commit bc5aa4b4cd
41 changed files with 255 additions and 945 deletions

View File

@@ -38,208 +38,4 @@ trait CollectsValues
{
return User::where('email', 'james@firefly')->first();
}
//
// /**
// * @return User
// */
// public function nonAdminUser(): User
// {
// return User::where('email', 'no_admin@firefly')->first();
// }
//
// /**
// * @return Budget
// */
// public function getRandomBudget(): Budget
// {
// return $this->user()->budgets()->inRandomOrder()->first();
// }
//
// /**
// * @return Category
// */
// public function getRandomCategory(): Category
// {
// return $this->user()->categories()->inRandomOrder()->first();
// }
//
// /**
// * @return Bill
// */
// public function getRandomBill(): Bill
// {
// return $this->user()->bills()->inRandomOrder()->first();
// }
//
// /**
// * @return PiggyBank
// */
// public function getRandomPiggyBank(): PiggyBank
// {
// return $this->user()->piggyBanks()->inRandomOrder()->first();
// }
//
//
// /**
// * @return Tag
// */
// public function getRandomTag(): Tag
// {
// return $this->user()->tags()->inRandomOrder()->first();
// }
//
// /**
// * @return TransactionJournal
// */
// public function getRandomWithdrawal(): TransactionJournal
// {
// return $this->getRandomJournal(TransactionType::WITHDRAWAL);
// }
//
// /**
// * @return TransactionJournal
// */
// public function getRandomTransfer(): TransactionJournal
// {
// return $this->getRandomJournal(TransactionType::TRANSFER);
// }
//
// /**
// * @return TransactionJournal
// */
// public function getRandomDeposit(): TransactionJournal
// {
// return $this->getRandomJournal(TransactionType::DEPOSIT);
// }
//
// /**
// * @param string $type
// *
// * @return TransactionJournal
// * @throws FireflyException
// */
// private function getRandomJournal(string $type): TransactionJournal
// {
// $query = DB::table('transactions')
// ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
// ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
// ->where('transaction_journals.user_id', $this->user()->id)
// ->whereNull('transaction_journals.deleted_at')
// ->whereNull('transactions.deleted_at')
// ->where('transaction_types.type', $type)
// ->groupBy('transactions.transaction_journal_id')
// ->having('ct', '=', 2)
// ->inRandomOrder()->take(1);
// $result = $query->get(
// [
// 'transactions.transaction_journal_id',
// 'transaction_journals.transaction_type_id',
// DB::raw('COUNT(transaction_journal_id) as ct'),
// ]
// )->first();
// if (null === $result) {
// throw new FireflyException(sprintf('Cannot find suitable journal "%s" to use.', $type));
// }
//
// return TransactionJournal::find((int)$result->transaction_journal_id);
//
// }
//
// /**
// * @return TransactionCurrency
// */
// public function getEuro(): TransactionCurrency
// {
// return TransactionCurrency::whereCode('EUR')->first();
// }
//
// /**
// * @return TransactionCurrency
// */
// public function getRandomCurrency(): TransactionCurrency
// {
// return TransactionCurrency::where('code', '!=', 'EUR')->inRandomOrder()->first();
// }
//
// /**
// * @return TransactionCurrency
// */
// public function getDollar(): TransactionCurrency
// {
// return TransactionCurrency::whereCode('USD')->first();
// }
//
// /**
// * @param int|null $except
// *
// * @return Account
// */
// public function getRandomAsset(?int $except = null): Account
// {
// return $this->getRandomAccount(AccountType::ASSET, $except);
// }
//
// /**
// * @param int|null $except
// *
// * @return Account
// */
// public function getRandomDebt(?int $except = null): Account
// {
// return $this->getRandomAccount(AccountType::DEBT, $except);
// }
//
// /**
// * @param int|null $except
// *
// * @return Account
// */
// public function getRandomLoan(?int $except = null): Account
// {
// return $this->getRandomAccount(AccountType::LOAN, $except);
// }
//
// /**
// * @param int|null $except
// *
// * @return Account
// */
// public function getRandomRevenue(?int $except = null): Account
// {
// return $this->getRandomAccount(AccountType::REVENUE, $except);
// }
//
// /**
// * @param int|null $except
// *
// * @return Account
// */
// public function getRandomExpense(?int $except = null): Account
// {
// return $this->getRandomAccount(AccountType::EXPENSE, $except);
// }
//
// /**
// * @param string $type
// *
// * @param int|null $except
// *
// * @return Account
// */
// private function getRandomAccount(string $type, ?int $except): Account
// {
// $query = Account::
// leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
// ->whereNull('accounts.deleted_at')
// ->where('accounts.user_id', $this->user()->id)
// ->where('account_types.type', $type)
// ->inRandomOrder()->take(1);
// if (null !== $except) {
// $query->where('accounts.id', '!=', $except);
// }
//
// return $query->first(['accounts.*']);
// }
}

View File

@@ -1,34 +0,0 @@
<?php
/*
* FakeValues.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\Traits;
trait FakeValues
{
// /**
// * @return string
// */
// protected function fakeName(): string {
// return '';
// }
}

View File

@@ -1,44 +0,0 @@
<?php
/*
* MocksDefaultValues.php
* Copyright (c) 2020 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/>.
*/
declare(strict_types=1);
namespace Tests\Traits;
/**
* Trait MocksDefaultValues
*/
trait MocksDefaultValues
{
// public function mockDefaultConfiguration(): void
// {
//
// $falseConfig = new Configuration;
// $falseConfig->data = false;
//
// $idConfig = new Configuration;
// $idConfig->data = 'abc';
//
// FireflyConfig::shouldReceive('get')->withArgs(['is_demo_site', false])->andReturn($falseConfig);
// FireflyConfig::shouldReceive('get')->withArgs(['installation_id', null])->andReturn($idConfig);
// }
}

View File

@@ -1,125 +0,0 @@
<?php
/*
* RandomValues.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\Traits;
use Carbon\Carbon;
/**
* Trait RandomValues
*/
trait RandomValues
{
/**
* @param $k
* @param $xs
*
* @return array|array[]
*/
protected function combinationsOf($k, $xs): array
{
if ($k === 0) {
return [[]];
}
if (count($xs) === 0) {
return [];
}
$x = $xs[0];
$xs1 = array_slice($xs, 1, count($xs) - 1);
$res1 = $this->combinationsOf($k - 1, $xs1);
for ($i = 0; $i < count($res1); $i++) {
array_splice($res1[$i], 0, 0, $x);
}
$res2 = $this->combinationsOf($k, $xs1);
return array_merge($res1, $res2);
}
/**
* @return string
*/
protected function getRandomAmount(): string
{
return number_format(rand(1000, 100000) / 100, '2', '.');
}
/**
* @return string
*/
protected function getRandomCurrencyCode(): string
{
return $this->randomFromArray(['EUR', 'USD', 'GBP']);
}
/**
* @return string
*/
protected function getRandomDateString(): string
{
$date = Carbon::now();
$date->subDays(rand(10, 100));
return $date->format('Y-m-d');
}
/**
* @return string
*/
protected function getRandomInterestPeriod(): string
{
return $this->randomFromArray(['daily', 'monthly', 'yearly']);
}
/**
* @return string
*/
protected function getRandomPercentage(): string
{
return rand(1, 10000) / 100;
}
/**
* @return string
*/
protected function randomAccountRole(): string
{
return $this->randomFromArray(['defaultAsset', 'sharedAsset', 'savingAsset']);
}
/**
* @param array $array
*
* @return mixed
*/
private function randomFromArray(array $array)
{
return $array[rand(0, count($array) - 1)];
}
/**
* @return string
*/
protected function randomLiabilityType(): string
{
return $this->randomFromArray(['loan', 'debt', 'mortgage']);
}
}

View File

@@ -23,136 +23,16 @@ declare(strict_types=1);
namespace Tests\Traits;
use Exception;
use JsonException;
use Log;
/**
* Trait TestHelpers
*/
trait TestHelpers
{
/**
* @return int
*/
public function randomInt(): int
{
$result = 4;
try {
$result = random_int(1, 100000);
} catch (Exception $e) {
Log::debug(sprintf('Could not generate random number: %s', $e->getMessage()));
}
return $result;
}
/**
* @param array $minimalSets
* @param array $startOptionalSets
* @param array $regenConfig
*
* @return array
*/
protected function genericDataProvider(array $minimalSets, array $startOptionalSets, array $regenConfig): array
{
$submissions = [];
/**
* @var string $i
* @var array $set
*/
foreach ($minimalSets as $i => $set) {
$body = [];
foreach ($set['fields'] as $ii => $value) {
$body[$ii] = $value;
}
// minimal set is part of all submissions:
$submissions[] = [[
'fields' => $body,
'parameters' => $set['parameters'] ?? [],
'ignore' => $set['ignore'] ?? [],
]];
// then loop and add fields:
$optionalSets = $startOptionalSets;
$keys = array_keys($optionalSets);
$count = count($keys) > self::MAX_ITERATIONS ? self::MAX_ITERATIONS : count($keys);
for ($iii = 1; $iii <= $count; $iii++) {
$combinations = $this->combinationsOf($iii, $keys);
// expand body with N extra fields:
foreach ($combinations as $iv => $extraFields) {
$second = $body;
$ignore = $set['ignore'] ?? []; // unused atm.
foreach ($extraFields as $v => $extraField) {
// now loop optional sets on $extraField and add whatever the config is:
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]);
}
}
}
$second = $this->regenerateValues($second, $regenConfig);
$submissions[] = [[
'fields' => $second,
'parameters' => $set['parameters'] ?? [],
'ignore' => $ignore,
]];
}
}
unset($second);
}
return $submissions;
}
/**
* @param $set
* @param $opts
*
* @return array
*/
protected function regenerateValues($set, $opts): array
{
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]();
}
}
}
}
}
}
return $set;
}
/**
* @param string $route
* @param array $content
*/
protected function updatedStoreAndCompare(string $route, array $content): void
protected function assertPOST(string $route, array $content): void
{
$submission = $content['submission'];
$expected = $content['expected'];
@@ -168,7 +48,7 @@ trait TestHelpers
// get return and compare each field
$responseAttributes = $responseJson['data']['attributes'];
$this->updatedCompareStorageArray($submission, $responseAttributes, $expected, $ignore);
$this->comparePOSTArray($submission, $responseAttributes, $expected, $ignore);
// ignore fields too!
}
@@ -179,11 +59,11 @@ trait TestHelpers
* @param array $expected
* @param array $ignore
*/
protected function updatedCompareStorageArray(array $submission, array $response, array $expected, array $ignore): void
private function comparePOSTArray(array $submission, array $response, array $expected, array $ignore): void
{
foreach ($response as $key => $value) {
if (is_array($value) && array_key_exists($key, $expected) && is_array($expected[$key])) {
$this->updatedCompareStorageArray($submission, $value, $expected[$key], $ignore[$key] ?? []);
$this->comparePOSTArray($submission, $value, $expected[$key], $ignore[$key] ?? []);
}
if (isset($expected[$key])) {
if (in_array($key, $ignore, true)) {
@@ -202,13 +82,6 @@ trait TestHelpers
$this->assertEquals($value, $expected[$key], $message);
}
// if($value !== $expected[$key]) {
// }
// var_dump($key);
// var_dump($value);
// var_dump($expected[$key]);
// exit;
}
}
@@ -219,47 +92,7 @@ trait TestHelpers
* @param string $route
* @param array $content
*/
protected function storeAndCompare(string $route, array $content): void
{
$submission = $content['fields'];
$parameters = $content['parameters'];
$ignore = $content['ignore'];
$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:\n%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 (array_key_exists($returnName, $submission) && !in_array($returnName, $ignore, true)) {
// TODO still based on account routine:
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($submission, $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);
}
}
}
}
/**
* @param string $route
* @param array $content
*/
protected function updatedUpdateAndCompare(string $route, array $content): void
protected function assertPUT(string $route, array $content): void
{
$submission = $content['submission'];
$ignore = $content['ignore'];
@@ -272,21 +105,21 @@ trait TestHelpers
// get return and compare each field
$responseAttributes = $responseJson['data']['attributes'];
$this->updatedCompareUpdatedArray($route, $submission, $responseAttributes, $ignore);
$this->comparePUTArray($route, $submission, $responseAttributes, $ignore);
}
/**
* @param string $url
* @param array $submission
* @param array $response
* @param array $ignore
* @param array $submission
* @param array $response
* @param array $ignore
*/
private function updatedCompareUpdatedArray(string $url, array $submission, array $response, array $ignore): void
private function comparePUTArray(string $url, array $submission, array $response, array $ignore): void
{
foreach ($response as $key => $value) {
if (is_array($value) && array_key_exists($key, $submission) && is_array($submission[$key])) {
$this->updatedCompareUpdatedArray($url, $submission[$key], $value, $ignore[$key] ?? []);
$this->comparePUTArray($url, $submission[$key], $value, $ignore[$key] ?? []);
}
if (isset($submission[$key])) {
@@ -310,162 +143,4 @@ trait TestHelpers
}
}
}
/**
* Some specials:
*
* @param string $area
* @param string $left
* @param string $right
*
* @return bool
*/
protected function ignoreCombination(string $area, string $left, string $right): bool
{
if ('api.v1.accounts.store' === $area) {
if ('expense' === $left
&& in_array($right, ['order', 'virtual_balance', 'opening_balance', 'opening_balance_date'])) {
return true;
}
}
return false;
}
/**
* @param string $route
* @param array $submission
*
* @throws JsonException
*/
protected function updateAndCompare(string $route, array $submission, array $ignored): void
{
// get original values:
$response = $this->get($route, ['Accept' => 'application/json']);
$status = $response->getStatusCode();
$this->assertEquals($status, 200, sprintf(sprintf('%s failed with 404.', $route)));
$response->assertStatus(200);
$originalString = $response->content();
$originalArray = json_decode($originalString, true, 512, JSON_THROW_ON_ERROR);
$originalAttributes = $originalArray['data']['attributes'];
// submit whatever is in submission:
// loop the fields we will update in Firefly III:
$submissionArray = [];
$fieldsToUpdate = array_keys($submission['fields']);
foreach ($fieldsToUpdate as $currentFieldName) {
$submissionArray[$currentFieldName] = $submission['fields'][$currentFieldName]['test_value'];
}
Log::debug(sprintf('Will PUT %s to %s', json_encode($submissionArray), $route));
$response = $this->put($route, $submissionArray, ['Accept' => 'application/json']);
$responseString = $response->content();
$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'] ?? [];
Log::debug(sprintf('Before: %s', json_encode($originalAttributes)));
Log::debug(sprintf('AFTER : %s', json_encode($responseAttributes)));
// loop it and compare:
foreach ($responseAttributes as $rKey => $rValue) {
// field should be ignored?
if (in_array($rKey, $ignored) || in_array($rKey, $submission['extra_ignore'])) {
continue;
}
// field in response was also in body:
if (array_key_exists($rKey, $submissionArray)) {
// comparison must be on array:
if (is_array($submissionArray[$rKey]) && is_array($rValue)) {
$this->compareArray($submissionArray, $rKey, $submissionArray[$rKey], $rValue);
}
if (!is_array($submissionArray[$rKey]) && !is_array($rValue)) {
if ($submissionArray[$rKey] !== $rValue) {
$message = sprintf(
"Expected field '%s' to be %s but its %s\nOriginal: %s\nSubmission: %s\nResult: %s",
$rKey,
var_export($submissionArray[$rKey], true),
var_export($rValue, true),
$originalString,
json_encode($submissionArray),
$responseString
);
$this->assertTrue(false, $message);
continue;
}
}
continue;
}
// field in response was not in body, but should be the same:
if (!array_key_exists($rKey, $submissionArray)) {
// original has this key too:
if (array_key_exists($rKey, $originalAttributes)) {
// but we can ignore it!
if (in_array($rKey, $submission['extra_ignore'])) {
continue;
}
// but it is different?
if ($originalAttributes[$rKey] !== $rValue) {
$message = sprintf(
"Untouched field '%s' should still be %s but changed to %s\nOriginal: %s\nSubmission: %s\nResult: %s",
$rKey,
var_export($originalAttributes[$rKey], true),
var_export($rValue, true),
$originalString,
json_encode($submissionArray),
$responseString
);
$this->assertTrue(false, $message);
}
}
continue;
}
}
}
/**
* @param array $fullOriginal
* @param string $key
* @param array $original
* @param array $returned
*/
protected function compareArray(array $fullOriginal, string $key, array $original, array $returned)
{
// TODO this should be configurable but OK
if (in_array($key, ['transactions', 'repetitions'], true) && 0 === count($original) && 0 !== count($returned)) {
// accept this.
return;
}
$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-array '%s' returned value %s does not match sent value %s.\n%s\n%s",
$key, var_export($returnValue, true), var_export($original[$objectKey][$returnKey], true),
json_encode($fullOriginal),
json_encode($returned),
);
$this->assertEquals($original[$objectKey][$returnKey], $returnValue, $message);
}
}
}
}
}