/** * @covers FireflyIII\Repositories\Account\AccountRepository::getSavingsAccounts * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetSavingsAccounts() { // create three accounts: FactoryMuffin::create('FireflyIII\\Models\\AccountType'); FactoryMuffin::create('FireflyIII\\Models\\AccountType'); $type = FactoryMuffin::create('FireflyIII\\Models\\AccountType'); $account1 = FactoryMuffin::create('FireflyIII\\Models\\Account'); $account1->account_type_id = $type->id; $account2 = FactoryMuffin::create('FireflyIII\\Models\\Account'); $account2->account_type_id = $type->id; $account3 = FactoryMuffin::create('FireflyIII\\Models\\Account'); $account3->account_type_id = $type->id; // make them savings accounts: $meta = new AccountMeta(); $meta->name = 'accountRole'; $meta->data = 'savingAsset'; $meta->account_id = $account1->id; $meta->save(); $meta = new AccountMeta(); $meta->name = 'accountRole'; $meta->data = 'savingAsset'; $meta->account_id = $account2->id; $meta->save(); $meta = new AccountMeta(); $meta->name = 'accountRole'; $meta->data = 'savingAsset'; $meta->account_id = $account3->id; $meta->save(); // assign to the same user: $account2->user_id = $account1->user_id; $account3->user_id = $account1->user_id; $account1->save(); $account2->save(); $account3->save(); $this->be($account1->user); // mock steam balance: Steam::shouldReceive('balance')->andReturn(0, 0, 1, 2, 4, 3); // get the result from the method: $result = $this->object->getSavingsAccounts(); $this->assertEquals(0, $result->get(0)->difference); $this->assertEquals(1, $result->get(1)->difference); $this->assertEquals(-1, $result->get(2)->difference); $this->assertEquals(100, $result->get(0)->percentage); $this->assertEquals(100, $result->get(1)->percentage); $this->assertEquals(25, $result->get(2)->percentage); }
/** * @param Account $account * @param array $data * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function updateMetadata(Account $account, array $data) { $validFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType']; foreach ($validFields as $field) { $entry = $account->accountMeta()->where('name', $field)->first(); // update if new data is present: if ($entry && isset($data[$field])) { $entry->data = $data[$field]; $entry->save(); } // no entry but data present? if (!$entry && isset($data[$field])) { $metaData = new AccountMeta(['account_id' => $account->id, 'name' => $field, 'data' => $data[$field]]); $metaData->save(); } } }
/** * @param Account $account * @param array $data * */ protected function updateMetadata(Account $account, array $data) { foreach ($this->validFields as $field) { $entry = $account->accountMeta()->where('name', $field)->first(); if (isset($data[$field])) { // update if new data is present: if (!is_null($entry)) { $entry->data = $data[$field]; $entry->save(); continue; } $metaData = new AccountMeta(['account_id' => $account->id, 'name' => $field, 'data' => $data[$field]]); $metaData->save(); } } }