Skip to content

Commit f87ed06

Browse files
committed
- wip: Created test models and factories
1 parent c1fb2ce commit f87ed06

18 files changed

Lines changed: 559 additions & 25 deletions
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Javaabu\Imports\Tests\TestSupport\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Support\Str;
7+
use Javaabu\Imports\Tests\TestSupport\Models\Brand;
8+
9+
class BrandFactory extends Factory
10+
{
11+
/**
12+
* The name of the factory's corresponding model.
13+
*
14+
* @var string
15+
*/
16+
protected $model = Brand::class;
17+
18+
/**
19+
* Define the model's default state.
20+
*
21+
* @return array<string, mixed>
22+
*/
23+
public function definition(): array
24+
{
25+
$name = $this->faker->company();
26+
27+
return [
28+
'name' => $name,
29+
'slug' => Str::slug($name),
30+
];
31+
}
32+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Javaabu\Imports\Tests\TestSupport\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Support\Str;
7+
use Javaabu\Imports\Tests\TestSupport\Models\Category;
8+
use Javaabu\Imports\Tests\TestSupport\Models\CategoryType;
9+
10+
class CategoryFactory extends Factory
11+
{
12+
/**
13+
* The name of the factory's corresponding model.
14+
*
15+
* @var string
16+
*/
17+
protected $model = Category::class;
18+
19+
/**
20+
* Define the model's default state.
21+
*
22+
* @return array<string, mixed>
23+
*/
24+
public function definition(): array
25+
{
26+
$name = $this->faker->word();
27+
28+
return [
29+
'name' => $name,
30+
'slug' => Str::slug($name),
31+
'category_type_id' => CategoryType::factory(),
32+
'icon' => null,
33+
];
34+
}
35+
36+
/**
37+
* Indicate that the category has an icon.
38+
*
39+
* @return Factory
40+
*/
41+
public function withIcon(): Factory
42+
{
43+
return $this->state(function (array $attributes) {
44+
return [
45+
'icon' => 'fa-' . $this->faker->word(),
46+
];
47+
});
48+
}
49+
50+
/**
51+
* Indicate that the category belongs to a specific category type.
52+
*
53+
* @param CategoryType|int $categoryType
54+
* @return Factory
55+
*/
56+
public function forCategoryType($categoryType): Factory
57+
{
58+
return $this->state(function (array $attributes) use ($categoryType) {
59+
return [
60+
'category_type_id' => $categoryType instanceof CategoryType ? $categoryType->id : $categoryType,
61+
];
62+
});
63+
}
64+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Javaabu\Imports\Tests\TestSupport\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Support\Str;
7+
use Javaabu\Imports\Tests\TestSupport\Models\CategoryType;
8+
9+
class CategoryTypeFactory extends Factory
10+
{
11+
/**
12+
* The name of the factory's corresponding model.
13+
*
14+
* @var string
15+
*/
16+
protected $model = CategoryType::class;
17+
18+
/**
19+
* Define the model's default state.
20+
*
21+
* @return array<string, mixed>
22+
*/
23+
public function definition(): array
24+
{
25+
$name = $this->faker->word();
26+
27+
return [
28+
'name' => $name,
29+
'slug' => Str::slug($name),
30+
'has_icon' => $this->faker->boolean(),
31+
];
32+
}
33+
34+
/**
35+
* Indicate that the category type has icons.
36+
*
37+
* @return Factory
38+
*/
39+
public function withIcons(): Factory
40+
{
41+
return $this->state(function (array $attributes) {
42+
return [
43+
'has_icon' => true,
44+
];
45+
});
46+
}
47+
48+
/**
49+
* Indicate that the category type doesn't have icons.
50+
*
51+
* @return Factory
52+
*/
53+
public function withoutIcons(): Factory
54+
{
55+
return $this->state(function (array $attributes) {
56+
return [
57+
'has_icon' => false,
58+
];
59+
});
60+
}
61+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Javaabu\Imports\Tests\TestSupport\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Support\Str;
7+
use Javaabu\Imports\Tests\TestSupport\Models\Brand;
8+
use Javaabu\Imports\Tests\TestSupport\Models\Product;
9+
10+
class ProductFactory extends Factory
11+
{
12+
/**
13+
* The name of the factory's corresponding model.
14+
*
15+
* @var string
16+
*/
17+
protected $model = Product::class;
18+
19+
/**
20+
* Define the model's default state.
21+
*
22+
* @return array<string, mixed>
23+
*/
24+
public function definition(): array
25+
{
26+
$name = $this->faker->productName();
27+
28+
return [
29+
'name' => $name,
30+
'slug' => Str::slug($name),
31+
'brand_id' => Brand::factory(),
32+
];
33+
}
34+
35+
/**
36+
* Indicate that the product has no brand.
37+
*
38+
* @return Factory
39+
*/
40+
public function withoutBrand(): Factory
41+
{
42+
return $this->state(function (array $attributes) {
43+
return [
44+
'brand_id' => null,
45+
];
46+
});
47+
}
48+
}

tests/TestSupport/Models/Brand.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Javaabu\Imports\Tests\TestSupport\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
use Javaabu\Imports\Tests\TestSupport\Factories\BrandFactory;
9+
10+
class Brand extends Model
11+
{
12+
use HasFactory;
13+
14+
/**
15+
* The attributes that are mass assignable.
16+
*
17+
* @var array
18+
*/
19+
protected $fillable = [
20+
'slug',
21+
'name',
22+
];
23+
24+
/**
25+
* Create a new factory instance for the model.
26+
*
27+
* @return \Illuminate\Database\Eloquent\Factories\Factory
28+
*/
29+
protected static function newFactory()
30+
{
31+
return new BrandFactory();
32+
}
33+
34+
/**
35+
* Get the products for this brand.
36+
*/
37+
public function products(): HasMany
38+
{
39+
return $this->hasMany(Product::class);
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Javaabu\Imports\Tests\TestSupport\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Javaabu\Imports\Tests\TestSupport\Factories\CategoryFactory;
9+
10+
class Category extends Model
11+
{
12+
use HasFactory;
13+
14+
/**
15+
* The attributes that are mass assignable.
16+
*
17+
* @var array
18+
*/
19+
protected $fillable = [
20+
'slug',
21+
'name',
22+
'category_type_id',
23+
'icon',
24+
];
25+
26+
/**
27+
* Create a new factory instance for the model.
28+
*
29+
* @return \Illuminate\Database\Eloquent\Factories\Factory
30+
*/
31+
protected static function newFactory()
32+
{
33+
return new CategoryFactory();
34+
}
35+
36+
/**
37+
* Get the category type that owns the category.
38+
*/
39+
public function categoryType(): BelongsTo
40+
{
41+
return $this->belongsTo(CategoryType::class);
42+
}
43+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Javaabu\Imports\Tests\TestSupport\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
use Javaabu\Imports\Tests\TestSupport\Factories\CategoryTypeFactory;
9+
10+
class CategoryType extends Model
11+
{
12+
use HasFactory;
13+
14+
/**
15+
* The attributes that are mass assignable.
16+
*
17+
* @var array
18+
*/
19+
protected $fillable = [
20+
'slug',
21+
'name',
22+
'has_icon',
23+
];
24+
25+
/**
26+
* The attributes that should be cast.
27+
*
28+
* @var array
29+
*/
30+
protected $casts = [
31+
'has_icon' => 'boolean',
32+
];
33+
34+
/**
35+
* Create a new factory instance for the model.
36+
*
37+
* @return \Illuminate\Database\Eloquent\Factories\Factory
38+
*/
39+
protected static function newFactory()
40+
{
41+
return new CategoryTypeFactory();
42+
}
43+
44+
/**
45+
* Get the categories for this category type.
46+
*/
47+
public function categories(): HasMany
48+
{
49+
return $this->hasMany(Category::class);
50+
}
51+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Javaabu\Imports\Tests\TestSupport\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Javaabu\Imports\Tests\TestSupport\Factories\ProductFactory;
9+
10+
class Product extends Model
11+
{
12+
use HasFactory;
13+
14+
/**
15+
* The attributes that are mass assignable.
16+
*
17+
* @var array
18+
*/
19+
protected $fillable = [
20+
'slug',
21+
'name',
22+
'brand_id',
23+
];
24+
25+
/**
26+
* Create a new factory instance for the model.
27+
*
28+
* @return \Illuminate\Database\Eloquent\Factories\Factory
29+
*/
30+
protected static function newFactory()
31+
{
32+
return new ProductFactory();
33+
}
34+
35+
/**
36+
* Get the brand that owns the product.
37+
*/
38+
public function brand(): BelongsTo
39+
{
40+
return $this->belongsTo(Brand::class);
41+
}
42+
}

0 commit comments

Comments
 (0)