From 806e3f93dbd3164ea8ab5fb0b7d9e42b55232c4e Mon Sep 17 00:00:00 2001 From: MonaHartdegen Date: Thu, 23 Jan 2020 08:33:47 +0100 Subject: [PATCH] added ProductsIndexCest; added factories --- .../src/Database/Factories/ChannelFactory.php | 74 +++++ .../Database/Factories/CurrencyFactory.php | 14 + .../src/Database/Factories/LocaleFactory.php | 3 +- .../Factories/InventorySourceFactory.php | 25 ++ .../Providers/InventoryServiceProvider.php | 3 + .../ProductAttributeValueFactory.php | 276 ++++++++++++++++++ .../src/Database/Factories/ProductFactory.php | 17 ++ .../Factories/ProductInventoryFactory.php | 19 ++ .../src/Providers/ProductServiceProvider.php | 3 + tests/functional/Product/ProductIndexCest.php | 121 ++++++++ 10 files changed, 553 insertions(+), 2 deletions(-) create mode 100644 packages/Webkul/Core/src/Database/Factories/ChannelFactory.php create mode 100644 packages/Webkul/Core/src/Database/Factories/CurrencyFactory.php create mode 100644 packages/Webkul/Inventory/src/Database/Factories/InventorySourceFactory.php create mode 100644 packages/Webkul/Product/src/Database/Factories/ProductAttributeValueFactory.php create mode 100644 packages/Webkul/Product/src/Database/Factories/ProductFactory.php create mode 100644 packages/Webkul/Product/src/Database/Factories/ProductInventoryFactory.php create mode 100644 tests/functional/Product/ProductIndexCest.php diff --git a/packages/Webkul/Core/src/Database/Factories/ChannelFactory.php b/packages/Webkul/Core/src/Database/Factories/ChannelFactory.php new file mode 100644 index 000000000..e846c2d8b --- /dev/null +++ b/packages/Webkul/Core/src/Database/Factories/ChannelFactory.php @@ -0,0 +1,74 @@ +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']); +});*/ \ No newline at end of file diff --git a/packages/Webkul/Core/src/Database/Factories/CurrencyFactory.php b/packages/Webkul/Core/src/Database/Factories/CurrencyFactory.php new file mode 100644 index 000000000..2b551eae9 --- /dev/null +++ b/packages/Webkul/Core/src/Database/Factories/CurrencyFactory.php @@ -0,0 +1,14 @@ +define(Currency::class, function (Faker $faker, array $attributes) { + + return [ + 'code' => $faker->unique()->currencyCode, + 'name' => $faker->word, + ]; + +}); \ No newline at end of file diff --git a/packages/Webkul/Core/src/Database/Factories/LocaleFactory.php b/packages/Webkul/Core/src/Database/Factories/LocaleFactory.php index 0ab390c2f..85cb8db33 100644 --- a/packages/Webkul/Core/src/Database/Factories/LocaleFactory.php +++ b/packages/Webkul/Core/src/Database/Factories/LocaleFactory.php @@ -1,10 +1,9 @@ define(Locale::class, function (Faker $faker, array $attributes) { return [ diff --git a/packages/Webkul/Inventory/src/Database/Factories/InventorySourceFactory.php b/packages/Webkul/Inventory/src/Database/Factories/InventorySourceFactory.php new file mode 100644 index 000000000..4b6777749 --- /dev/null +++ b/packages/Webkul/Inventory/src/Database/Factories/InventorySourceFactory.php @@ -0,0 +1,25 @@ +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, + ]; +}); \ No newline at end of file diff --git a/packages/Webkul/Inventory/src/Providers/InventoryServiceProvider.php b/packages/Webkul/Inventory/src/Providers/InventoryServiceProvider.php index 44f082046..9a9520998 100755 --- a/packages/Webkul/Inventory/src/Providers/InventoryServiceProvider.php +++ b/packages/Webkul/Inventory/src/Providers/InventoryServiceProvider.php @@ -3,6 +3,7 @@ namespace Webkul\Inventory\Providers; use Illuminate\Support\ServiceProvider; +use Illuminate\Database\Eloquent\Factory as EloquentFactory; class InventoryServiceProvider extends ServiceProvider { @@ -14,6 +15,8 @@ class InventoryServiceProvider extends ServiceProvider public function boot() { $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); + + $this->app->make(EloquentFactory::class)->load(__DIR__ . '/../Database/Factories'); } /** diff --git a/packages/Webkul/Product/src/Database/Factories/ProductAttributeValueFactory.php b/packages/Webkul/Product/src/Database/Factories/ProductAttributeValueFactory.php new file mode 100644 index 000000000..ca2d5fffd --- /dev/null +++ b/packages/Webkul/Product/src/Database/Factories/ProductAttributeValueFactory.php @@ -0,0 +1,276 @@ +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; + } + ]; +}); diff --git a/packages/Webkul/Product/src/Database/Factories/ProductFactory.php b/packages/Webkul/Product/src/Database/Factories/ProductFactory.php new file mode 100644 index 000000000..2ddd175ef --- /dev/null +++ b/packages/Webkul/Product/src/Database/Factories/ProductFactory.php @@ -0,0 +1,17 @@ +define(Product::class, function (Faker $faker, array $attributes) { + + $attributeFamilyId = AttributeFamily::pluck('id')->first(); + + return [ + 'sku' => $faker->uuid, + 'type' => 'simple', + 'attribute_family_id' => $attributeFamilyId, + ]; +}); \ No newline at end of file diff --git a/packages/Webkul/Product/src/Database/Factories/ProductInventoryFactory.php b/packages/Webkul/Product/src/Database/Factories/ProductInventoryFactory.php new file mode 100644 index 000000000..d90b315c2 --- /dev/null +++ b/packages/Webkul/Product/src/Database/Factories/ProductInventoryFactory.php @@ -0,0 +1,19 @@ +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; + }, + ]; +}); \ No newline at end of file diff --git a/packages/Webkul/Product/src/Providers/ProductServiceProvider.php b/packages/Webkul/Product/src/Providers/ProductServiceProvider.php index 902f5261b..9152f2dc5 100755 --- a/packages/Webkul/Product/src/Providers/ProductServiceProvider.php +++ b/packages/Webkul/Product/src/Providers/ProductServiceProvider.php @@ -6,6 +6,7 @@ use Illuminate\Support\ServiceProvider; use Webkul\Product\Models\ProductProxy; use Webkul\Product\Observers\ProductObserver; use Webkul\Product\Console\Commands\PriceUpdate; +use Illuminate\Database\Eloquent\Factory as EloquentFactory; class ProductServiceProvider extends ServiceProvider { @@ -18,6 +19,8 @@ class ProductServiceProvider extends ServiceProvider { $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); + $this->app->make(EloquentFactory::class)->load(__DIR__ . '/../Database/Factories'); + $this->app->register(EventServiceProvider::class); $this->publishes([ diff --git a/tests/functional/Product/ProductIndexCest.php b/tests/functional/Product/ProductIndexCest.php new file mode 100644 index 000000000..790e8fb0b --- /dev/null +++ b/tests/functional/Product/ProductIndexCest.php @@ -0,0 +1,121 @@ +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) + { + + } +}