firefly-iii/tests/Feature/Controllers/BillControllerTest.php

257 lines
10 KiB
PHP
Raw Normal View History

2017-02-12 05:00:11 -06:00
<?php
/**
* BillControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:42:21 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-02-12 05:00:11 -06:00
*/
2017-07-07 23:28:44 -05:00
declare(strict_types=1);
2017-02-12 05:00:11 -06:00
namespace Tests\Feature\Controllers;
2017-03-05 04:18:34 -06:00
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Bill;
use FireflyIII\Models\TransactionJournal;
2017-02-12 05:21:44 -06:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2017-03-05 04:18:34 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
2017-02-12 05:21:44 -06:00
use Illuminate\Support\Collection;
2017-02-12 05:00:11 -06:00
use Tests\TestCase;
2017-08-12 03:27:45 -05:00
/**
* Class BillControllerTest
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
2017-02-12 05:00:11 -06:00
class BillControllerTest extends TestCase
{
2017-02-12 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\BillController::create
*/
public function testCreate()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('bills.create'));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BillController::delete
*/
public function testDelete()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('bills.delete', [1]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BillController::destroy
*/
public function testDestroy()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(BillRepositoryInterface::class);
2017-02-12 05:21:44 -06:00
$repository->shouldReceive('destroy')->andReturn(true);
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
2017-03-18 14:53:44 -05:00
$this->session(['bills.delete.uri' => 'http://localhost']);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->post(route('bills.destroy', [1]));
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\BillController::edit
*/
public function testEdit()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('bills.edit', [1]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BillController::index
2017-02-17 13:14:38 -06:00
* @covers \FireflyIII\Http\Controllers\BillController::__construct
* @covers \FireflyIII\Http\Controllers\BillController::lastPaidDate
2017-02-12 05:21:44 -06:00
*/
public function testIndex()
{
2017-03-05 04:18:34 -06:00
// mock stuff
2017-03-18 05:02:02 -05:00
$bill = factory(Bill::class)->make();
2017-03-05 04:18:34 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(BillRepositoryInterface::class);
2017-03-18 05:02:02 -05:00
$repository->shouldReceive('getBills')->andReturn(new Collection([$bill]));
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-12-06 12:41:51 -06:00
$repository->shouldReceive('getPaidDatesInRange')->twice()->andReturn(new Collection([new Carbon, new Carbon, new Carbon]));
$repository->shouldReceive('getPayDatesInRange')->once()->andReturn(new Collection([new Carbon, new Carbon]));
2017-03-18 05:02:02 -05:00
$repository->shouldReceive('nextExpectedMatch')->andReturn(new Carbon);
2017-03-05 04:18:34 -06:00
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('bills.index'));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BillController::rescan
*/
public function testRescan()
{
2017-03-05 04:18:34 -06:00
// mock stuff
2017-03-18 05:02:02 -05:00
$journal = factory(TransactionJournal::class)->make();
2017-03-05 04:18:34 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(BillRepositoryInterface::class);
2017-03-18 05:02:02 -05:00
$repository->shouldReceive('getPossiblyRelatedJournals')->once()->andReturn(new Collection([$journal]));
$repository->shouldReceive('scan')->once();
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('bills.rescan', [1]));
$response->assertStatus(302);
$response->assertSessionHas('success');
}
2017-03-18 05:02:02 -05:00
/**
* @covers \FireflyIII\Http\Controllers\BillController::rescan
*/
public function testRescanInactive()
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$this->be($this->user());
$response = $this->get(route('bills.rescan', [3]));
$response->assertStatus(302);
$response->assertSessionHas('warning');
}
2017-02-12 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\BillController::show
*/
public function testShow()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$repository = $this->mock(BillRepositoryInterface::class);
$repository->shouldReceive('getYearAverage')->andReturn('0');
$repository->shouldReceive('getOverallAverage')->andReturn('0');
$repository->shouldReceive('nextExpectedMatch')->andReturn(new Carbon);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
$collector->shouldReceive('setBills')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10));
2017-12-06 12:41:51 -06:00
$repository->shouldReceive('getPaidDatesInRange')->twice()->andReturn(new Collection([new Carbon, new Carbon, new Carbon]));
$repository->shouldReceive('getPayDatesInRange')->once()->andReturn(new Collection([new Carbon, new Carbon, new Carbon]));
2017-03-05 04:18:34 -06:00
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('bills.show', [1]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BillController::store
*/
public function testStore()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(BillRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('store')->andReturn(new Bill);
2017-02-12 05:21:44 -06:00
$data = [
'name' => 'New Bill ' . rand(1000, 9999),
'match' => 'some words',
'amount_min' => '100',
'amount_currency_id_amount_min' => 1,
'amount_currency_id_amount_max' => 1,
'skip' => 0,
'amount_max' => '100',
'date' => '2016-01-01',
'repeat_freq' => 'monthly',
];
2017-03-18 14:53:44 -05:00
$this->session(['bills.create.uri' => 'http://localhost']);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->post(route('bills.store'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\BillController::update
*/
public function testUpdate()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(BillRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('update')->andReturn(new Bill);
2017-02-12 05:21:44 -06:00
$data = [
'name' => 'Updated Bill ' . rand(1000, 9999),
'match' => 'some more words',
'amount_min' => '100',
'amount_currency_id_amount_min' => 1,
'amount_currency_id_amount_max' => 1,
'skip' => 0,
'amount_max' => '100',
'date' => '2016-01-01',
'repeat_freq' => 'monthly',
];
2017-03-18 14:53:44 -05:00
$this->session(['bills.edit.uri' => 'http://localhost']);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->post(route('bills.update', [1]), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
2017-02-16 15:33:32 -06:00
}