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

238 lines
9.6 KiB
PHP
Raw Normal View History

2018-07-01 11:58:41 -05:00
<?php
/**
* AvailableBudgetControllerTest.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 Preferences;
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;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
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());
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-06-09 02:41:23 -05:00
$repository = $this->mock(BudgetRepositoryInterface::class);
$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:
2018-12-16 06:55:19 -06:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-07-01 11:58:41 -05:00
$repository->shouldReceive('setAvailableBudget')->once()->andReturn($availableBudget);
// 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
$repository = $this->mock(BudgetRepositoryInterface::class);
$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([0, ''])->once()->andReturnNull();
Amount::shouldReceive('getDefaultCurrency')->once()->andReturn(TransactionCurrency::find(5));
// mock calls:
2018-12-16 06:55:19 -06:00
$repository->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setAvailableBudget')->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-06-09 02:41:23 -05:00
$repository = $this->mock(BudgetRepositoryInterface::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:
$repository->shouldReceive('setUser')->once();
2018-09-02 13:27:26 -05:00
$repository->shouldReceive('setAvailableBudget')->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
$repository = $this->mock(BudgetRepositoryInterface::class);
$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);
$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:
$repository->shouldReceive('setUser');
$repository->shouldReceive('updateAvailableBudget')->once()->andReturn($availableBudget);
$currencyRepository->shouldReceive('findNull')->andReturn(TransactionCurrency::find(1));
// 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
}