added UserFactory and UserTest.php

This commit is contained in:
Peter Papp
2021-02-25 17:35:51 +01:00
parent ac38d35ae1
commit 2f332197e6
15 changed files with 97 additions and 73 deletions

View File

@@ -6,79 +6,19 @@ use App\Notifications\ResetPassword;
use App\Notifications\ResetUserPasswordNotification;
use ByteUnits\Metric;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Laravel\Cashier\Billable;
use Laravel\Passport\HasApiTokens;
use Kyslik\ColumnSortable\Sortable;
use Rinvex\Subscriptions\Traits\HasSubscriptions;
/**
* App\User
*
* @property int $id
* @property string $name
* @property string $email
* @property \Illuminate\Support\Carbon|null $email_verified_at
* @property string $password
* @property \Illuminate\Contracts\Routing\UrlGenerator|string $avatar
* @property string|null $remember_token
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\Laravel\Passport\Client[] $clients
* @property-read int|null $clients_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\FileManagerFolder[] $favourites
* @property-read int|null $favourites_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\FileManagerFile[] $files
* @property-read int|null $files_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\FileManagerFile[] $files_with_trashed
* @property-read int|null $files_with_trashed_count
* @property-read mixed $used_capacity
* @property-read \Illuminate\Database\Eloquent\Collection|\App\FileManagerFile[] $latest_uploads
* @property-read int|null $latest_uploads_count
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
* @property-read int|null $notifications_count
* @property-read \Illuminate\Database\Eloquent\Collection|\Laravel\Passport\Token[] $tokens
* @property-read int|null $tokens_count
* @method static \Illuminate\Database\Eloquent\Builder|\App\User newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\User newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\User query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereAvatar($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmail($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmailVerifiedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePassword($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRememberToken($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUpdatedAt($value)
* @mixin \Eloquent
* @property string $role
* @property string|null $stripe_id
* @property string|null $card_brand
* @property string|null $card_last_four
* @property string|null $trial_ends_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\FileManagerFolder[] $favourite_folders
* @property-read int|null $favourite_folders_count
* @property-read mixed $folder_tree
* @property-read mixed $storage
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Invoice[] $invoices
* @property-read int|null $invoices_count
* @property-read int|null $payment_cards_count
* @property-read \App\UserSettings|null $settings
* @property-read \Illuminate\Database\Eloquent\Collection|\Laravel\Cashier\Subscription[] $subscriptions
* @property-read int|null $subscriptions_count
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCardBrand($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCardLastFour($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRole($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereStripeId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\User whereTrialEndsAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|User sortable($defaultParameters = null)
*/
class User extends Authenticatable
{
use HasApiTokens, Notifiable, Billable, Sortable;
use Notifiable, Billable, Sortable, HasFactory, \Laravel\Sanctum\HasApiTokens;
protected $guarded = ['id', 'role'];
@@ -126,6 +66,10 @@ class User extends Authenticatable
'storage_capacity',
];
public $incrementing = false;
protected $keyType = 'string';
/**
* Get tax rate id for user
*
@@ -345,4 +289,16 @@ class User extends Authenticatable
{
return $this->hasOne(UserSettings::class);
}
/**
* Generate uuid
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->id = (string)Str::uuid();
});
}
}

View File

@@ -32,10 +32,10 @@
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.7",
"facade/ignition": "^2.3.6",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"fzaninotto/faker": "^1.9.1",
"mockery/mockery": "^1.3.1",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.0"
"phpunit/phpunit": "^9.5.2"
},
"config": {
"optimize-autoloader": true,

2
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "31b80dcbc3703ef7b5654f9afe18d13e",
"content-hash": "9f04ec7084949f2d0a1c343da44c30cd",
"packages": [
{
"name": "asm89/stack-cors",

View File

@@ -0,0 +1,61 @@
<?php
namespace Database\Factories;
use App\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'role' => $this->faker->randomElement(['user', 'admin']),
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => Hash::make('secret'),
'remember_token' => Str::random(10),
'created_at' => $this->faker->dateTimeBetween($startDate = '-36 months', $endDate = 'now', $timezone = null),
];
}
/**
* Configure the model factory.
*
* @return $this
*/
public function configure()
{
return $this->afterCreating(function (User $user) {
$user
->settings()
->create([
'storage_capacity' => $this->faker->randomNumber(1),
'name' => $this->faker->name,
'address' => $this->faker->address,
'state' => $this->faker->state,
'city' => $this->faker->city,
'postal_code' => $this->faker->postcode,
'country' => $this->faker->randomElement(['SK', 'CZ', 'DE', 'FR']),
'phone_number' => $this->faker->phoneNumber,
'timezone' => $this->faker->randomElement(['+1.0', '+2.0', '+3.0']),
]);
});
}
}

View File

@@ -2,6 +2,7 @@
namespace Tests\Feature;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
@@ -12,14 +13,20 @@ class UserTest extends TestCase
use DatabaseMigrations;
/**
* A basic feature test example.
*
* @return void
* @test
*/
public function test_example()
public function it_generate_and_store_user()
{
$response = $this->get('/');
$user = User::factory(User::class)
->create(['role' => 'user']);
$response->assertStatus(200);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'role' => 'user',
]);
$this->assertDatabaseHas('user_settings', [
'user_id' => $user->id,
]);
}
}