mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-18 15:39:50 +00:00
Refactored a lot of tests.
This commit is contained in:
@@ -22,6 +22,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Controllers;
|
||||
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
@@ -30,6 +31,7 @@ use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Mockery;
|
||||
use Preferences;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
@@ -56,13 +58,14 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testCannotCreate(): void
|
||||
{
|
||||
// mock stuff
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$this->mockDefaultSession();
|
||||
$this->mockIntroPreference('shown_demo_currencies_create');
|
||||
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
$userRepos->shouldReceive('hasRole')->once()->andReturn(false);
|
||||
// mock stuff
|
||||
$this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(false);
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.create'));
|
||||
@@ -75,16 +78,18 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testCannotDelete(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
// mock stuff
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$euro = $this->getEuro();
|
||||
|
||||
$repository->shouldReceive('currencyInUse')->andReturn(true);
|
||||
$userRepos->shouldReceive('hasRole')->once()->andReturn(true);
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.delete', [2]));
|
||||
$response = $this->get(route('currencies.delete', [$euro->id]));
|
||||
$response->assertStatus(302);
|
||||
// has bread crumb
|
||||
$response->assertSessionHas('error');
|
||||
@@ -95,18 +100,20 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testCannotDestroy(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
// mock stuff
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$euro = $this->getEuro();
|
||||
|
||||
$repository->shouldReceive('currencyInUse')->andReturn(true);
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
$userRepos->shouldReceive('hasRole')->once()->andReturn(true);
|
||||
|
||||
|
||||
$this->session(['currencies.delete.uri' => 'http://localhost']);
|
||||
$this->be($this->user());
|
||||
$response = $this->post(route('currencies.destroy', [1]));
|
||||
$response = $this->post(route('currencies.destroy', [$euro->id]));
|
||||
$response->assertStatus(302);
|
||||
$response->assertSessionHas('error');
|
||||
}
|
||||
@@ -116,12 +123,13 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testCreate(): void
|
||||
{
|
||||
// mock stuff
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$this->mockDefaultSession();
|
||||
$this->mockIntroPreference('shown_demo_currencies_create');
|
||||
|
||||
// mock stuff
|
||||
$this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->times(2)->andReturn(true);
|
||||
|
||||
$this->be($this->user());
|
||||
@@ -136,16 +144,19 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testDefaultCurrency(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
// mock stuff
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
$this->mock(UserRepositoryInterface::class);
|
||||
$euro = $this->getEuro();
|
||||
$currencyRepos->shouldReceive('enable')->once();
|
||||
|
||||
Preferences::shouldReceive('mark')->atLeast()->once();
|
||||
Preferences::shouldReceive('set')->withArgs(['currencyPreference', $euro->code])->atLeast()->once();
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.default', [1]));
|
||||
$response = $this->get(route('currencies.default', [$euro->id]));
|
||||
$response->assertStatus(302);
|
||||
$response->assertSessionHas('success');
|
||||
}
|
||||
@@ -155,17 +166,20 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testDelete(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
// mock stuff
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$euro = $this->getEuro();
|
||||
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
$repository->shouldReceive('currencyInUse')->andReturn(false);
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->times(2)->andReturn(true);
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.delete', [2]));
|
||||
$response = $this->get(route('currencies.delete', [$euro->id]));
|
||||
$response->assertStatus(200);
|
||||
// has bread crumb
|
||||
$response->assertSee('<ol class="breadcrumb">');
|
||||
@@ -176,10 +190,13 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testDestroy(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
// mock stuff
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$euro = $this->getEuro();
|
||||
|
||||
$repository->shouldReceive('currencyInUse')->andReturn(false);
|
||||
$repository->shouldReceive('destroy')->andReturn(true)->once();
|
||||
@@ -188,7 +205,7 @@ class CurrencyControllerTest extends TestCase
|
||||
|
||||
$this->session(['currencies.delete.uri' => 'http://localhost']);
|
||||
$this->be($this->user());
|
||||
$response = $this->post(route('currencies.destroy', [1]));
|
||||
$response = $this->post(route('currencies.destroy', [$euro->id]));
|
||||
$response->assertStatus(302);
|
||||
$response->assertSessionHas('success');
|
||||
}
|
||||
@@ -198,17 +215,21 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testDisable(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$currency = TransactionCurrency::first();
|
||||
$euro = $this->getEuro();
|
||||
|
||||
Preferences::shouldReceive('mark')->atLeast()->once();
|
||||
|
||||
$userRepos->shouldReceive('hasRole')->atLeast()->once()->andReturn(true);
|
||||
$repository->shouldReceive('currencyInuse')->atLeast()->once()->andReturn(false);
|
||||
$repository->shouldReceive('disable')->atLeast()->once()->andReturn(false);
|
||||
$repository->shouldReceive('get')->atLeast()->once()->andReturn(new Collection([$currency]));
|
||||
$repository->shouldReceive('get')->atLeast()->once()->andReturn(new Collection([$euro]));
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.disable', [$currency->id]));
|
||||
$response = $this->get(route('currencies.disable', [$euro->id]));
|
||||
$response->assertStatus(302);
|
||||
}
|
||||
|
||||
@@ -217,19 +238,23 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testDisableEnableFirst(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$currency = TransactionCurrency::first();
|
||||
$euro = $this->getEuro();
|
||||
|
||||
$userRepos->shouldReceive('hasRole')->atLeast()->once()->andReturn(true);
|
||||
$repository->shouldReceive('currencyInuse')->atLeast()->once()->andReturn(false);
|
||||
$repository->shouldReceive('disable')->atLeast()->once()->andReturn(false);
|
||||
$repository->shouldReceive('get')->atLeast()->once()->andReturn(new Collection);
|
||||
$repository->shouldReceive('getAll')->atLeast()->once()->andReturn(new Collection([$currency]));
|
||||
$repository->shouldReceive('getAll')->atLeast()->once()->andReturn(new Collection([$euro]));
|
||||
$repository->shouldReceive('enable')->atLeast()->once()->andReturn(true);
|
||||
Preferences::shouldReceive('mark')->atLeast()->once();
|
||||
Preferences::shouldReceive('set')->withArgs(['currencyPreference', $euro->code])->atLeast()->once();
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.disable', [$currency->id]));
|
||||
$response = $this->get(route('currencies.disable', [$euro->id]));
|
||||
$response->assertStatus(302);
|
||||
}
|
||||
|
||||
@@ -238,16 +263,19 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testDisableInUse(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$currency = TransactionCurrency::first();
|
||||
$euro = $this->getEuro();
|
||||
|
||||
$userRepos->shouldReceive('hasRole')->atLeast()->once()->andReturn(true);
|
||||
$repository->shouldReceive('currencyInuse')->atLeast()->once()->andReturn(true);
|
||||
$repository->shouldNotReceive('disable');
|
||||
Preferences::shouldReceive('mark')->atLeast()->once();
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.disable', [$currency->id]));
|
||||
$response = $this->get(route('currencies.disable', [$euro->id]));
|
||||
$response->assertStatus(302);
|
||||
}
|
||||
|
||||
@@ -256,18 +284,21 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testDisableNothingLeft(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$currency = TransactionCurrency::first();
|
||||
$euro = $this->getEuro();
|
||||
|
||||
$userRepos->shouldReceive('hasRole')->atLeast()->once()->andReturn(true);
|
||||
$repository->shouldReceive('currencyInuse')->atLeast()->once()->andReturn(false);
|
||||
$repository->shouldReceive('disable')->atLeast()->once()->andReturn(false);
|
||||
$repository->shouldReceive('get')->atLeast()->once()->andReturn(new Collection);
|
||||
$repository->shouldReceive('getAll')->atLeast()->once()->andReturn(new Collection);
|
||||
Preferences::shouldReceive('mark')->atLeast()->once();
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.disable', [$currency->id]));
|
||||
$response = $this->get(route('currencies.disable', [$euro->id]));
|
||||
$response->assertStatus(500);
|
||||
$response->assertSee('No currencies found.');
|
||||
}
|
||||
@@ -277,16 +308,16 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testEdit(): void
|
||||
{
|
||||
// mock stuff
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
$this->mockDefaultSession();
|
||||
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
// mock stuff
|
||||
$this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$euro = $this->getEuro();
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->times(2)->andReturn(true);
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.edit', [1]));
|
||||
$response = $this->get(route('currencies.edit', [$euro->id]));
|
||||
$response->assertStatus(200);
|
||||
// has bread crumb
|
||||
$response->assertSee('<ol class="breadcrumb">');
|
||||
@@ -297,14 +328,17 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testEnable(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$currency = TransactionCurrency::first();
|
||||
$this->mock(UserRepositoryInterface::class);
|
||||
$euro = $this->getEuro();
|
||||
|
||||
$repository->shouldReceive('enable')->atLeast()->once();
|
||||
Preferences::shouldReceive('mark')->atLeast()->once();
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.enable', [$currency->id]));
|
||||
$response = $this->get(route('currencies.enable', [$euro->id]));
|
||||
$response->assertStatus(302);
|
||||
}
|
||||
|
||||
@@ -314,6 +348,9 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testIndex(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
$this->mockIntroPreference('shown_demo_currencies_index');
|
||||
|
||||
// mock stuff
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
@@ -326,6 +363,14 @@ class CurrencyControllerTest extends TestCase
|
||||
$repository->shouldReceive('getAll')->andReturn($currencies);
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->times(2)->andReturn(true);
|
||||
|
||||
$pref = new Preference;
|
||||
$pref->data = 50;
|
||||
Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
|
||||
|
||||
$pref = new Preference;
|
||||
$pref->data = 'EUR';
|
||||
Preferences::shouldReceive('get')->withArgs(['currencyPreference', 'EUR'])->atLeast()->once()->andReturn($pref);
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.index'));
|
||||
$response->assertStatus(200);
|
||||
@@ -339,6 +384,9 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testIndexNoRights(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
$this->mockIntroPreference('shown_demo_currencies_index');
|
||||
|
||||
// mock stuff
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
||||
@@ -349,6 +397,14 @@ class CurrencyControllerTest extends TestCase
|
||||
$repository->shouldReceive('getAll')->andReturn(new Collection);
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->times(2)->andReturn(false);
|
||||
|
||||
$pref = new Preference;
|
||||
$pref->data = 50;
|
||||
Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
|
||||
|
||||
$pref = new Preference;
|
||||
$pref->data = 'EUR';
|
||||
Preferences::shouldReceive('get')->withArgs(['currencyPreference', 'EUR'])->atLeast()->once()->andReturn($pref);
|
||||
|
||||
$this->be($this->user());
|
||||
$response = $this->get(route('currencies.index'));
|
||||
$response->assertStatus(200);
|
||||
@@ -363,6 +419,8 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testStore(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
// mock stuff
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
@@ -391,6 +449,8 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testStoreError(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
// mock stuff
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
@@ -419,6 +479,8 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testStoreNoRights(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
// mock stuff
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
@@ -447,6 +509,8 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testUpdate(): void
|
||||
{
|
||||
$this->mockDefaultSession();
|
||||
|
||||
// mock stuff
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
@@ -455,6 +519,7 @@ class CurrencyControllerTest extends TestCase
|
||||
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
||||
$repository->shouldReceive('update')->andReturn(new TransactionCurrency);
|
||||
$userRepos->shouldReceive('hasRole')->once()->andReturn(true);
|
||||
Preferences::shouldReceive('mark')->atLeast()->once();
|
||||
|
||||
$this->session(['currencies.edit.uri' => 'http://localhost']);
|
||||
$data = [
|
||||
|
||||
Reference in New Issue
Block a user