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

240 lines
9.6 KiB
PHP
Raw Normal View History

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