From c55b80f46752d0feec78d2879e49da309e78717d Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 25 Aug 2018 20:45:42 +0200 Subject: [PATCH] Update test coverage. --- app/Helpers/Attachments/AttachmentHelper.php | 5 ++ app/Helpers/Collection/Balance.php | 2 + app/Helpers/Collection/BalanceEntry.php | 2 + app/Helpers/Collection/BalanceHeader.php | 2 + app/Helpers/Collection/BalanceLine.php | 2 + app/Helpers/Collection/Bill.php | 2 + app/Helpers/Collection/BillLine.php | 2 + app/Helpers/Collection/Category.php | 2 + .../Collector/TransactionCollector.php | 2 + app/Helpers/Filter/CountAttachmentsFilter.php | 1 + app/Helpers/Filter/EmptyFilter.php | 2 + app/Helpers/Filter/InternalTransferFilter.php | 2 + app/Helpers/Filter/NegativeAmountFilter.php | 2 + app/Helpers/Filter/OpposingAccountFilter.php | 2 + app/Helpers/Filter/PositiveAmountFilter.php | 2 + app/Helpers/Filter/SplitIndicatorFilter.php | 2 + app/Helpers/Filter/TransactionViewFilter.php | 2 + app/Helpers/Filter/TransferFilter.php | 2 + .../AttachmentHelperTest.php | 52 +++++++++++++- .../Helpers/{ => Chart}/MetaPieChartTest.php | 12 +++- .../Unit/Helpers/Filter/AmountFilterTest.php | 69 +++++++++++++++++++ 21 files changed, 169 insertions(+), 2 deletions(-) rename tests/Unit/Helpers/{ => Attachments}/AttachmentHelperTest.php (78%) rename tests/Unit/Helpers/{ => Chart}/MetaPieChartTest.php (98%) create mode 100644 tests/Unit/Helpers/Filter/AmountFilterTest.php diff --git a/app/Helpers/Attachments/AttachmentHelper.php b/app/Helpers/Attachments/AttachmentHelper.php index 8714dfec07..9c9c7bf55e 100644 --- a/app/Helpers/Attachments/AttachmentHelper.php +++ b/app/Helpers/Attachments/AttachmentHelper.php @@ -144,9 +144,11 @@ class AttachmentHelper implements AttachmentHelperInterface { $resource = tmpfile(); if (false === $resource) { + // @codeCoverageIgnoreStart Log::error('Cannot create temp-file for file upload.'); return false; + // @codeCoverageIgnoreEnd } $path = stream_get_meta_data($resource)['uri']; fwrite($resource, $content); @@ -341,9 +343,12 @@ class AttachmentHelper implements AttachmentHelperInterface if (!$this->validMime($file)) { $result = false; } + // @codeCoverageIgnoreStart + // can't seem to reach this point. if (true === $result && !$this->validSize($file)) { $result = false; } + // @codeCoverageIgnoreEnd if (true === $result && $this->hasFile($file, $model)) { $result = false; } diff --git a/app/Helpers/Collection/Balance.php b/app/Helpers/Collection/Balance.php index b10674cefd..71573853ea 100644 --- a/app/Helpers/Collection/Balance.php +++ b/app/Helpers/Collection/Balance.php @@ -26,6 +26,8 @@ use Illuminate\Support\Collection; /** * Class Balance. + * + * @codeCoverageIgnore */ class Balance { diff --git a/app/Helpers/Collection/BalanceEntry.php b/app/Helpers/Collection/BalanceEntry.php index 8f2e4fb195..86eddf607b 100644 --- a/app/Helpers/Collection/BalanceEntry.php +++ b/app/Helpers/Collection/BalanceEntry.php @@ -26,6 +26,8 @@ use FireflyIII\Models\Account as AccountModel; /** * Class BalanceEntry. + * + * @codeCoverageIgnore */ class BalanceEntry { diff --git a/app/Helpers/Collection/BalanceHeader.php b/app/Helpers/Collection/BalanceHeader.php index 46c3091333..3769b0f5c0 100644 --- a/app/Helpers/Collection/BalanceHeader.php +++ b/app/Helpers/Collection/BalanceHeader.php @@ -27,6 +27,8 @@ use Illuminate\Support\Collection; /** * Class BalanceHeader. + * + * @codeCoverageIgnore */ class BalanceHeader { diff --git a/app/Helpers/Collection/BalanceLine.php b/app/Helpers/Collection/BalanceLine.php index a679ef66ff..89668f0963 100644 --- a/app/Helpers/Collection/BalanceLine.php +++ b/app/Helpers/Collection/BalanceLine.php @@ -29,6 +29,8 @@ use Illuminate\Support\Collection; /** * Class BalanceLine. + * + * @codeCoverageIgnore */ class BalanceLine { diff --git a/app/Helpers/Collection/Bill.php b/app/Helpers/Collection/Bill.php index 9ce761345d..f950a93914 100644 --- a/app/Helpers/Collection/Bill.php +++ b/app/Helpers/Collection/Bill.php @@ -29,6 +29,8 @@ use Log; /** * Class Bill. + * + * @codeCoverageIgnore */ class Bill { diff --git a/app/Helpers/Collection/BillLine.php b/app/Helpers/Collection/BillLine.php index 4be29cb07a..eb1e4c8fee 100644 --- a/app/Helpers/Collection/BillLine.php +++ b/app/Helpers/Collection/BillLine.php @@ -27,6 +27,8 @@ use FireflyIII\Models\Bill as BillModel; /** * Class BillLine. + * + * @codeCoverageIgnore */ class BillLine { diff --git a/app/Helpers/Collection/Category.php b/app/Helpers/Collection/Category.php index 9868bdb945..ce774387ae 100644 --- a/app/Helpers/Collection/Category.php +++ b/app/Helpers/Collection/Category.php @@ -27,6 +27,8 @@ use Illuminate\Support\Collection; /** * Class Category. + * + * @codeCoverageIgnore */ class Category { diff --git a/app/Helpers/Collector/TransactionCollector.php b/app/Helpers/Collector/TransactionCollector.php index b397203fcd..cb06889b14 100644 --- a/app/Helpers/Collector/TransactionCollector.php +++ b/app/Helpers/Collector/TransactionCollector.php @@ -52,6 +52,8 @@ use Log; /** * Class TransactionCollector + * + * @codeCoverageIgnore */ class TransactionCollector implements TransactionCollectorInterface { diff --git a/app/Helpers/Filter/CountAttachmentsFilter.php b/app/Helpers/Filter/CountAttachmentsFilter.php index bcb61fa3c2..c47cb838d9 100644 --- a/app/Helpers/Filter/CountAttachmentsFilter.php +++ b/app/Helpers/Filter/CountAttachmentsFilter.php @@ -31,6 +31,7 @@ use Illuminate\Support\Collection; /** * Class CountAttachmentsFilter + * @codeCoverageIgnore */ class CountAttachmentsFilter implements FilterInterface { diff --git a/app/Helpers/Filter/EmptyFilter.php b/app/Helpers/Filter/EmptyFilter.php index 2235fe2bfa..716b3d9cdb 100644 --- a/app/Helpers/Filter/EmptyFilter.php +++ b/app/Helpers/Filter/EmptyFilter.php @@ -26,6 +26,8 @@ use Illuminate\Support\Collection; /** * Class EmptyFilter. + * + * @codeCoverageIgnore */ class EmptyFilter implements FilterInterface { diff --git a/app/Helpers/Filter/InternalTransferFilter.php b/app/Helpers/Filter/InternalTransferFilter.php index 1442db3d20..a72474c4e0 100644 --- a/app/Helpers/Filter/InternalTransferFilter.php +++ b/app/Helpers/Filter/InternalTransferFilter.php @@ -32,6 +32,8 @@ use Log; * This filter removes any filters that are from A to B or from B to A given a set of * account id's (in $parameters) where A and B are mentioned. So transfers between the mentioned * accounts will be removed. + * + * @codeCoverageIgnore */ class InternalTransferFilter implements FilterInterface { diff --git a/app/Helpers/Filter/NegativeAmountFilter.php b/app/Helpers/Filter/NegativeAmountFilter.php index b6b0f10cf6..58ebc6ed3b 100644 --- a/app/Helpers/Filter/NegativeAmountFilter.php +++ b/app/Helpers/Filter/NegativeAmountFilter.php @@ -30,6 +30,8 @@ use Log; * Class NegativeAmountFilter. * * This filter removes entries with a negative amount (the original modifier is -1). + * + * @codeCoverageIgnore */ class NegativeAmountFilter implements FilterInterface { diff --git a/app/Helpers/Filter/OpposingAccountFilter.php b/app/Helpers/Filter/OpposingAccountFilter.php index 5df2170ce6..681a4c26b2 100644 --- a/app/Helpers/Filter/OpposingAccountFilter.php +++ b/app/Helpers/Filter/OpposingAccountFilter.php @@ -31,6 +31,8 @@ use Log; * * This filter is similar to the internal transfer filter but only removes transactions when the opposing account is * amongst $parameters (list of account ID's). + * + * @codeCoverageIgnore */ class OpposingAccountFilter implements FilterInterface { diff --git a/app/Helpers/Filter/PositiveAmountFilter.php b/app/Helpers/Filter/PositiveAmountFilter.php index 155c6c3f33..257bdf43b4 100644 --- a/app/Helpers/Filter/PositiveAmountFilter.php +++ b/app/Helpers/Filter/PositiveAmountFilter.php @@ -33,6 +33,8 @@ use Log; * * This filter removes transactions with either a positive amount ($parameters = 1) or a negative amount * ($parameter = -1). This is helpful when a Collection has you with both transactions in a journal. + * + * @codeCoverageIgnore */ class PositiveAmountFilter implements FilterInterface { diff --git a/app/Helpers/Filter/SplitIndicatorFilter.php b/app/Helpers/Filter/SplitIndicatorFilter.php index afc42c30c5..609c3a424e 100644 --- a/app/Helpers/Filter/SplitIndicatorFilter.php +++ b/app/Helpers/Filter/SplitIndicatorFilter.php @@ -30,6 +30,8 @@ use Illuminate\Support\Collection; /** * Class SplitIndicatorFilter + * + * @codeCoverageIgnore */ class SplitIndicatorFilter implements FilterInterface { diff --git a/app/Helpers/Filter/TransactionViewFilter.php b/app/Helpers/Filter/TransactionViewFilter.php index 1c8dc2938a..baae12173d 100644 --- a/app/Helpers/Filter/TransactionViewFilter.php +++ b/app/Helpers/Filter/TransactionViewFilter.php @@ -35,6 +35,8 @@ use Log; * * This is used in the mass-edit routine. * + * @codeCoverageIgnore + * */ class TransactionViewFilter implements FilterInterface { diff --git a/app/Helpers/Filter/TransferFilter.php b/app/Helpers/Filter/TransferFilter.php index 40f2037a3b..26037700a8 100644 --- a/app/Helpers/Filter/TransferFilter.php +++ b/app/Helpers/Filter/TransferFilter.php @@ -30,6 +30,8 @@ use Illuminate\Support\Collection; * Class TransferFilter. * * This filter removes any transfers that are in the collection twice (from A to B and from B to A). + * + * @codeCoverageIgnore */ class TransferFilter implements FilterInterface { diff --git a/tests/Unit/Helpers/AttachmentHelperTest.php b/tests/Unit/Helpers/Attachments/AttachmentHelperTest.php similarity index 78% rename from tests/Unit/Helpers/AttachmentHelperTest.php rename to tests/Unit/Helpers/Attachments/AttachmentHelperTest.php index f70d66464a..4ecafc8d1a 100644 --- a/tests/Unit/Helpers/AttachmentHelperTest.php +++ b/tests/Unit/Helpers/Attachments/AttachmentHelperTest.php @@ -20,14 +20,16 @@ */ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Helpers\Attachments; +use Crypt; use FireflyIII\Helpers\Attachments\AttachmentHelper; use FireflyIII\Models\Attachment; use FireflyIII\Models\TransactionJournal; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Tests\TestCase; +use Log; /** * Class AttachmentHelperTest @@ -38,6 +40,15 @@ use Tests\TestCase; */ class AttachmentHelperTest extends TestCase { + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + /** * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper */ @@ -97,6 +108,45 @@ class AttachmentHelperTest extends TestCase Storage::disk('upload')->assertExists(sprintf('at-%d.data', $attachments->first()->id)); } + /** + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper + */ + public function testSaveAttachmentFromApi(): void + { + // mock calls: + Crypt::shouldReceive('encrypt')->times(2)->andReturn('Some encrypted content'); + Storage::fake('upload'); + + $path = public_path('apple-touch-icon.png'); + $helper = new AttachmentHelper; + $attachment = Attachment::first(); + + // call helper + $result = $helper->saveAttachmentFromApi($attachment, file_get_contents($path)); + + $this->assertTrue($result); + + } + + /** + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper + */ + public function testSaveAttachmentFromApiBadMime(): void + { + // mock calls: + Storage::fake('upload'); + + $path = public_path('browserconfig.xml'); + $helper = new AttachmentHelper; + $attachment = Attachment::first(); + + // call helper + $result = $helper->saveAttachmentFromApi($attachment, file_get_contents($path)); + + $this->assertFalse($result); + + } + /** * Test double file upload. Needs to be after testSave. * diff --git a/tests/Unit/Helpers/MetaPieChartTest.php b/tests/Unit/Helpers/Chart/MetaPieChartTest.php similarity index 98% rename from tests/Unit/Helpers/MetaPieChartTest.php rename to tests/Unit/Helpers/Chart/MetaPieChartTest.php index 9a30d81e47..5b44be9c61 100644 --- a/tests/Unit/Helpers/MetaPieChartTest.php +++ b/tests/Unit/Helpers/Chart/MetaPieChartTest.php @@ -20,7 +20,7 @@ */ declare(strict_types=1); -namespace Tests\Unit\Helpers; +namespace Tests\Unit\Helpers\Chart; use Carbon\Carbon; use FireflyIII\Helpers\Chart\MetaPieChart; @@ -35,6 +35,7 @@ use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Illuminate\Support\Collection; use Tests\TestCase; +use Log; /** * Class MetaPieChartTest @@ -45,6 +46,15 @@ use Tests\TestCase; */ class MetaPieChartTest extends TestCase { + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + /** * @covers \FireflyIII\Helpers\Chart\MetaPieChart */ diff --git a/tests/Unit/Helpers/Filter/AmountFilterTest.php b/tests/Unit/Helpers/Filter/AmountFilterTest.php new file mode 100644 index 0000000000..ae68185e44 --- /dev/null +++ b/tests/Unit/Helpers/Filter/AmountFilterTest.php @@ -0,0 +1,69 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Helpers\Filter; + + +use FireflyIII\Helpers\Filter\AmountFilter; +use FireflyIII\Models\Transaction; +use Illuminate\Support\Collection; +use Log; +use Tests\TestCase; + +/** + * + * Class AmountFilterTest + */ +class AmountFilterTest extends TestCase +{ + /** + * + */ + public function setUp(): void + { + parent::setUp(); + Log::debug(sprintf('Now in %s.', \get_class($this))); + } + + /** + * @covers \FireflyIII\Helpers\Filter\AmountFilter + */ + public function testBasicPositive(): void + { + $count = 0; + $collection = new Collection; + for ($i = 0; $i < 10; $i++) { + $amount = random_int(-10, 10); + $transaction = new Transaction; + $transaction->transaction_amount = (string)$amount; + if ($amount <= 0) { + $count++; + } + $collection->push($transaction); + } + + $filter = new AmountFilter(1); + $result = $filter->filter($collection); + $this->assertCount($count, $result); + } +} \ No newline at end of file