firefly-iii/tests/Api/V1/Controllers/CurrencyControllerTest.php

240 lines
9.7 KiB
PHP
Raw Normal View History

2018-04-15 12:20:24 -05:00
<?php
/**
* CurrencyControllerTest.php
2020-02-16 06:59:55 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-04-15 12:20:24 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-04-15 12:20:24 -05:00
*
* 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.
2018-04-15 12:20:24 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-04-15 12:20:24 -05:00
* 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.
2018-04-15 12:20:24 -05:00
*
* 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/>.
2018-04-15 12:20:24 -05:00
*/
declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
2019-06-29 12:47:31 -05:00
2018-04-15 12:20:24 -05:00
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2019-06-13 08:48:35 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-12-16 06:55:19 -06:00
use FireflyIII\Transformers\CurrencyTransformer;
2018-04-15 12:20:24 -05:00
use Laravel\Passport\Passport;
use Log;
use Preferences;
use Tests\TestCase;
use Amount;
2018-04-15 12:20:24 -05:00
/**
* Class CurrencyControllerTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-04-15 12:20:24 -05:00
*/
class CurrencyControllerTest extends TestCase
{
/**
*
*/
2018-07-02 09:06:49 -05:00
public function setUp(): void
2018-04-15 12:20:24 -05:00
{
parent::setUp();
Passport::actingAs($this->user());
2019-04-09 13:05:20 -05:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-04-15 12:20:24 -05:00
}
/**
2018-07-01 02:27:22 -05:00
* Store new currency.
*
2018-04-15 12:20:24 -05:00
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testStore(): void
{
2019-07-27 06:54:06 -05:00
$currency = $this->getEuro();
2019-06-13 11:07:49 -05:00
$repository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
$this->mock(UserRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
// 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]);
2018-04-15 12:20:24 -05:00
// mock facades.
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn($currency);
2019-07-31 09:53:09 -05:00
Preferences::shouldReceive('mark');
2018-04-15 12:20:24 -05:00
// 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,
2018-07-12 23:12:39 -05:00
'default' => '0',
2018-11-10 08:15:29 -06:00
'enabled' => '1',
2018-04-15 12:20:24 -05:00
];
// test API
$response = $this->post(route('api.v1.currencies.store'), $data, ['Accept' => 'application/json']);
2018-04-15 12:20:24 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
2018-07-01 02:27:22 -05:00
* Store new currency and make it default.
*
2018-04-15 12:20:24 -05:00
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testStoreWithDefault(): void
{
2019-07-27 06:54:06 -05:00
$currency = $this->getEuro();
2019-06-13 11:07:49 -05:00
$repository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
2019-06-13 08:48:35 -05:00
$userRepository = $this->mock(UserRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
2019-07-31 09:53:09 -05:00
// mock facades.
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn($currency);
2018-12-16 06:55:19 -06:00
// 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]);
2018-04-15 12:20:24 -05:00
$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();
2019-07-31 09:53:09 -05:00
//Preferences::shouldReceive('lastActivity')->once();
//Preferences::shouldReceive('getForUser')->once()->andReturn($preference);
2018-04-15 12:20:24 -05:00
// data to submit:
$data = [
'name' => 'New currency',
'code' => 'ABC',
'symbol' => 'A',
'decimal_places' => 2,
2018-07-12 23:12:39 -05:00
'default' => '1',
2018-11-10 08:15:29 -06:00
'enabled' => '1',
2018-04-15 12:20:24 -05:00
];
// test API
$response = $this->post(route('api.v1.currencies.store'), $data, ['Accept' => 'application/json']);
2018-04-15 12:20:24 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
2018-07-01 02:27:22 -05:00
* Update currency.
*
2018-04-15 12:20:24 -05:00
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testUpdate(): void
{
2019-07-27 06:54:06 -05:00
$currency = $this->getEuro();
2018-12-16 06:55:19 -06:00
$repository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
2019-06-13 08:48:35 -05:00
$this->mock(UserRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
// 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]);
2019-07-31 09:53:09 -05:00
Preferences::shouldReceive('mark');
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn($currency);
2018-04-15 12:20:24 -05:00
// 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',
2018-07-12 23:12:39 -05:00
'default' => '0',
2018-11-10 08:15:29 -06:00
'enabled' => '1',
2018-04-15 12:20:24 -05:00
];
// test API
2019-06-09 03:27:11 -05:00
$response = $this->put(route('api.v1.currencies.update', [$currency->code]), $data, ['Accept' => 'application/json']);
2018-04-15 12:20:24 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
2018-07-01 02:27:22 -05:00
* Update currency and make default.
*
2018-04-15 12:20:24 -05:00
* @covers \FireflyIII\Api\V1\Controllers\CurrencyController
*/
public function testUpdateWithDefault(): void
{
2019-07-27 06:54:06 -05:00
$currency = $this->getEuro();
2019-06-13 11:07:49 -05:00
$repository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(CurrencyTransformer::class);
2019-06-13 08:48:35 -05:00
$this->mock(UserRepositoryInterface::class);
2018-04-15 12:20:24 -05:00
$preference = new Preference;
$preference->data = 'EUR';
2018-12-16 06:55:19 -06:00
// 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]);
2018-04-15 12:20:24 -05:00
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('update')->andReturn($currency);
Preferences::shouldReceive('set')->withArgs(['currencyPreference', 'EUR'])->once();
Preferences::shouldReceive('mark')->once();
2019-07-31 09:53:09 -05:00
//Preferences::shouldReceive('lastActivity')->once();
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn($currency);
//Preferences::shouldReceive('getForUser')->once()->andReturn($preference);
2018-04-15 12:20:24 -05:00
// data to submit:
$data = [
'name' => 'Updated currency',
'code' => 'ABC',
'symbol' => '$E',
'decimal_places' => '2',
2018-07-12 23:12:39 -05:00
'default' => '1',
2018-11-10 08:15:29 -06:00
'enabled' => '1',
2018-04-15 12:20:24 -05:00
];
// test API
2019-06-09 03:27:11 -05:00
$response = $this->put(route('api.v1.currencies.update', [$currency->code]), $data, ['Accept' => 'application/json']);
2018-04-15 12:20:24 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
2018-05-05 09:51:32 -05:00
}