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

269 lines
10 KiB
PHP
Raw Normal View History

2017-02-12 05:00:11 -06:00
<?php
/**
* JsonControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace Tests\Feature\Controllers;
2017-03-05 06:21:36 -06:00
use Amount;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Category;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Account\AccountTaskerInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Support\Collection;
2017-02-12 05:00:11 -06:00
use Tests\TestCase;
2017-03-05 06:21:36 -06:00
/**
* Class JsonControllerTest
*
* @package Tests\Feature\Controllers
*/
2017-02-12 05:00:11 -06:00
class JsonControllerTest extends TestCase
{
2017-02-12 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\JsonController::action
*/
public function testAction()
{
2017-03-05 06:21:36 -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('json.action'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::boxBillsPaid
*/
public function testBoxBillsPaid()
{
2017-03-05 06:21:36 -06:00
// mock stuff
$billRepos = $this->mock(BillRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$billRepos->shouldReceive('getBillsUnpaidInRange')->andReturn('100');
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('json.box.paid'));
$response->assertStatus(200);
2017-03-05 06:21:36 -06:00
$response->assertExactJson(['amount' => Amount::format('100', false), 'amount_raw' => '100', 'box' => 'bills-unpaid']);
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::boxBillsUnpaid
*/
public function testBoxBillsUnpaid()
{
2017-03-05 06:21:36 -06:00
// mock stuff
$billRepos = $this->mock(BillRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$billRepos->shouldReceive('getBillsPaidInRange')->andReturn('-100');
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('json.box.unpaid'));
$response->assertStatus(200);
2017-03-05 06:21:36 -06:00
$response->assertExactJson(['amount' => Amount::format('100', false), 'amount_raw' => '100', 'box' => 'bills-paid']);
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::boxIn
*/
public function testBoxIn()
{
2017-03-05 06:21:36 -06:00
// mock stuff
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$tasker = $this->mock(AccountTaskerInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH])])->once()->andReturn(
new Collection
);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([([AccountType::DEFAULT, AccountType::ASSET])])->once()->andReturn(new Collection);
$tasker->shouldReceive('amountInInPeriod')->andReturn('100');
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('json.box.in'));
$response->assertStatus(200);
2017-03-05 06:21:36 -06:00
$response->assertExactJson(['amount' => Amount::format('100', false), 'amount_raw' => '100', 'box' => 'in']);
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::boxOut
*/
public function testBoxOut()
{
2017-03-05 06:21:36 -06:00
// mock stuff
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$tasker = $this->mock(AccountTaskerInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH])])->once()->andReturn(
new Collection
);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([([AccountType::DEFAULT, AccountType::ASSET])])->once()->andReturn(new Collection);
$tasker->shouldReceive('amountOutInPeriod')->andReturn('100');
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('json.box.out'));
$response->assertStatus(200);
2017-03-05 06:21:36 -06:00
$response->assertExactJson(['amount' => Amount::format('100', false), 'amount_raw' => '100', 'box' => 'out']);
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::categories
*/
public function testCategories()
{
2017-03-05 06:21:36 -06:00
// mock stuff
$category = factory(Category::class)->make();
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$categoryRepos->shouldReceive('getCategories')->andReturn(new Collection([$category]));
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('json.categories'));
$response->assertStatus(200);
2017-03-05 06:21:36 -06:00
$response->assertExactJson([$category->name]);
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::endTour
*/
public function testEndTour()
{
2017-03-05 06:21:36 -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->post(route('json.end-tour'));
$response->assertStatus(200);
2017-03-05 06:21:36 -06:00
$response->assertExactJson(['true']);
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::expenseAccounts
*/
public function testExpenseAccounts()
{
2017-03-05 06:21:36 -06:00
// mock stuff
$account = factory(Category::class)->make();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::EXPENSE, AccountType::BENEFICIARY]])->once()->andReturn(
new Collection([$account])
);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('json.expense-accounts'));
$response->assertStatus(200);
2017-03-05 06:21:36 -06:00
$response->assertExactJson([$account->name]);
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::revenueAccounts
*/
public function testRevenueAccounts()
{
2017-03-05 06:21:36 -06:00
// mock stuff
$account = factory(Category::class)->make();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::REVENUE]])->once()->andReturn(
new Collection([$account])
);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('json.revenue-accounts'));
$response->assertStatus(200);
2017-03-05 06:21:36 -06:00
$response->assertExactJson([$account->name]);
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::tags
*/
public function testTags()
{
2017-03-05 06:21:36 -06:00
// mock stuff
$tag = factory(Tag::class)->make();
$tagRepos = $this->mock(TagRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$tagRepos->shouldReceive('get')->andReturn(new Collection([$tag]))->once();
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('json.tags'));
$response->assertStatus(200);
2017-03-05 06:21:36 -06:00
$response->assertExactJson([$tag->tag]);
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::tour
*/
public function testTour()
{
2017-03-05 06:21:36 -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('json.tour'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::transactionJournals
*/
public function testTransactionJournals()
{
2017-03-05 06:21:36 -06:00
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('json.transaction-journals', ['deposit']));
$response->assertStatus(200);
2017-03-05 06:21:36 -06:00
$response->assertExactJson([]);
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\JsonController::trigger
*/
public function testTrigger()
{
2017-03-05 06:21:36 -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('json.trigger'));
$response->assertStatus(200);
}
2017-02-16 15:33:32 -06:00
}