Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit 7e3656f

Browse files
author
Mario Basic
committed
Added seeder class to seed sample data for demonstration purposes. Added model factories. Updated readme.
1 parent 5d07577 commit 7e3656f

11 files changed

+181
-10
lines changed

app/Console/Commands/CreateUser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ public function handle()
6262
'name' => $data['name'],
6363
'email' => $data['email'],
6464
'password' => bcrypt($data['password']),
65-
'api_token' => bin2hex(openssl_random_pseudo_bytes(16))
65+
'api_token' => bin2hex(openssl_random_pseudo_bytes(16)),
66+
'email_notifications' => true,
67+
'preferred_currency' => 'usd'
6668
]);
6769

6870
$this->info('User has been created.');

database/factories/ModelFactory.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,49 @@
1717
'email' => $faker->safeEmail,
1818
'password' => bcrypt(str_random(10)),
1919
'remember_token' => str_random(10),
20+
'api_token' => bin2hex(openssl_random_pseudo_bytes(16)),
21+
'email_notifications' => false, //$faker->boolean
22+
'preferred_currency' => $faker->randomElement(['hrk', 'usd', 'eur'])
23+
];
24+
});
25+
26+
$factory->define(App\Category::class, function (Faker\Generator $faker) {
27+
return [
28+
'name' => $faker->randomElement(['Hosting', 'Maintenance', 'Domain', 'SSL Certificate']),
29+
];
30+
});
31+
32+
$factory->define(App\Client::class, function (Faker\Generator $faker) {
33+
return [
34+
'name' => $faker->name,
35+
'tax_number' => $faker->randomNumber,
36+
'street' => $faker->streetAddress,
37+
'city' => $faker->city,
38+
'postal_code' => $faker->postcode
39+
];
40+
});
41+
42+
$factory->define(App\Service::class, function (Faker\Generator $faker) {
43+
return [
44+
'title' => $faker->sentence(3),
45+
'note' => $faker->text(200),
46+
'month' => (int) $faker->month,
47+
'day' => (int) $faker->dayOfMonth,
48+
'cost' => $faker->randomNumber(5),
49+
'currency' => $faker->randomElement(['hrk', 'eur', 'usd']),
50+
'active' => $faker->boolean,
51+
'exchange_rate' => $faker->randomFloat(4, 1, 11),
52+
'client_id' => factory(App\Client::class)->create()->id,
53+
'category_id' => factory(App\Category::class)->create()->id,
54+
];
55+
});
56+
57+
$factory->define(App\Occurrence::class, function (Faker\Generator $faker) {
58+
return [
59+
'occurs_at' => $faker->dateTimeThisYear,
60+
'offer_sent' => $faker->boolean,
61+
'payment_received' => $faker->boolean,
62+
'receipt_sent' => $faker->boolean,
63+
'service_id' => factory(App\Service::class)->create()->id
2064
];
2165
});

database/migrations/2016_07_16_212605_create_categories_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function up()
3232
public function down()
3333
{
3434
Schema::table('services', function(Blueprint $table) {
35-
$table->dropColumn('category_id');
3635
$table->dropForeign(['category_id']);
36+
$table->dropColumn('category_id');
3737
});
3838

3939
Schema::drop('categories');

database/migrations/2016_07_18_123554_drop_unique_index_for_tax_number_from_clients_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function up()
2626
public function down()
2727
{
2828
Schema::table('clients', function (Blueprint $table) {
29-
$table->unique('tax_number');
29+
// $table->unique('tax_number'); // can cause problems when refreshing db.
3030
});
3131
}
3232
}

database/seeds/DatabaseSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder
1111
*/
1212
public function run()
1313
{
14-
// $this->call(UsersTableSeeder::class);
14+
$this->call(SampleDataSeeder::class);
1515
}
1616
}

database/seeds/SampleDataSeeder.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
use Illuminate\Database\Seeder;
4+
5+
/**
6+
* Seeds the database with sample data so that the user
7+
* can see what the application looks like with data entered.
8+
*
9+
* 10 clients with 10 services each.
10+
* Each service is assigned a category from a pool of categories.
11+
*/
12+
class SampleDataSeeder extends Seeder
13+
{
14+
/**
15+
* Run the database seeds.
16+
*
17+
* @return void
18+
*/
19+
public function run()
20+
{
21+
/**
22+
* Just a shortcut for creating occurrences.
23+
*
24+
* When a service is created we want to trigger a event
25+
* `ServiceWasCreated` which then creates an occurrence
26+
* for that service.
27+
*/
28+
App\Service::created(function($service) {
29+
event(new App\Events\ServiceWasCreated($service));
30+
});
31+
32+
$categories = ['Hosting', 'Maintenance', 'Domain', 'SSL Certificate'];
33+
34+
array_map(function($category) {
35+
return factory(App\Category::class)->create([
36+
'name' => $category
37+
]);
38+
}, $categories);
39+
40+
$clients = factory(App\Client::class, 10)->create();
41+
42+
foreach($clients as $client) {
43+
$services = factory(App\Service::class, 10)->create([
44+
'client_id' => $client->id,
45+
'category_id' => rand(1, count($categories))
46+
]);
47+
}
48+
49+
/**
50+
* Sample User for testing purposes:
51+
*
52+
* email: sample.user@email.com
53+
* password: password
54+
*
55+
*/
56+
factory(App\User::class)->create([
57+
'name' => 'Sample User',
58+
'email' => 'sample@user.dev',
59+
'password' => bcrypt('password'),
60+
'preferred_currency' => 'usd'
61+
]);
62+
63+
/**
64+
* Cleanup
65+
*
66+
* Because there is a long standing bug in Laravel
67+
* with model factories, I need to manually remove extra
68+
* records created.
69+
*/
70+
App\Category::has('services', '=', 0)->delete();
71+
App\Client::has('services', '=', 0)->delete();
72+
73+
}
74+
}

readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ Install by issuing the Composer `create-project` command in your terminal:
3030
composer create-project --prefer-dist laravelista/kyle
3131
```
3232

33+
### Sample data
34+
35+
To populate Kyle with sample data and see how the Overview and Report pages look like, use this command:
36+
37+
```
38+
php artisan migrate --seed
39+
```
40+
41+
> Warning! This command will populate the database with a lot of sample data. Use only while testing or if you understand what will happen once this command has triggered.
42+
43+
This will create a sample user with which you can login:
44+
45+
| Email | Password |
46+
|-----------------------|----------|
47+
| sample@user.dev | password |
48+
3349
## Create new user
3450

3551
To create a new user use this command:

resources/assets/js/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ $('#category_id').selectize({
100100
}
101101
});
102102

103-
// Categories can be created on-the-fly
103+
// Clients can be created on-the-fly
104104
$('#client_id').selectize({
105105
persist: true,
106106
create: function (input, callback) {

tests/ExampleTest.php renamed to tests/CategoryTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
use Illuminate\Foundation\Testing\DatabaseMigrations;
55
use Illuminate\Foundation\Testing\DatabaseTransactions;
66

7-
class ExampleTest extends TestCase
7+
class CategoryTest extends TestCase
88
{
99
/**
10-
* A basic functional test example.
10+
* A basic test example.
1111
*
1212
* @return void
1313
*/
14-
public function testBasicExample()
14+
public function testExample()
1515
{
16-
$this->visit('/')
17-
->see('Kyle');
16+
$this->assertTrue(true);
1817
}
1918
}

tests/ClientTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Illuminate\Foundation\Testing\WithoutMiddleware;
4+
use Illuminate\Foundation\Testing\DatabaseMigrations;
5+
use Illuminate\Foundation\Testing\DatabaseTransactions;
6+
7+
class ClientTest extends TestCase
8+
{
9+
/**
10+
* A basic test example.
11+
*
12+
* @return void
13+
*/
14+
public function testExample()
15+
{
16+
$this->assertTrue(true);
17+
}
18+
}

tests/ServiceTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Illuminate\Foundation\Testing\WithoutMiddleware;
4+
use Illuminate\Foundation\Testing\DatabaseMigrations;
5+
use Illuminate\Foundation\Testing\DatabaseTransactions;
6+
7+
class ServiceTest extends TestCase
8+
{
9+
/**
10+
* A basic test example.
11+
*
12+
* @return void
13+
*/
14+
public function testExample()
15+
{
16+
$this->assertTrue(true);
17+
}
18+
}

0 commit comments

Comments
 (0)