Skip to content

Firefly III: a personal finances manager

Fixed tests, added migration stuff, added default user.

James Coleabbbba219ac8
diff --git a/app/controllers/MigrationController.php b/app/controllers/MigrationController.php
index 025b93412e..7e40a9b7b1 100644
--- a/app/controllers/MigrationController.php
+++ b/app/controllers/MigrationController.php
@@ -1,7 +1,94 @@
<?php
+use Firefly\Helper\Migration\MigrationHelperInterface as MHI;
+
class MigrationController extends BaseController
{
+ protected $migration;
+
+ public function __construct(MHI $migration)
+ {
+ $this->migration = $migration;
+
+ }
+
+ public function index()
+ {
+ return View::make('migrate.index');
+ }
+
+ public function postIndex()
+ {
+ if (Input::hasFile('exportFile')) {
+
+ // get content:
+ $file = Input::file('exportFile');
+ $path = $file->getRealPath();
+
+ $this->migration->loadFile($path);
+
+ if (!$this->migration->validFile()) {
+ return View::make('error')->with('message', 'Invalid JSON content.');
+ }
+ }
+
+ // then, start migrating!
+
+
+//
+//
+// // map old and new id's.
+// $map = [
+// 'accounts' => [],
+// 'beneficiaries' => [],
+// ];
+//
+// // get the account types we need
+// $beneficiaryAT = AccountType::where('description', 'Beneficiary account')->first();
+//
+// // save all accounts:
+// foreach ($JSON->accounts as $entry) {
+// // create account:
+// if ($entry->openingbalance == 0) {
+// $account = $this->accounts->store(['name' => $entry->name]);
+// } else {
+// $account = $this->accounts->storeWithInitialBalance(
+// ['name' => $entry->name],
+// new Carbon($entry->openingbalancedate),
+// floatval($entry->openingbalance)
+// );
+// }
+// $map['accounts'][$entry->id] = $account->id;
+// }
+// unset($entry);
+//
+// // save all components:
+// foreach ($JSON->components as $entry) {
+// switch ($entry->type->type) {
+// case 'beneficiary':
+// // create new account for beneficiary:
+// $account = $this->accounts->store(['name' => $entry->name]);
+// $map['beneficiaries'][$entry->id] = $account;
+// break;
+// case 'category':
+// // create new component for category:
+//// $account = $this->accounts->store(['name' => $entry->name]);
+//// $map['beneficiaries'][$entry->id] = $account;
+// break;
+// }
+// }
+// unset($JSON->accounts);
+//
+//
+// var_dump($JSON);
+// var_dump($map);
+//
+// exit;
+// }
+//
+ return View::make('error')->with('message', 'No file found.');
+ }
+
/*
public function index()
{
diff --git a/app/database/seeds/DatabaseSeeder.php b/app/database/seeds/DatabaseSeeder.php
index c45bdcb0b9..e4af46af43 100644
--- a/app/database/seeds/DatabaseSeeder.php
+++ b/app/database/seeds/DatabaseSeeder.php
@@ -16,6 +16,7 @@ class DatabaseSeeder extends Seeder
$this->call('TransactionCurrencySeeder');
$this->call('TransactionTypeSeeder');
$this->call('ComponentTypeSeeder');
+ $this->call('DefaultUserSeeder');
}
}
diff --git a/app/database/seeds/DefaultUserSeeder.php b/app/database/seeds/DefaultUserSeeder.php
new file mode 100644
index 0000000000..f8cebf254d
--- /dev/null
+++ b/app/database/seeds/DefaultUserSeeder.php
@@ -0,0 +1,27 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: sander
+ * Date: 03/07/14
+ * Time: 21:06
+ */
+class DefaultUserSeeder extends Seeder
+{
+ public function run()
+ {
+ DB::table('users')->delete();
+
+ User::create(
+ [
+ 'email' => '[email protected]',
+ 'password' => Hash::make('sander'),
+ 'verification' => null,
+ 'reset' => null,
+ 'remember_token' => null,
+ 'migrated' => false
+ ]
+ );
+ }
+
+}
\ No newline at end of file
diff --git a/app/lib/Firefly/Helper/Migration/MigrationHelper.php b/app/lib/Firefly/Helper/Migration/MigrationHelper.php
new file mode 100644
index 0000000000..06941dfcec
--- /dev/null
+++ b/app/lib/Firefly/Helper/Migration/MigrationHelper.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: sander
+ * Date: 03/07/14
+ * Time: 21:34
+ */
+
+namespace Firefly\Helper\Migration;
+
+
+class MigrationHelper implements MigrationHelperInterface
+{
+ protected $path;
+ protected $JSON;
+
+ public function loadFile($path)
+ {
+ $this->path = $path;
+ }
+
+ public function validFile()
+ {
+ // file does not exist:
+ if(!file_exists($this->path)) {
+ return false;
+ }
+
+ // load the content:
+ $content = file_get_contents($this->path);
+ if($content === false) {
+ return false;
+ }
+
+ // parse the content
+ $this->JSON = json_decode($content);
+ if(is_null($this->JSON)) {
+ return false;
+ }
+ }
+}
\ No newline at end of file
diff --git a/app/lib/Firefly/Helper/Migration/MigrationHelperInterface.php b/app/lib/Firefly/Helper/Migration/MigrationHelperInterface.php
new file mode 100644
index 0000000000..867a6da119
--- /dev/null
+++ b/app/lib/Firefly/Helper/Migration/MigrationHelperInterface.php
@@ -0,0 +1,18 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: sander
+ * Date: 03/07/14
+ * Time: 21:33
+ */
+
+namespace Firefly\Helper\Migration;
+
+
+interface MigrationHelperInterface
+{
+ public function loadFile($path);
+
+ public function validFile();
+
+}
\ No newline at end of file
diff --git a/app/tests/controllers/HomeControllerTest.php b/app/tests/controllers/HomeControllerTest.php
index 7caab01cd0..1b120abe3e 100644
--- a/app/tests/controllers/HomeControllerTest.php
+++ b/app/tests/controllers/HomeControllerTest.php
@@ -10,8 +10,13 @@ class HomeControllerTest extends TestCase
public function testIndex()
{
// mock:
- View::shouldReceive('make')->with('index');
+ View::shouldReceive('make')->with('index')->once()->andReturn(\Mockery::self())
+ ->shouldReceive('with')->once() // Pass a 'with' parameter
+ ->with('count', 0)
+ ;
Auth::shouldReceive('check')->andReturn(true);
+ $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
+ $accounts->shouldReceive('count')->andReturn(0);
// call
$this->call('GET', '/');