Also test attachments.

This commit is contained in:
James Cole
2021-03-13 14:33:48 +01:00
parent bd040c80b2
commit bdb298740a
19 changed files with 446 additions and 99 deletions

View File

@@ -50,9 +50,9 @@ class StoreControllerTest extends TestCase
/**
* @param array $submission
*
* X data Provider storeAccountDataProvider
* @dataProvider storeDataProvider
*
* @dataProvider emptyDataProvider
* @ data Provider emptyDataProvider
*/
public function testStore(array $submission): void
{
@@ -76,7 +76,7 @@ class StoreControllerTest extends TestCase
/**
* @return array
*/
public function storeAccountDataProvider(): array
public function storeDataProvider(): array
{
$minimalSets = $this->minimalSets();
$optionalSets = $this->optionalSets();
@@ -226,27 +226,4 @@ class StoreControllerTest extends TestCase
],
];
}
/**
* @param string $area
* @param string $left
* @param string $right
*
* @return bool
*/
private function ignoreCombination(string $area, string $left, string $right): bool
{
Log::debug(sprintf('Must ignore %s: %s vs %s?', $area, $left, $right));
if ('store-account' === $area) {
if ('expense' === $left && in_array($right, ['virtual_balance', 'opening_balance', 'opening_balance_date'])) {
Log::debug('Yes');
return true;
}
}
Log::debug('NO');
return false;
}
}

View File

@@ -49,7 +49,7 @@ class UpdateControllerTest extends TestCase
}
/**
* @dataProvider updateSetDataProvider
* @dataProvider updateDataProvider
*/
public function testUpdate(array $submission): void
{
@@ -69,7 +69,7 @@ class UpdateControllerTest extends TestCase
/**
* @return array
*/
public function updateSetDataProvider(): array
public function updateDataProvider(): array
{
$submissions = [];
$all = $this->updateDataSet();
@@ -160,14 +160,14 @@ class UpdateControllerTest extends TestCase
'fields' => [
'currency_id' => ['test_value' => (string)$faker->numberBetween(1, 10)],
],
'extra_ignore' => [],
'extra_ignore' => ['currency_code'],
],
'currency_code' => [
'id' => 1,
'fields' => [
'currency_code' => ['test_value' => $currencyCode],
],
'extra_ignore' => [],
'extra_ignore' => ['currency_id'],
],
'account_role' => [
'id' => 1,

View File

@@ -0,0 +1,152 @@
<?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\Attachment;
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.attachments.store';
$this->submitAndCompare($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 $faker->name;
// },
// 'iban' => function () {
// $faker = Factory::create();
//
// return $faker->iban();
// },
// 'account_number' => function () {
// $faker = Factory::create();
//
// return $faker->iban();
// },
];
return $this->genericDataProvider($minimalSets, $optionalSets, $regenConfig);
}
/**
* @return array
*/
private function minimalSets(): array
{
$faker = Factory::create();
$types = [
'Account',
'Budget',
'Bill',
'TransactionJournal',
'PiggyBank',
'Tag',
];
$type = $types[rand(0, count($types) - 1)];
return [
'default_file' => [
'fields' => [
'filename' => $faker->randomAscii,
'attachable_type' => $type,
'attachable_id' => '1',
],
],
];
}
/**
* @return \array[][]
*/
private function optionalSets(): array
{
$faker = Factory::create();
return [
'title' => [
'fields' => [
'title' => $faker->randomAscii,
],
],
'notes' => [
'fields' => [
'notes' => join(' ', $faker->words(5)),
],
],
];
}
}

View File

@@ -0,0 +1,123 @@
<?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\Attachment;
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.attachments.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();
$set = [
'filename' => [
'id' => 1,
'fields' => [
'filename' => ['test_value' => $faker->text(64)],
],
'extra_ignore' => [],
],
'title' => [
'id' => 1,
'fields' => [
'title' => ['test_value' => $faker->text(64)],
],
'extra_ignore' => [],
],
'notes' => [
'id' => 1,
'fields' => [
'notes' => ['test_value' => join(' ', $faker->words(5))],
],
'extra_ignore' => [],
],
'model' => [
'id' => 1,
'fields' => [
'attachable_type' => ['test_value' => 'TransactionJournal'],
'attachable_id' => ['test_value' => (string)2],
],
'extra_ignore' => [],
],
];
return $set;
}
}

View File

@@ -120,10 +120,9 @@ trait TestHelpers
// get original values:
$response = $this->get($route, ['Accept' => 'application/json']);
$response->assertStatus(200);
$originalString = $response->content();
$originalArray = json_decode($originalString, true, 512, JSON_THROW_ON_ERROR);
$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 = [];
@@ -131,13 +130,18 @@ trait TestHelpers
foreach ($fieldsToUpdate as $currentFieldName) {
$submissionArray[$currentFieldName] = $submission['fields'][$currentFieldName]['test_value'];
}
$response = $this->put($route, $submissionArray, ['Accept' => 'application/json']);
Log::debug(sprintf('Will PUT %s to %s', json_encode($submissionArray), $route));
$response = $this->put($route, $submissionArray, ['Accept' => 'application/json']);
$responseString = $response->content();
$response->assertStatus(200);
$responseArray = json_decode($responseString, true, 512, JSON_THROW_ON_ERROR);
$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?
@@ -165,10 +169,25 @@ trait TestHelpers
// 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, $originalArray)) {
if (array_key_exists($rKey, $originalAttributes)) {
// but we can ignore it!
if(in_array($rKey, $submission['extra_ignore'])) {
continue;
}
// but it is different?
if ($originalArray[$rKey] !== $rValue) {
$message = 'Some other value not correct!';
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);
}
}
@@ -242,7 +261,8 @@ trait TestHelpers
// compare results:
foreach ($responseJson['data']['attributes'] as $returnName => $returnValue) {
if (array_key_exists($returnName, $submission)) {
if ($this->ignoreCombination('store-account', $submission['type'], $returnName)) {
// TODO still based on account routine:
if ($this->ignoreCombination($route, $submission['type'] ?? 'blank', $returnName)) {
continue;
}
@@ -255,4 +275,25 @@ 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.attachments.store' === $area) {
if ('expense' === $left
&& in_array($right, ['virtual_balance', 'opening_balance', 'opening_balance_date'])) {
return true;
}
}
return false;
}
}