Skip to content

Commit da800be

Browse files
Replace laravel/legacy-factories with updated factory syntax
1 parent b0afb0e commit da800be

File tree

8 files changed

+63
-26
lines changed

8 files changed

+63
-26
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"require-dev": {
1616
"squizlabs/php_codesniffer": "^2.8",
1717
"wamania/php-stemmer": "^3.0",
18-
"orchestra/testbench": "^6.23|^7.0",
19-
"laravel/legacy-factories": "^1.1"
18+
"orchestra/testbench": "^6.23|^7.0"
2019
},
2120
"autoload": {
2221
"files": [

tests/Factories/CommentFactory.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Baril\Sqlout\Tests\Factories;
4+
5+
use Baril\Sqlout\Tests\Models\Comment;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use Illuminate\Support\Facades\Hash;
8+
use Illuminate\Support\Str;
9+
10+
class CommentFactory extends Factory
11+
{
12+
protected $model = Comment::class;
13+
14+
public function definition(): array
15+
{
16+
return [
17+
'author' => $this->faker->name(),
18+
'text' => $this->faker->text(50),
19+
];
20+
}
21+
}

tests/Factories/PostFactory.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Baril\Sqlout\Tests\Factories;
4+
5+
use Baril\Sqlout\Tests\Models\Post;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use Illuminate\Support\Facades\Hash;
8+
use Illuminate\Support\Str;
9+
10+
class PostFactory extends Factory
11+
{
12+
protected $model = Post::class;
13+
14+
public function definition(): array
15+
{
16+
return [
17+
'title' => $this->faker->sentence(3),
18+
'body' => $this->faker->text(50),
19+
];
20+
}
21+
}

tests/Models/Comment.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Baril\Sqlout\Tests\Models;
44

5-
use Illuminate\Database\Eloquent\Model;
65
use Baril\Sqlout\Searchable;
6+
use Baril\Sqlout\Tests\Factories\CommentFactory;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use Illuminate\Database\Eloquent\Model;
79

810
class Comment extends Model
911
{
10-
use Searchable;
12+
use HasFactory, Searchable;
1113

1214
public function post()
1315
{
@@ -26,4 +28,9 @@ public function scopeAuthor($query, $author)
2628
{
2729
$query->where('author', $author);
2830
}
31+
32+
protected static function newFactory()
33+
{
34+
return CommentFactory::new();
35+
}
2936
}

tests/Models/Post.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Baril\Sqlout\Tests\Models;
44

5+
use Baril\Sqlout\Searchable;
6+
use Baril\Sqlout\Tests\Factories\PostFactory;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
58
use Illuminate\Database\Eloquent\Model;
69
use Illuminate\Database\Eloquent\SoftDeletes;
7-
use Baril\Sqlout\Searchable;
810

911
class Post extends Model
1012
{
11-
use SoftDeletes, Searchable;
13+
use HasFactory, SoftDeletes, Searchable;
1214

1315
protected $weights = [
1416
'title' => 4,
@@ -21,4 +23,9 @@ public function toSearchableArray()
2123
'body' => $this->body,
2224
];
2325
}
26+
27+
protected static function newFactory()
28+
{
29+
return PostFactory::new();
30+
}
2431
}

tests/SearchTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ protected function setUp(): void
1616
{
1717
parent::setUp();
1818

19-
factory(Post::class, 5)->create();
20-
factory(Comment::class, 5)->create();
19+
Post::factory()->count(5)->create();
20+
Comment::factory()->count(5)->create();
2121
}
2222

2323
protected function newSearchQuery()

tests/TestCase.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ protected function setUp() : void
4545
{
4646
parent::setUp();
4747
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
48-
$this->withFactories(__DIR__ . '/database/factories');
4948
\DB::enableQueryLog();
5049
}
5150

tests/database/factories/ModelFactory.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)