Fix API tests

This commit is contained in:
James Cole 2020-03-15 15:32:09 +01:00
parent 208bece7ea
commit 328c960950
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
24 changed files with 112 additions and 20 deletions

View File

@ -46,6 +46,7 @@ class AccountControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}
@ -59,6 +60,7 @@ class AccountControllerTest extends TestCase
*/
public function testStoreInvalidBalance(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);

View File

@ -48,6 +48,7 @@ class AttachmentControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}
@ -114,10 +115,10 @@ class AttachmentControllerTest extends TestCase
// data to submit
$data = [
'filename' => 'Some new att',
'description' => sprintf('Attempt #%d', $this->randomInt()),
'model' => 'TransactionJournal',
'model_id' => $journal->id,
'filename' => 'Some new att',
'description' => sprintf('Attempt #%d', $this->randomInt()),
'attachable_type' => 'TransactionJournal',
'attachable_id' => $journal->id,
];
@ -158,10 +159,10 @@ class AttachmentControllerTest extends TestCase
$repository->shouldReceive('getNoteText')->andReturn('Hi There');
// data to submit
$data = [
'filename' => $attachment->filename,
'description' => sprintf('Attempt #%d', $this->randomInt()),
'model' => 'TransactionJournal',
'model_id' => 1,
'filename' => $attachment->filename,
'description' => sprintf('Attempt #%d', $this->randomInt()),
'attachable_type' => 'TransactionJournal',
'attachable_id' => 1,
];
// test API

View File

@ -50,6 +50,7 @@ class AvailableBudgetControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -46,6 +46,7 @@ class BillControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -50,6 +50,7 @@ class BudgetControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}
@ -86,7 +87,7 @@ class BudgetControllerTest extends TestCase
];
// test API
$response = $this->post(route('api.v1.budgets.store'), $data);
$response = $this->post(route('api.v1.budgets.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'budgets', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');

View File

@ -48,6 +48,7 @@ class BudgetLimitControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -48,6 +48,7 @@ class CategoryControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -51,6 +51,7 @@ class AccountControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -46,6 +46,7 @@ class AvailableBudgetControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -49,6 +49,7 @@ class CategoryControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -50,6 +50,7 @@ class CurrencyControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -48,6 +48,7 @@ class LinkTypeControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -51,6 +51,7 @@ class PiggyBankControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}
@ -171,5 +172,56 @@ class PiggyBankControllerTest extends TestCase
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\PiggyBankController
* @throws Exception
*/
public function testUpdateWithAmount(): void
{
// create stuff
$piggy = $this->getRandomPiggyBank();
// mock stuff:
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(PiggyBankTransformer::class);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
// mock calls:
$repository->shouldReceive('setUser');
$repository->shouldReceive('update')->once()->andReturn($piggy);
$repository->shouldReceive('setCurrentAmount')->once();
$repository->shouldReceive('getCurrentAmount')->andReturn('0');
$repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('12');
//$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$currencyRepos->shouldReceive('setUser');
$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn($this->getEuro());
$data = [
'name' => 'new pigy bank ' . $this->randomInt(),
'account_id' => 1,
'current_amount' => '5',
'target_amount' => '100',
];
// test API
$response = $this->put(route('api.v1.piggy_banks.update', [$piggy->id]), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'piggy_banks', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
}

View File

@ -49,6 +49,7 @@ class PreferencesControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}
@ -99,12 +100,12 @@ class PreferencesControllerTest extends TestCase
$saved = new Preference;
$saved->user_id = $this->user()->id;
$saved->name = 'twoFactorAuthEnabled';
$saved->name = 'customFiscalYear';
$saved->data = false;
$saved->save();
Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'frontPageAccounts', []])->andReturn($countable);
Preferences::shouldReceive('set')->atLeast()->once()->withArgs(['twoFactorAuthEnabled', '1'])->atLeast()->once()->andReturn($pref);
Preferences::shouldReceive('set')->atLeast()->once()->withArgs(['customFiscalYear', '1'])->atLeast()->once()->andReturn($pref);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
@ -116,7 +117,7 @@ class PreferencesControllerTest extends TestCase
/** @var Preference $preference */
$data = ['data' => '1'];
$response = $this->put(route('api.v1.preferences.update', ['twoFactorAuthEnabled']), $data, ['Accept' => 'application/json']);
$response = $this->put(route('api.v1.preferences.update', ['customFiscalYear']), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
@ -147,7 +148,7 @@ class PreferencesControllerTest extends TestCase
$saved = new Preference;
$saved->user_id = $this->user()->id;
$saved->name = 'twoFactorEnabled';
$saved->name = 'customFiscalYear';
$saved->data = false;
$saved->save();

View File

@ -49,6 +49,7 @@ class RecurrenceControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -35,9 +35,9 @@ use FireflyIII\Transformers\RuleTransformer;
use FireflyIII\Transformers\TransactionGroupTransformer;
use Laravel\Passport\Passport;
use Log;
use Mockery;
use Preferences;
use Tests\TestCase;
use Mockery;
/**
*
@ -55,6 +55,7 @@ class RuleControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -60,6 +60,7 @@ class RuleGroupControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -56,6 +56,7 @@ class SummaryControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -44,6 +44,7 @@ class TagControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}
@ -51,7 +52,7 @@ class TagControllerTest extends TestCase
* Create Tag over API.
*
* @covers \FireflyIII\Api\V1\Controllers\TagController
* @covers \FireflyIII\Api\V1\Requests\TagRequest
* @covers \FireflyIII\Api\V1\Requests\TagStoreRequest
*/
public function testStore(): void
{
@ -120,7 +121,7 @@ class TagControllerTest extends TestCase
* Update Tag over API.
*
* @covers \FireflyIII\Api\V1\Controllers\TagController
* @covers \FireflyIII\Api\V1\Requests\TagRequest
* @covers \FireflyIII\Api\V1\Requests\TagUpdateRequest
*/
public function testUpdate(): void
{

View File

@ -53,6 +53,7 @@ class TransactionControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -51,6 +51,7 @@ class TransactionLinkControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -48,6 +48,7 @@ class UserControllerTest extends TestCase
{
parent::setUp();
Passport::actingAs($this->user());
$this->mockDefaultConfiguration();
Log::info(sprintf('Now in %s.', get_class($this)));
}

View File

@ -422,11 +422,18 @@ trait TestDataTrait
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
'currency_decimal_places' => $currency->decimal_places,
'transaction_journals' => [],
'categories' => [
0 => [
'id' => 0,
'name' => 'no cat',
'transaction_journals' => [],
],
],
];
// add two random amounts:
for ($i = 0; $i < 2; $i++) {
$data[$currency->id]['transaction_journals'][$i] = [
$data[$currency->id]['categories'][0]['transaction_journals'][$i] = [
'amount' => $amount,
'date' => $date,
];
@ -464,11 +471,18 @@ trait TestDataTrait
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
'currency_decimal_places' => $currency->decimal_places,
'transaction_journals' => [],
'categories' => [
0 => [
'id' => 0,
'name' => 'no cat',
'transaction_journals' => [],
],
],
];
// add two random amounts:
for ($i = 0; $i < 2; $i++) {
$data[$currency->id]['transaction_journals'][$i] = [
$data[$currency->id]['categories'][0]['transaction_journals'][$i] = [
'amount' => $amount,
'date' => $date,
];

View File

@ -231,7 +231,11 @@ abstract class TestCase extends BaseTestCase
$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);
}
/**