Add some factory stuff before another refactoring.

This commit is contained in:
James Cole 2018-02-18 10:49:42 +01:00
parent eb0da038fb
commit f8bf6c163f
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 150 additions and 7 deletions

View 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'],
]
);
}
}

View File

@ -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;
}

View File

@ -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' => [],
],
]
);
}
}