diff --git a/app/Http/Controllers/ExportController.php b/app/Http/Controllers/ExportController.php index 865f8133eb..6bdec809f1 100644 --- a/app/Http/Controllers/ExportController.php +++ b/app/Http/Controllers/ExportController.php @@ -23,7 +23,6 @@ use FireflyIII\Models\AccountType; use FireflyIII\Models\ExportJob; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface; -use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface as EJRI; use Illuminate\Http\Response as LaravelResponse; use Preferences; use Response; @@ -55,8 +54,8 @@ class ExportController extends Controller } /** - * @param EJRI $repository - * @param ExportJob $job + * @param ExportJobRepositoryInterface $repository + * @param ExportJob $job * * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response * @throws FireflyException @@ -103,12 +102,12 @@ class ExportController extends Controller } /** - * @param AccountRepositoryInterface $repository - * @param EJRI $jobs + * @param AccountRepositoryInterface $repository + * @param ExportJobRepositoryInterface $jobs * * @return View */ - public function index(AccountRepositoryInterface $repository, EJRI $jobs) + public function index(AccountRepositoryInterface $repository, ExportJobRepositoryInterface $jobs) { // create new export job. $job = $jobs->create(); @@ -129,13 +128,13 @@ class ExportController extends Controller } /** - * @param ExportFormRequest $request - * @param AccountRepositoryInterface $repository - * @param EJRI $jobs + * @param ExportFormRequest $request + * @param AccountRepositoryInterface $repository + * @param ExportJobRepositoryInterface $jobs * * @return \Illuminate\Http\JsonResponse */ - public function postIndex(ExportFormRequest $request, AccountRepositoryInterface $repository, EJRI $jobs) + public function postIndex(ExportFormRequest $request, AccountRepositoryInterface $repository, ExportJobRepositoryInterface $jobs) { $job = $jobs->findByKey($request->get('job')); $settings = [ diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 460a3e98cd..1fe4cc1d43 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -17,7 +17,7 @@ use Carbon\Carbon; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collector\JournalCollectorInterface; use FireflyIII\Models\AccountType; -use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; use Illuminate\Http\Request; use Illuminate\Support\Collection; @@ -97,11 +97,11 @@ class HomeController extends Controller } /** - * @param ARI $repository + * @param AccountRepositoryInterface $repository * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View */ - public function index(ARI $repository) + public function index(AccountRepositoryInterface $repository) { $types = config('firefly.accountTypesByIdentifier.asset'); $count = $repository->count($types); diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index c32a34ff00..846f775566 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -33,6 +33,24 @@ $factory->define( } ); +$factory->define( + FireflyIII\Models\Tag::class, function (Faker\Generator $faker) { + return [ + 'id' => $faker->numberBetween(1, 10), + 'tag' => $faker->words(1, true), + ]; +} +); + +$factory->define( + FireflyIII\Models\Category::class, function (Faker\Generator $faker) { + return [ + 'id' => $faker->numberBetween(1, 10), + 'name' => $faker->words(3, true), + ]; +} +); + $factory->define( FireflyIII\Models\Budget::class, function (Faker\Generator $faker) { return [ diff --git a/tests/Feature/Controllers/CurrencyControllerTest.php b/tests/Feature/Controllers/CurrencyControllerTest.php index f58d0a2ab0..6795973aca 100644 --- a/tests/Feature/Controllers/CurrencyControllerTest.php +++ b/tests/Feature/Controllers/CurrencyControllerTest.php @@ -11,9 +11,18 @@ declare(strict_types = 1); namespace Tests\Feature\Controllers; +use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use Illuminate\Support\Collection; use Tests\TestCase; +/** + * Class CurrencyControllerTest + * + * @package Tests\Feature\Controllers + */ class CurrencyControllerTest extends TestCase { @@ -22,6 +31,10 @@ class CurrencyControllerTest extends TestCase */ public function testCreate() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('currencies.create')); $response->assertStatus(200); @@ -34,6 +47,10 @@ class CurrencyControllerTest extends TestCase */ public function testDefaultCurrency() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('currencies.default', [1])); $response->assertStatus(302); @@ -45,6 +62,12 @@ class CurrencyControllerTest extends TestCase */ public function testDelete() { + // mock stuff + $repository = $this->mock(CurrencyRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $repository->shouldReceive('canDeleteCurrency')->andReturn(true); + $this->be($this->user()); $response = $this->get(route('currencies.delete', [2])); $response->assertStatus(200); @@ -57,11 +80,16 @@ class CurrencyControllerTest extends TestCase */ public function testDestroy() { - $this->session(['currencies.delete.url' => 'http://localhost']); + // mock stuff $repository = $this->mock(CurrencyRepositoryInterface::class); $repository->shouldReceive('canDeleteCurrency')->andReturn(true); $repository->shouldReceive('destroy')->andReturn(true); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + + + $this->session(['currencies.delete.url' => 'http://localhost']); $this->be($this->user()); $response = $this->post(route('currencies.destroy', [1])); $response->assertStatus(302); @@ -73,6 +101,10 @@ class CurrencyControllerTest extends TestCase */ public function testEdit() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('currencies.edit', [2])); $response->assertStatus(200); @@ -86,6 +118,13 @@ class CurrencyControllerTest extends TestCase */ public function testIndex() { + // mock stuff + $repository = $this->mock(CurrencyRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $repository->shouldReceive('getCurrencyByPreference')->andReturn(new TransactionCurrency); + $repository->shouldReceive('get')->andReturn(new Collection); + $this->be($this->user()); $response = $this->get(route('currencies.index')); $response->assertStatus(200); @@ -98,6 +137,12 @@ class CurrencyControllerTest extends TestCase */ public function testStore() { + // mock stuff + $repository = $this->mock(CurrencyRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $repository->shouldReceive('store')->andReturn(new TransactionCurrency); + $this->session(['currencies.create.url' => 'http://localhost']); $data = [ 'name' => 'XX', @@ -116,6 +161,12 @@ class CurrencyControllerTest extends TestCase */ public function testUpdate() { + // mock stuff + $repository = $this->mock(CurrencyRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $repository->shouldReceive('update')->andReturn(new TransactionCurrency); + $this->session(['currencies.edit.url' => 'http://localhost']); $data = [ 'name' => 'XA', diff --git a/tests/Feature/Controllers/ExportControllerTest.php b/tests/Feature/Controllers/ExportControllerTest.php index 99adc38034..a956c79d6a 100644 --- a/tests/Feature/Controllers/ExportControllerTest.php +++ b/tests/Feature/Controllers/ExportControllerTest.php @@ -13,12 +13,20 @@ namespace Tests\Feature\Controllers; use Carbon\Carbon; use FireflyIII\Export\ProcessorInterface; +use FireflyIII\Models\AccountType; use FireflyIII\Models\ExportJob; +use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface; +use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use Illuminate\Support\Collection; use Tests\TestCase; +/** + * Class ExportControllerTest + * + * @package Tests\Feature\Controllers + */ class ExportControllerTest extends TestCase { @@ -27,7 +35,11 @@ class ExportControllerTest extends TestCase */ public function testDownload() { - $repository = $this->mock(ExportJobRepositoryInterface::class); + // mock stuff + $repository = $this->mock(ExportJobRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $repository->shouldReceive('exists')->once()->andReturn(true); $repository->shouldReceive('getContent')->once()->andReturn('Some content beep boop'); @@ -41,6 +53,10 @@ class ExportControllerTest extends TestCase */ public function testGetStatus() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('export.status', ['testExport'])); $response->assertStatus(200); @@ -52,6 +68,15 @@ class ExportControllerTest extends TestCase */ public function testIndex() { + // mock stuff + $repository = $this->mock(ExportJobRepositoryInterface::class); + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $repository->shouldReceive('create')->andReturn(new ExportJob); + $repository->shouldReceive('cleanup'); + $accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection); + $this->be($this->user()); $response = $this->get(route('export.index')); $response->assertStatus(200); @@ -65,13 +90,19 @@ class ExportControllerTest extends TestCase */ public function testPostIndex() { + // mock stuff + $repository = $this->mock(ExportJobRepositoryInterface::class); + $processor = $this->mock(ProcessorInterface::class); + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->andReturn(new TransactionJournal); + $this->session( ['first' => new Carbon('2014-01-01')] ); $data = [ - 'export_start_range' => '2015-01-01', 'export_end_range' => '2015-01-21', 'exportFormat' => 'csv', @@ -79,17 +110,14 @@ class ExportControllerTest extends TestCase 'job' => 'testExport', ]; - $accountRepository = $this->mock(AccountRepositoryInterface::class); - $accountRepository->shouldReceive('getAccountsById')->withArgs([$data['accounts']])->andReturn(new Collection); + $accountRepos->shouldReceive('getAccountsById')->withArgs([$data['accounts']])->andReturn(new Collection); - $processor = $this->mock(ProcessorInterface::class); $processor->shouldReceive('setSettings')->once(); $processor->shouldReceive('collectJournals')->once(); $processor->shouldReceive('convertJournals')->once(); $processor->shouldReceive('exportJournals')->once(); $processor->shouldReceive('createZipFile')->once(); - $repository = $this->mock(ExportJobRepositoryInterface::class); $repository->shouldReceive('changeStatus')->andReturn(true); $repository->shouldReceive('findByKey')->andReturn(new ExportJob); diff --git a/tests/Feature/Controllers/HelpControllerTest.php b/tests/Feature/Controllers/HelpControllerTest.php index 94afda9ae2..10eeacb65d 100644 --- a/tests/Feature/Controllers/HelpControllerTest.php +++ b/tests/Feature/Controllers/HelpControllerTest.php @@ -14,6 +14,11 @@ namespace Tests\Feature\Controllers; use FireflyIII\Helpers\Help\HelpInterface; use Tests\TestCase; +/** + * Class HelpControllerTest + * + * @package Tests\Feature\Controllers + */ class HelpControllerTest extends TestCase { diff --git a/tests/Feature/Controllers/HomeControllerTest.php b/tests/Feature/Controllers/HomeControllerTest.php index 0b80182db1..082fef6b4a 100644 --- a/tests/Feature/Controllers/HomeControllerTest.php +++ b/tests/Feature/Controllers/HomeControllerTest.php @@ -11,8 +11,21 @@ declare(strict_types = 1); namespace Tests\Feature\Controllers; +use FireflyIII\Helpers\Collector\JournalCollectorInterface; +use FireflyIII\Models\Account; +use FireflyIII\Models\AccountType; +use FireflyIII\Models\TransactionJournal; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Bill\BillRepositoryInterface; +use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use Illuminate\Support\Collection; use Tests\TestCase; +/** + * Class HomeControllerTest + * + * @package Tests\Feature\Controllers + */ class HomeControllerTest extends TestCase { /** @@ -21,6 +34,10 @@ class HomeControllerTest extends TestCase */ public function testDateRange() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); @@ -39,6 +56,10 @@ class HomeControllerTest extends TestCase */ public function testDisplayError() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('error')); $response->assertStatus(500); @@ -49,6 +70,10 @@ class HomeControllerTest extends TestCase */ public function testFlush() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('flush')); $response->assertStatus(302); @@ -64,17 +89,61 @@ class HomeControllerTest extends TestCase */ public function testIndex(string $range) { + // mock stuff + $account = factory(Account::class)->make(); + $collector = $this->mock(JournalCollectorInterface::class); + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $billRepos = $this->mock(BillRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $accountRepos->shouldReceive('count')->andReturn(1); + $accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection([$account])); + $accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account])); + $billRepos->shouldReceive('getBills')->andReturn(new Collection); + + $collector->shouldReceive('setAccounts')->andReturnSelf(); + $collector->shouldReceive('setRange')->andReturnSelf(); + $collector->shouldReceive('setLimit')->andReturnSelf(); + $collector->shouldReceive('setPage')->andReturnSelf(); + $collector->shouldReceive('getJournals')->andReturn(new Collection); + $this->be($this->user()); $this->changeDateRange($this->user(), $range); $response = $this->get(route('index')); $response->assertStatus(200); } + /** + * @covers \FireflyIII\Http\Controllers\HomeController::index + * @covers \FireflyIII\Http\Controllers\HomeController::__construct + * @covers \FireflyIII\Http\Controllers\Controller::__construct + * @dataProvider dateRangeProvider + * + * @param $range + */ + public function testIndexEmpty(string $range) + { + // mock stuff + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $accountRepos->shouldReceive('count')->andReturn(0); + + $this->be($this->user()); + $this->changeDateRange($this->user(), $range); + $response = $this->get(route('index')); + $response->assertStatus(302); + } + /** * @covers \FireflyIII\Http\Controllers\HomeController::testFlash */ public function testTestFlash() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('test-flash')); $response->assertStatus(302); diff --git a/tests/Feature/Controllers/ImportControllerTest.php b/tests/Feature/Controllers/ImportControllerTest.php index be9feee5ff..384876dfdf 100644 --- a/tests/Feature/Controllers/ImportControllerTest.php +++ b/tests/Feature/Controllers/ImportControllerTest.php @@ -13,16 +13,28 @@ namespace Tests\Feature\Controllers; use FireflyIII\Import\ImportProcedureInterface; use FireflyIII\Import\Setup\CsvSetup; +use FireflyIII\Models\TransactionJournal; +use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use Illuminate\Http\UploadedFile; use Tests\TestCase; +/** + * Class ImportControllerTest + * + * @package Tests\Feature\Controllers + */ class ImportControllerTest extends TestCase { /** * @covers \FireflyIII\Http\Controllers\ImportController::complete + * @covers \FireflyIII\Http\Controllers\ImportController::jobInCorrectStep + * @covers \FireflyIII\Http\Controllers\ImportController::redirectToCorrectStep */ public function testComplete() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('import.complete', ['complete'])); $response->assertStatus(200); @@ -31,9 +43,18 @@ class ImportControllerTest extends TestCase /** * @covers \FireflyIII\Http\Controllers\ImportController::configure + * @covers \FireflyIII\Http\Controllers\ImportController::makeImporter */ public function testConfigure() { + $setup = $this->mock(CsvSetup::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + + $setup->shouldReceive('setJob')->once(); + $setup->shouldReceive('configure')->once(); + $setup->shouldReceive('getConfigurationData')->andReturn(['specifics' => [], 'delimiters' => [], 'accounts' => []])->once(); + $this->be($this->user()); $response = $this->get(route('import.configure', ['configure'])); $response->assertStatus(200); @@ -45,10 +66,19 @@ class ImportControllerTest extends TestCase */ public function testDownload() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('import.download', ['configure'])); $response->assertStatus(200); - $response->assertSee('[]'); + $response->assertJson( + [ + 'delimiter' => 'tab', + 'column-roles-complete' => false, + 'column-mapping-complete' => false, + ] + ); } /** @@ -56,6 +86,9 @@ class ImportControllerTest extends TestCase */ public function testFinished() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('import.finished', ['finished'])); $response->assertStatus(200); @@ -68,6 +101,9 @@ class ImportControllerTest extends TestCase */ public function testIndex() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('import.index')); $response->assertStatus(200); @@ -78,6 +114,9 @@ class ImportControllerTest extends TestCase */ public function testJson() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('import.json', ['configure'])); $response->assertStatus(200); @@ -88,6 +127,9 @@ class ImportControllerTest extends TestCase */ public function testPostConfigure() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $importer = $this->mock(CsvSetup::class); $importer->shouldReceive('setJob')->once(); $importer->shouldReceive('saveImportConfiguration')->once(); @@ -104,6 +146,9 @@ class ImportControllerTest extends TestCase */ public function testPostSettings() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $importer = $this->mock(CsvSetup::class); $importer->shouldReceive('setJob')->once(); $importer->shouldReceive('storeSettings')->once(); @@ -119,6 +164,9 @@ class ImportControllerTest extends TestCase */ public function testSettings() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $importer = $this->mock(CsvSetup::class); $importer->shouldReceive('setJob')->once(); $importer->shouldReceive('requireUserSettings')->once()->andReturn(false); @@ -133,6 +181,9 @@ class ImportControllerTest extends TestCase */ public function testStart() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + /** @var ImportProcedureInterface $procedure */ $procedure = $this->mock(ImportProcedureInterface::class); @@ -149,6 +200,9 @@ class ImportControllerTest extends TestCase */ public function testStatus() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + // complete $this->be($this->user()); $response = $this->get(route('import.status', ['complete'])); @@ -160,6 +214,9 @@ class ImportControllerTest extends TestCase */ public function testUpload() { + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->andReturn(new TransactionJournal); + $path = resource_path('stubs/csv.csv'); $file = new UploadedFile($path, 'upload.csv', filesize($path), 'text/csv', null, true); $response = $this->post(route('import.upload'), [], [], ['import_file' => $file], ['Accept' => 'application/json']); diff --git a/tests/Feature/Controllers/JsonControllerTest.php b/tests/Feature/Controllers/JsonControllerTest.php index 38639ece72..015d5cb271 100644 --- a/tests/Feature/Controllers/JsonControllerTest.php +++ b/tests/Feature/Controllers/JsonControllerTest.php @@ -11,8 +11,26 @@ declare(strict_types = 1); namespace Tests\Feature\Controllers; +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; use Tests\TestCase; +/** + * Class JsonControllerTest + * + * @package Tests\Feature\Controllers + */ class JsonControllerTest extends TestCase { @@ -21,6 +39,10 @@ class JsonControllerTest extends TestCase */ public function testAction() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('json.action')); $response->assertStatus(200); @@ -31,9 +53,16 @@ class JsonControllerTest extends TestCase */ public function testBoxBillsPaid() { + // mock stuff + $billRepos = $this->mock(BillRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $billRepos->shouldReceive('getBillsUnpaidInRange')->andReturn('100'); + $this->be($this->user()); $response = $this->get(route('json.box.paid')); $response->assertStatus(200); + $response->assertExactJson(['amount' => Amount::format('100', false), 'amount_raw' => '100', 'box' => 'bills-unpaid']); } /** @@ -41,9 +70,17 @@ class JsonControllerTest extends TestCase */ public function testBoxBillsUnpaid() { + // mock stuff + $billRepos = $this->mock(BillRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $billRepos->shouldReceive('getBillsPaidInRange')->andReturn('-100'); + + $this->be($this->user()); $response = $this->get(route('json.box.unpaid')); $response->assertStatus(200); + $response->assertExactJson(['amount' => Amount::format('100', false), 'amount_raw' => '100', 'box' => 'bills-paid']); } /** @@ -51,9 +88,21 @@ class JsonControllerTest extends TestCase */ public function testBoxIn() { + // 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'); + $this->be($this->user()); $response = $this->get(route('json.box.in')); $response->assertStatus(200); + $response->assertExactJson(['amount' => Amount::format('100', false), 'amount_raw' => '100', 'box' => 'in']); } /** @@ -61,9 +110,21 @@ class JsonControllerTest extends TestCase */ public function testBoxOut() { + // 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'); + $this->be($this->user()); $response = $this->get(route('json.box.out')); $response->assertStatus(200); + $response->assertExactJson(['amount' => Amount::format('100', false), 'amount_raw' => '100', 'box' => 'out']); } /** @@ -71,9 +132,16 @@ class JsonControllerTest extends TestCase */ public function testCategories() { + // 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])); $this->be($this->user()); $response = $this->get(route('json.categories')); $response->assertStatus(200); + $response->assertExactJson([$category->name]); } /** @@ -81,9 +149,14 @@ class JsonControllerTest extends TestCase */ public function testEndTour() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->post(route('json.end-tour')); $response->assertStatus(200); + $response->assertExactJson(['true']); } /** @@ -91,9 +164,19 @@ class JsonControllerTest extends TestCase */ public function testExpenseAccounts() { + // 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]) + ); + $this->be($this->user()); $response = $this->get(route('json.expense-accounts')); $response->assertStatus(200); + $response->assertExactJson([$account->name]); } /** @@ -101,9 +184,19 @@ class JsonControllerTest extends TestCase */ public function testRevenueAccounts() { + // 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]) + ); + $this->be($this->user()); $response = $this->get(route('json.revenue-accounts')); $response->assertStatus(200); + $response->assertExactJson([$account->name]); } /** @@ -111,9 +204,17 @@ class JsonControllerTest extends TestCase */ public function testTags() { + // 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(); + $this->be($this->user()); $response = $this->get(route('json.tags')); $response->assertStatus(200); + $response->assertExactJson([$tag->tag]); } /** @@ -121,6 +222,10 @@ class JsonControllerTest extends TestCase */ public function testTour() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('json.tour')); $response->assertStatus(200); @@ -131,9 +236,19 @@ class JsonControllerTest extends TestCase */ public function testTransactionJournals() { + // 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); + $this->be($this->user()); $response = $this->get(route('json.transaction-journals', ['deposit'])); $response->assertStatus(200); + $response->assertExactJson([]); } /** @@ -141,6 +256,10 @@ class JsonControllerTest extends TestCase */ public function testTrigger() { + // mock stuff + $journalRepos = $this->mock(JournalRepositoryInterface::class); + $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); + $this->be($this->user()); $response = $this->get(route('json.trigger')); $response->assertStatus(200);