New test code.

This commit is contained in:
James Cole 2018-07-02 20:02:20 +02:00
parent f27eb084c7
commit 2e67bd3b78
8 changed files with 2083 additions and 11 deletions

View File

@ -68,13 +68,13 @@ class JournalLinkController extends Controller
/**
* Delete the resource.
*
* @param string $object
* @param TransactionJournalLink $link
*
* @return JsonResponse
*/
public function delete(string $object): JsonResponse
public function delete(TransactionJournalLink $link): JsonResponse
{
// todo delete object.
$this->repository->destroyLink($link);
return response()->json([], 204);
}
@ -88,8 +88,6 @@ class JournalLinkController extends Controller
*/
public function index(Request $request): JsonResponse
{
// create some objects:
$manager = new Manager;
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';

View File

@ -66,7 +66,7 @@ class RecurrenceController extends Controller
/**
* Delete the resource.
*
* @param string $object
* @param Recurrence $recurrence
*
* @return JsonResponse
*/

View File

@ -30,6 +30,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Rules\BelongsUser;
use Illuminate\Validation\Validator;
use InvalidArgumentException;
use Log;
/**
* Class RecurrenceRequest
@ -208,6 +209,7 @@ class RecurrenceRequest extends Request
$repository = app(AccountRepositoryInterface::class);
$repository->setUser(auth()->user());
$set = $repository->getAccountsById([$accountId]);
Log::debug(sprintf('Count of accounts found by ID %d is: %d', $accountId, $set->count()));
if ($set->count() === 1) {
/** @var Account $first */
$first = $set->first();
@ -399,7 +401,7 @@ class RecurrenceRequest extends Request
if (null !== $repetitions && null !== $repeatUntil) {
// expect a date OR count:
$validator->errors()->add('repeat_until', trans('validation.require_repeat_until'));
$validator->errors()->add('repetitions', trans('validation.require_repeat_until'));
$validator->errors()->add('nr_of_repetitions', trans('validation.require_repeat_until'));
return;
}

View File

@ -38,7 +38,7 @@ return [
'belongs_user' => 'This value is invalid for this field.',
'at_least_one_transaction' => 'Need at least one transaction.',
'at_least_one_repetition' => 'Need at least one repetition.',
'require_repeat_until' => 'Require either a number of repetitions, or an end date (repeats_until). Not both.',
'require_repeat_until' => 'Require either a number of repetitions, or an end date (repeat_until). Not both.',
'require_currency_info' => 'The content of this field is invalid without currency information.',
'equal_description' => 'Transaction description should not equal global description.',
'file_invalid_mime' => 'File ":name" is of type ":mime" which is not accepted as a new upload.',

View File

@ -24,7 +24,7 @@ declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use Laravel\Passport\Passport;
use Log;
@ -87,6 +87,7 @@ class CategoryControllerTest extends TestCase
$response = $this->get('/api/v1/categories');
$response->assertStatus(200);
$response->assertSee($categories->first()->name);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
@ -107,6 +108,7 @@ class CategoryControllerTest extends TestCase
$response = $this->get('/api/v1/categories/' . $category->id);
$response->assertStatus(200);
$response->assertSee($category->name);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
@ -116,7 +118,7 @@ class CategoryControllerTest extends TestCase
*/
public function testStore(): void
{
/** @var Budget $category */
/** @var Category $category */
$category = $this->user()->categories()->first();
// mock stuff:
@ -150,7 +152,7 @@ class CategoryControllerTest extends TestCase
// mock repositories
$repository = $this->mock(CategoryRepositoryInterface::class);
/** @var Budget $category */
/** @var Category $category */
$category = $this->user()->categories()->first();
// mock calls:

View File

@ -0,0 +1,239 @@
<?php
/**
* JournalLinkControllerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
use Illuminate\Support\Collection;
use Laravel\Passport\Passport;
use Log;
use Tests\TestCase;
/**
*
* Class JournalLinkControllerTest
*/
class JournalLinkControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Api\V1\Controllers\JournalLinkController
*/
public function testDelete(): void
{
// mock stuff:
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('destroyLink')->once()->andReturn(true);
// get a link
/** @var TransactionJournalLink $journalLink */
$journalLink = TransactionJournalLink::first();
// call API
$response = $this->delete('/api/v1/journal_links/' . $journalLink->id);
$response->assertStatus(204);
}
/**
* @covers \FireflyIII\Api\V1\Controllers\JournalLinkController
*/
public function testIndex(): void
{
$journalLinks = TransactionJournalLink::get();
$transaction = Transaction::first();
$transaction->date = new Carbon;
$transaction->transaction_type_type = 'Withdrawal';
// mock stuff:
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('findByName')->once()->andReturn(null);
$repository->shouldReceive('getJournalLinks')->once()->andReturn($journalLinks);
$journalRepos->shouldReceive('setUser')->once();
$collector->shouldReceive('setUser')->withAnyArgs();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setJournals')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
// call API
$response = $this->get('/api/v1/journal_links');
$response->assertStatus(200);
$response->assertSee($journalLinks->first()->id);
}
/**
* @covers \FireflyIII\Api\V1\Controllers\JournalLinkController
*/
public function testShow(): void
{
$journalLink = TransactionJournalLink::first();
$transaction = Transaction::first();
$transaction->date = new Carbon;
$transaction->transaction_type_type = 'Withdrawal';
// mock stuff:
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('setUser')->once();
$collector->shouldReceive('setUser')->withAnyArgs();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setJournals')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
// call API
$response = $this->get('/api/v1/journal_links/' . $journalLink->id);
$response->assertStatus(200);
$response->assertSee($journalLink->id);
}
/**
* @covers \FireflyIII\Api\V1\Controllers\JournalLinkController
* @covers \FireflyIII\Api\V1\Requests\JournalLinkRequest
*/
public function testStore(): void
{
$journalLink = TransactionJournalLink::first();
$journal = $this->user()->transactionJournals()->find(1);
$transaction = Transaction::first();
$transaction->date = new Carbon;
$transaction->transaction_type_type = 'Withdrawal';
// mock stuff:
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('setUser')->once();
$collector->shouldReceive('setUser')->withAnyArgs();
$collector->shouldReceive('setUser')->withAnyArgs();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setJournals')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$journalRepos->shouldReceive('findNull')->andReturn($journal);
$repository->shouldReceive('storeLink')->once()->andReturn($journalLink);
// data to submit
$data = [
'link_type_id' => '1',
'inward_id' => '1',
'outward_id' => '2',
'notes' => 'Some notes',
];
// test API
$response = $this->post('/api/v1/journal_links', $data);
$response->assertStatus(200);
$response->assertSee($journalLink->created_at->toAtomString()); // the creation moment.
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\JournalLinkController
* @covers \FireflyIII\Api\V1\Requests\JournalLinkRequest
*/
public function testUpdate(): void
{
// mock repositories
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$journalLink = TransactionJournalLink::first();
$journal = $this->user()->transactionJournals()->find(1);
$transaction = Transaction::first();
$transaction->date = new Carbon;
$transaction->transaction_type_type = 'Withdrawal';
// mock calls:
$repository->shouldReceive('setUser');
$journalRepos->shouldReceive('setUser')->once();
$collector->shouldReceive('setUser')->withAnyArgs();
$collector->shouldReceive('setUser')->withAnyArgs();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setJournals')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$journalRepos->shouldReceive('findNull')->andReturn($journal);
$repository->shouldReceive('updateLink')->once()->andReturn($journalLink);
// data to submit
$data = [
'link_type_id' => '1',
'inward_id' => '1',
'outward_id' => '2',
'notes' => 'Some notes',
];
// test API
$response = $this->put('/api/v1/journal_links/' . $journalLink->id, $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertSee($journalLink->created_at->toAtomString()); // the creation moment.
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
}

View File

@ -0,0 +1,200 @@
<?php
/**
* LinkTypeControllerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
use FireflyIII\Models\LinkType;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Laravel\Passport\Passport;
use Log;
use Tests\TestCase;
/**
*
* Class LinkTypeControllerTest
*/
class LinkTypeControllerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Passport::actingAs($this->user());
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* @covers \FireflyIII\Api\V1\Controllers\LinkTypeController
*/
public function testDelete(): void
{
// mock stuff:
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$userRepository = $this->mock(UserRepositoryInterface::class);
// create editable link type:
$linkType = LinkType::create(
[
'name' => 'random' . random_int(1, 100000),
'outward' => 'outward' . random_int(1, 100000),
'inward' => 'inward ' . random_int(1, 100000),
'editable' => true,
]
);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('destroy')->once()->andReturn(true);
// call API
$response = $this->delete('/api/v1/link_types/' . $linkType->id);
$response->assertStatus(204);
}
/**
* @covers \FireflyIII\Api\V1\Controllers\LinkTypeController
*/
public function testIndex(): void
{
$linkTypes = LinkType::get();
// mock stuff:
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$userRepository = $this->mock(UserRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('get')->once()->andReturn($linkTypes);
// call API
$response = $this->get('/api/v1/link_types');
$response->assertStatus(200);
$response->assertSee($linkTypes->first()->created_at->toAtomString());
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\LinkTypeController
*/
public function testShow(): void
{
$linkType = LinkType::first();
// mock stuff:
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$userRepository = $this->mock(UserRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
// call API
$response = $this->get('/api/v1/link_types/' . $linkType->id);
$response->assertStatus(200);
$response->assertSee($linkType->first()->created_at->toAtomString());
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\LinkTypeController
* @covers \FireflyIII\Api\V1\Requests\LinkTypeRequest
*/
public function testStore(): void
{
$linkType = LinkType::first();
// mock stuff:
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$userRepository = $this->mock(UserRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('store')->once()->andReturn($linkType);
$userRepository->shouldReceive('hasRole')->once()->andReturn(true);
// data to submit
$data = [
'name' => 'random' . random_int(1, 100000),
'outward' => 'outward' . random_int(1, 100000),
'inward' => 'inward ' . random_int(1, 100000),
'editable' => true,
];
// test API
$response = $this->post('/api/v1/link_types', $data);
$response->assertStatus(200);
$response->assertSee($linkType->created_at->toAtomString());
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\LinkTypeController
* @covers \FireflyIII\Api\V1\Requests\LinkTypeRequest
*/
public function testUpdate(): void
{
// mock stuff:
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$userRepository = $this->mock(UserRepositoryInterface::class);
$userRepository->shouldReceive('hasRole')->once()->andReturn(true);
// create editable link type:
$linkType = LinkType::create(
[
'name' => 'random' . random_int(1, 100000),
'outward' => 'outward' . random_int(1, 100000),
'inward' => 'inward ' . random_int(1, 100000),
'editable' => true,
]
);
// mock calls:
$repository->shouldReceive('setUser');
$repository->shouldReceive('update')->once()->andReturn($linkType);
// data to submit
$data = [
'name' => 'random' . random_int(1, 100000),
'outward' => 'outward' . random_int(1, 100000),
'inward' => 'inward ' . random_int(1, 100000),
'editable' => true,
];
// test API
$response = $this->put('/api/v1/link_types/' . $linkType->id, $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertSee($linkType->created_at->toAtomString());
}
}

File diff suppressed because it is too large Load Diff