From 78ba0f749c11b83ba3e523e794a78ca614eb5f6a Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 16 Feb 2018 22:47:08 +0100 Subject: [PATCH] tests for bill and attachment transformers. --- app/Transformers/AttachmentTransformer.php | 8 + app/Transformers/BillTransformer.php | 13 ++ .../AttachmentTransformerTest.php | 64 ++++++++ .../Unit/Transformers/BillTransformerTest.php | 145 ++++++++++++++++++ 4 files changed, 230 insertions(+) create mode 100644 tests/Unit/Transformers/AttachmentTransformerTest.php create mode 100644 tests/Unit/Transformers/BillTransformerTest.php diff --git a/app/Transformers/AttachmentTransformer.php b/app/Transformers/AttachmentTransformer.php index bbbf45a47f..18e990cc67 100644 --- a/app/Transformers/AttachmentTransformer.php +++ b/app/Transformers/AttachmentTransformer.php @@ -53,6 +53,8 @@ class AttachmentTransformer extends TransformerAbstract /** * BillTransformer constructor. * + * @codeCoverageIgnore + * * @param ParameterBag $parameters */ public function __construct(ParameterBag $parameters) @@ -61,6 +63,10 @@ class AttachmentTransformer extends TransformerAbstract } /** + * Attach the user. + * + * @codeCoverageIgnore + * * @param Attachment $attachment * * @return Item @@ -71,6 +77,8 @@ class AttachmentTransformer extends TransformerAbstract } /** + * Transform attachment. + * * @param Attachment $attachment * * @return array diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php index cdce182e43..fd366ed877 100644 --- a/app/Transformers/BillTransformer.php +++ b/app/Transformers/BillTransformer.php @@ -58,6 +58,8 @@ class BillTransformer extends TransformerAbstract /** * BillTransformer constructor. * + * @codeCoverageIgnore + * * @param ParameterBag $parameters */ public function __construct(ParameterBag $parameters) @@ -66,8 +68,11 @@ class BillTransformer extends TransformerAbstract } /** + * Include any attachments. + * * @param Bill $bill * + * @codeCoverageIgnore * @return FractalCollection */ public function includeAttachments(Bill $bill): FractalCollection @@ -78,8 +83,11 @@ class BillTransformer extends TransformerAbstract } /** + * Include any transactions. + * * @param Bill $bill * + * @codeCoverageIgnore * @return FractalCollection */ public function includeTransactions(Bill $bill): FractalCollection @@ -102,8 +110,11 @@ class BillTransformer extends TransformerAbstract } /** + * Include the user. + * * @param Bill $bill * + * @codeCoverageIgnore * @return \League\Fractal\Resource\Item */ public function includeUser(Bill $bill): Item @@ -112,6 +123,8 @@ class BillTransformer extends TransformerAbstract } /** + * Transform the bill. + * * @param Bill $bill * * @return array diff --git a/tests/Unit/Transformers/AttachmentTransformerTest.php b/tests/Unit/Transformers/AttachmentTransformerTest.php new file mode 100644 index 0000000000..b4d6a8b5f1 --- /dev/null +++ b/tests/Unit/Transformers/AttachmentTransformerTest.php @@ -0,0 +1,64 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + +use FireflyIII\Models\Account; +use FireflyIII\Models\Attachment; +use FireflyIII\Transformers\AttachmentTransformer; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + +/** + * Class AttachmentTransformerTest + */ +class AttachmentTransformerTest extends TestCase +{ + /** + * Test basic transformer + * + * @covers \FireflyIII\Transformers\AttachmentTransformer::transform + */ + public function testBasic() + { + $md5 = md5('hello' . rand(1, 10000)); + $attachment = Attachment::create( + [ + 'user_id' => $this->user()->id, + 'attachable_id' => 1, + 'attachable_type' => Account::class, + 'md5' => $md5, + 'filename' => 'hello.txt', + 'mime' => 'text/plain', + 'size' => 101, + 'uploaded' => 1, + ] + ); + + $transformer = new AttachmentTransformer(new ParameterBag); + $result = $transformer->transform($attachment); + $this->assertEquals($md5, $result['md5']); + $this->assertEquals('hello.txt', $result['filename']); + } + +} \ No newline at end of file diff --git a/tests/Unit/Transformers/BillTransformerTest.php b/tests/Unit/Transformers/BillTransformerTest.php new file mode 100644 index 0000000000..165f6fe5c5 --- /dev/null +++ b/tests/Unit/Transformers/BillTransformerTest.php @@ -0,0 +1,145 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + +use Carbon\Carbon; +use FireflyIII\Models\Bill; +use FireflyIII\Models\Note; +use FireflyIII\Repositories\Bill\BillRepositoryInterface; +use FireflyIII\Transformers\BillTransformer; +use Illuminate\Support\Collection; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + +/** + * Class BillTransformerTest + */ +class BillTransformerTest extends TestCase +{ + /** + * Basic coverage + * + * @covers \FireflyIII\Transformers\BillTransformer::transform + * @covers \FireflyIII\Transformers\BillTransformer::paidData + * @covers \FireflyIII\Transformers\BillTransformer::payDates + */ + public function testBasic() + { + + $bill = Bill::create( + [ + 'user_id' => $this->user()->id, + 'name' => 'Some bill ' . rand(1, 10000), + 'match' => 'word,' . rand(1, 10000), + 'amount_min' => 12.34, + 'amount_max' => 45.67, + 'date' => '2018-01-02', + 'repeat_freq' => 'weekly', + 'skip' => 0, + 'active' => 1, + ] + ); + $transformer = new BillTransformer(new ParameterBag); + $result = $transformer->transform($bill); + + $this->assertEquals($bill->name, $result['name']); + $this->assertTrue($result['active']); + } + + /** + * Basic coverage with a note. + * + * @covers \FireflyIII\Transformers\BillTransformer::transform + */ + public function testNote() + { + + $bill = Bill::create( + [ + 'user_id' => $this->user()->id, + 'name' => 'Some bill ' . rand(1, 10000), + 'match' => 'word,' . rand(1, 10000), + 'amount_min' => 12.34, + 'amount_max' => 45.67, + 'date' => '2018-01-02', + 'repeat_freq' => 'weekly', + 'skip' => 0, + 'active' => 1, + ] + ); + $noteText = 'I are a note ' . rand(1, 10000); + $note = Note::create( + [ + 'noteable_id' => $bill->id, + 'noteable_type' => Bill::class, + 'text' => $noteText, + ] + ); + $transformer = new BillTransformer(new ParameterBag); + $result = $transformer->transform($bill); + + $this->assertEquals($bill->name, $result['name']); + $this->assertEquals($noteText, $result['notes']); + $this->assertTrue($result['active']); + } + + /** + * Coverage for dates. + * + * @covers \FireflyIII\Transformers\BillTransformer::transform + * @covers \FireflyIII\Transformers\BillTransformer::paidData + * @covers \FireflyIII\Transformers\BillTransformer::payDates + * @covers \FireflyIII\Transformers\BillTransformer::lastPaidDate + * @covers \FireflyIII\Transformers\BillTransformer::nextDateMatch + */ + public function testWithDates() + { + // mock stuff + $repository = $this->mock(BillRepositoryInterface::class); + $repository->shouldReceive('setUser')->andReturnSelf(); + $repository->shouldReceive('getPaidDatesInRange')->andReturn(new Collection([new Carbon])); + $bill = Bill::create( + [ + 'user_id' => $this->user()->id, + 'name' => 'Some bill ' . rand(1, 10000), + 'match' => 'word,' . rand(1, 10000), + 'amount_min' => 12.34, + 'amount_max' => 45.67, + 'date' => '2018-01-02', + 'repeat_freq' => 'weekly', + 'skip' => 0, + 'active' => 1, + ] + ); + $parameters = new ParameterBag(); + $parameters->set('start', new Carbon('2018-01-01')); + $parameters->set('end', new Carbon('2018-01-31')); + $transformer = new BillTransformer($parameters); + $result = $transformer->transform($bill); + + $this->assertEquals($bill->name, $result['name']); + $this->assertTrue($result['active']); + } + +} \ No newline at end of file