From e2d0ee125f83f5a287f3a2626e39044379422364 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 5 Feb 2016 15:01:33 +0100 Subject: [PATCH] Cleaning up test data. --- app/Support/Migration/TestData.php | 223 +++- database/seeds/DatabaseSeeder.php | 7 +- database/seeds/OldTestDataSeeder.php | 68 ++ database/seeds/TestDataSeeder.php | 199 +--- database/seeds/VisualTestDataSeeder.php | 1306 +++++++++++------------ tests/TestCase.php | 8 +- 6 files changed, 962 insertions(+), 849 deletions(-) create mode 100644 database/seeds/OldTestDataSeeder.php diff --git a/app/Support/Migration/TestData.php b/app/Support/Migration/TestData.php index 69b8d12f1b..da34e4061c 100644 --- a/app/Support/Migration/TestData.php +++ b/app/Support/Migration/TestData.php @@ -18,8 +18,10 @@ use FireflyIII\Models\Attachment; use FireflyIII\Models\Bill; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; +use FireflyIII\Models\Category; use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBankEvent; +use FireflyIII\Models\Role; use FireflyIII\Models\Rule; use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleGroup; @@ -37,7 +39,6 @@ use Log; */ class TestData { - /** * @param User $user */ @@ -228,6 +229,139 @@ class TestData } } + /** + * @param User $user + */ + public static function createCategories(User $user) + { + Category::firstOrCreateEncrypted(['name' => 'Groceries', 'user_id' => $user->id]); + Category::firstOrCreateEncrypted(['name' => 'Car', 'user_id' => $user->id]); + } + + /** + * @param User $user + */ + public static function createExpenseAccounts(User $user) + { + $expenses = ['Adobe', 'Google', 'Vitens', 'Albert Heijn', 'PLUS', 'Apple', 'Bakker', 'Belastingdienst', 'bol.com', 'Cafe Central', 'conrad.nl', + 'coolblue', 'Shell', + 'DUO', 'Etos', 'FEBO', 'Greenchoice', 'Halfords', 'XS4All', 'iCentre', 'Jumper', 'Land lord']; + foreach ($expenses as $name) { + // create account: + Account::create( + [ + 'user_id' => $user->id, + 'account_type_id' => 4, + 'name' => $name, + 'active' => 1, + 'encrypted' => 1, + ] + ); + } + + } + + /** + * @param User $user + * @param string $description + * @param Carbon $date + * @param string $amount + * + * @return TransactionJournal + */ + public static function createRent(User $user, string $description, Carbon $date, string $amount): TransactionJournal + { + $fromAccount = TestData::findAccount($user, 'TestData Checking Account'); + $toAccount = TestData::findAccount($user, 'Land lord'); + $category = Category::firstOrCreateEncrypted(['name' => 'Rent', 'user_id' => $user->id]); + $budget = Budget::firstOrCreateEncrypted(['name' => 'Bills', 'user_id' => $user->id]); + $journal = TransactionJournal::create( + [ + 'user_id' => $user->id, + 'transaction_type_id' => 1, + 'transaction_currency_id' => 1, + 'bill_id' => 1, + 'description' => $description, + 'completed' => 1, + 'date' => $date, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount * -1, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount, + + ] + ); + $journal->categories()->save($category); + $journal->budgets()->save($budget); + + return $journal; + + } + + + /** + * @param User $user + * @param string $description + * @param Carbon $date + * @param string $amount + * + * @return TransactionJournal + */ + public static function createIncome(User $user, string $description, Carbon $date, string $amount): TransactionJournal + { + $date = new Carbon($date->format('Y-m') . '-23'); // paid on 23rd. + $today = new Carbon; + if ($date >= $today) { + return new TransactionJournal; + } + $toAccount = TestData::findAccount($user, 'TestData Checking Account'); + $fromAccount = TestData::findAccount($user, 'Job'); + $category = Category::firstOrCreateEncrypted(['name' => 'Salary', 'user_id' => $user->id]); + // create journal: + + $journal = TransactionJournal::create( + [ + 'user_id' => $user->id, + 'transaction_type_id' => 2, + 'transaction_currency_id' => 1, + 'description' => $description, + 'completed' => 1, + 'date' => $date, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount * -1, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount, + + ] + ); + $journal->categories()->save($category); + + return $journal; + + } + /** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @param User $user @@ -366,6 +500,26 @@ class TestData } + /** + * @param User $user + */ + public static function createRevenueAccounts(User $user) + { + $revenues = ['Job', 'Belastingdienst', 'Bank', 'KPN', 'Google']; + foreach ($revenues as $name) { + // create account: + Account::create( + [ + 'user_id' => $user->id, + 'account_type_id' => 5, + 'name' => $name, + 'active' => 1, + 'encrypted' => 1, + ] + ); + } + } + /** * @param User $user */ @@ -468,6 +622,22 @@ class TestData ); } + /** + * @return User + */ + public static function createUsers(): User + { + $user = User::create(['email' => 'thegrumpydictator@gmail.com', 'password' => bcrypt('james'), 'reset' => null, 'remember_token' => null]); + User::create(['email' => 'thegrumpydictator+empty@gmail.com', 'password' => bcrypt('james'), 'reset' => null, 'remember_token' => null]); + User::create(['email' => 'thegrumpydictator+deleteme@gmail.com', 'password' => bcrypt('james'), 'reset' => null, 'remember_token' => null]); + + + $admin = Role::where('name', 'owner')->first(); + $user->attachRole($admin); + + return $user; + } + /** * @param User $user * @param $name @@ -508,5 +678,56 @@ class TestData return null; } + /** + * @param User $user + * @param Carbon $date + */ + public static function openingBalanceSavings(User $user, Carbon $date) + { + // opposing account for opening balance: + $opposing = Account::create( + [ + 'user_id' => $user->id, + 'account_type_id' => 6, + 'name' => 'Opposing for savings', + 'active' => 1, + 'encrypted' => 1, + ] + ); + + // savings + $savings = TestData::findAccount($user, 'TestData Savings'); + + $journal = TransactionJournal::create( + [ + 'user_id' => $user->id, + 'transaction_type_id' => 4, + 'transaction_currency_id' => 1, + 'description' => 'Opening balance for savings account', + 'completed' => 1, + 'date' => $date->format('Y-m-d'), + ] + ); + + // transactions + Transaction::create( + [ + 'account_id' => $opposing->id, + 'transaction_journal_id' => $journal->id, + 'amount' => -10000, + ] + ); + + Transaction::create( + [ + 'account_id' => $savings->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 10000, + ] + ); + + + } + } diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 69a846e857..bf4ec69932 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -23,14 +23,9 @@ class DatabaseSeeder extends Seeder $this->call('PermissionSeeder'); // set up basic test data (as little as possible): - if (App::environment() == 'testing') { + if (App::environment() == 'testing' || App::environment() == 'local') { $this->call('TestDataSeeder'); } - - // this one is reserved for more extensive testing. - if (App::environment() == 'local') { - $this->call('VisualTestDataSeeder'); - } } } diff --git a/database/seeds/OldTestDataSeeder.php b/database/seeds/OldTestDataSeeder.php new file mode 100644 index 0000000000..856ffe164c --- /dev/null +++ b/database/seeds/OldTestDataSeeder.php @@ -0,0 +1,68 @@ +start = Carbon::create()->subYear()->startOfYear(); + + } + + /** + * Run the database seeds. + * + * @return void + */ + public function run() + { + // create budget limits for these budgets +// TestData::createBudgetLimit($user, new Carbon, 'Groceries', 400); +// TestData::createBudgetLimit($user, new Carbon, 'Bills', 1000); +// TestData::createBudgetLimit($user, new Carbon, 'Car', 100); + + // create some categories for user #1 + $this->createCategories($user); + + // create some piggy banks for user #1 + TestData::createPiggybanks($user); + + // create some expense accounts for user #1 + $this->createExpenseAccounts($user); + + // create some revenue accounts for user #1 + $this->createRevenueAccounts($user); + + // create journal + attachment: + TestData::createAttachments($user, $this->start); + + // create opening balance for savings account: + $this->openingBalanceSavings($user); + + // need at least one rule group and one rule: + TestData::createRules($user); + + // create a tag: + TestData::createTags($user); + } + +} diff --git a/database/seeds/TestDataSeeder.php b/database/seeds/TestDataSeeder.php index 487d5a85a0..fb281c39b3 100644 --- a/database/seeds/TestDataSeeder.php +++ b/database/seeds/TestDataSeeder.php @@ -1,14 +1,15 @@ start = Carbon::create()->subYear()->startOfYear(); + $this->start = Carbon::create()->subYears(2)->startOfYear(); + $this->end = Carbon::now(); } @@ -35,153 +39,58 @@ class TestDataSeeder extends Seeder */ public function run() { - $user = User::create(['email' => 'thegrumpydictator@gmail.com', 'password' => bcrypt('james'), 'reset' => null, 'remember_token' => null]); - User::create(['email' => 'thegrumpydictator+empty@gmail.com', 'password' => bcrypt('james'), 'reset' => null, 'remember_token' => null]); - User::create(['email' => 'thegrumpydictator+deleteme@gmail.com', 'password' => bcrypt('james'), 'reset' => null, 'remember_token' => null]); + // start by creating all users: + // method will return the first user. + $user = TestData::createUsers(); - - $admin = Role::where('name', 'owner')->first(); - $user->attachRole($admin); - - - // create asset accounts for user #1. + // create all kinds of static data: TestData::createAssetAccounts($user); - - // create bills for user #1 TestData::createBills($user); - - // create some budgets for user #1 TestData::createBudgets($user); - - // create budget limits for these budgets - TestData::createBudgetLimit($user, new Carbon, 'Groceries', 400); - TestData::createBudgetLimit($user, new Carbon, 'Bills', 1000); - TestData::createBudgetLimit($user, new Carbon, 'Car', 100); - - // create some categories for user #1 - $this->createCategories($user); - - // create some piggy banks for user #1 + TestData::createCategories($user); TestData::createPiggybanks($user); - - // create some expense accounts for user #1 - $this->createExpenseAccounts($user); - - // create some revenue accounts for user #1 - $this->createRevenueAccounts($user); - - // create journal + attachment: + TestData::createExpenseAccounts($user); + TestData::createRevenueAccounts($user); TestData::createAttachments($user, $this->start); - - // create opening balance for savings account: - $this->openingBalanceSavings($user); - - // need at least one rule group and one rule: + TestData::openingBalanceSavings($user, $this->start); TestData::createRules($user); - // create a tag: - TestData::createTags($user); - } + // loop from start to end, create dynamic info. + $current = clone $this->start; + while ($current < $this->end) { + $month = $current->format('F Y'); + // create salaries: + TestData::createIncome($user, 'Salary ' . $month, $current, strval(rand(2000, 2100))); - /** - * @param User $user - */ - private function createCategories(User $user) - { - Category::firstOrCreateEncrypted(['name' => 'Groceries', 'user_id' => $user->id]); - Category::firstOrCreateEncrypted(['name' => 'Car', 'user_id' => $user->id]); - } + // pay bills: + TestData::createRent($user, 'Rent for ' . $month, $current, '800'); + // $this->createWater('Water bill for ' . $month, $current, 15); + // $this->createTV('TV bill for ' . $month, $current, 60); + // $this->createPower('Power bill for ' . $month, $current, 120); - /** - * @param User $user - */ - private function createExpenseAccounts(User $user) - { - $expenses = ['Adobe', 'Google', 'Vitens', 'Albert Heijn', 'PLUS', 'Apple', 'Bakker', 'Belastingdienst', 'bol.com', 'Cafe Central', 'conrad.nl', - 'coolblue', 'Shell', - 'DUO', 'Etos', 'FEBO', 'Greenchoice', 'Halfords', 'XS4All', 'iCentre', 'Jumper', 'Land lord']; - foreach ($expenses as $name) { - // create account: - Account::create( - [ - 'user_id' => $user->id, - 'account_type_id' => 4, - 'name' => $name, - 'active' => 1, - 'encrypted' => 1, - ] - ); + + // pay daily groceries: + // $this->createGroceries($current); + + // create tag (each type of tag, for date): + // TestData::createTags($this->user, $current); + + // go out for drinks: + // $this->createDrinksAndOthers($current); + + // save money every month: + // $this->createSavings($current); + + // buy gas for the car every month: + // $this->createCar($current); + + // create budget limits. + TestData::createBudgetLimit($user, $current, 'Groceries', '400'); + TestData::createBudgetLimit($user, $current, 'Bills', '1000'); + TestData::createBudgetLimit($user, $current, 'Car', '100'); + + echo 'Created test data for ' . $month . "\n"; + $current->addMonth(); } - - } - - /** - * @param User $user - */ - private function createRevenueAccounts(User $user) - { - $revenues = ['Job', 'Belastingdienst', 'Bank', 'KPN', 'Google']; - foreach ($revenues as $name) { - // create account: - Account::create( - [ - 'user_id' => $user->id, - 'account_type_id' => 5, - 'name' => $name, - 'active' => 1, - 'encrypted' => 1, - ] - ); - } - } - - /** - * @param User $user - */ - private function openingBalanceSavings(User $user) - { - // opposing account for opening balance: - $opposing = Account::create( - [ - 'user_id' => $user->id, - 'account_type_id' => 6, - 'name' => 'Opposing for savings', - 'active' => 1, - 'encrypted' => 1, - ] - ); - - // savings - $savings = TestData::findAccount($user, 'TestData Savings'); - - $journal = TransactionJournal::create( - [ - 'user_id' => $user->id, - 'transaction_type_id' => 4, - 'transaction_currency_id' => 1, - 'description' => 'Opening balance for savings account', - 'completed' => 1, - 'date' => $this->start->format('Y-m-d'), - ] - ); - - // transactions - Transaction::create( - [ - 'account_id' => $opposing->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -10000, - ] - ); - - Transaction::create( - [ - 'account_id' => $savings->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 10000, - ] - ); - - } } diff --git a/database/seeds/VisualTestDataSeeder.php b/database/seeds/VisualTestDataSeeder.php index 672ff770ac..0db640c9b0 100644 --- a/database/seeds/VisualTestDataSeeder.php +++ b/database/seeds/VisualTestDataSeeder.php @@ -28,13 +28,622 @@ use Illuminate\Database\Seeder; class VisualTestDataSeeder extends Seeder { - /** @var User */ - protected $user; + /** @var Carbon */ + public $end; + /** @var Carbon */ + public $start; + + /** + * TestDataSeeder constructor. + */ + public function __construct() + { + $this->start = Carbon::create()->subYears(2)->startOfYear(); + $this->end = Carbon::now(); + + } /** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - public function createRules() + public function run() + { + $this->createUsers(); + + // create accounts: + TestData::createAssetAccounts($this->user); + $this->createExpenseAccounts(); + $this->createRevenueAccounts(); + TestData::createBills($this->user); + TestData::createPiggybanks($this->user); + + // create some budgets for user + TestData::createBudgets($this->user); + + $this->createRules(); + + // preference to only see account #1 on frontpage. + $this->createPreferences(); + + // dates: + $start = Carbon::now()->subYears(2)->startOfMonth(); + $end = Carbon::now()->endOfDay(); + + // create journal + attachment: + TestData::createAttachments($this->user, $start); + + + $current = clone $start; + while ($current < $end) { + $month = $current->format('F Y'); + // create salaries: + $this->createIncome('Salary ' . $month, $current, rand(2000, 2100)); + + // pay bills: + $this->createRent('Rent for ' . $month, $current, 800); + $this->createWater('Water bill for ' . $month, $current, 15); + $this->createTV('TV bill for ' . $month, $current, 60); + $this->createPower('Power bill for ' . $month, $current, 120); + + + // pay daily groceries: + $this->createGroceries($current); + + // create tag (each type of tag, for date): + TestData::createTags($this->user, $current); + + // go out for drinks: + $this->createDrinksAndOthers($current); + + // save money every month: + $this->createSavings($current); + + // buy gas for the car every month: + $this->createCar($current); + + // budget limit for this month, on "Groceries". + TestData::createBudgetLimit($this->user, $current, 'Groceries', 400); + TestData::createBudgetLimit($this->user, $current, 'Bills', 1000); + TestData::createBudgetLimit($this->user, $current, 'Car', 100); + + echo 'Created test data for ' . $month . "\n"; + $current->addMonth(); + } + + } + + /** + * @param $date + * + * @return static + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + protected function createCar($date) + { + // twice: + $date = new Carbon($date->format('Y-m') . '-10'); // paid on 10th + $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); + $toAccount = TestData::findAccount($this->user, 'Shell'); + $category = Category::firstOrCreateEncrypted(['name' => 'Car', 'user_id' => $this->user->id]); + $budget = Budget::firstOrCreateEncrypted(['name' => 'Car', 'user_id' => $this->user->id]); + $amount = rand(4000, 5000) / 100; + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user->id, + 'transaction_type_id' => 1, + 'transaction_currency_id' => 1, + 'description' => 'Bought gas', + 'completed' => 1, + 'date' => $date, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount * -1, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount, + + ] + ); + $journal->categories()->save($category); + $journal->budgets()->save($budget); + + // and again! + $date = new Carbon($date->format('Y-m') . '-20'); // paid on 20th + $amount = rand(4000, 5000) / 100; + + + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user->id, + 'transaction_type_id' => 1, + 'transaction_currency_id' => 1, + 'description' => 'Gas for car', + 'completed' => 1, + 'date' => $date, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount * -1, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount, + + ] + ); + + // and again! + + return $journal; + } + + /** + * @param Carbon $date + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + protected function createDrinksAndOthers(Carbon $date) + { + $start = clone $date; + $end = clone $date; + $today = new Carbon; + $start->startOfMonth(); + $end->endOfMonth(); + $current = clone $start; + while ($current < $end && $current < $today) { + + // weekly drink: + $thisDate = clone $current; + $thisDate->addDay(); + $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); + $toAccount = TestData::findAccount($this->user, 'Cafe Central'); + $category = Category::firstOrCreateEncrypted(['name' => 'Drinks', 'user_id' => $this->user->id]); + $budget = Budget::firstOrCreateEncrypted(['name' => 'Going out', 'user_id' => $this->user->id]); + $amount = rand(1500, 3600) / 100; + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user->id, + 'transaction_type_id' => 1, + 'transaction_currency_id' => 1, + 'description' => 'Going out for drinks', + 'completed' => 1, + 'date' => $thisDate, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount * -1, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount, + + ] + ); + $journal->categories()->save($category); + $journal->budgets()->save($budget); + + // shopping at some (online) shop: + + + $current->addWeek(); + } + } + + protected function createExpenseAccounts() + { + $expenses = ['Adobe', 'Google', 'Vitens', 'Albert Heijn', 'PLUS', 'Apple', 'Bakker', 'Belastingdienst', 'bol.com', 'Cafe Central', 'conrad.nl', + 'coolblue', 'Shell', + 'DUO', 'Etos', 'FEBO', 'Greenchoice', 'Halfords', 'XS4All', 'iCentre', 'Jumper', 'Land lord']; + foreach ($expenses as $name) { + // create account: + Account::create( + [ + 'user_id' => $this->user->id, + 'account_type_id' => 4, + 'name' => $name, + 'active' => 1, + 'encrypted' => 1, + ] + ); + } + + } + + /** + * @param Carbon $date + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + protected function createGroceries(Carbon $date) + { + $start = clone $date; + $end = clone $date; + $today = new Carbon; + $start->startOfMonth(); + $end->endOfMonth(); + + $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); + $stores = ['Albert Heijn', 'PLUS', 'Bakker']; + $descriptions = ['Groceries', 'Bought some groceries', 'Got groceries']; + $category = Category::firstOrCreateEncrypted(['name' => 'Daily groceries', 'user_id' => $this->user->id]); + $budget = Budget::firstOrCreateEncrypted(['name' => 'Groceries', 'user_id' => $this->user->id]); + + $current = clone $start; + while ($current < $end && $current < $today) { + // daily groceries: + $amount = rand(1500, 2500) / 100; + $toAccount = TestData::findAccount($this->user, $stores[rand(0, count($stores) - 1)]); + + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user->id, + 'transaction_type_id' => 1, + 'transaction_currency_id' => 1, + 'description' => $descriptions[rand(0, count($descriptions) - 1)], + 'completed' => 1, + 'date' => $current, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount * -1, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount, + + ] + ); + $journal->categories()->save($category); + $journal->budgets()->save($budget); + + + $current->addDay(); + } + } + + + + /** + * @param $description + * @param Carbon $date + * @param $amount + * + * @return TransactionJournal + */ + protected function createPower($description, Carbon $date, $amount) + { + $date = new Carbon($date->format('Y-m') . '-06'); // paid on 10th + $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); + $toAccount = TestData::findAccount($this->user, 'Greenchoice'); + $category = Category::firstOrCreateEncrypted(['name' => 'House', 'user_id' => $this->user->id]); + $budget = Budget::firstOrCreateEncrypted(['name' => 'Bills', 'user_id' => $this->user->id]); + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user->id, + 'transaction_type_id' => 1, + 'transaction_currency_id' => 1, + 'description' => $description, + 'completed' => 1, + 'date' => $date, + ] + ); + if($journal->getErrors()->count() > 0) { + echo $journal->getErrors()->first(); + } + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount * -1, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount, + + ] + ); + $journal->categories()->save($category); + $journal->budgets()->save($budget); + + return $journal; + + } + + protected function createPreferences() + { + $preference = new Preference; + $preference->name = 'frontPageAccounts'; + $preference->data = [1]; + $preference->user()->associate($this->user); + $preference->save(); + } + + + /** + * + */ + protected function createRevenueAccounts() + { + $revenues = ['Job', 'Belastingdienst', 'Bank', 'KPN', 'Google']; + foreach ($revenues as $name) { + // create account: + Account::create( + [ + 'user_id' => $this->user->id, + 'account_type_id' => 5, + 'name' => $name, + 'active' => 1, + 'encrypted' => 1, + ] + ); + } + } + + /** + * @param Carbon $date + * + * @return TransactionJournal + */ + protected function createSavings(Carbon $date) + { + $date = new Carbon($date->format('Y-m') . '-24'); // paid on 24th. + $toAccount = TestData::findAccount($this->user, 'TestData Savings'); + $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); + $category = Category::firstOrCreateEncrypted(['name' => 'Money management', 'user_id' => $this->user->id]); + // create journal: + + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user->id, + 'transaction_type_id' => 3, + 'transaction_currency_id' => 1, + 'description' => 'Save money', + 'completed' => 1, + 'date' => $date, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => -150, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 150, + + ] + ); + $journal->categories()->save($category); + + return $journal; + + } + + /** + * @param $description + * @param Carbon $date + * @param $amount + * + * @return TransactionJournal + */ + protected function createTV($description, Carbon $date, $amount) + { + $date = new Carbon($date->format('Y-m') . '-15'); // paid on 10th + $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); + $toAccount = TestData::findAccount($this->user, 'XS4All'); + $category = Category::firstOrCreateEncrypted(['name' => 'House', 'user_id' => $this->user->id]); + $budget = Budget::firstOrCreateEncrypted(['name' => 'Bills', 'user_id' => $this->user->id]); + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user->id, + 'transaction_type_id' => 1, + 'transaction_currency_id' => 1, + 'description' => $description, + 'completed' => 1, + 'date' => $date, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount * -1, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount, + + ] + ); + $journal->categories()->save($category); + $journal->budgets()->save($budget); + + return $journal; + + } + + /** + * + */ + protected function createUsers() + { + User::create(['email' => 'thegrumpydictator@gmail.com', 'password' => bcrypt('james'), 'reset' => null, 'remember_token' => null]); + $this->user = User::whereEmail('thegrumpydictator@gmail.com')->first(); + + // create rights: + $role = Role::find(1); + $this->user->roles()->save($role); + + } + + /** + * @param $description + * @param Carbon $date + * @param $amount + * + * @return TransactionJournal + */ + protected function createWater($description, Carbon $date, $amount) + { + $date = new Carbon($date->format('Y-m') . '-10'); // paid on 10th + $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); + $toAccount = TestData::findAccount($this->user, 'Vitens'); + $category = Category::firstOrCreateEncrypted(['name' => 'House', 'user_id' => $this->user->id]); + $budget = Budget::firstOrCreateEncrypted(['name' => 'Bills', 'user_id' => $this->user->id]); + $journal = TransactionJournal::create( + [ + 'user_id' => $this->user->id, + 'transaction_type_id' => 1, + 'transaction_currency_id' => 1, + 'description' => $description, + 'completed' => 1, + 'date' => $date, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount * -1, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => $amount, + + ] + ); + $journal->categories()->save($category); + $journal->budgets()->save($budget); + + return $journal; + + } + + /** + * @param $name + * + * @return Bill|null + */ + protected function findBill($name) + { + /** @var Bill $bill */ + foreach (Bill::get() as $bill) { + if ($bill->name == $name && $this->user->id == $bill->user_id) { + return $bill; + break; + } + } + + return null; + } + + /** + * @param $name + * + * @return Category|null + */ + protected function findCategory($name) + { + + /** @var Category $category */ + foreach (Category::get() as $category) { + if ($category->name == $name && $this->user->id == $category->user_id) { + return $category; + break; + } + } + + return null; + } + + /** + * @param $name + * + * @return PiggyBank|null + */ + protected function findPiggyBank($name) + { + + /** @var Budget $budget */ + foreach (PiggyBank::get() as $piggyBank) { + $account = $piggyBank->account()->first(); + if ($piggyBank->name == $name && $this->user->id == $account->user_id) { + return $piggyBank; + break; + } + } + + return null; + } + + /** + * @param $tagName + * + * @return Tag|null + * @internal param $tag + */ + protected function findTag($tagName) + { + /** @var Tag $tag */ + foreach (Tag::get() as $tag) { + if ($tag->tag == $tagName && $this->user->id == $tag->user_id) { + return $tag; + break; + } + } + + return null; + } + + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + private function createRules() { // three rule groups to get started. $ruleGroup = new RuleGroup; @@ -430,696 +1039,5 @@ class VisualTestDataSeeder extends Seeder unset($ruleAction); } - /** - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) - */ - public function run() - { - $this->createUsers(); - - // create accounts: - TestData::createAssetAccounts($this->user); - $this->createExpenseAccounts(); - $this->createRevenueAccounts(); - TestData::createBills($this->user); - TestData::createPiggybanks($this->user); - - // create some budgets for user - TestData::createBudgets($this->user); - - $this->createRules(); - - // preference to only see account #1 on frontpage. - $this->createPreferences(); - - // dates: - $start = Carbon::now()->subYears(2)->startOfMonth(); - $end = Carbon::now()->endOfDay(); - - // create journal + attachment: - TestData::createAttachments($this->user, $start); - - - $current = clone $start; - while ($current < $end) { - $month = $current->format('F Y'); - // create salaries: - $this->createIncome('Salary ' . $month, $current, rand(2000, 2100)); - - // pay bills: - $this->createRent('Rent for ' . $month, $current, 800); - $this->createWater('Water bill for ' . $month, $current, 15); - $this->createTV('TV bill for ' . $month, $current, 60); - $this->createPower('Power bill for ' . $month, $current, 120); - - - // pay daily groceries: - $this->createGroceries($current); - - // create tag (each type of tag, for date): - TestData::createTags($this->user, $current); - - // go out for drinks: - $this->createDrinksAndOthers($current); - - // save money every month: - $this->createSavings($current); - - // buy gas for the car every month: - $this->createCar($current); - - // budget limit for this month, on "Groceries". - TestData::createBudgetLimit($this->user, $current, 'Groceries', 400); - TestData::createBudgetLimit($this->user, $current, 'Bills', 1000); - TestData::createBudgetLimit($this->user, $current, 'Car', 100); - - echo 'Created test data for ' . $month . "\n"; - $current->addMonth(); - } - - } - - /** - * @param $date - * - * @return static - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) - */ - protected function createCar($date) - { - // twice: - $date = new Carbon($date->format('Y-m') . '-10'); // paid on 10th - $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); - $toAccount = TestData::findAccount($this->user, 'Shell'); - $category = Category::firstOrCreateEncrypted(['name' => 'Car', 'user_id' => $this->user->id]); - $budget = Budget::firstOrCreateEncrypted(['name' => 'Car', 'user_id' => $this->user->id]); - $amount = rand(4000, 5000) / 100; - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user->id, - 'transaction_type_id' => 1, - 'transaction_currency_id' => 1, - 'description' => 'Bought gas', - 'completed' => 1, - 'date' => $date, - ] - ); - Transaction::create( - [ - 'account_id' => $fromAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount * -1, - - ] - ); - Transaction::create( - [ - 'account_id' => $toAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount, - - ] - ); - $journal->categories()->save($category); - $journal->budgets()->save($budget); - - // and again! - $date = new Carbon($date->format('Y-m') . '-20'); // paid on 20th - $amount = rand(4000, 5000) / 100; - - - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user->id, - 'transaction_type_id' => 1, - 'transaction_currency_id' => 1, - 'description' => 'Gas for car', - 'completed' => 1, - 'date' => $date, - ] - ); - Transaction::create( - [ - 'account_id' => $fromAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount * -1, - - ] - ); - Transaction::create( - [ - 'account_id' => $toAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount, - - ] - ); - - // and again! - - return $journal; - } - - /** - * @param Carbon $date - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) - */ - protected function createDrinksAndOthers(Carbon $date) - { - $start = clone $date; - $end = clone $date; - $today = new Carbon; - $start->startOfMonth(); - $end->endOfMonth(); - $current = clone $start; - while ($current < $end && $current < $today) { - - // weekly drink: - $thisDate = clone $current; - $thisDate->addDay(); - $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); - $toAccount = TestData::findAccount($this->user, 'Cafe Central'); - $category = Category::firstOrCreateEncrypted(['name' => 'Drinks', 'user_id' => $this->user->id]); - $budget = Budget::firstOrCreateEncrypted(['name' => 'Going out', 'user_id' => $this->user->id]); - $amount = rand(1500, 3600) / 100; - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user->id, - 'transaction_type_id' => 1, - 'transaction_currency_id' => 1, - 'description' => 'Going out for drinks', - 'completed' => 1, - 'date' => $thisDate, - ] - ); - Transaction::create( - [ - 'account_id' => $fromAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount * -1, - - ] - ); - Transaction::create( - [ - 'account_id' => $toAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount, - - ] - ); - $journal->categories()->save($category); - $journal->budgets()->save($budget); - - // shopping at some (online) shop: - - - $current->addWeek(); - } - } - - protected function createExpenseAccounts() - { - $expenses = ['Adobe', 'Google', 'Vitens', 'Albert Heijn', 'PLUS', 'Apple', 'Bakker', 'Belastingdienst', 'bol.com', 'Cafe Central', 'conrad.nl', - 'coolblue', 'Shell', - 'DUO', 'Etos', 'FEBO', 'Greenchoice', 'Halfords', 'XS4All', 'iCentre', 'Jumper', 'Land lord']; - foreach ($expenses as $name) { - // create account: - Account::create( - [ - 'user_id' => $this->user->id, - 'account_type_id' => 4, - 'name' => $name, - 'active' => 1, - 'encrypted' => 1, - ] - ); - } - - } - - /** - * @param Carbon $date - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) - */ - protected function createGroceries(Carbon $date) - { - $start = clone $date; - $end = clone $date; - $today = new Carbon; - $start->startOfMonth(); - $end->endOfMonth(); - - $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); - $stores = ['Albert Heijn', 'PLUS', 'Bakker']; - $descriptions = ['Groceries', 'Bought some groceries', 'Got groceries']; - $category = Category::firstOrCreateEncrypted(['name' => 'Daily groceries', 'user_id' => $this->user->id]); - $budget = Budget::firstOrCreateEncrypted(['name' => 'Groceries', 'user_id' => $this->user->id]); - - $current = clone $start; - while ($current < $end && $current < $today) { - // daily groceries: - $amount = rand(1500, 2500) / 100; - $toAccount = TestData::findAccount($this->user, $stores[rand(0, count($stores) - 1)]); - - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user->id, - 'transaction_type_id' => 1, - 'transaction_currency_id' => 1, - 'description' => $descriptions[rand(0, count($descriptions) - 1)], - 'completed' => 1, - 'date' => $current, - ] - ); - Transaction::create( - [ - 'account_id' => $fromAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount * -1, - - ] - ); - Transaction::create( - [ - 'account_id' => $toAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount, - - ] - ); - $journal->categories()->save($category); - $journal->budgets()->save($budget); - - - $current->addDay(); - } - } - - /** - * @param $description - * @param Carbon $date - * @param $amount - * - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) - * - * @return TransactionJournal - */ - protected function createIncome($description, Carbon $date, $amount) - { - $date = new Carbon($date->format('Y-m') . '-23'); // paid on 23rd. - $today = new Carbon; - if ($date >= $today) { - return null; - } - $toAccount = TestData::findAccount($this->user, 'TestData Checking Account'); - $fromAccount = TestData::findAccount($this->user, 'Job'); - $category = Category::firstOrCreateEncrypted(['name' => 'Salary', 'user_id' => $this->user->id]); - // create journal: - - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user->id, - 'transaction_type_id' => 2, - 'transaction_currency_id' => 1, - 'description' => $description, - 'completed' => 1, - 'date' => $date, - ] - ); - Transaction::create( - [ - 'account_id' => $fromAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount * -1, - - ] - ); - Transaction::create( - [ - 'account_id' => $toAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount, - - ] - ); - $journal->categories()->save($category); - - return $journal; - - } - - /** - * @param $description - * @param Carbon $date - * @param $amount - * - * @return TransactionJournal - */ - protected function createPower($description, Carbon $date, $amount) - { - $date = new Carbon($date->format('Y-m') . '-06'); // paid on 10th - $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); - $toAccount = TestData::findAccount($this->user, 'Greenchoice'); - $category = Category::firstOrCreateEncrypted(['name' => 'House', 'user_id' => $this->user->id]); - $budget = Budget::firstOrCreateEncrypted(['name' => 'Bills', 'user_id' => $this->user->id]); - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user->id, - 'transaction_type_id' => 1, - 'transaction_currency_id' => 1, - 'description' => $description, - 'completed' => 1, - 'date' => $date, - ] - ); - Transaction::create( - [ - 'account_id' => $fromAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount * -1, - - ] - ); - Transaction::create( - [ - 'account_id' => $toAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount, - - ] - ); - $journal->categories()->save($category); - $journal->budgets()->save($budget); - - return $journal; - - } - - protected function createPreferences() - { - $preference = new Preference; - $preference->name = 'frontPageAccounts'; - $preference->data = [1]; - $preference->user()->associate($this->user); - $preference->save(); - } - - /** - * @param $description - * @param Carbon $date - * @param $amount - * - * @return TransactionJournal - */ - protected function createRent($description, Carbon $date, $amount) - { - $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); - $toAccount = TestData::findAccount($this->user, 'Land lord'); - $category = Category::firstOrCreateEncrypted(['name' => 'Rent', 'user_id' => $this->user->id]); - $budget = Budget::firstOrCreateEncrypted(['name' => 'Bills', 'user_id' => $this->user->id]); - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user->id, - 'transaction_type_id' => 1, - 'transaction_currency_id' => 1, - 'bill_id' => 1, - 'description' => $description, - 'completed' => 1, - 'date' => $date, - ] - ); - Transaction::create( - [ - 'account_id' => $fromAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount * -1, - - ] - ); - Transaction::create( - [ - 'account_id' => $toAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount, - - ] - ); - $journal->categories()->save($category); - $journal->budgets()->save($budget); - - return $journal; - - } - - /** - * - */ - protected function createRevenueAccounts() - { - $revenues = ['Job', 'Belastingdienst', 'Bank', 'KPN', 'Google']; - foreach ($revenues as $name) { - // create account: - Account::create( - [ - 'user_id' => $this->user->id, - 'account_type_id' => 5, - 'name' => $name, - 'active' => 1, - 'encrypted' => 1, - ] - ); - } - } - - /** - * @param Carbon $date - * - * @return TransactionJournal - */ - protected function createSavings(Carbon $date) - { - $date = new Carbon($date->format('Y-m') . '-24'); // paid on 24th. - $toAccount = TestData::findAccount($this->user, 'TestData Savings'); - $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); - $category = Category::firstOrCreateEncrypted(['name' => 'Money management', 'user_id' => $this->user->id]); - // create journal: - - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user->id, - 'transaction_type_id' => 3, - 'transaction_currency_id' => 1, - 'description' => 'Save money', - 'completed' => 1, - 'date' => $date, - ] - ); - Transaction::create( - [ - 'account_id' => $fromAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -150, - - ] - ); - Transaction::create( - [ - 'account_id' => $toAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 150, - - ] - ); - $journal->categories()->save($category); - - return $journal; - - } - - /** - * @param $description - * @param Carbon $date - * @param $amount - * - * @return TransactionJournal - */ - protected function createTV($description, Carbon $date, $amount) - { - $date = new Carbon($date->format('Y-m') . '-15'); // paid on 10th - $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); - $toAccount = TestData::findAccount($this->user, 'XS4All'); - $category = Category::firstOrCreateEncrypted(['name' => 'House', 'user_id' => $this->user->id]); - $budget = Budget::firstOrCreateEncrypted(['name' => 'Bills', 'user_id' => $this->user->id]); - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user->id, - 'transaction_type_id' => 1, - 'transaction_currency_id' => 1, - 'description' => $description, - 'completed' => 1, - 'date' => $date, - ] - ); - Transaction::create( - [ - 'account_id' => $fromAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount * -1, - - ] - ); - Transaction::create( - [ - 'account_id' => $toAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount, - - ] - ); - $journal->categories()->save($category); - $journal->budgets()->save($budget); - - return $journal; - - } - - /** - * - */ - protected function createUsers() - { - User::create(['email' => 'thegrumpydictator@gmail.com', 'password' => bcrypt('james'), 'reset' => null, 'remember_token' => null]); - $this->user = User::whereEmail('thegrumpydictator@gmail.com')->first(); - - // create rights: - $role = Role::find(1); - $this->user->roles()->save($role); - - } - - /** - * @param $description - * @param Carbon $date - * @param $amount - * - * @return TransactionJournal - */ - protected function createWater($description, Carbon $date, $amount) - { - $date = new Carbon($date->format('Y-m') . '-10'); // paid on 10th - $fromAccount = TestData::findAccount($this->user, 'TestData Checking Account'); - $toAccount = TestData::findAccount($this->user, 'Vitens'); - $category = Category::firstOrCreateEncrypted(['name' => 'House', 'user_id' => $this->user->id]); - $budget = Budget::firstOrCreateEncrypted(['name' => 'Bills', 'user_id' => $this->user->id]); - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user->id, - 'transaction_type_id' => 1, - 'transaction_currency_id' => 1, - 'description' => $description, - 'completed' => 1, - 'date' => $date, - ] - ); - Transaction::create( - [ - 'account_id' => $fromAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount * -1, - - ] - ); - Transaction::create( - [ - 'account_id' => $toAccount->id, - 'transaction_journal_id' => $journal->id, - 'amount' => $amount, - - ] - ); - $journal->categories()->save($category); - $journal->budgets()->save($budget); - - return $journal; - - } - - /** - * @param $name - * - * @return Bill|null - */ - protected function findBill($name) - { - /** @var Bill $bill */ - foreach (Bill::get() as $bill) { - if ($bill->name == $name && $this->user->id == $bill->user_id) { - return $bill; - break; - } - } - - return null; - } - - /** - * @param $name - * - * @return Category|null - */ - protected function findCategory($name) - { - - /** @var Category $category */ - foreach (Category::get() as $category) { - if ($category->name == $name && $this->user->id == $category->user_id) { - return $category; - break; - } - } - - return null; - } - - /** - * @param $name - * - * @return PiggyBank|null - */ - protected function findPiggyBank($name) - { - - /** @var Budget $budget */ - foreach (PiggyBank::get() as $piggyBank) { - $account = $piggyBank->account()->first(); - if ($piggyBank->name == $name && $this->user->id == $account->user_id) { - return $piggyBank; - break; - } - } - - return null; - } - - /** - * @param $tagName - * - * @return Tag|null - * @internal param $tag - */ - protected function findTag($tagName) - { - /** @var Tag $tag */ - foreach (Tag::get() as $tag) { - if ($tag->tag == $tagName && $this->user->id == $tag->user_id) { - return $tag; - break; - } - } - - return null; - } - } diff --git a/tests/TestCase.php b/tests/TestCase.php index 15d5dcd64d..b4c57aa833 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -31,15 +31,17 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase 'data' => $range, ] ); + // set period to match? + } // if selected "custom", change the session to a weird custom range: // (20 days): - if($range === "custom") { + if ($range === "custom") { $this->session( [ 'start' => Carbon::now(), - 'end' => Carbon::now()->subDays(20), - ] + 'end' => Carbon::now()->subDays(20), + ] ); } }