Improve test coverage.

This commit is contained in:
James Cole
2018-12-12 20:30:25 +01:00
parent 159fffef2e
commit 2a30293905
87 changed files with 1897 additions and 840 deletions

View File

@@ -24,8 +24,6 @@ declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\TransactionCollector;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
@@ -33,6 +31,8 @@ use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Laravel\Passport\Passport;
use Log;
use Tests\TestCase;
@@ -157,36 +157,19 @@ class CategoryControllerTest extends TestCase
*/
public function testTransactionsBasic(): void
{
$category = $this->user()->categories()->first();
// get some transactions using the collector:
Log::info('This transaction collector is OK, because it is used in a test:');
$collector = new TransactionCollector;
$collector->setUser($this->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
$collector->setAllAssetAccounts();
$collector->setLimit(5)->setPage(1);
try {
$paginator = $collector->getPaginatedTransactions();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
// mock stuff:
$category = $this->user()->categories()->first();
$repository = $this->mock(JournalRepositoryInterface::class);
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$billRepos = $this->mock(BillRepositoryInterface::class);
$paginator = new LengthAwarePaginator(new Collection, 0, 50);
$categoryRepos->shouldReceive('setUser')->atLeast()->once();
$billRepos->shouldReceive('setUser');
$repository->shouldReceive('setUser');
$currencyRepository->shouldReceive('setUser');
$repository->shouldReceive('getNoteText')->atLeast()->once()->andReturn('Note');
$repository->shouldReceive('getMetaField')->atLeast()->once()->andReturn(null);
$repository->shouldReceive('getMetaDateString')->atLeast()->once()->andReturn('2018-01-01');
$collector->shouldReceive('setUser')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
@@ -206,7 +189,7 @@ class CategoryControllerTest extends TestCase
$response = $this->get(route('api.v1.categories.transactions', [$category->id]));
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(['meta' => ['pagination' => ['total' => true, 'count' => true, 'per_page' => 5, 'current_page' => 1, 'total_pages' => true]],]);
$response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'per_page' => 50, 'current_page' => 1, 'total_pages' => 1]],]);
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
@@ -218,34 +201,18 @@ class CategoryControllerTest extends TestCase
*/
public function testTransactionsRange(): void
{
$category = $this->user()->categories()->first();
// get some transactions using the collector:
Log::info('This transaction collector is OK, because it is used in a test:');
$collector = new TransactionCollector;
$collector->setUser($this->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
$collector->setAllAssetAccounts();
$collector->setLimit(5)->setPage(1);
try {
$paginator = $collector->getPaginatedTransactions();
} catch (FireflyException $e) {
$this->assertTrue(false, $e->getMessage());
}
// mock stuff:
$category = $this->user()->categories()->first();
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$repository = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$billRepos = $this->mock(BillRepositoryInterface::class);
$paginator = new LengthAwarePaginator(new Collection, 0, 50);
$categoryRepos->shouldReceive('setUser')->atLeast()->once();
$billRepos->shouldReceive('setUser');
$repository->shouldReceive('setUser');
$currencyRepository->shouldReceive('setUser');
$repository->shouldReceive('getNoteText')->atLeast()->once()->andReturn('Note');
$repository->shouldReceive('getMetaField')->atLeast()->once()->andReturn(null);
$repository->shouldReceive('getMetaDateString')->atLeast()->once()->andReturn('2018-01-01');
$collector->shouldReceive('setUser')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
@@ -257,21 +224,14 @@ class CategoryControllerTest extends TestCase
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator);
// mock some calls:
// test API
$response = $this->get(
route('api.v1.categories.transactions', [$category->id]) . '?' . http_build_query(['start' => '2018-01-01', 'end' => '2018-01-31'])
);
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(['meta' => ['pagination' => ['total' => true, 'count' => true, 'per_page' => 5, 'current_page' => 1, 'total_pages' => true]],]);
$response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'per_page' => 50, 'current_page' => 1, 'total_pages' => 1]],]);
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}