mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Add some factory stuff before another refactoring.
This commit is contained in:
parent
eb0da038fb
commit
f8bf6c163f
72
app/Factory/TagFactory.php
Normal file
72
app/Factory/TagFactory.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* TagFactory.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 FireflyIII\Factory;
|
||||
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\User;
|
||||
|
||||
/**
|
||||
* Class TagFactory
|
||||
*/
|
||||
class TagFactory
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* TagFactory constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Tag|null
|
||||
*/
|
||||
public function create(array $data): ?Tag
|
||||
{
|
||||
return Tag::create(
|
||||
[
|
||||
'user_id' => $data['user']->id,
|
||||
'tag' => $data['tag'],
|
||||
'tagMode' => 'nothing',
|
||||
'date' => $data['date'],
|
||||
'description'=> $data['description'],
|
||||
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Factory;
|
||||
|
||||
use FireflyIII\Events\StoredTransactionJournal;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Bill;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
@ -74,8 +73,8 @@ class TransactionJournalFactory
|
||||
public function create(array $data): TransactionJournal
|
||||
{
|
||||
$type = $this->findTransactionType($data['type']);
|
||||
$bill = $this->findBill($data['bill_id'], $data['bill_name']);
|
||||
$piggyBank = $this->findPiggyBank($data['piggy_bank_id'], $data['piggy_bank_name']);
|
||||
//$bill = $this->findBill($data['bill_id'], $data['bill_name']);
|
||||
//$piggyBank = $this->findPiggyBank($data['piggy_bank_id'], $data['piggy_bank_name']);
|
||||
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||
$values = [
|
||||
'user_id' => $data['user'],
|
||||
@ -91,10 +90,6 @@ class TransactionJournalFactory
|
||||
|
||||
$journal = $this->repository->storeBasic($values);
|
||||
|
||||
// todo link other stuff to journal (meta-data etc). tags
|
||||
// todo
|
||||
// start creating transactions:
|
||||
|
||||
$factory = app(TransactionFactory::class);
|
||||
$factory->setUser($this->user);
|
||||
|
||||
@ -112,6 +107,24 @@ class TransactionJournalFactory
|
||||
$factory->create($journal, $piggyBank);
|
||||
}
|
||||
|
||||
// store tags and link them:
|
||||
/** @var TagFactory $factory */
|
||||
$factory = app(TagFactory::class);
|
||||
$factory->setUser($journal->user);
|
||||
foreach ($data['tags'] as $string) {
|
||||
$tagData = [
|
||||
'tag' => $string,
|
||||
'date' => null,
|
||||
'description' => null,
|
||||
'latitude' => null,
|
||||
'longitude' => null,
|
||||
'zoom_level' => null,
|
||||
'user' => $journal->user,
|
||||
];
|
||||
$tag = $factory->create($tagData);
|
||||
$this->repository->addTag($journal, $tag);
|
||||
}
|
||||
|
||||
return $journal;
|
||||
}
|
||||
|
||||
|
@ -546,4 +546,62 @@ class TransactionControllerTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the minimum amount of data required to create a withdrawal.
|
||||
* Add some tags as well. Expect to see them in the result.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\TransactionController::store
|
||||
*/
|
||||
public function testSuccessStoreTags()
|
||||
{
|
||||
$tags = [
|
||||
'TagOne' . rand(1, 1000),
|
||||
'TagTwoBlarg' . rand(1, 1000),
|
||||
'SomeThreeTag' . rand(1, 1000),
|
||||
];
|
||||
$account = auth()->user()->accounts()->where('account_type_id', 3)->first();
|
||||
$data = [
|
||||
'description' => 'Some transaction #' . rand(1, 1000),
|
||||
'date' => '2018-01-01',
|
||||
'type' => 'withdrawal',
|
||||
'tags' => join(',', $tags),
|
||||
'transactions' => [
|
||||
[
|
||||
'amount' => '10',
|
||||
'currency_id' => 1,
|
||||
'source_id' => $account->id,
|
||||
],
|
||||
|
||||
|
||||
],
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->post('/api/v1/transactions?include=tags', $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
foreach ($tags as $tag) {
|
||||
$response->assertSee($tag);
|
||||
}
|
||||
$response->assertJson(
|
||||
[
|
||||
'data' => [
|
||||
'type' => 'transactions',
|
||||
'attributes' => [
|
||||
'description' => $data['description'],
|
||||
'date' => $data['date'],
|
||||
'source_id' => $account->id,
|
||||
'source_name' => $account->name,
|
||||
'type' => 'Withdrawal',
|
||||
'source_type' => 'Asset account',
|
||||
'destination_name' => 'Cash account',
|
||||
'destination_type' => 'Cash account',
|
||||
'amount' => -10,
|
||||
],
|
||||
'links' => true,
|
||||
'includes' => [],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user