mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Expand test code.
This commit is contained in:
@@ -24,18 +24,6 @@ declare(strict_types=1);
|
||||
namespace Tests\Traits;
|
||||
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\Bill;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
|
||||
/**
|
||||
@@ -50,208 +38,208 @@ 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.*']);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @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.*']);
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -24,11 +24,11 @@ namespace Tests\Traits;
|
||||
|
||||
trait FakeValues
|
||||
{
|
||||
// /**
|
||||
// * @return string
|
||||
// */
|
||||
// protected function fakeName(): string {
|
||||
// return '';
|
||||
// }
|
||||
// /**
|
||||
// * @return string
|
||||
// */
|
||||
// protected function fakeName(): string {
|
||||
// return '';
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -23,25 +23,22 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\Traits;
|
||||
|
||||
use FireflyIII\Models\Configuration;
|
||||
use FireflyConfig;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
// }
|
||||
// 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);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -54,21 +54,12 @@ trait RandomValues
|
||||
return array_merge($res1, $res2);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function randomAccountRole(): string
|
||||
protected function getRandomAmount(): string
|
||||
{
|
||||
return $this->randomFromArray(['defaultAsset', 'sharedAsset', 'savingAsset']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function randomLiabilityType(): string
|
||||
{
|
||||
return $this->randomFromArray(['loan', 'debt', 'mortgage']);
|
||||
return number_format(rand(1000, 100000) / 100, '2', '.');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,14 +70,6 @@ trait RandomValues
|
||||
return $this->randomFromArray(['EUR', 'USD', 'GBP']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getRandomAmount(): string
|
||||
{
|
||||
return number_format(rand(1000, 100000) / 100, '2', '.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@@ -98,6 +81,14 @@ trait RandomValues
|
||||
return $date->format('Y-m-d');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getRandomInterestPeriod(): string
|
||||
{
|
||||
return $this->randomFromArray(['daily', 'monthly', 'yearly']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@@ -109,9 +100,9 @@ trait RandomValues
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getRandomInterestPeriod(): string
|
||||
protected function randomAccountRole(): string
|
||||
{
|
||||
return $this->randomFromArray(['daily', 'monthly', 'yearly']);
|
||||
return $this->randomFromArray(['defaultAsset', 'sharedAsset', 'savingAsset']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,4 +114,12 @@ trait RandomValues
|
||||
{
|
||||
return $array[rand(0, count($array) - 1)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function randomLiabilityType(): string
|
||||
{
|
||||
return $this->randomFromArray(['loan', 'debt', 'mortgage']);
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,21 @@ use Log;
|
||||
*/
|
||||
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
|
||||
@@ -105,22 +120,6 @@ trait TestHelpers
|
||||
return $submissions;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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 $set
|
||||
* @param $opts
|
||||
@@ -149,6 +148,136 @@ trait TestHelpers
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param array $content
|
||||
*/
|
||||
protected function updatedStoreAndCompare(string $route, array $content): void
|
||||
{
|
||||
$submission = $content['submission'];
|
||||
$expected = $content['expected'];
|
||||
$ignore = $content['ignore'];
|
||||
|
||||
// submit body
|
||||
$response = $this->post($route, $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');
|
||||
|
||||
// get return and compare each field
|
||||
$responseAttributes = $responseJson['data']['attributes'];
|
||||
$this->updatedCompareStorageArray($submission, $responseAttributes, $expected, $ignore);
|
||||
|
||||
// ignore fields too!
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $submission
|
||||
* @param array $response
|
||||
* @param array $expected
|
||||
* @param array $ignore
|
||||
*/
|
||||
protected function updatedCompareStorageArray(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] ?? []);
|
||||
}
|
||||
if (isset($expected[$key])) {
|
||||
if (in_array($key, $ignore, true)) {
|
||||
continue;
|
||||
}
|
||||
if (!in_array($key, $ignore, true)) {
|
||||
$message = sprintf(
|
||||
"Field '%s' with value %s is expected to be %s.\nSubmitted:\n%s\nIgnored: %s\nReturned\n%s",
|
||||
$key,
|
||||
var_export($value, true),
|
||||
var_export($expected[$key], true),
|
||||
json_encode($submission),
|
||||
json_encode($ignore),
|
||||
json_encode($response)
|
||||
);
|
||||
|
||||
$this->assertEquals($value, $expected[$key], $message);
|
||||
}
|
||||
|
||||
// if($value !== $expected[$key]) {
|
||||
// }
|
||||
// var_dump($key);
|
||||
// var_dump($value);
|
||||
// var_dump($expected[$key]);
|
||||
// exit;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param array $content
|
||||
*/
|
||||
protected function storeAndCompare(string $route, array $content): void
|
||||
{
|
||||
$submission = $content['fields'];
|
||||
$parameters = $content['parameters'];
|
||||
$ignore = $content['ignore'];
|
||||
// submit!
|
||||
$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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -247,47 +376,6 @@ 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'];
|
||||
// submit!
|
||||
$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 array $fullOriginal
|
||||
* @param string $key
|
||||
@@ -297,7 +385,7 @@ trait TestHelpers
|
||||
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)) {
|
||||
if (in_array($key, ['transactions', 'repetitions'], true) && 0 === count($original) && 0 !== count($returned)) {
|
||||
// accept this.
|
||||
return;
|
||||
}
|
||||
@@ -326,25 +414,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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user