diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.php index bbd7248915..6fe50edd23 100644 --- a/app/Repositories/PiggyBank/PiggyBankRepository.php +++ b/app/Repositories/PiggyBank/PiggyBankRepository.php @@ -145,14 +145,20 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface /** * Set all piggy banks to order 0. * - * @return void + * @return boolean */ public function reset() { - DB::table('piggy_banks') - ->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.id') - ->where('accounts.user_id', Auth::user()->id) - ->update(['order' => 0, 'piggy_banks.updated_at' => DB::Raw('NOW()')]); + // split query to make it work in sqlite: + $set = PiggyBank:: + leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.id') + ->where('accounts.user_id', Auth::user()->id)->get(['piggy_banks.*']); + foreach ($set as $e) { + $e->order = 0; + $e->save(); + } + + return true; } /** diff --git a/tests/repositories/PiggyBankRepositoryTest.php b/tests/repositories/PiggyBankRepositoryTest.php index acbb997467..ef7ff93d20 100644 --- a/tests/repositories/PiggyBankRepositoryTest.php +++ b/tests/repositories/PiggyBankRepositoryTest.php @@ -1,5 +1,9 @@ markTestIncomplete( - 'This test has not been implemented yet.' - ); + /** @var PiggyBankRepetition $repetition */ + $repetition = FactoryMuffin::create('FireflyIII\Models\PiggyBankRepetition'); + $repetition->startdate = new Carbon('2014-01-01'); + $repetition->targetdate = new Carbon('2014-12-31'); + $repetition->save(); + + $parts = $this->object->calculateParts($repetition); + + $this->assertCount(1, $parts); + + + } + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::calculateParts + */ + public function testCalculatePartsWithReminder() + { + /** @var PiggyBankRepetition $repetition */ + $repetition = FactoryMuffin::create('FireflyIII\Models\PiggyBankRepetition'); + /** @var PiggyBank $piggyBank */ + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBank->startdate = new Carbon('2014-01-01'); + $piggyBank->targetdate = new Carbon('2014-12-31'); + $piggyBank->remind_me = 1; + $piggyBank->reminder = 'monthly'; + $repetition->startdate = new Carbon('2014-01-01'); + $repetition->targetdate = new Carbon('2014-12-31'); + $repetition->piggy_bank_id = $piggyBank->id; + $repetition->save(); + $piggyBank->save(); + + $parts = $this->object->calculateParts($repetition); + $this->assertCount(12, $parts); + + } /** * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::createEvent - * @todo Implement testCreateEvent(). */ public function testCreateEvent() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->object->createEvent($piggyBank, 100); + + $this->assertCount(1, $piggyBank->piggybankevents()->get()); } /** * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::createPiggyBankPart - * @todo Implement testCreatePiggyBankPart(). */ public function testCreatePiggyBankPart() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $repetition = FactoryMuffin::create('FireflyIII\Models\PiggyBankRepetition'); + $data = [ + 'repetition' => $repetition, + 'amountPerBar' => 100, + 'currentAmount' => 100, + 'cumulativeAmount' => 100, + 'startDate' => new Carbon, + 'targetDate' => new Carbon, + ]; + $part = $this->object->createPiggyBankPart($data); + $this->assertEquals($data['amountPerBar'], $part->getAmountPerBar()); } /** * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::destroy - * @todo Implement testDestroy(). */ public function testDestroy() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + + $this->object->destroy($piggyBank); + + $this->assertCount(0, PiggyBank::where('id', $piggyBank->id)->whereNull('deleted_at')->get()); + } /** * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::getEventSummarySet - * @todo Implement testGetEventSummarySet(). */ public function testGetEventSummarySet() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $set = $this->object->getEventSummarySet($piggyBank); + + $this->assertCount(0, $set); } /** @@ -96,46 +136,51 @@ class PiggyBankRepositoryTest extends TestCase */ public function testGetEvents() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $set = $this->object->getEvents($piggyBank); + + $this->assertCount(0, $set); } /** * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::getPiggyBanks - * @todo Implement testGetPiggyBanks(). */ public function testGetPiggyBanks() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + $set = $this->object->getPiggyBanks(); + + $this->assertCount(1, $set); } /** * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::reset - * @todo Implement testReset(). */ public function testReset() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBank->order = 4; + $piggyBank->save(); + $this->be($piggyBank->account->user); + $this->object->reset(); + + $this->assertCount(1, PiggyBank::where('order', 0)->get()); } /** * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::setOrder - * @todo Implement testSetOrder(). */ public function testSetOrder() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBank->order = 4; + $this->be($piggyBank->account->user); + $piggyBank->save(); + + $this->object->setOrder($piggyBank->id, 3); + $newPiggy = PiggyBank::find($piggyBank->id); + $this->assertEquals(3, $newPiggy->order); } /** @@ -144,10 +189,21 @@ class PiggyBankRepositoryTest extends TestCase */ public function testStore() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + $data = [ + 'remind_me' => 1, + 'account_id' => $account->id, + 'name' => 'Some piggy', + 'targetamount' => 100, + 'reminder_skip' => 0, + 'order' => 1, + + ]; + + $piggyBank = $this->object->store($data); + + $this->assertEquals(1, $piggyBank->id); } /** @@ -156,9 +212,21 @@ class PiggyBankRepositoryTest extends TestCase */ public function testUpdate() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + + $data = [ + 'name' => 'Update piggy ' . rand(1, 100), + 'account_id' => $piggyBank->account_id, + 'targetamount' => 101, + 'targetdate' => new Carbon, + 'reminder' => null, + 'startdate' => new Carbon, + 'remind_me' => '1' + ]; + + $new = $this->object->update($piggyBank, $data); + + $this->assertEquals($data['name'], $new->name); + $this->assertEquals($piggyBank->id, $new->id); } }