mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-03 12:10:42 -06:00
Updated code coverage, improved binder code.
This commit is contained in:
parent
5cb948d0f3
commit
a3ec741d67
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Support\Binder;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
@ -31,52 +32,41 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
*/
|
||||
class AccountList implements BinderInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @param $route
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public static function routeBinder($value, $route): Collection
|
||||
public static function routeBinder(string $value, Route $route): Collection
|
||||
{
|
||||
if (auth()->check()) {
|
||||
$ids = explode(',', $value);
|
||||
// filter ids:
|
||||
$ids = self::filterIds($ids);
|
||||
$list = [];
|
||||
$incoming = explode(',', $value);
|
||||
foreach ($incoming as $entry) {
|
||||
$list[] = intval($entry);
|
||||
}
|
||||
$list = array_unique($list);
|
||||
if (count($list) === 0) {
|
||||
throw new NotFoundHttpException; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/** @var \Illuminate\Support\Collection $object */
|
||||
$object = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->whereIn('accounts.id', $ids)
|
||||
->where('user_id', auth()->user()->id)
|
||||
->get(['accounts.*']);
|
||||
if ($object->count() > 0) {
|
||||
$object = $object->sortBy(
|
||||
/** @var \Illuminate\Support\Collection $collection */
|
||||
$collection = auth()->user()->accounts()
|
||||
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
||||
->whereIn('accounts.id', $list)
|
||||
->get(['accounts.*']);
|
||||
if ($collection->count() > 0) {
|
||||
$collection = $collection->sortBy(
|
||||
function (Account $account) {
|
||||
return $account->name;
|
||||
}
|
||||
);
|
||||
|
||||
return $object;
|
||||
return $collection;
|
||||
}
|
||||
}
|
||||
throw new NotFoundHttpException;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $ids
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected static function filterIds(array $ids): array
|
||||
{
|
||||
$new = [];
|
||||
foreach ($ids as $id) {
|
||||
if (intval($id) > 0) {
|
||||
$new[] = $id;
|
||||
}
|
||||
}
|
||||
$new = array_unique($new);
|
||||
|
||||
return $new;
|
||||
}
|
||||
}
|
||||
|
@ -22,16 +22,18 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Binder;
|
||||
|
||||
use Illuminate\Routing\Route;
|
||||
|
||||
/**
|
||||
* Interface BinderInterface.
|
||||
*/
|
||||
interface BinderInterface
|
||||
{
|
||||
/**
|
||||
* @param $value
|
||||
* @param $route
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function routeBinder($value, $route);
|
||||
public static function routeBinder(string $value, Route $route);
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Support\Binder;
|
||||
|
||||
use FireflyIII\Models\Budget;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
@ -32,12 +33,12 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
class BudgetList implements BinderInterface
|
||||
{
|
||||
/**
|
||||
* @param $value
|
||||
* @param $route
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
* @return Collection
|
||||
*/
|
||||
public static function routeBinder($value, $route): Collection
|
||||
public static function routeBinder(string $value, Route $route): Collection
|
||||
{
|
||||
if (auth()->check()) {
|
||||
$ids = explode(',', $value);
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Support\Binder;
|
||||
|
||||
use FireflyIII\Models\Category;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
@ -32,12 +33,12 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
class CategoryList implements BinderInterface
|
||||
{
|
||||
/**
|
||||
* @param $value
|
||||
* @param $route
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
* @return Collection
|
||||
*/
|
||||
public static function routeBinder($value, $route): Collection
|
||||
public static function routeBinder(string $value, Route $route): Collection
|
||||
{
|
||||
if (auth()->check()) {
|
||||
$ids = explode(',', $value);
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Support\Binder;
|
||||
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use Illuminate\Routing\Route;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
@ -31,15 +32,15 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
class CurrencyCode implements BinderInterface
|
||||
{
|
||||
/**
|
||||
* @param $value
|
||||
* @param $route
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
public static function routeBinder($value, $route)
|
||||
public static function routeBinder(string $value, Route $route): TransactionCurrency
|
||||
{
|
||||
if (auth()->check()) {
|
||||
$currency = TransactionCurrency::where('code', $value)->first();
|
||||
$currency = TransactionCurrency::where('code', trim($value))->first();
|
||||
if (null !== $currency) {
|
||||
return $currency;
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ namespace FireflyIII\Support\Binder;
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use FireflyIII\Helpers\FiscalHelper;
|
||||
use Illuminate\Routing\Route;
|
||||
use Log;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
@ -34,12 +35,12 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
class Date implements BinderInterface
|
||||
{
|
||||
/**
|
||||
* @param $value
|
||||
* @param $route
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
* @return Carbon
|
||||
*/
|
||||
public static function routeBinder($value, $route): Carbon
|
||||
public static function routeBinder(string $value, Route $route): Carbon
|
||||
{
|
||||
$fiscalHelper = new FiscalHelper;
|
||||
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Support\Binder;
|
||||
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
@ -32,12 +33,12 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
class JournalList implements BinderInterface
|
||||
{
|
||||
/**
|
||||
* @param $value
|
||||
* @param $route
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function routeBinder($value, $route): Collection
|
||||
public static function routeBinder(string $value, Route $route): Collection
|
||||
{
|
||||
if (auth()->check()) {
|
||||
$ids = explode(',', $value);
|
||||
|
@ -24,6 +24,7 @@ namespace FireflyIII\Support\Binder;
|
||||
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
@ -33,12 +34,12 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
class TagList implements BinderInterface
|
||||
{
|
||||
/**
|
||||
* @param $value
|
||||
* @param $route
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
* @return Collection
|
||||
*/
|
||||
public static function routeBinder($value, $route): Collection
|
||||
public static function routeBinder(string $value, Route $route): Collection
|
||||
{
|
||||
if (auth()->check()) {
|
||||
$tags = explode(',', $value);
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Support\Binder;
|
||||
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Routing\Route;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
@ -31,20 +32,20 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
class UnfinishedJournal implements BinderInterface
|
||||
{
|
||||
/**
|
||||
* @param $value
|
||||
* @param $route
|
||||
* @param string $value
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
* @return TransactionJournal
|
||||
*/
|
||||
public static function routeBinder($value, $route): TransactionJournal
|
||||
public static function routeBinder(string $value, Route $route): TransactionJournal
|
||||
{
|
||||
if (auth()->check()) {
|
||||
$object = TransactionJournal::where('transaction_journals.id', $value)
|
||||
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
||||
->where('completed', 0)
|
||||
->where('user_id', auth()->user()->id)->first(['transaction_journals.*']);
|
||||
if ($object) {
|
||||
return $object;
|
||||
$journal = auth()->user()->transactionJournals()->where('transaction_journals.id', $value)
|
||||
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
||||
->where('completed', 0)
|
||||
->where('user_id', auth()->user()->id)->first(['transaction_journals.*']);
|
||||
if (!is_null($journal)) {
|
||||
return $journal;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@ namespace FireflyIII\Support\Facades;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
* Class Amount.
|
||||
*/
|
||||
class Amount extends Facade
|
||||
|
@ -25,7 +25,8 @@ namespace FireflyIII\Support\Facades;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* Class Amount.
|
||||
* @codeCoverageIgnore
|
||||
* Class ExpandedForm.
|
||||
*/
|
||||
class ExpandedForm extends Facade
|
||||
{
|
||||
|
@ -25,6 +25,7 @@ namespace FireflyIII\Support\Facades;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
* Class FireflyConfig.
|
||||
*/
|
||||
class FireflyConfig extends Facade
|
||||
|
@ -25,6 +25,7 @@ namespace FireflyIII\Support\Facades;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
* Class Navigation.
|
||||
*/
|
||||
class Navigation extends Facade
|
||||
|
@ -25,6 +25,7 @@ namespace FireflyIII\Support\Facades;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
* Class Preferences.
|
||||
*/
|
||||
class Preferences extends Facade
|
||||
|
@ -25,6 +25,7 @@ namespace FireflyIII\Support\Facades;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
* Class Steam.
|
||||
*/
|
||||
class Steam extends Facade
|
||||
|
@ -25,6 +25,7 @@ namespace Tests\Unit\Helpers;
|
||||
|
||||
|
||||
use FireflyIII\Http\Middleware\Binder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Route;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Tests\TestCase;
|
||||
@ -55,6 +56,80 @@ class BinderTest extends TestCase
|
||||
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::handle
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::__construct
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::performBinding
|
||||
* @covers \FireflyIII\Support\Binder\AccountList::routeBinder
|
||||
*/
|
||||
public function testAccountList()
|
||||
{
|
||||
Route::middleware(Binder::class)->any(
|
||||
'/_test/binder/{accountList}', function (Collection $accounts) {
|
||||
return 'count: ' . $accounts->count();
|
||||
}
|
||||
);
|
||||
$this->be($this->user());
|
||||
$response = $this->get('/_test/binder/1,2');
|
||||
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||
$response->assertSee('count: 2');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::handle
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::__construct
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::performBinding
|
||||
* @covers \FireflyIII\Support\Binder\AccountList::routeBinder
|
||||
*/
|
||||
public function testAccountListEmpty()
|
||||
{
|
||||
Route::middleware(Binder::class)->any(
|
||||
'/_test/binder/{accountList}', function (Collection $accounts) {
|
||||
return 'count: ' . $accounts->count();
|
||||
}
|
||||
);
|
||||
$this->be($this->user());
|
||||
$response = $this->get('/_test/binder/');
|
||||
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::handle
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::__construct
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::performBinding
|
||||
* @covers \FireflyIII\Support\Binder\AccountList::routeBinder
|
||||
*/
|
||||
public function testAccountListInvalid()
|
||||
{
|
||||
Route::middleware(Binder::class)->any(
|
||||
'/_test/binder/{accountList}', function (Collection $accounts) {
|
||||
return 'count: ' . $accounts->count();
|
||||
}
|
||||
);
|
||||
$this->be($this->user());
|
||||
$response = $this->get('/_test/binder/0,1,2');
|
||||
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||
$response->assertSee('count: 2');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::handle
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::__construct
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::performBinding
|
||||
* @covers \FireflyIII\Support\Binder\AccountList::routeBinder
|
||||
*/
|
||||
public function testAccountListNotLoggedIn()
|
||||
{
|
||||
Route::middleware(Binder::class)->any(
|
||||
'/_test/binder/{accountList}', function (Collection $accounts) {
|
||||
return 'count: ' . $accounts->count();
|
||||
}
|
||||
);
|
||||
$response = $this->get('/_test/binder/1,2');
|
||||
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::handle
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::__construct
|
||||
@ -92,7 +167,6 @@ class BinderTest extends TestCase
|
||||
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::handle
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::__construct
|
||||
@ -1063,5 +1137,60 @@ class BinderTest extends TestCase
|
||||
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::handle
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::__construct
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::performBinding
|
||||
* @covers \FireflyIII\Support\Binder\UnfinishedJournal::routeBinder
|
||||
*/
|
||||
public function testUnfinishedJournal()
|
||||
{
|
||||
$journal = $this->user()->transactionJournals()->where('completed', 0)->first();
|
||||
Route::middleware(Binder::class)->any(
|
||||
'/_test/binder/{unfinishedJournal}', function () {
|
||||
return 'OK';
|
||||
}
|
||||
);
|
||||
$this->be($this->user());
|
||||
$response = $this->get('/_test/binder/' . $journal->id);
|
||||
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::handle
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::__construct
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::performBinding
|
||||
* @covers \FireflyIII\Support\Binder\UnfinishedJournal::routeBinder
|
||||
*/
|
||||
public function testUnfinishedJournalFinished()
|
||||
{
|
||||
$journal = $this->user()->transactionJournals()->where('completed', 1)->first();
|
||||
Route::middleware(Binder::class)->any(
|
||||
'/_test/binder/{unfinishedJournal}', function () {
|
||||
return 'OK';
|
||||
}
|
||||
);
|
||||
$response = $this->get('/_test/binder/' . $journal->id);
|
||||
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::handle
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::__construct
|
||||
* @covers \FireflyIII\Http\Middleware\Binder::performBinding
|
||||
* @covers \FireflyIII\Support\Binder\UnfinishedJournal::routeBinder
|
||||
*/
|
||||
public function testUnfinishedJournalNotLoggedIn()
|
||||
{
|
||||
$journal = $this->user()->transactionJournals()->where('completed', 0)->first();
|
||||
Route::middleware(Binder::class)->any(
|
||||
'/_test/binder/{unfinishedJournal}', function () {
|
||||
return 'OK';
|
||||
}
|
||||
);
|
||||
$response = $this->get('/_test/binder/' . $journal->id);
|
||||
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user