Skip to content

Commit 30ea52e

Browse files
committed
Testing for lara 6 / 7 model factories
1 parent eced08a commit 30ea52e

File tree

7 files changed

+44
-110
lines changed

7 files changed

+44
-110
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"require-dev": {
2222
"doctrine/dbal": "~2.3",
23+
"laravel/legacy-factories": "^1.0.4",
2324
"mockery/mockery": "^1.0",
2425
"orchestra/testbench": "^4.0|^5.0|^6.0",
2526
"phpunit/phpunit": "^8.0|^9.0"

tests/Factories/ProjectFactory.php

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

tests/Factories/TaskFactory.php

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

tests/Factories/UserFactory.php

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
11
<?php
22

3-
namespace Phpsa\LaravelApiController\Tests\Factories;
4-
53
use Illuminate\Support\Str;
64
use Phpsa\LaravelApiController\Tests\Models\User;
7-
use Illuminate\Database\Eloquent\Factories\Factory;
8-
9-
class UserFactory extends Factory
10-
{
11-
/**
12-
* The name of the factory's corresponding model.
13-
*
14-
* @var string
15-
*/
16-
protected $model = User::class;
17-
18-
/**
19-
* Define the model's default state.
20-
*
21-
* @return array
22-
*/
23-
public function definition()
24-
{
25-
return [
26-
'name' => $this->faker->name,
27-
'email' => $this->faker->unique()->email,
28-
'email_verified_at' => now(),
29-
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
30-
'remember_token' => Str::random(10),
31-
];
32-
}
33-
}
5+
use Faker\Generator as Faker;
6+
7+
$factory->define(User::class, function (Faker $faker) {
8+
return [
9+
'name' => $faker->name,
10+
'email' => $faker->unique()->email,
11+
'email_verified_at' => now(),
12+
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
13+
'remember_token' => Str::random(10),
14+
];
15+
});

tests/Models/User.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
namespace Phpsa\LaravelApiController\Tests\Models;
44

55
use Illuminate\Database\Eloquent\Builder;
6-
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
//use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
99
use Phpsa\LaravelApiController\Tests\Factories\UserFactory as FactoriesUserFactory;
1010

1111
class User extends Authenticatable
1212
{
1313

14-
use HasFactory, Notifiable;
14+
use Notifiable;
1515

1616
protected $table = 'users';
1717

@@ -46,12 +46,6 @@ class User extends Authenticatable
4646
];
4747

4848

49-
protected static function newFactory()
50-
{
51-
return FactoriesUserFactory::new();
52-
}
53-
54-
5549
public function scopeHas2Fa(Builder $builder, ?bool $on = null)
5650
{
5751
switch ($on) {

tests/TestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44

55
use ReflectionClass;
66
use Illuminate\Http\Request;
7+
use Orchestra\Testbench\Concerns\WithFactories;
78
use Orchestra\Testbench\TestCase as BaseTestCase;
9+
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
810

911
class TestCase extends BaseTestCase
1012
{
1113

14+
use WithFactories;
15+
1216
protected function setUp(): void
1317
{
1418
parent::setUp();
1519
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
20+
$this->withFactories(__DIR__.'/Factories');
1621
}
1722

1823
/**

tests/UserControllerTest.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Phpsa\LaravelApiController\Tests;
44

5-
use Illuminate\Database\Eloquent\Builder;
65
use Mockery;
76
use function PHPUnit\Framework\assertEquals;
87

@@ -14,6 +13,14 @@
1413
class UserControllerTest extends TestCase
1514
{
1615

16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
factory(User::class, 100)->create();
20+
factory(User::class, 1)->create([
21+
'email' => '[email protected]'
22+
]);
23+
}
1724

1825
/**
1926
* Define routes setup.
@@ -29,8 +36,9 @@ protected function defineRoutes($router)
2936

3037
public function test_user_policy_all_approved()
3138
{
32-
User::factory(100)->create();
33-
assertEquals(100, User::count());
39+
// factory(User::class, 100)->create();
40+
41+
assertEquals(101, User::count());
3442

3543
$policy = Mockery::mock(UserPolicy::class)->makePartial();
3644
app()->instance(UserPolicy::class, $policy);
@@ -45,13 +53,12 @@ public function test_user_policy_all_approved()
4553
$json = $response->decodeResponseJson();
4654

4755
$this->assertArrayHasKey('meta', $json);
48-
$this->assertEquals(100, $json['meta']['total']);
56+
$this->assertEquals(101, $json['meta']['total']);
4957
}
5058

5159
public function test_user_policy_not_allowed()
5260
{
53-
User::factory(10)->create();
54-
assertEquals(10, User::count());
61+
//factory(User::class, 100)->create();
5562

5663
$policy = Mockery::mock(UserPolicy::class)->makePartial();
5764
app()->instance(UserPolicy::class, $policy);
@@ -66,10 +73,10 @@ public function test_user_policy_not_allowed()
6673

6774
public function test_filtering()
6875
{
69-
User::factory(100)->create();
70-
User::factory()->create([
71-
'email' => '[email protected]'
72-
]);
76+
// factory(User::class, 100)->create();
77+
// factory(User::class, 100)->create([
78+
// 'email' => '[email protected]'
79+
// ]);
7380

7481
assertEquals(1, User::where('email', '[email protected]')->count());
7582
$this->actingAs(User::first());
@@ -98,10 +105,14 @@ public function test_calls_scopes()
98105
{
99106

100107
//scopeHas2Fa
101-
User::factory(100)->create();
102-
User::factory()->create([
103-
'email' => '[email protected]'
104-
]);
108+
// factory(User::class, 100)->create();
109+
// factory(User::class, 100)->create([
110+
// 'email' => '[email protected]'
111+
// ]);
112+
//User::factory(100)->create();
113+
// User::factory()->create([
114+
// 'email' => '[email protected]'
115+
// ]);
105116

106117
assertEquals(1, User::where('email', '[email protected]')->count());
107118

0 commit comments

Comments
 (0)