firefly-iii/tests/Unit/Factory/AttachmentFactoryTest.php
2020-02-16 13:59:55 +01:00

145 lines
4.2 KiB
PHP

<?php
/**
* AttachmentFactoryTest.php
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Factory;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AttachmentFactory;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Log;
use Tests\TestCase;
/**
*
* Class AttachmentFactoryTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class AttachmentFactoryTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Factory\AttachmentFactory
*/
public function testCreate(): void
{
$journal = $this->getRandomWithdrawal();
$data = [
'model_id' => $journal->id,
'model' => TransactionJournal::class,
'filename' => 'testfile.pdf',
'title' => 'File name',
'notes' => 'Some notes',
];
/** @var AttachmentFactory $factory */
$factory = app(AttachmentFactory::class);
$factory->setUser($this->user());
try {
$result = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals($data['title'], $result->title);
$this->assertEquals(1, $result->notes()->count());
}
/**
* @covers \FireflyIII\Factory\AttachmentFactory
*/
public function testCreateTransaction(): void
{
$journal = $this->getRandomWithdrawal();
$transaction = $journal->transactions()->first();
$data = [
'model_id' => $transaction->id,
'model' => Transaction::class,
'filename' => 'testfile.pdf',
'title' => 'File name',
'notes' => 'Some notes',
];
/** @var AttachmentFactory $factory */
$factory = app(AttachmentFactory::class);
$factory->setUser($this->user());
try {
$result = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals($data['title'], $result->title);
$this->assertEquals($result->attachable_id, $journal->id);
$this->assertEquals(1, $result->notes()->count());
}
/**
* @covers \FireflyIII\Factory\AttachmentFactory
*/
public function testCreateTransactionAppendModel(): void
{
$journal = $this->getRandomWithdrawal();
$transaction = $journal->transactions()->first();
$data = [
'model_id' => $transaction->id,
'model' => 'Transaction',
'filename' => 'testfile.pdf',
'title' => 'File name',
'notes' => 'Some notes',
];
/** @var AttachmentFactory $factory */
$factory = app(AttachmentFactory::class);
$factory->setUser($this->user());
try {
$result = $factory->create($data);
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertEquals($data['title'], $result->title);
$this->assertEquals($result->attachable_id, $journal->id);
$this->assertEquals(1, $result->notes()->count());
}
}