Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Jun 18, 2024
2 parents 6261eae + 4982645 commit 16d3426
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Foundation/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* }
* @phpstan-type TWorkbenchDiscoversConfig array{
* config: bool,
* factories: bool,
* web: bool,
* api: bool,
* commands: bool,
Expand All @@ -69,6 +70,7 @@
* }
* @phpstan-type TWorkbenchOptionalDiscoversConfig array{
* config?: bool,
* factories?: bool,
* web?: bool,
* api?: bool,
* commands?: bool,
Expand Down Expand Up @@ -150,6 +152,7 @@ class Config extends Fluent implements ConfigContract
'assets' => [],
'discovers' => [
'config' => false,
'factories' => false,
'web' => false,
'api' => false,
'commands' => false,
Expand All @@ -167,6 +170,7 @@ class Config extends Fluent implements ConfigContract
*/
protected array $workbenchDiscoversConfig = [
'config' => false,
'factories' => false,
'web' => false,
'api' => false,
'commands' => false,
Expand Down
24 changes: 22 additions & 2 deletions src/Foundation/UndefinedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,27 @@
/**
* @internal
*/
readonly class UndefinedValue
readonly class UndefinedValue implements \JsonSerializable
{
//
/**
* Determine if value is equivalent to "undefined" or "null".
*
* @param mixed $value
* @return bool
*/
public static function equalsTo($value)
{
return $value instanceof UndefinedValue || \is_null($value);
}

/**
* Get the value as json.
*
* @return null
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return null;
}
}
37 changes: 36 additions & 1 deletion src/Workbench/Workbench.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Application as Artisan;
use Illuminate\Console\Command;
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Foundation\Events\DiagnosingHealth;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -156,6 +157,40 @@ public static function discoverRoutes(ApplicationContract $app, ConfigContract $
$blade->componentNamespace('Workbench\\App\\View\\Components', 'workbench');
}
});

if (($discoversConfig['factories'] ?? false) === true) {
Factory::guessFactoryNamesUsing(static function ($modelName) {
/** @var class-string<\Illuminate\Database\Eloquent\Model> $modelName */
$workbenchNamespace = 'Workbench\\App\\';

$modelBasename = str_starts_with($modelName, $workbenchNamespace.'Models\\')
? Str::after($modelName, $workbenchNamespace.'Models\\')
: Str::after($modelName, $workbenchNamespace);

/** @var class-string<\Illuminate\Database\Eloquent\Factories\Factory> $factoryName */
$factoryName = 'Workbench\\Database\\Factories\\'.$modelBasename.'Factory';

return $factoryName;
});

Factory::guessModelNamesUsing(static function ($factory) {
/** @var \Illuminate\Database\Eloquent\Factories\Factory $factory */
$workbenchNamespace = 'Workbench\\App\\';

$namespacedFactoryBasename = Str::replaceLast(
'Factory', '', Str::replaceFirst('Workbench\\Database\\Factories\\', '', \get_class($factory))
);

$factoryBasename = Str::replaceLast('Factory', '', class_basename($factory));

/** @var class-string<\Illuminate\Database\Eloquent\Model> $modelName */
$modelName = class_exists($workbenchNamespace.'Models\\'.$namespacedFactoryBasename)
? $workbenchNamespace.'Models\\'.$namespacedFactoryBasename
: $workbenchNamespace.$factoryBasename;

return $modelName;
});
}
}

/**
Expand All @@ -180,7 +215,7 @@ public static function discoverCommandsRoutes(ApplicationContract $app): void
$command = $namespace.str_replace(
['/', '.php'],
['\\', ''],
Str::after($command->getRealPath(), (string) realpath(workbench_path('app')))
Str::after($command->getRealPath(), (string) realpath(workbench_path('app').DIRECTORY_SEPARATOR))
);

if (
Expand Down
1 change: 1 addition & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ workbench:
health: true
discovers:
config: true
factories: true
web: true
api: true
commands: true
Expand Down
4 changes: 4 additions & 0 deletions tests/Foundation/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function it_can_load_configuration_file()
'assets' => [],
'discovers' => [
'config' => false,
'factories' => false,
'web' => false,
'api' => false,
'commands' => false,
Expand All @@ -60,6 +61,7 @@ public function it_can_load_configuration_file()

$this->assertSame([
'config' => false,
'factories' => false,
'web' => false,
'api' => false,
'commands' => false,
Expand Down Expand Up @@ -105,6 +107,7 @@ public function it_can_load_default_configuration()
'assets' => [],
'discovers' => [
'config' => false,
'factories' => false,
'web' => false,
'api' => false,
'commands' => false,
Expand All @@ -115,6 +118,7 @@ public function it_can_load_default_configuration()

$this->assertSame([
'config' => false,
'factories' => false,
'web' => false,
'api' => false,
'commands' => false,
Expand Down
16 changes: 16 additions & 0 deletions tests/Workbench/DiscoversTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Orchestra\Testbench\Tests\Workbench;

use Composer\InstalledVersions;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;

#[WithConfig('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF')]
class DiscoversTest extends TestCase
Expand Down Expand Up @@ -97,4 +99,18 @@ public function it_can_discover_translation_files()
{
$this->assertSame('Good Morning', __('workbench::welcome.morning'));
}

#[Test]
#[TestWith(["Workbench\\Database\\Factories\\Illuminate\\Foundation\\Auh\\UserFactory", "Illuminate\\Foundation\\Auh\\User"])]
#[TestWith(["Workbench\\Database\\Factories\\UserFactory", "Workbench\\App\\Models\\User"])]
public function it_can_discover_database_factories_from_model(string $factory, string $model)
{
$this->assertSame($factory, Factory::resolveFactoryName($model));
}

#[Test]
public function it_can_discover_model_from_factory()
{
$this->assertSame('Workbench\App\Models\User', \Workbench\Database\Factories\UserFactory::new()->modelName());
}
}
2 changes: 2 additions & 0 deletions tests/Workbench/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function it_can_resolve_workbench()
'assets' => [],
'discovers' => [
'config' => false,
'factories' => false,
'web' => true,
'api' => false,
'commands' => false,
Expand All @@ -68,6 +69,7 @@ public function it_can_resolve_workbench_without_bound()
'assets' => [],
'discovers' => [
'config' => false,
'factories' => false,
'web' => false,
'api' => false,
'commands' => false,
Expand Down
42 changes: 42 additions & 0 deletions workbench/app/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Workbench\App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
use HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];

/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
39 changes: 39 additions & 0 deletions workbench/database/factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Workbench\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class UserFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}

/**
* Indicate that the model's email address should be unverified.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function unverified()
{
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
}

0 comments on commit 16d3426

Please sign in to comment.