Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Refactoring to Laravel 8 Class Model Factories. #5190

Merged
merged 9 commits into from Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Expand Up @@ -29,7 +29,6 @@
"khaled.alshamaa/ar-php": "^6.0.0",
"konekt/concord": "^1.2",
"laravel/framework": "^8.0",
"laravel/legacy-factories": "^1.1",
"laravel/scout": "^8.0",
"laravel/socialite": "^5.0",
"laravel/tinker": "^2.0",
Expand All @@ -43,7 +42,7 @@
"codeception/codeception": "^4.1",
"codeception/module-asserts": "^1.1",
"codeception/module-filesystem": "^1.0",
"codeception/module-laravel5": "^1.0",
"codeception/module-laravel": "^2.0",
"codeception/module-webdriver": "^1.0",
"filp/whoops": "^2.0",
"mockery/mockery": "^1.3.1",
Expand Down
1,637 changes: 871 additions & 766 deletions composer.lock

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions database/factories/UserFactory.php
@@ -1,6 +1,6 @@
<?php

use Faker\Generator as Faker;
namespace Database\Factories;

/*
|--------------------------------------------------------------------------
Expand All @@ -13,11 +13,12 @@
|
*/

$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
];
});
use Illuminate\Database\Eloquent\Factories\Factory;

devansh-webkul marked this conversation as resolved.
Show resolved Hide resolved
class UserFactory extends Factory
{
public function definition(): array
{
return [];
devansh-webkul marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion packages/Webkul/API/Http/Resources/Catalog/Attribute.php
Expand Up @@ -12,7 +12,7 @@ class Attribute extends JsonResource
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
public function toArray($request): array
{
return [
'id' => $this->id,
Expand Down
Expand Up @@ -12,13 +12,13 @@ class AttributeOption extends JsonResource
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
public function toArray($request): array
{
return [
'id' => $this->id,
'admin_name' => $this->admin_name,
'label' => $this->label,
'swatch_value' => $this->swatch_value
'swatch_value' => $this->swatch_value,
];
}
}
190 changes: 124 additions & 66 deletions packages/Webkul/Attribute/src/Database/Factories/AttributeFactory.php
@@ -1,84 +1,142 @@
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
namespace Webkul\Attribute\Database\Factories;

use Faker\Generator as Faker;
use Webkul\Attribute\Models\Attribute;
use Webkul\Core\Models\Locale;
use Illuminate\Database\Eloquent\Factories\Factory;

$factory->define(Attribute::class, function (Faker $faker, array $attributes) {
$types = [
'text',
'textarea',
'price',
'boolean',
'select',
'multiselect',
'datetime',
'date',
'image',
'file',
'checkbox',
];
class AttributeFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Attribute::class;

$locales = Locale::pluck('code')->all();
/**
* @var array
*/
protected $states = [
'validation_numeric',
'validation_email',
'validation_decimal',
'validation_url',
'required',
'unique',
'filterable',
'configurable',
];

// array $attributes does not contain any locale code
if (count(array_diff_key(array_flip($locales), $attributes) ) === count($locales)) {
$localeCode = $locales[0];
/**
* Define the model's default state.
*
* @return array
*/
public function definition(): array
{
$types = [
'text',
'textarea',
'price',
'boolean',
'select',
'multiselect',
'datetime',
'date',
'image',
'file',
'checkbox',
];

$attributes[$localeCode] = [
'name' => $faker->word,
return [
'admin_name' => $this->faker->word,
'code' => $this->faker->word,
'type' => array_rand($types),
'validation' => '',
'position' => $this->faker->randomDigit,
'is_required' => false,
'is_unique' => false,
'value_per_locale' => false,
'value_per_channel' => false,
'is_filterable' => false,
'is_configurable' => false,
'is_user_defined' => true,
'is_visible_on_front' => true,
'swatch_type' => null,
'use_in_flat' => true,
];
}

return [
'admin_name' => $faker->word,
'code' => $faker->word,
'type' => array_rand($types),
'validation' => '',
'position' => $faker->randomDigit,
'is_required' => false,
'is_unique' => false,
'value_per_locale' => false,
'value_per_channel' => false,
'is_filterable' => false,
'is_configurable' => false,
'is_user_defined' => true,
'is_visible_on_front' => true,
'swatch_type' => null,
'use_in_flat' => true,
];
});

$factory->state(Attribute::class, 'validation_numeric', [
'validation' => 'numeric',
]);
public function validation_numeric(): AttributeFactory
{
return $this->state(function (array $attributes) {
return [
'validation' => 'numeric',
];
});
}

$factory->state(Attribute::class, 'validation_email', [
'validation' => 'email',
]);
public function validation_email(): AttributeFactory
{
return $this->state(function (array $attributes) {
return [
'validation' => 'email',
];
});
}

$factory->state(Attribute::class, 'validation_decimal', [
'validation' => 'decimal',
]);
public function validation_decimal(): AttributeFactory
{
return $this->state(function (array $attributes) {
return [
'validation' => 'decimal',
];
});
}

$factory->state(Attribute::class, 'validation_url', [
'validation' => 'url',
]);
public function validation_url(): AttributeFactory
{
return $this->state(function (array $attributes) {
return [
'validation' => 'url',
];
});
}

$factory->state(Attribute::class, 'required', [
'is_required' => true,
]);
public function required(): AttributeFactory
{
return $this->state(function (array $attributes) {
return [
'is_required' => true,
];
});
}

$factory->state(Attribute::class, 'unique', [
'is_unique' => true,
]);
public function unique(): AttributeFactory
{
return $this->state(function (array $attributes) {
return [
'is_unique' => true,
];
});
}

$factory->state(Attribute::class, 'filterable', [
'is_filterable' => true,
]);
public function filterable(): AttributeFactory
{
return $this->state(function (array $attributes) {
return [
'is_filterable' => true,
];
});
}

$factory->state(Attribute::class, 'configurable', [
'is_configurable' => true,
]);
public function configurable(): AttributeFactory
{
return $this->state(function (array $attributes) {
return [
'is_configurable' => true,
];
});
}
}
@@ -1,15 +1,32 @@
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
namespace Webkul\Attribute\Database\Factories;

use Faker\Generator as Faker;
use Webkul\Attribute\Models\AttributeFamily;
use Illuminate\Database\Eloquent\Factories\Factory;

$factory->define(AttributeFamily::class, function (Faker $faker) {
return [
'name' => $faker->word(),
'code' => $faker->word(),
'is_user_defined' => rand(0, 1),
'status' => 0,
];
});
class AttributeFamilyFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = AttributeFamily::class;

/**
* Define the model's default state.
*
* @return array
* @throws \Exception
*/
public function definition(): array
{
return [
'name' => $this->faker->word(),
'code' => $this->faker->word(),
'is_user_defined' => random_int(0, 1),
'status' => 0,
];
}
}