Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Implement category and configuration tests.
James Cole3bca9b06f8f1
diff --git a/app/Models/Configuration.php b/app/Models/Configuration.phpindex 3a0617d92f..8f29c16dc0 100644--- a/app/Models/Configuration.php+++ b/app/Models/Configuration.php@@ -29,6 +29,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;* Class Configuration.** @property string $data+ * @property string $name*/class Configuration extends Model{diff --git a/routes/api.php b/routes/api.phpindex 8083a43212..37a33015f7 100644--- a/routes/api.php+++ b/routes/api.php@@ -130,7 +130,7 @@ Route::group(// Configuration API routes:Route::get('', ['uses' => 'ConfigurationController@index', 'as' => 'index']);- Route::put('', ['uses' => 'ConfigurationController@update', 'as' => 'update']);+ Route::post('', ['uses' => 'ConfigurationController@update', 'as' => 'update']);});diff --git a/tests/Api/V1/Controllers/BudgetLimitControllerTest.php b/tests/Api/V1/Controllers/BudgetLimitControllerTest.phpindex 1af35c2f91..47a222a80f 100644--- a/tests/Api/V1/Controllers/BudgetLimitControllerTest.php+++ b/tests/Api/V1/Controllers/BudgetLimitControllerTest.php@@ -249,6 +249,7 @@ class BudgetLimitControllerTest extends TestCase// call API$response = $this->post('/api/v1/budget_limits', $data);$response->assertStatus(500);+ $response->assertSee('Unknown budget.');}/**@@ -324,6 +325,7 @@ class BudgetLimitControllerTest extends TestCase// call API$response = $this->put('/api/v1/budget_limits/' . $budgetLimit->id, $data);$response->assertStatus(500);+ $response->assertSee('Unknown budget.');}}\ No newline at end of filediff --git a/tests/Api/V1/Controllers/CategoryControllerTest.php b/tests/Api/V1/Controllers/CategoryControllerTest.phpnew file mode 100644index 0000000000..4782bac76c--- /dev/null+++ b/tests/Api/V1/Controllers/CategoryControllerTest.php@@ -0,0 +1,175 @@+<?php+/**+ * CategoryControllerTest.php+ * Copyright (c) 2018 [email protected]+ *+ * This file is part of Firefly III.+ *+ * Firefly III is free software: you can redistribute it and/or modify+ * it under the terms of the GNU General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * Firefly III 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 General Public License for more details.+ *+ * You should have received a copy of the GNU General Public License+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.+ */++declare(strict_types=1);++namespace Tests\Api\V1\Controllers;+++use FireflyIII\Models\Budget;+use FireflyIII\Repositories\Category\CategoryRepositoryInterface;+use Laravel\Passport\Passport;+use Log;+use Tests\TestCase;++/**+ *+ * Class CategoryControllerTest+ */+class CategoryControllerTest extends TestCase+{+ /**+ *+ */+ public function setUp(): void+ {+ parent::setUp();+ Passport::actingAs($this->user());+ Log::debug(sprintf('Now in %s.', \get_class($this)));+ }++ /**+ * Delete a category.+ *+ * @covers \FireflyIII\Api\V1\Controllers\CategoryController+ */+ public function testDelete(): void+ {+ // mock stuff:+ $repository = $this->mock(CategoryRepositoryInterface::class);++ // mock calls:+ $repository->shouldReceive('setUser')->once();+ $repository->shouldReceive('destroy')->once()->andReturn(true);++ // get category:+ $category = $this->user()->categories()->first();++ // call API+ $response = $this->delete('/api/v1/categories/' . $category->id);+ $response->assertStatus(204);+ }++ /**+ * Show all categories+ *+ * @covers \FireflyIII\Api\V1\Controllers\CategoryController+ */+ public function testIndex(): void+ {+ $categories = $this->user()->categories()->get();+ // mock stuff:+ $repository = $this->mock(CategoryRepositoryInterface::class);++ // mock calls:+ $repository->shouldReceive('setUser')->once();+ $repository->shouldReceive('getCategories')->once()->andReturn($categories);++ // call API+ $response = $this->get('/api/v1/categories');+ $response->assertStatus(200);+ $response->assertSee($categories->first()->name);+ }++ /**+ * Show a single category.+ *+ * @covers \FireflyIII\Api\V1\Controllers\CategoryController+ */+ public function testShow(): void+ {+ $category = $this->user()->categories()->first();+ // mock stuff:+ $repository = $this->mock(CategoryRepositoryInterface::class);++ // mock calls:+ $repository->shouldReceive('setUser')->once();++ // call API+ $response = $this->get('/api/v1/categories/' . $category->id);+ $response->assertStatus(200);+ $response->assertSee($category->name);+ }++ /**+ * Store a new category.+ *+ * @covers \FireflyIII\Api\V1\Controllers\CategoryController+ */+ public function testStore(): void+ {+ /** @var Budget $category */+ $category = $this->user()->categories()->first();++ // mock stuff:+ $repository = $this->mock(CategoryRepositoryInterface::class);++ // mock calls:+ $repository->shouldReceive('setUser')->once();+ $repository->shouldReceive('store')->once()->andReturn($category);++ // data to submit+ $data = [+ 'name' => 'Some category',+ 'active' => '1',+ ];++ // test API+ $response = $this->post('/api/v1/categories', $data);+ $response->assertStatus(200);+ $response->assertJson(['data' => ['type' => 'categories', 'links' => true],]);+ $response->assertSee($category->name); // the amount+ $response->assertHeader('Content-Type', 'application/vnd.api+json');+ }++ /**+ * Update a category.+ *+ * @covers \FireflyIII\Api\V1\Controllers\CategoryController+ */+ public function testUpdate(): void+ {+ // mock repositories+ $repository = $this->mock(CategoryRepositoryInterface::class);++ /** @var Budget $category */+ $category = $this->user()->categories()->first();++ // mock calls:+ $repository->shouldReceive('setUser');+ $repository->shouldReceive('update')->once()->andReturn($category);++ // data to submit+ $data = [+ 'name' => 'Some new category',+ 'active' => '1',+ ];++ // test API+ $response = $this->put('/api/v1/categories/' . $category->id, $data, ['Accept' => 'application/json']);+ $response->assertStatus(200);+ $response->assertJson(['data' => ['type' => 'categories', 'links' => true],]);+ $response->assertHeader('Content-Type', 'application/vnd.api+json');+ $response->assertSee($category->name);+ }+++}\ No newline at end of filediff --git a/tests/Api/V1/Controllers/ConfigurationControllerTest.php b/tests/Api/V1/Controllers/ConfigurationControllerTest.phpnew file mode 100644index 0000000000..ab958a1f2f--- /dev/null+++ b/tests/Api/V1/Controllers/ConfigurationControllerTest.php@@ -0,0 +1,185 @@+<?php+/**+ * ConfigurationControllerTest.php+ * Copyright (c) 2018 [email protected]+ *+ * This file is part of Firefly III.+ *+ * Firefly III is free software: you can redistribute it and/or modify+ * it under the terms of the GNU General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * Firefly III 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 General Public License for more details.+ *+ * You should have received a copy of the GNU General Public License+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.+ */++declare(strict_types=1);++namespace Tests\Api\V1\Controllers;++use FireflyConfig;+use FireflyIII\Models\Configuration;+use Laravel\Passport\Passport;+use Log;+use Tests\TestCase;++/**+ *+ * Class ConfigurationControllerTest+ */+class ConfigurationControllerTest extends TestCase+{+ /**+ *+ */+ public function setUp(): void+ {+ parent::setUp();+ Passport::actingAs($this->user());+ Log::debug(sprintf('Now in %s.', \get_class($this)));+ }++ /**+ * Get configuration variables.+ *+ * @covers \FireflyIII\Api\V1\Controllers\ConfigurationController+ */+ public function testIndex(): void+ {+ $demoConfig = new Configuration;+ $demoConfig->name = 'is_demo_site';+ $demoConfig->data = false;++ $permConfig = new Configuration;+ $permConfig->name = 'permission_update_check';+ $permConfig->data = -1;++ $lastConfig = new Configuration;+ $lastConfig->name = 'last_update_check';+ $lastConfig->data = 123456789;++ $singleConfig = new Configuration;+ $singleConfig->name = 'single_user_mode';+ $singleConfig->data = true;++ FireflyConfig::shouldReceive('get')->withArgs(['is_demo_site'])->andReturn($demoConfig)->once();+ FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check'])->andReturn($permConfig)->once();+ FireflyConfig::shouldReceive('get')->withArgs(['last_update_check'])->andReturn($lastConfig)->once();+ FireflyConfig::shouldReceive('get')->withArgs(['single_user_mode'])->andReturn($singleConfig)->once();++ $expected = [+ 'data' => [+ 'is_demo_site' => false,+ 'permission_update_check' => -1,+ 'last_update_check' => 123456789,+ 'single_user_mode' => true,+ ],+ ];++ $response = $this->get('/api/v1/configuration');+ $response->assertStatus(200);+ $response->assertExactJson($expected);+ $response->assertHeader('Content-Type', 'application/vnd.api+json');+ }+++ /**+ * Get configuration variables.+ *+ * @covers \FireflyIII\Api\V1\Controllers\ConfigurationController+ */+ public function testIndexNotOwner(): void+ {+ Passport::actingAs($this->emptyUser());+ $response = $this->get('/api/v1/configuration');+ $response->assertStatus(500);+ $response->assertSee('No access to method.');+ }++ /**+ * Set configuration variables.+ *+ * @covers \FireflyIII\Api\V1\Controllers\ConfigurationController+ */+ public function testUpdate(): void+ {+ $data = [+ 'name' => 'permission_update_check',+ 'value' => 1,++ ];++ $demoConfig = new Configuration;+ $demoConfig->name = 'is_demo_site';+ $demoConfig->data = false;++ $permConfig = new Configuration;+ $permConfig->name = 'permission_update_check';+ $permConfig->data = -1;++ $lastConfig = new Configuration;+ $lastConfig->name = 'last_update_check';+ $lastConfig->data = 123456789;++ $singleConfig = new Configuration;+ $singleConfig->name = 'single_user_mode';+ $singleConfig->data = true;++ FireflyConfig::shouldReceive('get')->withArgs(['is_demo_site'])->andReturn($demoConfig)->once();+ FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check'])->andReturn($permConfig)->once();+ FireflyConfig::shouldReceive('get')->withArgs(['last_update_check'])->andReturn($lastConfig)->once();+ FireflyConfig::shouldReceive('get')->withArgs(['single_user_mode'])->andReturn($singleConfig)->once();+ FireflyConfig::shouldReceive('set')->once()->withArgs(['permission_update_check',1]);+++ $expected = [+ 'data' => [+ 'is_demo_site' => false,+ 'permission_update_check' => -1,+ 'last_update_check' => 123456789,+ 'single_user_mode' => true,+ ],+ ];++ $response = $this->post('/api/v1/configuration', $data);+ $response->assertStatus(200);+ $response->assertExactJson($expected);+ }++ /**+ * Set configuration variable that you're not allowed to change+ *+ * @covers \FireflyIII\Api\V1\Controllers\ConfigurationController+ */+ public function testUpdateInvalid(): void+ {+ $data = [+ 'name' => 'last_update_check',+ 'value' => 'true',+ ];+ $response = $this->post('/api/v1/configuration', $data);+ $response->assertStatus(500);+ $response->assertSee('You cannot edit this configuration value.');+ }++ /**+ * Set configuration variables but you're not the owner.+ *+ * @covers \FireflyIII\Api\V1\Controllers\ConfigurationController+ */+ public function testUpdateNotOwner(): void+ {+ Passport::actingAs($this->emptyUser());+ $response = $this->post('/api/v1/configuration');+ $response->assertStatus(500);+ $response->assertSee('No access to method.');+ }+++}\ No newline at end of file