mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-24 04:41:01 +00:00
Improve API and test coverage.
This commit is contained in:
@@ -50,8 +50,7 @@ class BillControllerTest extends TestCase
|
||||
/**
|
||||
* Send delete
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\BillController::delete
|
||||
* @covers \FireflyIII\Api\V1\Controllers\BillController::__construct
|
||||
* @covers \FireflyIII\Api\V1\Controllers\BillController
|
||||
*/
|
||||
public function testDelete()
|
||||
{
|
||||
|
||||
267
tests/Api/V1/Controllers/CurrencyControllerTest.php
Normal file
267
tests/Api/V1/Controllers/CurrencyControllerTest.php
Normal file
@@ -0,0 +1,267 @@
|
||||
<?php
|
||||
/**
|
||||
* CurrencyControllerTest.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\Preference;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
use Laravel\Passport\Passport;
|
||||
use Log;
|
||||
use Mockery;
|
||||
use Preferences;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class CurrencyControllerTest
|
||||
*/
|
||||
class CurrencyControllerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
Passport::actingAs($this->user());
|
||||
Log::debug(sprintf('Now in %s.', get_class($this)));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Send delete
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
|
||||
*/
|
||||
public function testDelete(): void
|
||||
{
|
||||
// mock stuff:
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
//$userRepos->shouldReceive('setUser')->once();
|
||||
|
||||
$userRepos->shouldReceive('hasRole')->once()->withArgs([Mockery::any(), 'owner'])->andReturn(true);
|
||||
$repository->shouldReceive('canDeleteCurrency')->once()->andReturn(true);
|
||||
|
||||
$repository->shouldReceive('destroy')->once()->andReturn(true);
|
||||
|
||||
// get a currency
|
||||
$currency = TransactionCurrency::first();
|
||||
|
||||
// call API
|
||||
$response = $this->delete('/api/v1/currencies/' . $currency->id);
|
||||
$response->assertStatus(204);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show index.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
|
||||
*/
|
||||
public function testIndex(): void
|
||||
{
|
||||
$collection = TransactionCurrency::get();
|
||||
// mock stuff:
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('get')->withNoArgs()->andReturn($collection)->once();
|
||||
|
||||
// test API
|
||||
$response = $this->get('/api/v1/currencies');
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['data' => [],]);
|
||||
$response->assertJson(
|
||||
['meta' => ['pagination' => ['total' => $collection->count(), 'count' => $collection->count(), 'per_page' => 50, 'current_page' => 1,
|
||||
'total_pages' => 1]],]
|
||||
);
|
||||
$response->assertJson(
|
||||
['links' => ['self' => true, 'first' => true, 'last' => true,],]
|
||||
);
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test show of a currency.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
|
||||
*/
|
||||
public function testShow(): void
|
||||
{
|
||||
// create stuff
|
||||
$currency = TransactionCurrency::first();
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
|
||||
// test API
|
||||
$response = $this->get('/api/v1/currencies/' . $currency->id);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(
|
||||
['data' => [
|
||||
'type' => 'currencies',
|
||||
'id' => $currency->id,
|
||||
],]
|
||||
);
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
|
||||
* @covers \FireflyIII\Api\V1\Requests\CurrencyRequest
|
||||
*/
|
||||
public function testStore(): void
|
||||
{
|
||||
|
||||
$currency = TransactionCurrency::first();
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('store')->andReturn($currency);
|
||||
|
||||
// data to submit:
|
||||
$data = [
|
||||
'name' => 'New currency',
|
||||
'code' => 'ABC',
|
||||
'symbol' => 'A',
|
||||
'decimal_places' => 2,
|
||||
'default' => 'false',
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->post('/api/v1/currencies', $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]);
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
$response->assertSee($currency->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
|
||||
* @covers \FireflyIII\Api\V1\Requests\CurrencyRequest
|
||||
*/
|
||||
public function testStoreWithDefault(): void
|
||||
{
|
||||
$currency = TransactionCurrency::first();
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$preference = new Preference;
|
||||
$preference->data = 'EUR';
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('store')->andReturn($currency);
|
||||
Preferences::shouldReceive('set')->withArgs(['currencyPreference', 'EUR'])->once();
|
||||
Preferences::shouldReceive('mark')->once();
|
||||
Preferences::shouldReceive('lastActivity')->once();
|
||||
Preferences::shouldReceive('getForUser')->once()->andReturn($preference);
|
||||
|
||||
// data to submit:
|
||||
$data = [
|
||||
'name' => 'New currency',
|
||||
'code' => 'ABC',
|
||||
'symbol' => 'A',
|
||||
'decimal_places' => 2,
|
||||
'default' => 'true',
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->post('/api/v1/currencies', $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]);
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
$response->assertSee($currency->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
|
||||
* @covers \FireflyIII\Api\V1\Requests\CurrencyRequest
|
||||
*/
|
||||
public function testUpdate(): void
|
||||
{
|
||||
$currency = TransactionCurrency::first();
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('update')->andReturn($currency);
|
||||
|
||||
// data to submit:
|
||||
$data = [
|
||||
'name' => 'Updated currency',
|
||||
'code' => 'ABC',
|
||||
'symbol' => '$E',
|
||||
'decimal_places' => '2',
|
||||
'default' => 'false',
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->put('/api/v1/currencies/' . $currency->id, $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]);
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
$response->assertSee($currency->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
|
||||
* @covers \FireflyIII\Api\V1\Requests\CurrencyRequest
|
||||
*/
|
||||
public function testUpdateWithDefault(): void
|
||||
{
|
||||
$currency = TransactionCurrency::first();
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$preference = new Preference;
|
||||
$preference->data = 'EUR';
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('update')->andReturn($currency);
|
||||
Preferences::shouldReceive('set')->withArgs(['currencyPreference', 'EUR'])->once();
|
||||
Preferences::shouldReceive('mark')->once();
|
||||
Preferences::shouldReceive('lastActivity')->once();
|
||||
Preferences::shouldReceive('getForUser')->once()->andReturn($preference);
|
||||
|
||||
// data to submit:
|
||||
$data = [
|
||||
'name' => 'Updated currency',
|
||||
'code' => 'ABC',
|
||||
'symbol' => '$E',
|
||||
'decimal_places' => '2',
|
||||
'default' => 'true',
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->put('/api/v1/currencies/' . $currency->id, $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]);
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
$response->assertSee($currency->name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user