. */ 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; use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use Laravel\Passport\Passport; use Log; use Tests\TestCase; /** * * Class CategoryControllerTest */ class CategoryControllerTest extends TestCase { /** * */ public function setUp(): void { parent::setUp(); Passport::actingAs($this->user()); Log::info(sprintf('Now in %s.', \get_class($this))); } /** * Delete a category. * * @covers \FireflyIII\Api\V1\Controllers\CategoryController */ public function testDelete(): void { // mock stuff: $repository = $this->mock(CategoryRepositoryInterface::class); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('destroy')->once()->andReturn(true); // get category: $category = $this->user()->categories()->first(); // call API $response = $this->delete('/api/v1/categories/' . $category->id); $response->assertStatus(204); } /** * Show all categories * * @covers \FireflyIII\Api\V1\Controllers\CategoryController */ public function testIndex(): void { $categories = $this->user()->categories()->get(); // mock stuff: $repository = $this->mock(CategoryRepositoryInterface::class); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('getCategories')->once()->andReturn($categories); // call API $response = $this->get('/api/v1/categories'); $response->assertStatus(200); $response->assertSee($categories->first()->name); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } /** * Show a single category. * * @covers \FireflyIII\Api\V1\Controllers\CategoryController */ public function testShow(): void { $category = $this->user()->categories()->first(); // mock stuff: $repository = $this->mock(CategoryRepositoryInterface::class); // mock calls: $repository->shouldReceive('setUser')->once(); // call API $response = $this->get('/api/v1/categories/' . $category->id); $response->assertStatus(200); $response->assertSee($category->name); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } /** * Store a new category. * * @covers \FireflyIII\Api\V1\Controllers\CategoryController * @covers \FireflyIII\Api\V1\Requests\CategoryRequest */ public function testStore(): void { /** @var Category $category */ $category = $this->user()->categories()->first(); // mock stuff: $repository = $this->mock(CategoryRepositoryInterface::class); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('store')->once()->andReturn($category); // data to submit $data = [ 'name' => 'Some category', 'active' => '1', ]; // test API $response = $this->post('/api/v1/categories', $data); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'categories', 'links' => true],]); $response->assertSee($category->name); // the amount $response->assertHeader('Content-Type', 'application/vnd.api+json'); } /** * Show index. * * @covers \FireflyIII\Api\V1\Controllers\CategoryController */ 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: $repository = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $billRepos = $this->mock(BillRepositoryInterface::class); $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(); $collector->shouldReceive('withBudgetInformation')->andReturnSelf(); $collector->shouldReceive('setCategory')->andReturnSelf(); $collector->shouldReceive('removeFilter')->andReturnSelf(); $collector->shouldReceive('setLimit')->andReturnSelf(); $collector->shouldReceive('setPage')->andReturnSelf(); $collector->shouldReceive('setTypes')->andReturnSelf(); $collector->shouldReceive('setAllAssetAccounts')->andReturnSelf(); $collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator); // mock some calls: // test API $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(['links' => ['self' => true, 'first' => true, 'last' => true,],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } /** * Show index. * * @covers \FireflyIII\Api\V1\Controllers\CategoryController */ 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: $repository = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $billRepos = $this->mock(BillRepositoryInterface::class); $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(); $collector->shouldReceive('withBudgetInformation')->andReturnSelf(); $collector->shouldReceive('setCategory')->andReturnSelf(); $collector->shouldReceive('removeFilter')->andReturnSelf(); $collector->shouldReceive('setAllAssetAccounts')->andReturnSelf(); $collector->shouldReceive('setLimit')->andReturnSelf(); $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(['links' => ['self' => true, 'first' => true, 'last' => true,],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } /** * Update a category. * * @covers \FireflyIII\Api\V1\Controllers\CategoryController * @covers \FireflyIII\Api\V1\Requests\CategoryRequest */ public function testUpdate(): void { // mock repositories $repository = $this->mock(CategoryRepositoryInterface::class); /** @var Category $category */ $category = $this->user()->categories()->first(); // mock calls: $repository->shouldReceive('setUser'); $repository->shouldReceive('update')->once()->andReturn($category); // data to submit $data = [ 'name' => 'Some new category', 'active' => '1', ]; // test API $response = $this->put('/api/v1/categories/' . $category->id, $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'categories', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); $response->assertSee($category->name); } }