. */ declare(strict_types=1); namespace Tests\Api\V1\Controllers; use FireflyIII\Models\Preference; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Transformers\PreferenceTransformer; use Laravel\Passport\Passport; use Log; use Preferences; use Tests\TestCase; /** * * Class PreferencesControllerTest * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ class PreferencesControllerTest extends TestCase { /** * Set up test */ public function setUp(): void { parent::setUp(); Passport::actingAs($this->user()); Log::info(sprintf('Now in %s.', get_class($this))); } /** * @covers \FireflyIII\Api\V1\Controllers\PreferenceController */ public function testUpdateArray(): void { $transformer = $this->mock(PreferenceTransformer::class); $accountRepos = $this->mock(AccountRepositoryInterface::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]); $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']); $response->assertStatus(200); } /** * @covers \FireflyIII\Api\V1\Controllers\PreferenceController */ public function testUpdateBoolean(): void { $transformer = $this->mock(PreferenceTransformer::class); $accountRepos = $this->mock(AccountRepositoryInterface::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]); $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']); $response->assertStatus(200); } /** * @covers \FireflyIII\Api\V1\Controllers\PreferenceController */ public function testUpdateDefault(): void { $transformer = $this->mock(PreferenceTransformer::class); $accountRepos = $this->mock(AccountRepositoryInterface::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]); $accountRepos->shouldReceive('setUser')->atLeast()->once(); /** @var Preference $preference */ $preference = Preferences::setForUser($this->user(), 'currencyPreference', false); $data = ['data' => 'EUR']; $response = $this->put(route('api.v1.preferences.update', [$preference->name]), $data, ['Accept' => 'application/json']); $response->assertStatus(200); } /** * @covers \FireflyIII\Api\V1\Controllers\PreferenceController */ public function testUpdateInteger(): void { $transformer = $this->mock(PreferenceTransformer::class); $accountRepos = $this->mock(AccountRepositoryInterface::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]); $accountRepos->shouldReceive('setUser')->atLeast()->once(); /** @var Preference $preference */ $preference = Preferences::setForUser($this->user(), 'listPageSize', 13); $data = ['data' => '434']; $response = $this->put(route('api.v1.preferences.update', [$preference->name]), $data, ['Accept' => 'application/json']); $response->assertStatus(200); } }