Clean up tests, test only the important things.

This commit is contained in:
James Cole
2019-04-12 04:53:18 +02:00
parent 6f063a134f
commit 5ac39dbdef
65 changed files with 464 additions and 4016 deletions

View File

@@ -74,450 +74,16 @@ class CurrencyControllerTest extends TestCase
}
/**
* Test the list of accounts.
*
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testAccounts(): void
{
// mock stuff:
$currency = TransactionCurrency::first();
$account = $this->getRandomAsset();
$collection = new Collection([$account]);
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock 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:
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('getAccountsByType')->withAnyArgs()->andReturn($collection)->atLeast()->once();
$repository->shouldReceive('getMetaValue')->atLeast()->once()->andReturn('1');
// test API
$response = $this->get(route('api.v1.currencies.accounts', [$currency->code]));
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(['meta' => ['pagination' => ['total' => 1, 'count' => 1, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]);
$response->assertJson(
['links' => ['self' => true, 'first' => true, 'last' => true,],]
);
$response->assertSee('type=all'); // default returns this.
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Show all available budgets.
*
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testAvailableBudgets(): void
{
// mock stuff:
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(AvailableBudgetTransformer::class);
$collection = new Collection([AvailableBudget::first()]);
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
// mock calls:
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$budgetRepos->shouldReceive('setUser')->atLeast()->once();
$budgetRepos->shouldReceive('getAvailableBudgets')->once()->andReturn($collection);
// call API
$currency = TransactionCurrency::first();
$response = $this->get(route('api.v1.currencies.available_budgets', [$currency->code]));
$response->assertStatus(200);
}
/**
* Show all bills
*
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testBills(): void
{
// create stuff
$paginator = new LengthAwarePaginator(new Collection([Bill::first()]), 0, 50, 1);
// mock stuff:
$repository = $this->mock(BillRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(BillTransformer::class);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
// mock calls:
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('getPaginator')->withAnyArgs()->andReturn($paginator)->once();
// test API
$currency = TransactionCurrency::first();
$response = $this->get(route('api.v1.currencies.bills', [$currency->code]));
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]);
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testBudgetLimits(): void
{
$repository = $this->mock(BudgetRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(BudgetLimitTransformer::class);
$currency = TransactionCurrency::first();
$budgetLimit = BudgetLimit::first();
$budgetLimit->transaction_currency_id = $currency->id;
$collection = new Collection([$budgetLimit]);
// mock 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('getAllBudgetLimits')->once()->andReturn($collection);
$currencyRepos->shouldReceive('setUser')->once();
$response = $this->get(route('api.v1.currencies.budget_limits', [$currency->code]));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testCer(): void
{
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyExchangeRateTransformer::class);
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
// mock calls
$repository->shouldReceive('setUser')->once()->atLeast()->once();
$repository->shouldReceive('getExchangeRates')->once()->andReturn(new Collection);
$currency = TransactionCurrency::first();
$response = $this->get(route('api.v1.currencies.cer', [$currency->code]));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Send delete
*
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testDelete(): void
{
// mock stuff:
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$userRepos->shouldReceive('hasRole')->once()->withArgs([Mockery::any(), 'owner'])->andReturn(true);
$repository->shouldReceive('currencyInUse')->once()->andReturn(false);
$repository->shouldReceive('destroy')->once()->andReturn(true);
// get a currency
$currency = TransactionCurrency::first();
// call API
$response = $this->delete('/api/v1/currencies/' . $currency->code);
$response->assertStatus(204);
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testDisable(): void
{
// create stuff
$currency = TransactionCurrency::first();
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
// mock 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')->once();
$repository->shouldReceive('disable')->once();
$repository->shouldReceive('currencyInUse')->once()->andReturnFalse();
// test API
$response = $this->post(route('api.v1.currencies.disable', [$currency->code]));
$response->assertStatus(200);
$response->assertJson(
['data' => [
'type' => 'currencies',
],]
);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testDisableInUse(): void
{
// create stuff
$currency = TransactionCurrency::first();
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('currencyInUse')->once()->andReturnTrue();
// test API
$response = $this->post(route('api.v1.currencies.disable', [$currency->code]));
$response->assertStatus(409);
$response->assertJson([]);
$response->assertHeader('Content-Type', 'application/json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testEnable(): void
{
// create stuff
$currency = TransactionCurrency::first();
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
// mock 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')->once();
$repository->shouldReceive('enable')->once();
// test API
$response = $this->post(route('api.v1.currencies.enable', [$currency->code]));
$response->assertStatus(200);
$response->assertJson(
['data' => [
'type' => 'currencies',
],]
);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Show index.
*
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testIndex(): void
{
// mock stuff:
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('getAll')->withNoArgs()->andReturn(new Collection)->once();
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
// test API
$response = $this->get('/api/v1/currencies');
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(
[
'meta' => [
'pagination' => [
'total' => 0,
'count' => 0,
'per_page' => true, // depends on user preference.
'current_page' => 1,
'total_pages' => 1,
],
],
]
);
$response->assertJson(
['links' => ['self' => true, 'first' => true, 'last' => true,],]
);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testMakeDefault(): void
{
// create stuff
$currency = TransactionCurrency::first();
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('enable')->once();
// mock 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]);
// test API
$response = $this->post(route('api.v1.currencies.default', [$currency->code]));
$response->assertStatus(200);
$response->assertJson(
['data' => [
'type' => 'currencies',
],]
);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testRecurrences(): void
{
// mock stuff:
$recurrence = Recurrence::first();
$repository = $this->mock(RecurringRepositoryInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(RecurrenceTransformer::class);
// mock calls:
$currencyRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('setUser');
$repository->shouldReceive('getAll')->once()->andReturn(new Collection([$recurrence]));
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
// call API
$currency = TransactionCurrency::first();
$response = $this->get(route('api.v1.currencies.recurrences', [$currency->code]));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testRules(): void
{
$ruleRepos = $this->mock(RuleRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(RuleTransformer::class);
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$ruleRepos->shouldReceive('getAll')->once()->andReturn(new Collection([Rule::first()]));
$currencyRepos->shouldReceive('setUser')->once();
// call API
$currency = TransactionCurrency::first();
$response = $this->get(route('api.v1.currencies.rules', [$currency->code]));
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Test show of a currency.
*
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testShow(): void
{
// create stuff
$currency = TransactionCurrency::first();
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
// mock 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]);
// test API
$response = $this->get('/api/v1/currencies/' . $currency->code);
$response->assertStatus(200);
$response->assertJson(
['data' => [
'type' => 'currencies',
],]
);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Store new currency.
*
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
* @covers \FireflyIII\Api\V1\Requests\CurrencyRequest
*/
public function testStore(): void
{
$currency = TransactionCurrency::first();
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
// mock transformer
@@ -552,13 +118,11 @@ class CurrencyControllerTest extends TestCase
* Store new currency and make it default.
*
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
* @covers \FireflyIII\Api\V1\Requests\CurrencyRequest
*/
public function testStoreWithDefault(): void
{
$currency = TransactionCurrency::first();
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
// mock transformer
@@ -595,110 +159,15 @@ class CurrencyControllerTest extends TestCase
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testTransactionsBasic(): void
{
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
$currency = TransactionCurrency::first();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(TransactionTransformer::class);
$accountRepos->shouldReceive('setUser');
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$paginator = new LengthAwarePaginator(new Collection, 0, 50);
$repository = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser');
$currencyRepository->shouldReceive('setUser');
$collector->shouldReceive('setUser')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setCurrency')->andReturnSelf();
$collector->shouldReceive('removeFilter')->andReturnSelf();
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator);
// test API
$response = $this->get(route('api.v1.currencies.transactions', [$currency->code]));
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'per_page' => 50, 'current_page' => 1, 'total_pages' => 1]],]);
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testTransactionsRange(): void
{
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
$currency = TransactionCurrency::first();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(TransactionTransformer::class);
$accountRepos->shouldReceive('setUser');
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$paginator = new LengthAwarePaginator(new Collection, 0, 50);
$repository = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('setUser');
$currencyRepository->shouldReceive('setUser');
$collector->shouldReceive('setUser')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setCurrency')->andReturnSelf();
$collector->shouldReceive('removeFilter')->andReturnSelf();
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator);
// test API
$response = $this->get(
route('api.v1.currencies.transactions', [$currency->code]) . '?' . http_build_query(['start' => '2018-01-01', 'end' => '2018-01-31'])
);
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'per_page' => 50, 'current_page' => 1, 'total_pages' => 1]],]);
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Update currency.
*
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
* @covers \FireflyIII\Api\V1\Requests\CurrencyRequest
*/
public function testUpdate(): void
{
$currency = TransactionCurrency::first();
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
// mock transformer
@@ -733,13 +202,11 @@ class CurrencyControllerTest extends TestCase
* Update currency and make default.
*
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
* @covers \FireflyIII\Api\V1\Requests\CurrencyRequest
*/
public function testUpdateWithDefault(): void
{
$currency = TransactionCurrency::first();
$repository = $this->mock(CurrencyRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
$preference = new Preference;
$preference->data = 'EUR';