Improve test coverage.

This commit is contained in:
James Cole
2019-08-03 14:45:37 +02:00
parent 75c2529d3e
commit cf121fea50
16 changed files with 296 additions and 971 deletions

View File

@@ -28,6 +28,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Transformers\PreferenceTransformer;
use Laravel\Passport\Passport;
use Log;
use Mockery;
use Preferences;
use Tests\TestCase;
@@ -59,6 +60,13 @@ class PreferencesControllerTest extends TestCase
$transformer = $this->mock(PreferenceTransformer::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
// mock calls to preferences facade.
$pref = new Preference;
$pref->data = [1, 2, 3];
Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'frontPageAccounts', []])->andReturn($pref);
Preferences::shouldReceive('set')->atLeast()->once()->withArgs(['frontPageAccounts', ['4', '5', '6']])->atLeast()->once()->andReturn($pref);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
@@ -67,10 +75,10 @@ class PreferencesControllerTest extends TestCase
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
$accountRepos->shouldReceive('setUser')->atLeast()->once();
/** @var Preference $preference */
$preference = Preferences::setForUser($this->user(), 'frontPageAccounts', [1, 2, 3]);
$data = ['data' => '4,5,6'];
$response = $this->put(route('api.v1.preferences.update', [$preference->name]), $data, ['Accept' => 'application/json']);
///** @var Preference $preference */
//$preference = Preferences::setForUser($this->user(), 'frontPageAccounts', [1, 2, 3]);
$data = ['data' => '4,5,6'];
$response = $this->put(route('api.v1.preferences.update', ['frontPageAccounts']), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
}
@@ -83,6 +91,21 @@ class PreferencesControllerTest extends TestCase
$transformer = $this->mock(PreferenceTransformer::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
// mock calls to preferences facade.
$pref = new Preference;
$pref->data = false;
$countable = new Preference;
$countable->data = [1];
$saved = new Preference;
$saved->user_id = $this->user()->id;
$saved->name = 'twoFactorAuthEnabled';
$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);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
@@ -92,12 +115,12 @@ class PreferencesControllerTest extends TestCase
$accountRepos->shouldReceive('setUser')->atLeast()->once();
/** @var Preference $preference */
$preference = Preferences::setForUser($this->user(), 'twoFactorAuthEnabled', false);
$data = ['data' => '1'];
$response = $this->put(route('api.v1.preferences.update', [$preference->name]), $data, ['Accept' => 'application/json']);
$data = ['data' => '1'];
$response = $this->put(route('api.v1.preferences.update', ['twoFactorAuthEnabled']), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
}
/**
@@ -116,10 +139,23 @@ class PreferencesControllerTest extends TestCase
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
$accountRepos->shouldReceive('setUser')->atLeast()->once();
/** @var Preference $preference */
$preference = Preferences::setForUser($this->user(), 'currencyPreference', false);
// mock calls to preferences facade.
$pref = new Preference;
$pref->data = 'EUR';
$countable = new Preference;
$countable->data = [1];
$saved = new Preference;
$saved->user_id = $this->user()->id;
$saved->name = 'twoFactorEnabled';
$saved->data = false;
$saved->save();
Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'frontPageAccounts', []])->andReturn($countable);
Preferences::shouldReceive('set')->atLeast()->once()->withArgs(['currencyPreference', 'EUR'])->atLeast()->once()->andReturn($pref);
$data = ['data' => 'EUR'];
$response = $this->put(route('api.v1.preferences.update', [$preference->name]), $data, ['Accept' => 'application/json']);
$response = $this->put(route('api.v1.preferences.update', ['currencyPreference']), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
}
@@ -140,10 +176,23 @@ class PreferencesControllerTest extends TestCase
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
$accountRepos->shouldReceive('setUser')->atLeast()->once();
/** @var Preference $preference */
$preference = Preferences::setForUser($this->user(), 'listPageSize', 13);
// mock calls to preferences facade.
$pref = new Preference;
$pref->data = 'EUR';
$countable = new Preference;
$countable->data = [1];
$saved = new Preference;
$saved->user_id = $this->user()->id;
$saved->name = 'listPageSize';
$saved->data = 200;
$saved->save();
Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'frontPageAccounts', []])->andReturn($countable);
Preferences::shouldReceive('set')->atLeast()->once()->withArgs(['listPageSize', '434'])->atLeast()->once()->andReturn($pref);
$data = ['data' => '434'];
$response = $this->put(route('api.v1.preferences.update', [$preference->name]), $data, ['Accept' => 'application/json']);
$response = $this->put(route('api.v1.preferences.update', ['listPageSize']), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
}