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

190 lines
6.6 KiB
PHP
Raw Normal View History

2018-02-18 03:31:15 -06:00
<?php
/**
* BillControllerTest.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 Exception;
2018-02-18 03:31:15 -06:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2018-12-16 06:55:19 -06:00
use FireflyIII\Transformers\BillTransformer;
2018-02-18 03:31:15 -06:00
use Laravel\Passport\Passport;
2018-02-28 13:18:47 -06:00
use Log;
2018-02-18 03:31:15 -06:00
use Tests\TestCase;
/**
* Class BillControllerTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-02-18 03:31:15 -06:00
*/
class BillControllerTest extends TestCase
{
/**
*
*/
2018-09-02 13:27:26 -05:00
public function setUp(): void
2018-02-18 03:31:15 -06: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-02-18 03:31:15 -06:00
}
/**
2018-07-01 02:27:22 -05:00
* Store with minimum amount more than maximum amount
2018-08-06 12:14:30 -05:00
*
2018-07-01 02:27:22 -05:00
* @covers \FireflyIII\Api\V1\Controllers\BillController
* @throws Exception
2018-02-18 03:31:15 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testStoreMinOverMax(): void
2018-02-18 03:31:15 -06:00
{
// create stuff
2019-06-09 02:41:23 -05:00
$bill = $this->user()->bills()->first();
$repository = $this->mock(BillRepositoryInterface::class);
2018-02-18 03:31:15 -06:00
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('store')->andReturn($bill);
// data to submit:
$data = [
2019-06-16 06:16:46 -05:00
'name' => 'New bill #' . $this->randomInt(),
'match' => 'some,words,' . $this->randomInt(),
2018-02-18 03:31:15 -06:00
'amount_min' => '66.34',
'amount_max' => '45.67',
'date' => '2018-01-01',
2018-04-27 04:29:09 -05:00
'currency_id' => 1,
2018-02-18 03:31:15 -06:00
'repeat_freq' => 'monthly',
'skip' => 0,
'automatch' => 1,
'active' => 1,
];
// test API
2018-12-16 06:55:19 -06:00
$response = $this->post(route('api.v1.bills.store'), $data, ['Accept' => 'application/json']);
2018-02-18 03:31:15 -06:00
$response->assertStatus(422);
$response->assertExactJson(
[
'message' => 'The given data was invalid.',
'errors' => [
'amount_min' => ['The minimum amount cannot be larger than the maximum amount.'],
],
]
);
$response->assertHeader('Content-Type', 'application/json');
}
/**
2018-07-01 02:27:22 -05:00
* Store a valid bill
*
* @covers \FireflyIII\Api\V1\Controllers\BillController
* @throws Exception
2018-02-18 03:31:15 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testStoreValid(): void
2018-02-18 03:31:15 -06:00
{
// create stuff
2018-12-16 06:55:19 -06:00
$bill = $this->user()->bills()->first();
$repository = $this->mock(BillRepositoryInterface::class);
$transformer = $this->mock(BillTransformer::class);
// 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-02-18 03:31:15 -06:00
// mock calls:
2018-12-16 06:55:19 -06:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-18 03:31:15 -06:00
$repository->shouldReceive('store')->andReturn($bill);
2018-12-16 06:55:19 -06:00
2018-02-18 03:31:15 -06:00
// data to submit:
$data = [
2019-06-16 06:16:46 -05:00
'name' => 'New bill #' . $this->randomInt(),
'match' => 'some,words,' . $this->randomInt(),
2018-02-18 03:31:15 -06:00
'amount_min' => '12.34',
'amount_max' => '45.67',
'date' => '2018-01-01',
'repeat_freq' => 'monthly',
'skip' => 0,
'automatch' => 1,
'active' => 1,
2018-04-27 04:29:09 -05:00
'currency_id' => 1,
2018-02-18 03:31:15 -06:00
];
// test API
2018-12-16 06:55:19 -06:00
$response = $this->post(route('api.v1.bills.store'), $data, ['Accept' => 'application/json']);
2018-02-18 03:31:15 -06:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'bills', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
2018-12-10 14:45:44 -06:00
2018-02-18 03:31:15 -06:00
/**
2018-07-01 02:27:22 -05:00
* Update a valid bill.
*
* @covers \FireflyIII\Api\V1\Controllers\BillController
* @throws Exception
2018-02-18 03:31:15 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testUpdateValid(): void
2018-02-18 03:31:15 -06:00
{
// create stuff
2018-12-16 06:55:19 -06:00
$bill = $this->user()->bills()->first();
$repository = $this->mock(BillRepositoryInterface::class);
$transformer = $this->mock(BillTransformer::class);
2018-02-18 03:31:15 -06:00
// mock calls:
2018-12-16 06:55:19 -06:00
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-18 03:31:15 -06:00
$repository->shouldReceive('update')->andReturn($bill);
// data to submit:
$data = [
2019-06-16 06:16:46 -05:00
'name' => 'New bill #' . $this->randomInt(),
'match' => 'some,words,' . $this->randomInt(),
2018-02-18 03:31:15 -06:00
'amount_min' => '12.34',
'amount_max' => '45.67',
'date' => '2018-01-01',
'repeat_freq' => 'monthly',
'skip' => 0,
'automatch' => 1,
'active' => 1,
2018-04-27 04:29:09 -05:00
'currency_id' => 1,
2018-02-18 03:31:15 -06:00
];
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-02-18 03:31:15 -06:00
// test API
2018-12-16 06:55:19 -06:00
$response = $this->put(route('api.v1.bills.update', [$bill->id]), $data, ['Accept' => 'application/json']);
2018-02-18 03:31:15 -06:00
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'bills', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
2018-03-04 08:14:29 -06:00
}