Skip to content

Firefly III: a personal finances manager

Some experimental model tests.

James Cole84f4e80da1c6
diff --git a/app/models/Account.php b/app/models/Account.php
index d3495e49f2..98f5c7e038 100644
--- a/app/models/Account.php
+++ b/app/models/Account.php
@@ -6,8 +6,19 @@ class Account extends Elegant
public static $rules
= [
- 'name' => 'required|between:1,100',
- 'user_id' => 'required|exists:users,id'
+ 'name' => 'required|between:1,100',
+ 'user_id' => 'required|exists:users,id',
+ 'account_type_id' => 'required|exists:account_types,id',
+ 'active' => 'required|between:0,1|numeric'
+
+ ];
+
+ public static $factory
+ = [
+ 'name' => 'string',
+ 'user_id' => 'factory|User',
+ 'account_type_id' => 'factory|AccountType',
+ 'active' => '1'
];
public function accountType()
diff --git a/app/models/AccountType.php b/app/models/AccountType.php
index 1cf95abb45..7ef7db459d 100644
--- a/app/models/AccountType.php
+++ b/app/models/AccountType.php
@@ -1,10 +1,16 @@
<?php
-class AccountType extends Eloquent {
+class AccountType extends Eloquent
+{
- public function accounts() {
+ public static $factory
+ = [
+ 'description' => 'string'
+ ];
+
+ public function accounts()
+ {
return $this->hasMany('Account');
}
-
}
\ No newline at end of file
diff --git a/app/models/Preference.php b/app/models/Preference.php
index 45cd23fb99..b9c82a3f09 100644
--- a/app/models/Preference.php
+++ b/app/models/Preference.php
@@ -9,6 +9,12 @@ class Preference extends Elegant
'data' => 'required'
];
+ public static $factory = [
+ 'user_id' => 'factory|User',
+ 'name' => 'string',
+ 'data' => 'string'
+ ];
+
public function user()
{
return $this->belongsTo('User');
diff --git a/app/models/User.php b/app/models/User.php
index f8c8a2fe08..b988887997 100644
--- a/app/models/User.php
+++ b/app/models/User.php
@@ -18,6 +18,14 @@ class User extends Elegant implements UserInterface, RemindableInterface
'password' => 'required|between:60,60',
'reset' => 'between:32,32',
];
+
+ public static $factory
+ = [
+ 'email' => 'email',
+ 'password' => 'string',
+ 'migrated' => '0'
+
+ ];
/**
* The database table used by the model.
*
diff --git a/app/tests/models/AllModelsTest.php b/app/tests/models/AllModelsTest.php
new file mode 100644
index 0000000000..e7150eded5
--- /dev/null
+++ b/app/tests/models/AllModelsTest.php
@@ -0,0 +1,55 @@
+<?php
+
+use Zizaco\FactoryMuff\Facade\FactoryMuff;
+
+class AllModelsTest extends TestCase
+{
+ /**
+ * Default preparation for each test
+ */
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->prepareForTests();
+ }
+
+ /**
+ * Migrate the database
+ */
+ private function prepareForTests()
+ {
+ Artisan::call('migrate');
+ Artisan::call('db:seed');
+ }
+
+
+ /**
+ * User tests
+ */
+ public function testUser()
+ {
+ $user = FactoryMuff::create('User');
+ $pref = FactoryMuff::create('Preference');
+ $account = FactoryMuff::create('Account');
+
+ $account->user()->associate($user);
+ $pref->user()->associate($user);
+
+ $this->assertEquals($account->user_id, $user->id);
+ $this->assertEquals($pref->user_id,$user->id);
+
+
+ $this->assertTrue(true);
+
+ }
+
+ /**
+ * Account tests
+ */
+ public function testUserAccounts()
+ {
+ $this->assertTrue(true);
+
+ }
+}
\ No newline at end of file
diff --git a/app/tests/models/TransactionJournalTest.php b/app/tests/models/TransactionJournalTest.php
deleted file mode 100644
index 540dfe2bd9..0000000000
--- a/app/tests/models/TransactionJournalTest.php
+++ /dev/null
@@ -1,30 +0,0 @@
-<?php
-
-class TransactionJournalTest extends TestCase
-{
- /**
- * Default preparation for each test
- */
- public function setUp()
- {
- parent::setUp();
-
- $this->prepareForTests();
- }
-
- /**
- * Migrate the database
- */
- private function prepareForTests()
- {
- Artisan::call('migrate');
- }
-
- /**
- * Test accounts
- */
- public function testJournal()
- {
- $this->assertTrue(true);
- }
-}
\ No newline at end of file
diff --git a/app/tests/models/UserTest.php b/app/tests/models/UserTest.php
deleted file mode 100644
index bfe00206ac..0000000000
--- a/app/tests/models/UserTest.php
+++ /dev/null
@@ -1,75 +0,0 @@
-<?php
-
-class UserTest extends TestCase
-{
- /**
- * Default preparation for each test
- */
- public function setUp()
- {
- parent::setUp();
-
- $this->prepareForTests();
- }
-
- /**
- * Migrate the database
- */
- private function prepareForTests()
- {
- Artisan::call('migrate');
- }
-
- /**
- * Username is required
- */
- public function testUsernameIsRequired()
- {
- // Create a new User
- $user = new User;
- $user->migrated = 0;
- $user->password = Str::random(60);
-
- // User should not save
- $this->assertFalse($user->isValid());
-
- // Save the errors
- $errors = $user->validator->messages()->all();
-// // There should be 1 error
- $this->assertCount(1, $errors);
-
-// // The username error should be set
- $this->assertEquals($errors[0], "The email field is required.");
- }
-
- /**
- * Test accounts
- */
- public function testUserAccounts()
- {
- // Create a new User
- $user = new User;
- $user->email = 'bla';
- $user->password = 'bla';
- $user->migrated = 0;
- $user->save();
-
- // account type:
- $at = new AccountType;
- $at->description = 'Bla';
- $at->save();
-
-
- $account = new Account;
- $account->name = 'bla';
- $account->active = 1;
- $account->accountType()->associate($at);
- $account->user()->associate($user);
-
- $account->save();
-
- $this->assertCount(1,$user->accounts()->get());
-
- }
-
-}
\ No newline at end of file
diff --git a/composer.json b/composer.json
index 54937a4e48..48211d2bc9 100644
--- a/composer.json
+++ b/composer.json
@@ -28,7 +28,8 @@
"barryvdh/laravel-debugbar": "1.*",
"barryvdh/laravel-ide-helper": "1.*",
"mockery/mockery": "dev-master",
- "satooshi/php-coveralls": "dev-master"
+ "satooshi/php-coveralls": "dev-master",
+ "zizaco/factory-muff": "dev-master"
},
"autoload": {
"classmap": [