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

238 lines
9.7 KiB
PHP
Raw Normal View History

2018-07-01 11:58:41 -05:00
<?php
/**
* AvailableBudgetControllerTest.php
2020-02-16 06:59:55 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-01 11:58:41 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-01 11:58:41 -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-07-01 11:58:41 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-07-01 11:58:41 -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-07-01 11:58:41 -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-07-01 11:58:41 -05:00
*/
declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
2018-12-21 09:38:10 -06:00
use Amount;
use FireflyIII\Factory\TransactionCurrencyFactory;
2018-07-01 11:58:41 -05:00
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\TransactionCurrency;
2019-09-04 10:39:39 -05:00
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
2018-07-01 11:58:41 -05:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2018-12-16 06:55:19 -06:00
use FireflyIII\Transformers\AvailableBudgetTransformer;
2018-07-01 11:58:41 -05:00
use Laravel\Passport\Passport;
use Log;
use Tests\TestCase;
/**
*
* Class AvailableBudgetControllerTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-07-01 11:58:41 -05:00
*/
class AvailableBudgetControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
2020-03-15 09:32:09 -05:00
$this->mockDefaultConfiguration();
2019-04-09 13:05:20 -05:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-07-01 11:58:41 -05:00
}
/**
* Store new available budget.
*
* @covers \FireflyIII\Api\V1\Controllers\AvailableBudgetController
*/
public function testStore(): void
{
Log::info(sprintf('Now in test %s.', __METHOD__));
2019-09-04 10:39:39 -05:00
$abRepository = $this->mock(AvailableBudgetRepositoryInterface::class);
2019-06-09 02:41:23 -05:00
$transformer = $this->mock(AvailableBudgetTransformer::class);
$factory = $this->mock(TransactionCurrencyFactory::class);
$availableBudget = new AvailableBudget;
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-12-21 09:38:10 -06:00
$factory->shouldReceive('find')->withArgs([2, ''])->once()->andReturn(TransactionCurrency::find(2));
2018-07-01 11:58:41 -05:00
// mock calls:
2019-09-04 10:39:39 -05:00
$abRepository->shouldReceive('setUser')->atLeast()->once();
$abRepository->shouldReceive('store')->once()->andReturn($availableBudget);
2018-07-01 11:58:41 -05:00
// data to submit
$data = [
2018-12-21 09:38:10 -06:00
'currency_id' => '2',
2018-08-16 23:45:57 -05:00
'amount' => '100',
2018-12-16 06:55:19 -06:00
'start' => '2018-01-01',
'end' => '2018-01-31',
2018-07-01 11:58:41 -05:00
];
// test API
2018-12-16 06:55:19 -06:00
$response = $this->post(route('api.v1.available_budgets.store'), $data);
2018-07-01 11:58:41 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'available_budgets', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Store new available budget without a valid currency.
*
* @covers \FireflyIII\Api\V1\Controllers\AvailableBudgetController
*/
2018-09-02 13:27:26 -05:00
public function testStoreNoCurrencyAtAll(): void
{
Log::info(sprintf('Now in test %s.', __METHOD__));
// mock stuff:
2019-06-09 02:41:23 -05:00
$transformer = $this->mock(AvailableBudgetTransformer::class);
$factory = $this->mock(TransactionCurrencyFactory::class);
2019-09-04 10:39:39 -05:00
$abRepository = $this->mock(AvailableBudgetRepositoryInterface::class);
2019-06-09 02:41:23 -05:00
$availableBudget = new AvailableBudget;
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-12-21 09:38:10 -06:00
$factory->shouldReceive('find')->withArgs([0, ''])->once()->andReturnNull();
Amount::shouldReceive('getDefaultCurrency')->once()->andReturn(TransactionCurrency::find(5));
// mock calls:
2019-09-04 10:39:39 -05:00
$abRepository->shouldReceive('setUser')->atLeast()->once();
$abRepository->shouldReceive('store')->once()->andReturn($availableBudget);
// data to submit
$data = [
2018-12-21 09:38:10 -06:00
'amount' => '100',
'start' => '2018-01-01',
'end' => '2018-01-31',
];
// test API
2018-12-16 06:55:19 -06:00
$response = $this->post(route('api.v1.available_budgets.store'), $data);
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
2018-09-02 13:27:26 -05:00
/**
* Store new available budget without a valid currency.
*
* @covers \FireflyIII\Api\V1\Controllers\AvailableBudgetController
*/
2018-09-02 13:27:26 -05:00
public function testStoreNoCurrencyId(): void
{
Log::info(sprintf('Now in test %s.', __METHOD__));
2018-09-02 13:27:26 -05:00
/** @var AvailableBudget $availableBudget */
$availableBudget = $this->user()->availableBudgets()->first();
// mock stuff:
2019-09-04 10:39:39 -05:00
$abRepository = $this->mock(AvailableBudgetRepositoryInterface::class);
$transformer = $this->mock(AvailableBudgetTransformer::class);
$factory = $this->mock(TransactionCurrencyFactory::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-12-21 09:38:10 -06:00
$factory->shouldReceive('find')->withArgs([0, 'EUR'])->once()->andReturnNull();
Amount::shouldReceive('getDefaultCurrency')->once()->andReturn(TransactionCurrency::find(5));
// mock calls:
2019-09-04 10:39:39 -05:00
$abRepository->shouldReceive('setUser')->once();
$abRepository->shouldReceive('store')->once()->andReturn($availableBudget);
// data to submit
$data = [
'currency_code' => 'EUR',
'amount' => '100',
2018-12-16 06:55:19 -06:00
'start' => '2018-01-01',
'end' => '2018-01-31',
];
// test API
2018-12-16 06:55:19 -06:00
$response = $this->post(route('api.v1.available_budgets.store'), $data);
2018-09-02 13:27:26 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'available_budgets', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
2018-07-01 11:58:41 -05:00
/**
* Update available budget.
*
* @covers \FireflyIII\Api\V1\Controllers\AvailableBudgetController
*
*/
public function testUpdate(): void
{
Log::info(sprintf('Now in test %s.', __METHOD__));
2018-07-01 11:58:41 -05:00
// mock repositories
2019-09-04 10:39:39 -05:00
$abRepository = $this->mock(AvailableBudgetRepositoryInterface::class);
2018-07-01 11:58:41 -05:00
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(AvailableBudgetTransformer::class);
2018-12-21 09:38:10 -06:00
$factory = $this->mock(TransactionCurrencyFactory::class);
2019-09-04 10:39:39 -05:00
$euro = $this->getEuro();
// mock facades:
Amount::shouldReceive('getDefaultCurrency')->atLeast()->once()->andReturn($euro);
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-07-01 11:58:41 -05:00
2018-12-21 09:38:10 -06:00
$factory->shouldReceive('find')->withArgs([1, ''])->once()->andReturnNull();
2018-07-01 11:58:41 -05:00
/** @var AvailableBudget $availableBudget */
$availableBudget = $this->user()->availableBudgets()->first();
// mock calls:
2019-09-04 10:39:39 -05:00
$abRepository->shouldReceive('setUser');
$abRepository->shouldReceive('updateAvailableBudget')->once()->andReturn($availableBudget);
2019-07-27 06:54:06 -05:00
$currencyRepository->shouldReceive('findNull')->andReturn($this->getEuro());
2018-07-01 11:58:41 -05:00
// data to submit
$data = [
2018-08-16 23:45:57 -05:00
'currency_id' => '1',
'amount' => '100',
2018-12-21 09:38:10 -06:00
'start' => '2018-01-01',
'end' => '2018-01-31',
2018-07-01 11:58:41 -05:00
];
// test API
2018-12-16 06:55:19 -06:00
$response = $this->put(route('api.v1.available_budgets.update', $availableBudget->id), $data, ['Accept' => 'application/json']);
2018-07-01 11:58:41 -05:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'available_budgets', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
2018-07-22 13:33:17 -05:00
}