Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Basic test, no value.
James Cole93856a3c5765
diff --git a/tests/Api/V1/Controllers/UserControllerTest.php b/tests/Api/V1/Controllers/UserControllerTest.phpindex 2feddd5e7b..ce2966d070 100644--- a/tests/Api/V1/Controllers/UserControllerTest.php+++ b/tests/Api/V1/Controllers/UserControllerTest.php@@ -46,8 +46,6 @@ class UserControllerTest extends TestCase*/public function setUp(): void{- self::markTestIncomplete('Incomplete for refactor.');- return;parent::setUp();Passport::actingAs($this->user());$this->mockDefaultConfiguration();@@ -63,190 +61,7 @@ class UserControllerTest extends TestCase*/public function testStoreBasic(): void{- $data = [- 'email' => 'some_new@user' . $this->randomInt() . '.com',- ];-- // mock- $userRepos = $this->mock(UserRepositoryInterface::class);- $transformer = $this->mock(UserTransformer::class);- $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);- $userRepos->shouldReceive('store')->once()->andReturn($this->user());-- // 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.users.store'), $data, ['Content-Type' => 'application/x-www-form-urlencoded']);- $response->assertStatus(200);- }-- /**- * Store new user using JSON.- *- * @covers \FireflyIII\Api\V1\Controllers\UserController- * @covers \FireflyIII\Api\V1\Requests\UserStoreRequest- */- public function testStoreBasicJson(): void- {- $data = [- 'email' => 'some_new@user' . $this->randomInt() . '.com',- 'blocked' => true,- 'blocked_code' => 'email_changed',- ];-- // mock- $userRepos = $this->mock(UserRepositoryInterface::class);- $transformer = $this->mock(UserTransformer::class);- $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);- $userRepos->shouldReceive('store')->once()->andReturn($this->user());-- // 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->postJson('/api/v1/users', $data, ['Accept' => 'application/json']);- $response->assertStatus(200);- }-- /**- * Store user with info already used.- *- * @covers \FireflyIII\Api\V1\Controllers\UserController- * @covers \FireflyIII\Api\V1\Requests\UserStoreRequest- */- public function testStoreNotUnique(): void- {- $data = [- 'email' => $this->user()->email,- 'blocked' => 0,- ];-- // mock- $userRepos = $this->mock(UserRepositoryInterface::class);- $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);- // test API- $response = $this->post(route('api.v1.users.store'), $data, ['Accept' => 'application/json']);- $response->assertStatus(422);- $response->assertExactJson(- [- 'message' => 'The given data was invalid.',- 'errors' => [- 'email' => [- 'The email address has already been taken.',- ],- ],- ]- );- }-- /**- * Store user with info already used.- *- * @covers \FireflyIII\Api\V1\Controllers\UserController- * @covers \FireflyIII\Api\V1\Requests\UserStoreRequest- */- public function testStoreNotUniqueJson(): void- {- $data = [- 'email' => $this->user()->email,- 'blocked' => 0,- ];-- // mock- $userRepos = $this->mock(UserRepositoryInterface::class);- $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);- // test API- $response = $this->postJson('/api/v1/users', $data, ['Accept' => 'application/json']);- $response->assertStatus(422);- $response->assertExactJson(- [- 'message' => 'The given data was invalid.',- 'errors' => [- 'email' => [- 'The email address has already been taken.',- ],- ],- ]- );- }-- /**- * Update user.- *- * @covers \FireflyIII\Api\V1\Controllers\UserController- * @covers \FireflyIII\Api\V1\Requests\UserStoreRequest- */- public function testUpdate(): void- {- // create a user first:- $user = User::create(['email' => 'some@newu' . $this->randomInt() . 'ser.nl', 'password' => 'hello', 'blocked' => 0]);-- // data:- $data = [- 'email' => 'some-new@email' . $this->randomInt() . '.com',- 'blocked' => 0,- ];-- // mock- $userRepos = $this->mock(UserRepositoryInterface::class);- $transformer = $this->mock(UserTransformer::class);- $userRepos->shouldReceive('update')->once()->andReturn($user);- $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);-- // 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]);-- // call API- $response = $this->put(route('api.v1.users.update', $user->id), $data, ['Accept' => 'application/json']);- $response->assertStatus(200);- }-- /**- * Update user.- *- * @covers \FireflyIII\Api\V1\Controllers\UserController- * @covers \FireflyIII\Api\V1\Requests\UserStoreRequest- */- public function testUpdateJson(): void- {- // create a user first:- $user = User::create(['email' => 'some@newu' . $this->randomInt() . 'ser.nl', 'password' => 'hello', 'blocked' => 0]);-- // data:- $data = [- 'email' => 'some-new@email' . $this->randomInt() . '.com',- 'blocked' => 0,- ];-- // mock- $userRepos = $this->mock(UserRepositoryInterface::class);- $transformer = $this->mock(UserTransformer::class);- $userRepos->shouldReceive('update')->once()->andReturn($user);- $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);-- // 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]);-- // call API- $response = $this->putJson('/api/v1/users/' . $user->id, $data, ['Accept' => 'application/json']);- $response->assertStatus(200);+ $this->assertTrue(true);}}diff --git a/tests/TestCase.php b/tests/TestCase.phpindex 3d61276596..610ca27616 100644--- a/tests/TestCase.php+++ b/tests/TestCase.php@@ -23,20 +23,17 @@ declare(strict_types=1);namespace Tests;+use FireflyIII\User;use Illuminate\Foundation\Testing\TestCase as BaseTestCase;+use Tests\Traits\MocksDefaultValues;+use Tests\Traits\TestHelpers;/*** Class TestCase- *- * @SuppressWarnings(PHPMD.NumberOfChildren)- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)- * @SuppressWarnings(PHPMD.TooManyPublicMethods)- * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)*/abstract class TestCase extends BaseTestCase{- use CreatesApplication;+ use CreatesApplication, MocksDefaultValues, TestHelpers;/*** @return array@@ -54,4 +51,13 @@ abstract class TestCase extends BaseTestCase];}+ /**+ * @return User+ */+ public function user(): User+ {+ return User::find(1);+ }++}diff --git a/tests/Traits/MocksDefaultValues.php b/tests/Traits/MocksDefaultValues.phpnew file mode 100644index 0000000000..65d05a62f2--- /dev/null+++ b/tests/Traits/MocksDefaultValues.php@@ -0,0 +1,47 @@+<?php+/*+ * MocksDefaultValues.php+ * Copyright (c) 2020 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++declare(strict_types=1);++namespace Tests\Traits;++use FireflyIII\Models\Configuration;+use FireflyConfig;++/**+ * Trait MocksDefaultValues+ */+trait MocksDefaultValues+{+ public function mockDefaultConfiguration(): void+ {++ $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);+ }++}diff --git a/tests/Traits/TestHelpers.php b/tests/Traits/TestHelpers.phpnew file mode 100644index 0000000000..b3928c3e6e--- /dev/null+++ b/tests/Traits/TestHelpers.php@@ -0,0 +1,48 @@+<?php+/*+ * TestHelpers.php+ * Copyright (c) 2020 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++declare(strict_types=1);++namespace Tests\Traits;+use Exception;+use Log;++/**+ * Trait TestHelpers+ */+trait TestHelpers+{+ /**+ * @return int+ */+ public function randomInt(): int+ {+ $result = 4;+ try {+ $result = random_int(1, 100000);+ } catch (Exception $e) {+ Log::debug(sprintf('Could not generate random number: %s', $e->getMessage()));+ }++ return $result;+ }++}