Skip to content

Firefly III: a personal finances manager

Tests for API controllers

James Coleecd4a862ff55
diff --git a/tests/Api/V1/Controllers/AboutControllerTest.php b/tests/Api/V1/Controllers/AboutControllerTest.php
new file mode 100644
index 0000000000..5d73971fd1
--- /dev/null
+++ b/tests/Api/V1/Controllers/AboutControllerTest.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * AboutControllerTest.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\Transformers\UserTransformer;
+use Laravel\Passport\Passport;
+use Tests\TestCase;
+
+/**
+ * Class AboutControllerTest
+ */
+class AboutControllerTest extends TestCase
+{
+ /**
+ * Set up test
+ */
+ public function setUp()
+ {
+ parent::setUp();
+ Passport::actingAs($this->user());
+ }
+
+ /**
+ * Test the about endpoint
+ *
+ * @covers \FireflyIII\Api\V1\Controllers\AboutController::__construct
+ * @covers \FireflyIII\Api\V1\Controllers\AboutController::about
+ */
+ public function testAbout()
+ {
+ // test API
+ $response = $this->get('/api/v1/about');
+ $response->assertStatus(200);
+ $response->assertJson(
+ ['data' => [
+ 'version' => true,
+ 'api_version' => true,
+ 'php_version' => true,
+ ]]
+ );
+ }
+
+ /**
+ * Test user end point
+ * @covers \FireflyIII\Api\V1\Controllers\AboutController::user
+ */
+ public function testUser()
+ {
+ // test API
+ $response = $this->get('/api/v1/about/user');
+ $response->assertStatus(200);
+ $response->assertJson(['data' => ['attributes' => true, 'links' => true]]);
+ $this->assertEquals($this->user()->id, $response->json()['data']['id']);
+ }
+
+
+}
\ No newline at end of file
diff --git a/tests/Api/V1/Controllers/AccountControllerTest.php b/tests/Api/V1/Controllers/AccountControllerTest.php
new file mode 100644
index 0000000000..1dfb8489c8
--- /dev/null
+++ b/tests/Api/V1/Controllers/AccountControllerTest.php
@@ -0,0 +1,101 @@
+<?php
+/**
+ * AccountControllerTest.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\Account;
+use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
+use Laravel\Passport\Passport;
+use Tests\TestCase;
+
+/**
+ * Class AccountControllerTest
+ */
+class AccountControllerTest extends TestCase
+{
+ /**
+ *
+ */
+ public function setUp()
+ {
+ parent::setUp();
+ Passport::actingAs($this->user());
+ }
+
+ /**
+ * Destroy account over API.
+ *
+ * @covers \FireflyIII\Api\V1\Controllers\AccountController::delete
+ */
+ public function testDestroy()
+ {
+ // mock stuff:
+ $repository = $this->mock(AccountRepositoryInterface::class);
+ $currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
+ // mock calls:
+ $repository->shouldReceive('setUser')->once();
+ $repository->shouldReceive('destroy')->once()->andReturn(true);
+ $currencyRepos->shouldReceive('setUser')->once();
+
+ // get account:
+ $account = $this->user()->accounts()->first();
+
+ // call API
+ $response = $this->delete('/api/v1/accounts/' . $account->id);
+ $response->assertStatus(204);
+
+ }
+
+ /**
+ * @covers \FireflyIII\Api\V1\Controllers\AccountController::__construct
+ * @covers \FireflyIII\Api\V1\Controllers\AccountController::index
+ */
+ public function testIndex()
+ {
+ // create stuff
+ $accounts = factory(Account::class, 10)->create();
+
+ // mock stuff:
+ $repository = $this->mock(AccountRepositoryInterface::class);
+ $currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
+
+ // mock calls:
+ $repository->shouldReceive('setUser')->once();
+ $repository->shouldReceive('getAccountsByType')->withAnyArgs()->andReturn($accounts)->once();
+ $currencyRepos->shouldReceive('setUser')->once();
+
+ // test API
+ $response = $this->get('/api/v1/accounts');
+ $response->assertStatus(200);
+ $response->assertJson(['data' => [],]);
+ $response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'per_page' => 50, 'current_page' => 1, 'total_pages' => 1]],]);
+ $response->assertJson(
+ ['links' => ['self' => true, 'first' => true, 'last' => true,],]
+ );
+ $response->assertSee('type=all'); // default returns this.
+ $response->assertHeader('Content-Type', 'application/vnd.api+json');
+ }
+
+
+}
\ No newline at end of file