Tests for API controllers

This commit is contained in:
James Cole 2018-02-17 14:46:12 +01:00
parent 632d50a0d0
commit ecd4a862ff
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,78 @@
<?php
/**
* AboutControllerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* 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']);
}
}

View File

@ -0,0 +1,101 @@
<?php
/**
* AccountControllerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* 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');
}
}