generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from Temepest74/factory-support
Factory support
- Loading branch information
Showing
4 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Cjmellor\Approval\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
trait MustBeApprovedFactory | ||
{ | ||
public function withoutApproval(): Factory | ||
{ | ||
return $this->afterMaking(function (Model $model) { | ||
if (in_array(MustBeApproved::class, class_uses($model))) { | ||
$model->withoutApproval(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Cjmellor\Approval\Tests\Feature\Factories; | ||
|
||
use Cjmellor\Approval\Concerns\MustBeApprovedFactory; | ||
use Cjmellor\Approval\Tests\Models\FakeModel; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class FakeModelFactory extends Factory | ||
{ | ||
use MustBeApprovedFactory; | ||
|
||
protected $model = FakeModel::class; | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'name' => 'Bob', | ||
'meta' => 'green', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
use Cjmellor\Approval\Tests\Models\FakeModel; | ||
|
||
test(description: 'a model is added via a factory when the "withoutApproval()" method is used ', closure: function () { | ||
FakeModel::factory()->withoutApproval()->create(); | ||
|
||
$this->assertDatabaseHas('fake_models', [ | ||
'name' => 'Bob', | ||
'meta' => 'green', | ||
]); | ||
|
||
$this->assertDatabaseMissing('approvals', [ | ||
'new_data' => json_encode([ | ||
'name' => 'Bob', | ||
]), | ||
]); | ||
}); | ||
|
||
test(description: 'many models are added via a factory when the "withoutApproval()" method is used ', closure: function () { | ||
FakeModel::factory()->withoutApproval()->count(4)->create(); | ||
|
||
$this->assertCount(4, FakeModel::all()); | ||
|
||
FakeModel::all()->each(function ($model) { | ||
$this->assertEquals('Bob', $model->name); | ||
$this->assertEquals('green', $model->meta); | ||
}); | ||
|
||
$this->assertDatabaseMissing('approvals', [ | ||
'new_data' => json_encode([ | ||
'name' => 'Bob', | ||
]), | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters