Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-14 #15

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ jobs:
with:
level: 9
php_version: "8.2"
path: src/ tests/Unit/
path: config/ database/ resources/ src/ tests/Feature/ tests/Unit/
- name: Stopping timer
if: ${{ !cancelled() }}
id: timer_end
Expand Down
3 changes: 2 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@
__DIR__.'/database',
// __DIR__.'/lang',
// __DIR__.'/routes',
__DIR__.'/resources',
__DIR__.'/src',
// __DIR__.'/tests/Feature',
__DIR__.'/tests/Feature',
__DIR__.'/tests/Unit',
])
->name('*.php')
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Playground CI Workflow](https://github.com/gammamatrix/playground-test/actions/workflows/ci.yml/badge.svg?branch=develop)](https://raw.githubusercontent.com/gammamatrix/playground-test/testing/develop/testdox.txt)
[![Test Coverage](https://raw.githubusercontent.com/gammamatrix/playground-test/testing/develop/coverage.svg)](tests)
[![PHPStan Level 9 src and tests](https://img.shields.io/badge/PHPStan-level%209-brightgreen)](.github/workflows/ci.yml#L120)

The Playground Test package.

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"php": "^8.1",
"fakerphp/faker": "^1.9.1",
"friendsofphp/php-cs-fixer": "^3.41",
"gammamatrix/playground": "dev-develop|dev-master|dev-feature/*|^73.0",
"larastan/larastan": "^2.0",
"orchestra/testbench": "8.*",
"phpstan/phpstan-phpunit": "^1.3",
Expand Down
25 changes: 21 additions & 4 deletions database/factories/AbstractUserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,39 @@ abstract class AbstractUserFactory extends Factory
*/
protected $model = AbstractUser::class;

/**
* The current password being used by the factory.
*/
protected static ?string $password = null;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$password = config('playground-test.password', 'password');
if (empty(static::$password)) {
$password = config('auth.testing.password');
$test_password_hashed = config('auth.testing.hashed');

if (empty($password) || ! is_string($password)) {
$password = md5(Carbon::now()->format('c'));
$test_password_hashed = false;
}

if (! $test_password_hashed) {
$password = Hash::make($password);
}

static::$password = $password;
}

return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => Carbon::now()->format('Y-m-d H:i:s'),
'password' => Hash::make(
$password && is_string($password) ? $password : md5(date('c'))
),
'password' => static::$password,
'remember_token' => Str::random(10),
];
}
Expand Down
112 changes: 112 additions & 0 deletions database/factories/PlaygroundUserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Playground
*/
namespace Database\Factories\Playground\Test\Models;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Playground\Test\Models\PlaygroundUser;

/**
* \Database\Factories\Playground\Test\Models\PlaygroundUserFactory
*/
class PlaygroundUserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var class-string<PlaygroundUser>
*/
protected $model = PlaygroundUser::class;

/**
* The current password being used by the factory.
*/
protected static ?string $password = null;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
if (empty(static::$password)) {
$password = config('auth.testing.password');
$test_password_hashed = config('auth.testing.hashed');

if (empty($password) || ! is_string($password)) {
$password = md5(Carbon::now()->format('c'));
$test_password_hashed = false;
}

if (! $test_password_hashed) {
$password = Hash::make($password);
}

static::$password = $password;
}

return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => Carbon::now()->format('Y-m-d H:i:s'),
'role' => 'user',
'password' => static::$password,
'remember_token' => Str::random(10),
];
}

/**
* Indicate that the model's email address should be unverified.
*/
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}

/**
* Indicate that the user has the admin role.
*/
public function admin(): Factory
{
return $this->state(fn (array $attributes) => [
'role' => 'admin',
]);
}

/**
* Indicate that the user has the manager role.
*/
public function manager(): Factory
{
return $this->state(fn (array $attributes) => [
'role' => 'manager',
]);
}

/**
* Indicate that the user has the wheel role.
*/
public function wheel(): Factory
{
return $this->state(fn (array $attributes) => [
'role' => 'wheel',
]);
}

/**
* Indicate that the user has the root role.
*/
public function root(): Factory
{
return $this->state(fn (array $attributes) => [
'role' => 'root',
]);
}
}
112 changes: 112 additions & 0 deletions database/factories/PlaygroundUserWithSanctumFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Playground
*/
namespace Database\Factories\Playground\Test\Models;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Playground\Test\Models\PlaygroundUserWithSanctum;

/**
* \Database\Factories\Playground\Test\Models\PlaygroundUserWithSanctumFactory
*/
class PlaygroundUserWithSanctumFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var class-string<PlaygroundUserWithSanctum>
*/
protected $model = PlaygroundUserWithSanctum::class;

/**
* The current password being used by the factory.
*/
protected static ?string $password = null;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
if (empty(static::$password)) {
$password = config('auth.testing.password');
$test_password_hashed = config('auth.testing.hashed');

if (empty($password) || ! is_string($password)) {
$password = md5(Carbon::now()->format('c'));
$test_password_hashed = false;
}

if (! $test_password_hashed) {
$password = Hash::make($password);
}

static::$password = $password;
}

return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => Carbon::now()->format('Y-m-d H:i:s'),
'role' => 'user',
'password' => static::$password,
'remember_token' => Str::random(10),
];
}

/**
* Indicate that the model's email address should be unverified.
*/
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}

/**
* Indicate that the user has the admin role.
*/
public function admin(): Factory
{
return $this->state(fn (array $attributes) => [
'role' => 'admin',
]);
}

/**
* Indicate that the user has the manager role.
*/
public function manager(): Factory
{
return $this->state(fn (array $attributes) => [
'role' => 'manager',
]);
}

/**
* Indicate that the user has the wheel role.
*/
public function wheel(): Factory
{
return $this->state(fn (array $attributes) => [
'role' => 'wheel',
]);
}

/**
* Indicate that the user has the root role.
*/
public function root(): Factory
{
return $this->state(fn (array $attributes) => [
'role' => 'root',
]);
}
}
31 changes: 31 additions & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
namespace Database\Factories\Playground\Test\Models;

use Illuminate\Database\Eloquent\Factories\Factory;
use Playground\Test\Models\User;

/**
Expand All @@ -17,4 +18,34 @@ class UserFactory extends AbstractUserFactory
* @var class-string<User>
*/
protected $model = User::class;

/**
* Set the user up as an admin user.
*/
public function admin(): Factory
{
return $this->state(fn (array $attributes) => [
'email' => '[email protected]',
]);
}

/**
* Set the user up as a guest user.
*/
public function guest(): Factory
{
return $this->state(fn (array $attributes) => [
'email' => '[email protected]',
]);
}

/**
* Set the user up as a manager user.
*/
public function manager(): Factory
{
return $this->state(fn (array $attributes) => [
'email' => '[email protected]',
]);
}
}
Loading
Loading