Skip to content

Commit 5e0d503

Browse files
committed
Upgrade to Laravel 8
1 parent ea17a3a commit 5e0d503

File tree

9 files changed

+79
-31
lines changed

9 files changed

+79
-31
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"php": "^7.3",
1616
"illuminate/database": "^8.0",
1717
"illuminate/support": "^8.0",
18-
"laravel/legacy-factories": "^1.0",
19-
"mockery/mockery": "^1.2"
18+
"mockery/mockery": "^1.3.1"
2019
},
2120
"require-dev": {
2221
"phpunit/phpunit": "^8.4|^9.0",
@@ -30,7 +29,8 @@
3029
"autoload-dev": {
3130
"psr-4": {
3231
"Illuminated\\Testing\\Tests\\": "tests/",
33-
"Illuminated\\Testing\\Tests\\App\\": "tests/fixture/app/"
32+
"Illuminated\\Testing\\Tests\\App\\": "tests/fixture/app/",
33+
"Illuminated\\Testing\\Tests\\Database\\Factories\\": "tests/fixture/database/factories/"
3434
}
3535
},
3636
"config": {

src/Asserts/EloquentAsserts.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminated\Testing\Asserts;
44

5+
use Illuminate\Database\Eloquent\Factories\Factory;
56
use Illuminate\Database\Eloquent\Relations\BelongsTo;
67
use Illuminate\Database\Eloquent\Relations\HasMany;
78
use Illuminate\Support\Str;
@@ -186,8 +187,10 @@ protected function assertEloquentHasMany(string $class, string $relation)
186187
$childKey = $childModel->getKeyName();
187188
$childForeignKey = $hasManyRelation->getForeignKeyName();
188189

189-
$parent = factory($class)->create();
190-
$children = factory(get_class($childModel), 3)->create([$childForeignKey => $parent->{$parentKey}]);
190+
$parent = Factory::factoryForModel($class)->create();
191+
$children = Factory::factoryForModel(get_class($childModel))
192+
->count(3)
193+
->create([$childForeignKey => $parent->{$parentKey}]);
191194

192195
$this->assertCollectionsEqual($children, $parent->{$relation}, $childKey);
193196
}
@@ -212,9 +215,9 @@ protected function assertEloquentHasCreateFor(string $class, string $relation, s
212215
$this->assertMethodExists($class, $createMethod);
213216

214217
/** @var \Illuminate\Database\Eloquent\Model $child */
215-
$parent = factory($class)->create();
218+
$parent = Factory::factoryForModel($class)->create();
216219
$child = $parent->{$createMethod}(
217-
factory(get_class($hasManyRelation->getRelated()))
220+
Factory::factoryForModel(get_class($hasManyRelation->getRelated()))
218221
->make()
219222
->toArray()
220223
);
@@ -244,9 +247,10 @@ protected function assertEloquentHasCreateManyFor(string $class, string $relatio
244247
$childModel = $hasManyRelation->getRelated();
245248
$childKey = $childModel->getKeyName();
246249

247-
$parent = factory($class)->create();
250+
$parent = Factory::factoryForModel($class)->create();
248251
$children = $parent->{$createManyMethod}(
249-
factory(get_class($childModel), 3)
252+
Factory::factoryForModel(get_class($childModel))
253+
->count(3)
250254
->make()
251255
->toArray()
252256
);
@@ -273,8 +277,8 @@ protected function assertEloquentBelongsTo(string $class, string $relation)
273277
$parentKey = $parentModel->getKeyName();
274278
$childForeignKey = $belongsToRelation->getForeignKeyName();
275279

276-
$parent = factory(get_class($parentModel))->create();
277-
$child = factory($class)->create([$childForeignKey => $parent->{$parentKey}]);
280+
$parent = Factory::factoryForModel(get_class($parentModel))->create();
281+
$child = Factory::factoryForModel($class)->create([$childForeignKey => $parent->{$parentKey}]);
278282

279283
$this->assertEquals($parent->fresh()->toArray(), $child->{$relation}->toArray());
280284
}

src/Asserts/LogFileAsserts.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function seeLogFile(string $path)
3232
protected function dontSeeLogFile(string $path)
3333
{
3434
$message = "Failed asserting that log file `{$path}` not exists.";
35-
self::assertFileDoesNotExist($this->composeLogFilePath($path), $message);
35+
$this->assertFileDoesNotExist($this->composeLogFilePath($path), $message);
3636
}
3737

3838
/**
@@ -51,7 +51,7 @@ protected function seeInLogFile(string $path, $message)
5151
$content = File::get($this->composeLogFilePath($path));
5252
foreach ($messages as $item) {
5353
$pattern = $this->composeRegexPattern($item);
54-
self::assertMatchesRegularExpression($pattern, $content, "Failed asserting that file `{$path}` contains `{$item}`.");
54+
$this->assertMatchesRegularExpression($pattern, $content, "Failed asserting that file `{$path}` contains `{$item}`.");
5555
}
5656
}
5757

@@ -71,7 +71,7 @@ protected function dontSeeInLogFile(string $path, $message)
7171
$content = File::get($this->composeLogFilePath($path));
7272
foreach ($messages as $item) {
7373
$pattern = $this->composeRegexPattern($item);
74-
self::assertDoesNotMatchRegularExpression($pattern, $content, "Failed asserting that file `{$path}` not contains `{$item}`.");
74+
$this->assertDoesNotMatchRegularExpression($pattern, $content, "Failed asserting that file `{$path}` not contains `{$item}`.");
7575
}
7676
}
7777

tests/Asserts/DatabaseAssertsTest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ public function it_has_database_missing_table_assertion()
2222
/** @test */
2323
public function it_has_database_has_many_assertion()
2424
{
25-
factory(Post::class)->create(['title' => 'First Post']);
26-
factory(Post::class)->create(['title' => 'Second Post']);
27-
factory(Post::class)->create(['title' => 'Third Post']);
25+
Post::factory()->createMany([
26+
['title' => 'First Post'],
27+
['title' => 'Second Post'],
28+
['title' => 'Third Post'],
29+
]);
2830

2931
$this->assertDatabaseHasMany('posts', [
3032
['title' => 'First Post'],

tests/TestCase.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminated\Testing\Tests;
44

55
use Illuminate\Contracts\Console\Kernel as KernelContract;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
67
use Illuminated\Testing\TestingTools;
78
use Illuminated\Testing\Tests\App\Console\Kernel;
89
use Illuminated\Testing\Tests\App\Providers\FixtureServiceProvider;
@@ -69,7 +70,7 @@ protected function setUpDatabase()
6970
*/
7071
private function setUpFactories()
7172
{
72-
$this->withFactories(__DIR__ . '/fixture/database/factories');
73+
Factory::useNamespace('Illuminated\\Testing\\Tests\\Database\\Factories\\');
7374
}
7475

7576
/**

tests/fixture/app/Comment.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Illuminated\Testing\Tests\App;
44

5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
56
use Illuminate\Database\Eloquent\Model;
67

78
class Comment extends Model
89
{
10+
use HasFactory;
11+
912
/**
1013
* The table associated with the model.
1114
*

tests/fixture/app/Post.php

+2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Illuminated\Testing\Tests\App;
44

5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
56
use Illuminate\Database\Eloquent\Model;
67

78
class Post extends Model
89
{
910
use Commentable;
11+
use HasFactory;
1012

1113
/**
1214
* The table associated with the model.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
<?php
22

3+
namespace Illuminated\Testing\Tests\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
36
use Illuminated\Testing\Tests\App\Comment;
47

5-
/** @var \Illuminate\Database\Eloquent\Factory $factory */
6-
$factory->define(Comment::class, function (Faker\Generator $faker) {
7-
return [
8-
'body' => $faker->sentence,
9-
];
10-
});
8+
class CommentFactory extends Factory
9+
{
10+
/**
11+
* The name of the factory's corresponding model.
12+
*
13+
* @var string
14+
*/
15+
protected $model = Comment::class;
16+
17+
/**
18+
* Define the model's default state.
19+
*
20+
* @return array
21+
*/
22+
public function definition()
23+
{
24+
return [
25+
'body' => $this->faker->sentence,
26+
];
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
<?php
22

3+
namespace Illuminated\Testing\Tests\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
36
use Illuminated\Testing\Tests\App\Post;
47

5-
/** @var \Illuminate\Database\Eloquent\Factory $factory */
6-
$factory->define(Post::class, function (Faker\Generator $faker) {
7-
return [
8-
'title' => $faker->sentence,
9-
'publish_at' => $faker->dateTimeThisYear,
10-
];
11-
});
8+
class PostFactory extends Factory
9+
{
10+
/**
11+
* The name of the factory's corresponding model.
12+
*
13+
* @var string
14+
*/
15+
protected $model = Post::class;
16+
17+
/**
18+
* Define the model's default state.
19+
*
20+
* @return array
21+
*/
22+
public function definition()
23+
{
24+
return [
25+
'title' => $this->faker->sentence,
26+
'publish_at' => $this->faker->dateTimeThisYear,
27+
];
28+
}
29+
}

0 commit comments

Comments
 (0)