Skip to content

Commit

Permalink
Add factory() method for Factory.php
Browse files Browse the repository at this point in the history
  • Loading branch information
david_smith committed Oct 15, 2024
1 parent c0d6d69 commit e4841f2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ class User

public $first_name;
public $last_name;

public static function factory(array $states): UserFactory
{
return new UserFactory($states);
}
}

class UserFactory
{
use \Zerotoprod\DataModelFactory\Factory;
Expand Down Expand Up @@ -70,7 +66,7 @@ class UserFactory
}
}

$User = User::factory([User::last_name => 'Doe'])
$User = UserFactory::factory([User::last_name => 'Doe'])
->setFirstName('Jane')
->make();

Expand Down
17 changes: 16 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Zerotoprod\DataModelFactory;

use Tests\Unit\Override\UserFactory;

/**
* Factory for Instantiating a Class.
*
Expand Down Expand Up @@ -43,7 +45,7 @@
* ```
*
* @link https://github.com/zero-to-prod/data-model-factory
* @see https://github.com/zero-to-prod/data-model
* @see https://github.com/zero-to-prod/data-model
*/
trait Factory
{
Expand Down Expand Up @@ -77,6 +79,19 @@ private function definition(): array
return [];
}

/**
* Get a new factory instance for the model
*
* @return self
*
* @link https://github.com/zero-to-prod/data-model-factory
* @see https://github.com/zero-to-prod/data-model
*/
public static function factory(array $context = []): self
{
return new static($context);
}

/**
* Instantiates the class using `$this->context`.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Factory/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FactoryTest extends TestCase
*/
public function from(): void
{
$User = User::factory()->make();
$User = UserFactory::factory()->make();

self::assertEquals('John', $User->first_name);
self::assertEquals('N/A', $User->last_name);
Expand Down
5 changes: 0 additions & 5 deletions tests/Unit/Factory/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ class User
public const last_name = 'last_name';
public $first_name;
public $last_name;

public static function factory(array $states = []): UserFactory
{
return new UserFactory($states);
}
}

0 comments on commit e4841f2

Please sign in to comment.