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

1100 lines
41 KiB
PHP
Raw Normal View History

2018-02-18 10:31:15 +01:00
<?php
/**
* TransactionControllerTest.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;
2018-09-27 06:26:03 +02:00
use Exception;
2019-04-16 16:20:46 +02:00
use FireflyIII\Events\StoredTransactionGroup;
use FireflyIII\Events\UpdatedTransactionGroup;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2018-02-18 10:31:15 +01:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2019-04-16 16:20:46 +02:00
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
use FireflyIII\Transformers\TransactionGroupTransformer;
use FireflyIII\Validation\AccountValidator;
2018-02-18 10:31:15 +01:00
use Illuminate\Support\Collection;
use Laravel\Passport\Passport;
2018-02-28 20:18:47 +01:00
use Log;
2018-02-18 10:31:15 +01:00
use Tests\TestCase;
/**
* Class TransactionControllerTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-02-18 10:31:15 +01:00
*/
class TransactionControllerTest extends TestCase
{
/**
*
*/
2018-09-02 20:13:25 +02:00
public function setUp(): void
2018-02-18 10:31:15 +01: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-02-18 10:31:15 +01:00
}
2018-02-19 20:02:27 +01:00
/**
2019-04-16 16:20:46 +02:00
* Submit empty description.
2018-02-19 20:02:27 +01:00
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
2018-02-19 20:02:27 +01:00
*/
2019-04-16 16:20:46 +02:00
public function testStoreFailDescription(): void
2018-02-19 20:02:27 +01:00
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$source = $this->getRandomAsset();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$validator = $this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-02-28 20:18:47 +01:00
$data = [
2018-02-19 20:02:27 +01:00
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'description' => '',
'date' => '2018-01-01',
'type' => 'withdrawal',
'amount' => '10',
'source_id' => $source->id,
2018-02-19 20:02:27 +01:00
],
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
2018-02-19 20:02:27 +01:00
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.description' => ['The description field is required.'],
2018-02-19 20:02:27 +01:00
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
2018-02-19 20:02:27 +01:00
]
);
}
/**
2019-04-16 16:20:46 +02:00
* Fail the valid destination information test.
2018-02-19 20:02:27 +01:00
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
2018-02-19 20:02:27 +01:00
*/
2019-04-16 16:20:46 +02:00
public function testStoreFailDestination(): void
2018-02-19 20:02:27 +01:00
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$source = $this->getRandomAsset();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$validator = $this->mock(AccountValidator::class);
2018-02-19 20:02:27 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-19 20:02:27 +01:00
2019-04-16 16:20:46 +02:00
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
/** fail destination */
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(false)
->andSet('destError', 'Some error');
2018-02-28 20:18:47 +01:00
$data = [
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'description' => 'Fails anyway',
'date' => '2018-01-01',
'type' => 'withdrawal',
'amount' => '10',
2019-04-16 16:20:46 +02:00
'source_id' => $source->id,
],
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.destination_id' => ['Some error'],
'transactions.0.destination_name' => ['Some error'],
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
]
);
}
/**
2019-04-16 16:20:46 +02:00
* Submit foreign currency info, but no foreign currency amount.
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
*/
2019-04-16 16:20:46 +02:00
public function testStoreFailForeignCurrencyAmount(): void
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$source = $this->getRandomAsset();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$validator = $this->mock(AccountValidator::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-02-28 20:18:47 +01:00
$data = [
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'description' => 'Test',
'date' => '2018-01-01',
'type' => 'withdrawal',
'amount' => '10',
'foreign_currency_code' => 'USD',
'source_id' => $source->id,
],
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.foreign_amount' => ['The content of this field is invalid without foreign amount information.'],
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
]
);
}
/**
2019-04-16 16:20:46 +02:00
* Submit foreign currency, but no foreign currency info.
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
*/
2019-04-16 16:20:46 +02:00
public function testStoreFailForeignCurrencyInfo(): void
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$source = $this->getRandomAsset();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$validator = $this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-02-28 20:18:47 +01:00
$data = [
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'description' => 'Test',
'date' => '2018-01-01',
'type' => 'withdrawal',
'amount' => '10',
'foreign_amount' => '11',
'source_id' => $source->id,
],
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.foreign_amount' => ['The content of this field is invalid without currency information.'],
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
]
);
}
/**
2019-04-16 16:20:46 +02:00
* Fail the valid source information test.
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
*/
2019-04-16 16:20:46 +02:00
public function testStoreFailSource(): void
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$source = $this->getRandomAsset();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$validator = $this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
/** source info returns FALSE **/
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(false)
->andSet('sourceError', 'Some error');
2018-02-28 20:18:47 +01:00
$data = [
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'description' => 'Some withdrawal ',
'date' => '2018-01-01',
'type' => 'withdrawal',
'amount' => '10',
2019-04-16 16:20:46 +02:00
'source_id' => $source->id,
],
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.source_id' => ['Some error'],
'transactions.0.source_name' => ['Some error'],
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
]
);
}
/**
2019-04-16 16:20:46 +02:00
* Submit multiple transactions but no group title.
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
*/
2019-04-16 16:20:46 +02:00
public function testStoreFailStoreGroupTitle(): void
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$source = $this->getRandomAsset();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$validator = $this->mock(AccountValidator::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-02-28 20:18:47 +01:00
$data = [
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'description' => 'Some description',
'date' => '2018-01-01',
'type' => 'withdrawal',
'amount' => '10',
2019-04-16 16:20:46 +02:00
'source_id' => $source->id,
],
[
'description' => 'Some description',
'date' => '2018-01-01',
'type' => 'withdrawal',
'amount' => '10',
'source_id' => $source->id,
],
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'group_title' => ['A group title is mandatory when there is more than one transaction.'],
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
]
);
}
/**
2019-04-16 16:20:46 +02:00
* Submit multiple transactions but no group title.
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
*/
2019-04-16 16:20:46 +02:00
public function testStoreFailStoreNoTransactions(): void
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
$data = [
2019-04-16 16:20:46 +02:00
'transactions' => [
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.description' => ['Need at least one transaction.', 'The description field is required.'],
'transactions.0.type' => ['Invalid transaction type.'],
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
]
);
}
/**
2019-04-16 16:20:46 +02:00
* Try to submit different transaction types for a withdrawal.
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
*/
2019-04-16 16:20:46 +02:00
public function testStoreFailTypes(): void
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$source = $this->getRandomAsset();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$validator = $this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
$validator->shouldReceive('setTransactionType')->withArgs(['deposit'])->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-02-28 20:18:47 +01:00
$data = [
2019-04-16 16:20:46 +02:00
'group_title' => 'Hi there',
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'description' => 'Some description',
'date' => '2018-01-01',
'type' => 'withdrawal',
'amount' => '10',
'source_id' => $source->id,
],
[
'description' => 'Some description',
'date' => '2018-01-01',
'type' => 'deposit',
'amount' => '10',
2019-04-16 16:20:46 +02:00
'destination_id' => $source->id,
],
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.source_id' => ['All accounts in this field must be equal.'],
'transactions.0.type' => ['All splits must be of the same type.'],
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
]
);
}
/**
2019-04-16 16:20:46 +02:00
* Try to submit different transaction types for a deposit.
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
*/
2019-04-16 16:20:46 +02:00
public function testStoreFailTypesDeposit(): void
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$source = $this->getRandomAsset();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$validator = $this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
$validator->shouldReceive('setTransactionType')->withArgs(['deposit'])->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-02-28 20:18:47 +01:00
$data = [
2019-04-16 16:20:46 +02:00
'group_title' => 'Hi there',
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'description' => 'Some description',
'date' => '2018-01-01',
'type' => 'deposit',
'amount' => '10',
2019-04-16 16:20:46 +02:00
'destination_id' => $source->id,
],
[
'description' => 'Some description',
'date' => '2018-01-01',
'type' => 'withdrawal',
'amount' => '10',
'source_id' => $source->id,
],
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.destination_id' => ['All accounts in this field must be equal.'],
'transactions.0.type' => ['All splits must be of the same type.'],
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
]
2018-02-18 20:40:32 +01:00
);
}
/**
2019-04-16 16:20:46 +02:00
* Try to submit different transaction types for a transfer.
2018-02-18 20:40:32 +01:00
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
2018-02-18 20:40:32 +01:00
*/
2019-04-16 16:20:46 +02:00
public function testStoreFailTypesTransfer(): void
2018-02-18 20:40:32 +01:00
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$source = $this->getRandomAsset();
$dest = $this->getRandomAsset($source->id);
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$validator = $this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['transfer'])->atLeast()->once();
$validator->shouldReceive('setTransactionType')->withArgs(['deposit'])->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
2018-02-28 20:18:47 +01:00
$data = [
2019-04-16 16:20:46 +02:00
'group_title' => 'Hi there',
2018-02-18 20:40:32 +01:00
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'description' => 'Some description',
'date' => '2018-01-01',
'type' => 'transfer',
'amount' => '10',
'destination_id' => $source->id,
'source_id' => $dest->id,
],
[
'description' => 'Some description',
'date' => '2018-01-01',
'type' => 'deposit',
'amount' => '10',
'destination_id' => $dest->id,
'source_id' => $source->id,
2018-02-18 20:40:32 +01:00
],
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
2018-02-18 20:40:32 +01:00
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.destination_id' => ['All accounts in this field must be equal.'],
'transactions.0.source_id' => ['All accounts in this field must be equal.'],
'transactions.0.type' => ['All splits must be of the same type.'],
2018-02-18 20:40:32 +01:00
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
2018-02-18 20:40:32 +01:00
]
);
}
/**
2019-04-16 16:20:46 +02:00
* Submit the minimum amount of data required to create a single, unsplit withdrawal.
2018-02-18 20:40:32 +01:00
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionStoreRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
2018-02-18 20:40:32 +01:00
*/
2019-04-16 16:20:46 +02:00
public function testStoreOK(): void
2018-02-18 20:40:32 +01:00
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$source = $this->getRandomAsset();
$group = $this->getRandomWithdrawalGroup();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$transformer = $this->mock(TransactionGroupTransformer::class);
$validator = $this->mock(AccountValidator::class);
$collector = $this->mock(GroupCollectorInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
$repository->shouldReceive('setUser')->atLeast()->once();
// validator:
$validator->shouldReceive('setTransactionType')->withArgs(['withdrawal'])->atLeast()->once();
$validator->shouldReceive('validateSource')->atLeast()->once()->andReturn(true);
$validator->shouldReceive('validateDestination')->atLeast()->once()->andReturn(true);
// transformer is called:
$transformer->shouldReceive('setParameters')->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->atLeast()->once();
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 1]);
$transformer->shouldReceive('getDefaultIncludes')->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->atLeast()->once()->andReturn([]);
// collector is called:
$collector->shouldReceive('setTransactionGroup')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('withAPIInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('getGroups')->atLeast()->once()->andReturn(new Collection([[]]));
// expect to store the group:
$repository->shouldReceive('store')->atLeast()->once()->andReturn($group);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// expect the event:
try {
$this->expectsEvents(StoredTransactionGroup::class);
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
2018-02-28 20:18:47 +01:00
2018-02-18 20:40:32 +01:00
2018-02-28 20:18:47 +01:00
$data = [
2018-02-18 20:40:32 +01:00
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'description' => 'Some description',
'date' => '2018-01-01',
'type' => 'withdrawal',
2018-02-18 20:40:32 +01:00
'amount' => '10',
2019-04-16 16:20:46 +02:00
'source_id' => $source->id,
2018-02-18 20:40:32 +01:00
],
],
];
// test API
$response = $this->post(route('api.v1.transactions.store'), $data, ['Accept' => 'application/json']);
2019-04-16 16:20:46 +02:00
$response->assertStatus(200);
2018-02-18 20:40:32 +01:00
$response->assertExactJson(
[
2019-04-16 16:20:46 +02:00
'data' => [
'attributes' => [],
'id' => '1',
'links' => [
'self' => 'http://localhost/api/v1/transactions/1',
2018-02-18 20:40:32 +01:00
],
2019-04-16 16:20:46 +02:00
'type' => 'transactions',
2018-02-18 20:40:32 +01:00
],
]
);
}
/**
2019-04-16 16:20:46 +02:00
* Submit a bad journal ID during update.
2018-02-18 20:40:32 +01:00
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionUpdateRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
2018-02-18 20:40:32 +01:00
*/
2019-04-16 16:20:46 +02:00
public function testUpdateFailBadJournal(): void
2018-02-18 20:40:32 +01:00
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$group = $this->getRandomWithdrawalGroup();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
$data = [
2019-04-16 16:20:46 +02:00
'group_title' => 'Empty',
2018-02-18 20:40:32 +01:00
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'order' => 0,
'transaction_journal_id' => -1,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'description' => 'Some new description',
],
[
'order' => 0,
'transaction_journal_id' => -1,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'description' => 'Some new description',
2018-02-18 20:40:32 +01:00
],
],
];
// test API
2019-04-16 16:20:46 +02:00
$response = $this->put(sprintf('/api/v1/transactions/%d', $group->id), $data, ['Accept' => 'application/json']);
2018-02-18 20:40:32 +01:00
$response->assertStatus(422);
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.source_name' => ['When updating a transaction with splits, each split must have a valid transaction journal id (field "transaction_journal_id").'],
'transactions.1.source_name' => ['When updating a transaction with splits, each split must have a valid transaction journal id (field "transaction_journal_id").'],
2018-02-18 20:40:32 +01:00
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
2018-02-18 20:40:32 +01:00
]
);
2019-04-16 16:20:46 +02:00
2018-02-18 20:40:32 +01:00
}
/**
2019-04-16 16:20:46 +02:00
* Update transaction but fail to submit equal destination accounts for a deposit.
2018-02-18 20:40:32 +01:00
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionUpdateRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
2018-02-18 20:40:32 +01:00
*/
2019-04-16 16:20:46 +02:00
public function testUpdateFailDepositDestination(): void
2018-02-18 20:40:32 +01:00
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$group = $this->getRandomWithdrawalGroup();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-18 20:40:32 +01:00
2018-02-28 20:18:47 +01:00
$data = [
2019-04-16 16:20:46 +02:00
'group_title' => 'Empty',
2018-02-18 20:40:32 +01:00
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'transaction_journal_id' => 0,
'order' => 0,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'type' => 'deposit',
'destination_name' => 'A',
'description' => 'Some new description',
],
[
'transaction_journal_id' => 0,
'order' => 0,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'type' => 'deposit',
'destination_name' => 'B',
'description' => 'Some new description',
2018-02-18 20:40:32 +01:00
],
],
];
// test API
2019-04-16 16:20:46 +02:00
$response = $this->put(sprintf('/api/v1/transactions/%d', $group->id), $data, ['Accept' => 'application/json']);
2018-02-18 20:40:32 +01:00
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.destination_id' => ['All accounts in this field must be equal.'],
2018-02-18 20:40:32 +01:00
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
2018-02-18 20:40:32 +01:00
]
);
2019-04-16 16:20:46 +02:00
$response->assertStatus(422);
2018-02-18 20:40:32 +01:00
}
/**
2019-04-16 16:20:46 +02:00
* Update transaction but fail to submit equal destination accounts for a deposit.
2018-02-18 20:40:32 +01:00
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionUpdateRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
2018-02-18 20:40:32 +01:00
*/
2019-04-16 16:20:46 +02:00
public function testUpdateFailTransferDestination(): void
2018-02-18 20:40:32 +01:00
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
return;
2019-04-16 16:20:46 +02:00
// mock data:
$group = $this->getRandomWithdrawalGroup();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
$data = [
2019-04-16 16:20:46 +02:00
'group_title' => 'Empty',
2018-02-18 20:40:32 +01:00
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'transaction_journal_id' => 0,
'order' => 0,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'type' => 'transfer',
'destination_name' => 'A',
'description' => 'Some new description',
],
[
'transaction_journal_id' => 0,
'order' => 0,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'type' => 'transfer',
'destination_name' => 'B',
'description' => 'Some new description',
2018-02-18 20:40:32 +01:00
],
],
];
// test API
2019-04-16 16:20:46 +02:00
$response = $this->put(sprintf('/api/v1/transactions/%d', $group->id), $data, ['Accept' => 'application/json']);
2018-02-18 20:40:32 +01:00
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.destination_id' => ['All accounts in this field must be equal.'],
'transactions.0.source_id' => ['All accounts in this field must be equal.'],
2018-02-18 20:40:32 +01:00
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
2018-02-18 20:40:32 +01:00
]
);
2019-04-16 16:20:46 +02:00
$response->assertStatus(422);
2018-02-18 20:40:32 +01:00
}
/**
2019-04-16 16:20:46 +02:00
* Update transaction but fail to submit equal transaction types.
2018-02-18 20:40:32 +01:00
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionUpdateRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
2018-02-18 20:40:32 +01:00
*/
2019-04-16 16:20:46 +02:00
public function testUpdateFailTypes(): void
2018-02-18 20:40:32 +01:00
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
2019-06-09 09:41:23 +02:00
2019-06-09 08:26:23 +02:00
return;
2019-04-16 16:20:46 +02:00
// mock data:
$group = $this->getRandomWithdrawalGroup();
2018-02-18 20:40:32 +01:00
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
$data = [
2019-04-16 16:20:46 +02:00
'group_title' => 'Empty',
'transactions' => [
2018-02-18 20:40:32 +01:00
[
2019-04-16 16:20:46 +02:00
'transaction_journal_id' => 0,
'order' => 0,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'type' => 'withdrawal',
'description' => 'Some new description',
],
[
'transaction_journal_id' => 0,
'order' => 0,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'type' => 'deposit',
'description' => 'Some new description',
2018-02-18 20:40:32 +01:00
],
],
];
// test API
2019-04-16 16:20:46 +02:00
$response = $this->put(sprintf('/api/v1/transactions/%d', $group->id), $data, ['Accept' => 'application/json']);
2018-02-18 20:40:32 +01:00
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.type' => ['All splits must be of the same type.'],
2018-02-18 20:40:32 +01:00
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
2018-02-18 20:40:32 +01:00
]
);
2019-04-16 16:20:46 +02:00
$response->assertStatus(422);
2018-02-18 20:40:32 +01:00
}
/**
2019-04-16 16:20:46 +02:00
* Update transaction but fail to submit equal source accounts for a withdrawal.
2018-02-18 20:40:32 +01:00
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionUpdateRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
2018-02-18 20:40:32 +01:00
*/
2019-04-16 16:20:46 +02:00
public function testUpdateFailWithdrawalSource(): void
2018-02-18 20:40:32 +01:00
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
2019-06-09 09:41:23 +02:00
2019-06-09 08:26:23 +02:00
return;
2019-04-16 16:20:46 +02:00
// mock data:
$group = $this->getRandomWithdrawalGroup();
2018-02-18 20:40:32 +01:00
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$this->mock(AccountValidator::class);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setUser')->atLeast()->once();
2018-02-28 20:18:47 +01:00
$data = [
2019-04-16 16:20:46 +02:00
'group_title' => 'Empty',
'transactions' => [
2018-02-18 20:40:32 +01:00
[
2019-04-16 16:20:46 +02:00
'transaction_journal_id' => 0,
'order' => 0,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'type' => 'withdrawal',
'source_name' => 'A',
'description' => 'Some new description',
],
[
'transaction_journal_id' => 0,
'order' => 0,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'type' => 'withdrawal',
'source_name' => 'B',
'description' => 'Some new description',
2018-02-18 20:40:32 +01:00
],
],
];
// test API
2019-04-16 16:20:46 +02:00
$response = $this->put(sprintf('/api/v1/transactions/%d', $group->id), $data, ['Accept' => 'application/json']);
2018-02-18 20:40:32 +01:00
$response->assertExactJson(
[
'errors' => [
2019-04-16 16:20:46 +02:00
'transactions.0.source_id' => ['All accounts in this field must be equal.'],
2018-02-18 20:40:32 +01:00
],
2019-04-16 16:20:46 +02:00
'message' => 'The given data was invalid.',
2018-02-18 20:40:32 +01:00
]
);
2019-04-16 16:20:46 +02:00
$response->assertStatus(422);
2018-02-18 20:40:32 +01:00
}
/**
2019-04-16 16:20:46 +02:00
* Submit the minimum amount of data to update a single withdrawal.
2018-02-18 20:40:32 +01:00
*
2019-04-16 16:20:46 +02:00
* @covers \FireflyIII\Api\V1\Requests\TransactionUpdateRequest
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Api\V1\Controllers\TransactionController
2018-02-18 20:40:32 +01:00
*/
2019-04-16 16:20:46 +02:00
public function testUpdateOK(): void
2018-02-18 20:40:32 +01:00
{
2019-06-09 08:26:23 +02:00
$this->markTestIncomplete('Needs to be rewritten for v4.8.0');
2019-06-09 09:41:23 +02:00
2019-06-09 08:26:23 +02:00
return;
2019-04-16 16:20:46 +02:00
// mock data:
$group = $this->getRandomWithdrawalGroup();
2019-04-16 16:20:46 +02:00
// mock repository
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
2018-02-28 20:18:47 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2019-04-16 16:20:46 +02:00
$transformer = $this->mock(TransactionGroupTransformer::class);
$this->mock(AccountValidator::class);
$collector = $this->mock(GroupCollectorInterface::class);
// some mock calls:
$journalRepos->shouldReceive('setUser')->atLeast()->once();
$collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
$repository->shouldReceive('setUser')->atLeast()->once();
// call stuff:
$repository->shouldReceive('update')->atLeast()->once()->andReturn($group);
// transformer is called:
$transformer->shouldReceive('setParameters')->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->atLeast()->once();
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 1]);
$transformer->shouldReceive('getDefaultIncludes')->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->atLeast()->once()->andReturn([]);
2018-02-28 20:18:47 +01:00
2019-04-16 16:20:46 +02:00
// collector is called:
$collector->shouldReceive('setTransactionGroup')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('withAPIInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('getGroups')->atLeast()->once()->andReturn(new Collection([[]]));
2018-02-28 20:18:47 +01:00
$data = [
2019-04-16 16:20:46 +02:00
'group_title' => 'Empty',
2018-02-18 20:40:32 +01:00
'transactions' => [
[
2019-04-16 16:20:46 +02:00
'order' => 0,
'reconciled' => 'false',
'tags' => [],
'interest_date' => '2019-01-01',
'description' => 'Some new description',
2018-02-18 20:40:32 +01:00
],
],
];
2019-04-16 16:20:46 +02:00
// expect the event:
try {
$this->expectsEvents(UpdatedTransactionGroup::class);
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
2018-02-18 20:40:32 +01:00
// test API
2019-04-16 16:20:46 +02:00
$response = $this->put(sprintf('/api/v1/transactions/%d', $group->id), $data, ['Accept' => 'application/json']);
2018-02-18 20:40:32 +01:00
$response->assertExactJson(
[
2019-04-16 16:20:46 +02:00
'data' => [
'attributes' => [],
'id' => '1',
'links' => [
'self' => 'http://localhost/api/v1/transactions/1',
2018-02-18 20:40:32 +01:00
],
2019-04-16 16:20:46 +02:00
'type' => 'transactions',
2018-02-18 20:40:32 +01:00
],
]
);
2019-04-16 16:20:46 +02:00
$response->assertStatus(200);
}
2018-03-04 15:14:29 +01:00
}