added attribute (-related) factories; added functional test

This commit is contained in:
MonaHartdegen 2019-12-13 14:59:45 +01:00
parent 2b0385c14e
commit b5f0c1db79
4 changed files with 283 additions and 0 deletions

View File

@ -0,0 +1,84 @@
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use Faker\Generator as Faker;
use Webkul\Attribute\Models\Attribute;
use Webkul\Core\Models\Locale;
$factory->define(Attribute::class, function (Faker $faker, array $attributes) {
$types = [
'text',
'textarea',
'price',
'boolean',
'select',
'multiselect',
'datetime',
'date',
'image',
'file',
'checkbox',
];
$locales = Locale::pluck('code')->all();
// array $attributes does not contain any locale code
if (count(array_diff_key(array_flip($locales), $attributes) ) === count($locales)) {
$localeCode = $locales[0];
$attributes[$localeCode] = [
'name' => $faker->word,
];
}
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',
]);
$factory->state(Attribute::class, 'validation_email', [
'validation' => 'email',
]);
$factory->state(Attribute::class, 'validation_decimal', [
'validation' => 'decimal',
]);
$factory->state(Attribute::class, 'validation_url', [
'validation' => 'url',
]);
$factory->state(Attribute::class, 'required', [
'is_required' => true,
]);
$factory->state(Attribute::class, 'unique', [
'is_unique' => true,
]);
$factory->state(Attribute::class, 'filterable', [
'is_filterable' => true,
]);
$factory->state(Attribute::class, 'configurable', [
'is_configurable' => true,
]);

View File

@ -0,0 +1,83 @@
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use Faker\Generator as Faker;
use Webkul\Attribute\Models\Attribute;
use Webkul\Attribute\Models\AttributeOption;
use Webkul\Core\Models\Locale;
$factory->define(AttributeOption::class, function (Faker $faker, array $attributes) {
$locales = Locale::pluck('code')->all();
// array $attributes does not contain any locale code
if (count(array_diff_key(array_flip($locales), $attributes) ) === count($locales)) {
$localeCode = $locales[0];
$attributes[$localeCode] = [
'label' => $faker->word,
];
}
return [
'admin_name' => $faker->word,
'sort_order' => $faker->randomDigit,
'attribute_id' => function () {
return factory(Attribute::class)->create()->id;
},
'swatch_value' => null,
];
});
$factory->defineAs(AttributeOption::class, 'swatch_color', function (Faker $faker, array $attributes) {
return [
'admin_name' => $faker->word,
'sort_order' => $faker->randomDigit,
'attribute_id' => function () {
return factory(Attribute::class)
->create(['swatch_type' => 'color'])
->id;
},
'swatch_value' => $faker->hexColor,
];
});
$factory->defineAs(AttributeOption::class, 'swatch_image', function (Faker $faker, array $attributes) {
return [
'admin_name' => $faker->word,
'sort_order' => $faker->randomDigit,
'attribute_id' => function () {
return factory(Attribute::class)
->create(['swatch_type' => 'image'])
->id;
},
'swatch_value' => '/tests/_data/ProductImageExampleForUpload.jpg',
];
});
$factory->defineAs(AttributeOption::class, 'swatch_dropdown', function (Faker $faker, array $attributes) {
return [
'admin_name' => $faker->word,
'sort_order' => $faker->randomDigit,
'attribute_id' => function () {
return factory(Attribute::class)
->create(['swatch_type' => 'dropdown'])
->id;
},
'swatch_value' => null,
];
});
$factory->defineAs(AttributeOption::class, 'swatch_text', function (Faker $faker, array $attributes) {
return [
'admin_name' => $faker->word,
'sort_order' => $faker->randomDigit,
'attribute_id' => function () {
return factory(Attribute::class)
->create(['swatch_type' => 'text'])
->id;
},
'swatch_value' => null,
];
});

View File

@ -2,6 +2,7 @@
namespace Webkul\Attribute\Providers;
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
use Illuminate\Support\ServiceProvider;
class AttributeServiceProvider extends ServiceProvider
@ -14,6 +15,8 @@ class AttributeServiceProvider extends ServiceProvider
public function boot()
{
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
$this->app->make(EloquentFactory::class)->load(__DIR__ . '/../Database/Factories');
}
/**

View File

@ -0,0 +1,113 @@
<?php
namespace Tests\Functional\Product;
use FunctionalTester;
use Faker\Factory;
use Webkul\Attribute\Models\Attribute;
use Webkul\Attribute\Models\AttributeFamily;
use Webkul\Attribute\Models\AttributeOption;
use Webkul\Core\Models\Locale;
use Webkul\Product\Models\Product;
use Webkul\Product\Models\ProductAttributeValue;
class ProductCest
{
/** @var Factory $faker */
private $faker;
/** @var Attribute $attributeBrand */
private $attributeBrand;
/** @var AttributeOption $attributeBrandDefaultOption */
private $attributeBrandDefaultOption;
/** @var AttributeOption $attributeBrandOption */
private $attributeBrandOption;
public function _before(FunctionalTester $I)
{
$this->faker = Factory::create();
$this->attributeBrand = $I->grabRecord(Attribute::class, [
'code' => 'brand',
'admin_name' => 'Brand',
]);
$locales = Locale::pluck('code')->all();
$defaultAttributeOptionAttributes = [
'attribute_id' => $this->attributeBrand->id,
'admin_name' => 'no-brand',
'sort_order' => 0,
];
foreach ($locales as $locale) {
$defaultAttributeOptionAttributes[$locale] = [
'label' => '',
];
}
$this->attributeBrandDefaultOption = $I->have(AttributeOption::class,
$defaultAttributeOptionAttributes);
$this->attributeBrandOption = $I->have(AttributeOption::class, [
'attribute_id' => $this->attributeBrand->id,
]);
}
public function selectEmptyAttributeOptionOnProductCreation(FunctionalTester $I)
{
$I->loginAsAdmin();
$I->amOnAdminRoute('admin.catalog.products.create');
$I->see(__('admin::app.catalog.products.add-title'), 'h1');
$I->selectOption('select#type', 'simple');
$attributeFamily = $I->grabRecord(AttributeFamily::class, [
'code' => 'default',
]);
$I->selectOption('select#attribute_family_id', $attributeFamily->id);
$sku = $this->faker->randomNumber(3);
$I->fillField('sku', $sku);
$I->click(__('admin::app.catalog.products.save-btn-title'));
$I->seeInSource('Product created successfully.');
$I->seeCurrentRouteIs('admin.catalog.products.edit');
$productTitle = $this->faker->word;
$productUrlKey = $this->faker->slug;
$I->fillField('name', $productTitle);
$I->fillField('url_key', $productUrlKey);
$I->selectOption($this->attributeBrand->code,
$this->attributeBrandDefaultOption->id);
$I->fillField('price', $this->faker->randomFloat(2));
$I->fillField('weight', $this->faker->randomDigit);
$I->fillField('#short_description', $this->faker->paragraph(1, true));
$I->fillField('#description', $this->faker->paragraph(5, true));
$I->click(__('admin::app.catalog.products.save-btn-title'));
$I->seeInSource('Product updated successfully.');
$I->seeCurrentRouteIs('admin.catalog.products.index');
$product = $I->grabRecord(Product::class, [
'sku' => $sku,
'type' => 'simple',
'attribute_family_id' => $attributeFamily->id,
]);
$I->seeRecord(ProductAttributeValue::class, [
'product_id' => $product->id,
'attribute_id' => $this->attributeBrand->id,
'integer_value' => $this->attributeBrandDefaultOption->id,
'text_value' => null,
]);
}
}