added ProductsIndexCest; added factories

This commit is contained in:
MonaHartdegen 2020-01-23 08:33:47 +01:00
parent 4381d54029
commit 806e3f93db
10 changed files with 553 additions and 2 deletions

View File

@ -0,0 +1,74 @@
<?php
use Faker\Generator as Faker;
use Webkul\Core\Models\Channel;
use Webkul\Core\Models\Currency;
use Webkul\Inventory\Models\InventorySource;
use Webkul\Core\Models\Locale;
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(Channel::class, function (Faker $faker, array $attributes) {
$seoTitle = $attributes['seo_title'] ?? $faker->word;
$seoDescription = $attributes['seo_description'] ?? $faker->words(10, true);
$seoKeywords = $attributes['seo_keywords'] ?? $faker->words(3, true);
$seoData = [
'meta_title' => $seoTitle,
'meta_description' => $seoDescription,
'meta_keywords' => $seoKeywords,
];
unset($attributes['seo_title'], $attributes['seo_description'], $attributes['seo_keywords']);
/*if (! isset($attributes['locales'])) {
$attributes['default_locale_id'] = $attributes['default_locale_id'] ?? function () {
return factory(Locale::class)->create()->id;
};
}
unset($attributes['locales']);
if (! isset($attributes['inventory_sources'])) {
$attributes['inventory_sources'] = $attributes['inventory_sources'] ?? function () {
return factory(InventorySource::class)->create()->id;
};
}
unset($attributes['inventory_sources']);
if (! isset($attributes['currencies'])) {
$attributes['base_currency_id'] = $attributes['base_currency_id'] ?? function () {
return factory(Currency::class)->create()->id;
};
}
unset($attributes['currencies']);*/
return [
'code' => $faker->unique()->word,
'name' => $faker->word,
'default_locale_id' => function () {
return factory(Locale::class)->create()->id;
},
'base_currency_id' => function () {
return factory(Currency::class)->create()->id;
},
'root_category_id' => function() {
return factory(\Webkul\Category\Models\Category::class)->create()->id;
},
'home_seo' => json_encode($seoData),
];
});
/*$factory->afterCreating(Channel::class, function (Channel $channel, Faker $faker) {
$channel->locales()->sync($data['locales']);
}, 'channel_sync_locales');
$factory->afterCreating(Channel::class, function (Channel $channel, Faker $faker) {
$channel->currencies()->sync($data['currencies']);
});
$factory->afterCreating(Channel::class, function (Channel $channel, Faker $faker) {
$channel->inventory_sources()->sync($data['inventory_sources']);
});*/

View File

@ -0,0 +1,14 @@
<?php
use Faker\Generator as Faker;
use Webkul\Core\Models\Currency;
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(Currency::class, function (Faker $faker, array $attributes) {
return [
'code' => $faker->unique()->currencyCode,
'name' => $faker->word,
];
});

View File

@ -1,10 +1,9 @@
<?php <?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use Faker\Generator as Faker; use Faker\Generator as Faker;
use Webkul\Core\Models\Locale; use Webkul\Core\Models\Locale;
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(Locale::class, function (Faker $faker, array $attributes) { $factory->define(Locale::class, function (Faker $faker, array $attributes) {
return [ return [

View File

@ -0,0 +1,25 @@
<?php
use Faker\Generator as Faker;
use Webkul\Inventory\Models\InventorySource;
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(InventorySource::class, function (Faker $faker) {
$code = $faker->unique()->word;
return [
'code' => $code,
'name' => $code,
'description' => $faker->sentence,
'contact_name' => $faker->name,
'contact_email' => $faker->safeEmail,
'contact_number' => $faker->phoneNumber,
'country' => $faker->countryCode,
'state' => $faker->state,
'city' => $faker->city,
'street' => $faker->streetAddress,
'postcode' => $faker->postcode,
'priority' => 0,
'status' => 1,
];
});

View File

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

View File

@ -0,0 +1,276 @@
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use Faker\Generator as Faker;
use Webkul\Product\Models\Product;
use Webkul\Product\Models\ProductAttributeValue;
use Webkul\Attribute\Models\AttributeOption;
$factory->defineAs(ProductAttributeValue::class, 'sku', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'text_value' => $faker->uuid,
'attribute_id' => 1,
];
});
$factory->defineAs(ProductAttributeValue::class, 'name', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'locale' => 'en', //$faker->languageCode,
'channel' => 'default',
'text_value' => $faker->words(2, true),
'attribute_id' => 2,
];
});
$factory->defineAs(ProductAttributeValue::class, 'url_key', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'text_value' => $faker->unique()->slug,
'attribute_id' => 3,
];
});
$factory->defineAs(ProductAttributeValue::class, 'tax_category_id', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'channel' => 'default',
'integer_value' => null, // ToDo
'attribute_id' => 4,
];
});
$factory->defineAs(ProductAttributeValue::class, 'new', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'boolean_value' => 1,
'attribute_id' => 5,
];
});
$factory->defineAs(ProductAttributeValue::class, 'featured', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'boolean_value' => 1,
'attribute_id' => 6,
];
});
$factory->defineAs(ProductAttributeValue::class, 'visible_individually', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'boolean_value' => 1,
'attribute_id' => 7,
];
});
$factory->defineAs(ProductAttributeValue::class, 'status', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'boolean_value' => 1,
'attribute_id' => 8,
];
});
$factory->defineAs(ProductAttributeValue::class, 'short_description', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'locale' => 'en', //$faker->languageCode,
'channel' => 'default',
'text_value' => $faker->sentence,
'attribute_id' => 9,
];
});
$factory->defineAs(ProductAttributeValue::class, 'description', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'locale' => 'en', //$faker->languageCode,
'channel' => 'default',
'text_value' => $faker->sentences(3, true),
'attribute_id' => 10,
];
});
$factory->defineAs(ProductAttributeValue::class, 'price', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'float_value' => $faker->randomFloat(4, 0, 1000),
'attribute_id' => 11,
];
});
$factory->defineAs(ProductAttributeValue::class, 'cost', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'channel' => 'default',
'float_value' => $faker->randomFloat(4, 0, 10),
'attribute_id' => 12,
];
});
$factory->defineAs(ProductAttributeValue::class, 'special_price', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'float_value' => $faker->randomFloat(4, 0, 100),
'attribute_id' => 13,
];
});
$factory->defineAs(ProductAttributeValue::class, 'special_price_from', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'channel' => 'default',
'date_value' => $faker->dateTimeBetween('-5 days', 'now', 'Europe/Berlin'),
'attribute_id' => 14,
];
});
$factory->defineAs(ProductAttributeValue::class, 'special_price_to', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'channel' => 'default',
'date_value' => $faker->dateTimeBetween('now', '+ 5 days', 'Europe/Berlin'),
'attribute_id' => 15,
];
});
$factory->defineAs(ProductAttributeValue::class, 'meta_title', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'locale' => 'en', //$faker->languageCode,
'channel' => 'default',
'text_value' => $faker->words(2, true),
'attribute_id' => 16,
];
});
$factory->defineAs(ProductAttributeValue::class, 'meta_keywords', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'locale' => 'en', //$faker->languageCode,
'channel' => 'default',
'text_value' => $faker->words(5, true),
'attribute_id' => 17,
];
});
$factory->defineAs(ProductAttributeValue::class, 'meta_description', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'locale' => 'en', //$faker->languageCode,
'channel' => 'default',
'text_value' => $faker->sentence,
'attribute_id' => 18,
];
});
$factory->defineAs(ProductAttributeValue::class, 'width', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'integer_value' => $faker->numberBetween(1, 50),
'attribute_id' => 19,
];
});
$factory->defineAs(ProductAttributeValue::class, 'height', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'integer_value' => $faker->numberBetween(1, 50),
'attribute_id' => 20,
];
});
$factory->defineAs(ProductAttributeValue::class, 'depth', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'integer_value' => $faker->numberBetween(1, 50),
'attribute_id' => 21,
];
});
$factory->defineAs(ProductAttributeValue::class, 'weight', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'integer_value' => $faker->numberBetween(1, 50),
'attribute_id' => 22,
];
});
$factory->defineAs(ProductAttributeValue::class, 'color', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'integer_value' => $faker->numberBetween(1, 5),
'attribute_id' => 23,
];
});
$factory->defineAs(ProductAttributeValue::class, 'size', function ( Faker $faker) {
return [
'product_id' => function() {
return factory(Product::class)->create()->id;
},
'integer_value' => $faker->numberBetween(1, 5),
'attribute_id' => 24,
];
});
$factory->defineAs(ProductAttributeValue::class, 'brand', function ( Faker $faker) {
return [
'product_id' => function () {
return factory(Product::class)->create()->id;
},
'attribute_id' => 25,
'integer_value' => function () {
return factory(AttributeOption::class)->create()->id;
}
];
});

View File

@ -0,0 +1,17 @@
<?php
use Faker\Generator as Faker;
use Webkul\Product\Models\Product;
use Webkul\Attribute\Models\AttributeFamily;
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(Product::class, function (Faker $faker, array $attributes) {
$attributeFamilyId = AttributeFamily::pluck('id')->first();
return [
'sku' => $faker->uuid,
'type' => 'simple',
'attribute_family_id' => $attributeFamilyId,
];
});

View File

@ -0,0 +1,19 @@
<?php
use Faker\Generator as Faker;
use Webkul\Inventory\Models\InventorySource;
use Webkul\Product\Models\Product;
use Webkul\Product\Models\ProductInventory;
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(ProductInventory::class, function (Faker $faker) {
return [
'qty' => $faker->numberBetween(1, 20),
'product_id' => function () {
return factory(Product::class)->create()->id;
},
'inventory_source_id' => function () {
return factory(InventorySource::class)->create()->id;
},
];
});

View File

@ -6,6 +6,7 @@ use Illuminate\Support\ServiceProvider;
use Webkul\Product\Models\ProductProxy; use Webkul\Product\Models\ProductProxy;
use Webkul\Product\Observers\ProductObserver; use Webkul\Product\Observers\ProductObserver;
use Webkul\Product\Console\Commands\PriceUpdate; use Webkul\Product\Console\Commands\PriceUpdate;
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
class ProductServiceProvider extends ServiceProvider class ProductServiceProvider extends ServiceProvider
{ {
@ -18,6 +19,8 @@ class ProductServiceProvider extends ServiceProvider
{ {
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
$this->app->make(EloquentFactory::class)->load(__DIR__ . '/../Database/Factories');
$this->app->register(EventServiceProvider::class); $this->app->register(EventServiceProvider::class);
$this->publishes([ $this->publishes([

View File

@ -0,0 +1,121 @@
<?php
namespace Tests\Functional\Product;
use FunctionalTester;
use Webkul\Category\Models\Category;
use Webkul\Category\Models\CategoryTranslation;
use Webkul\Core\Models\Channel;
use Webkul\Core\Models\Currency;
use Webkul\Core\Models\Locale;
use Webkul\Product\Models\Product;
use Faker\Factory;
use Webkul\Product\Models\ProductAttributeValue;
class ProductIndexCest
{
private $faker;
private $defaultChannel;
private $secondChannel;
private $localeEn;
private $localeDe;
private $productDefaultChannel;
private $productSecondChannel;
private $productNameDefaultChannel = [];
private $productNameSecondChannel = [];
public function _before(FunctionalTester $I)
{
$this->faker = Factory::create();
$this->localeEn = $I->grabRecord(Locale::class, [
'code' => 'en',
]);
$I->assertNotNull($this->localeEn);
$this->localeDe = $I->have(Locale::class, [
'code' => 'de',
'name' => 'German',
]);
$this->defaultChannel = $I->grabRecord(Channel::class, [
'code' => 'default',
]);
$I->assertNotNull($this->defaultChannel);
$currency = $I->grabRecord(Currency::class, [
'code' => 'USD',
]);
$rootCategoryTranslation = $I->grabRecord(CategoryTranslation::class, [
'slug' => 'root',
'locale' => 'en',
]);
$I->assertNotNull($rootCategoryTranslation);
$this->productDefaultChannel = $I->have(Product::class);
$this->productNameDefaultChannel['en'] = $I->have(ProductAttributeValue::class, [
'product_id' => $this->productDefaultChannel->id,
'locale' => $this->localeEn->code,
'channel' => $this->defaultChannel->code,
], 'name');
$this->secondChannel = $I->have(Channel::class, [
/*'locales' => [
$this->localeEn->id,
$this->localeDe->id,
],*/
'default_locale_id' => $this->localeEn->id,
/*'currencies' => [
$currency->id,
],*/
'base_currency_id' => $currency->id,
'root_category_id' => $rootCategoryTranslation->category_id,
]);
$this->productSecondChannel = $I->have(Product::class);
$this->productNameSecondChannel['en'] = $I->have(ProductAttributeValue::class, [
'product_id' => $this->productSecondChannel->id,
'locale' => $this->localeEn->code,
'channel' => $this->secondChannel->code,
], 'name');
$this->productNameSecondChannel['de'] = $I->have(ProductAttributeValue::class, [
'product_id' => $this->productSecondChannel->id,
'locale' => $this->localeDe->code,
'channel' => $this->secondChannel->code,
], 'name');
}
public function testProductIndex(FunctionalTester $I)
{
$I->loginAsAdmin();
$I->amOnAdminRoute('admin.catalog.products.index');
$I->see(__('admin::app.catalog.products.title'), 'h1');
$I->see($this->productNameDefaultChannel['en']->text_value, 'td');
$I->see($this->productNameSecondChannel['en']->text_value, 'td');
}
public function testProductIndexWithChannelFilter(FunctionalTester $I)
{
}
public function testProductIndexWithLocaleFilter(FunctionalTester $I)
{
}
public function testProductIndexWithChannelAndLocaleFilter(FunctionalTester $I)
{
}
}