Files
firefly-iii/tests/Feature/Controllers/Transaction/LinkControllerTest.php

202 lines
7.1 KiB
PHP
Raw Normal View History

2017-12-23 17:42:07 +01:00
<?php
/**
* LinkControllerTest.php
* Copyright (c) 2017 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\Feature\Controllers\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
2018-03-24 06:08:50 +01:00
use Log;
2017-12-23 17:42:07 +01:00
use Tests\TestCase;
/**
* Class LinkControllerTest
*/
class LinkControllerTest extends TestCase
{
2018-03-24 06:08:50 +01:00
/**
*
*/
public function setUp()
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
2018-03-24 06:08:50 +01:00
}
2017-12-23 17:42:07 +01:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController::__construct
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController::delete
*/
2018-05-11 19:58:10 +02:00
public function testDelete(): void
2017-12-23 17:42:07 +01:00
{
2018-02-28 15:50:00 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$linkRepos = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->get(route('transactions.link.delete', [1]));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController::destroy
*/
2018-05-11 19:58:10 +02:00
public function testDestroy(): void
2017-12-23 17:42:07 +01:00
{
2018-02-28 15:50:00 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
2017-12-23 17:42:07 +01:00
$repository->shouldReceive('destroyLink');
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$this->session(['journal_links.delete.uri' => 'http://localhost/']);
$response = $this->post(route('transactions.link.destroy', [1]));
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
2018-03-24 06:08:50 +01:00
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController::store
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalLinkRequest
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testStore(): void
2017-12-23 17:42:07 +01:00
{
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$data = [
'link_other' => 8,
'link_type' => '1_inward',
];
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-06-02 06:11:13 +02:00
$journalRepos->shouldReceive('findNull')->andReturn(new TransactionJournal);
2017-12-29 09:05:35 +01:00
$repository->shouldReceive('findLink')->andReturn(false);
$repository->shouldReceive('storeLink')->andReturn(new TransactionJournalLink);
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->post(route('transactions.link.store', [1]), $data);
$response->assertStatus(302);
2017-12-29 09:05:35 +01:00
$response->assertSessionHas('success');
2017-12-23 17:42:07 +01:00
$response->assertRedirect(route('transactions.show', [1]));
}
2018-06-02 06:11:13 +02:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController::store
* @covers \FireflyIII\Http\Requests\JournalLinkRequest
*/
public function testStoreSame(): void
{
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$data = [
'link_other' => 8,
'link_type' => '1_inward',
];
$journal = $this->user()->transactionJournals()->first();
$journalRepos->shouldReceive('firstNull')->andReturn($journal);
$journalRepos->shouldReceive('findNull')->andReturn($journal);
$repository->shouldReceive('findLink')->andReturn(false);
$repository->shouldReceive('storeLink')->andReturn(new TransactionJournalLink);
$this->be($this->user());
$response = $this->post(route('transactions.link.store', [$journal->id]), $data);
$response->assertStatus(302);
$response->assertSessionHas('error');
$response->assertRedirect(route('transactions.show', [1]));
}
2017-12-23 17:42:07 +01:00
/**
2018-03-24 06:08:50 +01:00
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController::store
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalLinkRequest
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testStoreAlreadyLinked(): void
2017-12-23 17:42:07 +01:00
{
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$data = [
'link_other' => 8,
'link_type' => '1_inward',
];
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-06-02 06:11:13 +02:00
$journalRepos->shouldReceive('findNull')->andReturn(new TransactionJournal);
2017-12-29 09:05:35 +01:00
$repository->shouldReceive('findLink')->andReturn(true);
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->post(route('transactions.link.store', [1]), $data);
$response->assertStatus(302);
2017-12-29 09:05:35 +01:00
$response->assertSessionHas('error');
2017-12-23 17:42:07 +01:00
$response->assertRedirect(route('transactions.show', [1]));
}
/**
2018-03-24 06:08:50 +01:00
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController::store
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalLinkRequest
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testStoreInvalid(): void
2017-12-23 17:42:07 +01:00
{
2018-02-28 15:50:00 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$linkRepos = $this->mock(LinkTypeRepositoryInterface::class);
$data = [
2017-12-23 17:42:07 +01:00
'link_other' => 0,
'link_type' => '1_inward',
];
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 15:50:00 +01:00
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$response = $this->post(route('transactions.link.store', [1]), $data);
$response->assertStatus(302);
$response->assertSessionHas('error');
$response->assertRedirect(route('transactions.show', [1]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController::switchLink
*/
2018-05-11 19:58:10 +02:00
public function testSwitchLink(): void
2017-12-23 17:42:07 +01:00
{
2018-02-28 15:50:00 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-12-23 17:42:07 +01:00
$repository->shouldReceive('switchLink')->andReturn(false);
$this->be($this->user());
$response = $this->get(route('transactions.link.switch', [1]));
$response->assertStatus(302);
}
2018-03-04 15:14:29 +01:00
}