|
|
@@ -0,0 +1,181 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Webkul\BagistoApi\Tests\Unit\Migration;
|
|
|
+
|
|
|
+use App\Services\Asteria\Magento1ProductReader;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Tests\TestCase;
|
|
|
+
|
|
|
+class Magento1ProductReaderTest extends TestCase
|
|
|
+{
|
|
|
+ private string $sqlitePath;
|
|
|
+
|
|
|
+ private string $connection = 'asteria_test';
|
|
|
+
|
|
|
+ /** @var array<string, mixed> */
|
|
|
+ private array $readerConfig = [
|
|
|
+ 'media_base_url' => 'https://img.example.com/media/catalog/product',
|
|
|
+ 'store_media_url' => 'https://img.example.com/media/',
|
|
|
+ 'base_attributes' => ['name', 'price', 'status', 'description', 'short_description', 'url_key'],
|
|
|
+ 'include_eav_attributes' => ['hair_colorr'],
|
|
|
+ 'variant_option_titles' => [],
|
|
|
+ 'skip_option_titles' => ['Free Gift'],
|
|
|
+ 'enabled_status' => 1,
|
|
|
+ ];
|
|
|
+
|
|
|
+ protected function setUp(): void
|
|
|
+ {
|
|
|
+ parent::setUp();
|
|
|
+
|
|
|
+ $this->sqlitePath = sys_get_temp_dir().'/asteria_product_reader_'.uniqid('', true).'.sqlite';
|
|
|
+ touch($this->sqlitePath);
|
|
|
+
|
|
|
+ config()->set('database.connections.'.$this->connection, [
|
|
|
+ 'driver' => 'sqlite',
|
|
|
+ 'database' => $this->sqlitePath,
|
|
|
+ 'prefix' => '',
|
|
|
+ 'foreign_key_constraints' => false,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ DB::purge($this->connection);
|
|
|
+ MagentoSchema::create($this->connection);
|
|
|
+ $this->seedEnabledProduct(10, 'WIG-001');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function tearDown(): void
|
|
|
+ {
|
|
|
+ DB::purge($this->connection);
|
|
|
+
|
|
|
+ if (is_file($this->sqlitePath)) {
|
|
|
+ @unlink($this->sqlitePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ parent::tearDown();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_it_hydrates_eav_images_stock_categories_and_variants(): void
|
|
|
+ {
|
|
|
+ $reader = new Magento1ProductReader($this->connection, $this->readerConfig);
|
|
|
+ $products = $reader->fetchProducts(0, 50);
|
|
|
+
|
|
|
+ $this->assertCount(1, $products);
|
|
|
+
|
|
|
+ $product = $products->first();
|
|
|
+ $this->assertSame(10, $product['entity_id']);
|
|
|
+ $this->assertSame('WIG-001', $product['sku']);
|
|
|
+ $this->assertSame('Lace Front Wig', $product['name']);
|
|
|
+ $this->assertEquals(99.0, (float) $product['price']);
|
|
|
+ $this->assertSame('Natural Black', $product['hair_colorr']);
|
|
|
+ $this->assertSame('Nice wig', $product['short_description']);
|
|
|
+ $this->assertStringContainsString('https://img.example.com/media/wysiwyg/wigs/18.jpg', $product['description']);
|
|
|
+ $this->assertSame('https://img.example.com/media/catalog/product/w/i/wig.jpg', $product['base_image']);
|
|
|
+ $this->assertSame('https://img.example.com/media/catalog/product/w/i/wig-2.jpg', $product['additional_images']);
|
|
|
+ $this->assertSame(['Lace Wigs'], $product['categories']);
|
|
|
+ $this->assertEquals(12, (float) $product['qty']);
|
|
|
+ $this->assertSame('hair_length,hair_color', $product['super_attributes']);
|
|
|
+ $this->assertCount(2, $product['options']);
|
|
|
+ $this->assertCount(4, $product['variants']);
|
|
|
+ $this->assertSame('WIG-001-1-1', $product['variants'][0]['sku']);
|
|
|
+ $this->assertSame('12"', $product['variants'][0]['Hair Length']);
|
|
|
+ $this->assertSame('12"', $product['variants'][0]['hair_length']);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_it_pages_from_the_last_entity_id_and_skips_disabled_products(): void
|
|
|
+ {
|
|
|
+ MagentoSchema::seedProduct($this->connection, [
|
|
|
+ 'entity_id' => 11,
|
|
|
+ 'sku' => 'WIG-002',
|
|
|
+ 'name' => 'Second Wig',
|
|
|
+ 'price' => 50,
|
|
|
+ ]);
|
|
|
+ MagentoSchema::seedProduct($this->connection, [
|
|
|
+ 'entity_id' => 12,
|
|
|
+ 'sku' => 'WIG-OFF',
|
|
|
+ 'name' => 'Disabled',
|
|
|
+ 'price' => 10,
|
|
|
+ 'status' => 2,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $reader = new Magento1ProductReader($this->connection, $this->readerConfig);
|
|
|
+
|
|
|
+ $page = $reader->fetchProducts(10, 50);
|
|
|
+ $this->assertCount(1, $page);
|
|
|
+ $this->assertSame('WIG-002', $page->first()['sku']);
|
|
|
+
|
|
|
+ $skus = $reader->fetchProducts(0, 50)->pluck('sku')->all();
|
|
|
+ $this->assertSame(['WIG-001', 'WIG-002'], $skus);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_it_exports_select_attribute_definitions_with_options(): void
|
|
|
+ {
|
|
|
+ $reader = new Magento1ProductReader($this->connection, $this->readerConfig);
|
|
|
+ $attrs = $reader->fetchAttributeDefinitions();
|
|
|
+
|
|
|
+ $this->assertCount(1, $attrs);
|
|
|
+ $attr = $attrs->first();
|
|
|
+ $this->assertSame('hair_colorr', $attr['code']);
|
|
|
+ $this->assertSame('select', $attr['type']);
|
|
|
+ $this->assertSame('Natural Black', $attr['options'][0]['label']);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_it_omits_skipped_option_titles_from_variant_dimensions(): void
|
|
|
+ {
|
|
|
+ $reader = new Magento1ProductReader($this->connection, $this->readerConfig);
|
|
|
+ $product = $reader->fetchProducts(0, 1)->first();
|
|
|
+
|
|
|
+ $titles = array_column($product['options'], 'title');
|
|
|
+ $this->assertNotContains('Free Gift', $titles);
|
|
|
+ $this->assertContains('Hair Length', $titles);
|
|
|
+ $this->assertContains('Hair Color', $titles);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function seedEnabledProduct(int $entityId, string $sku): void
|
|
|
+ {
|
|
|
+ MagentoSchema::seedProduct($this->connection, [
|
|
|
+ 'entity_id' => $entityId,
|
|
|
+ 'sku' => $sku,
|
|
|
+ 'name' => 'Lace Front Wig',
|
|
|
+ 'price' => '99.00',
|
|
|
+ 'short_description' => '<p>Nice wig</p>',
|
|
|
+ 'description' => '<p>{{media url="wysiwyg/wigs/18.jpg"}}</p>',
|
|
|
+ 'hair_colorr' => MagentoSchema::HAIR_COLORR_OPTION_ID,
|
|
|
+ 'qty' => 12,
|
|
|
+ 'is_in_stock' => 1,
|
|
|
+ 'images' => ['/w/i/wig.jpg', '/w/i/wig-2.jpg'],
|
|
|
+ 'categories' => [
|
|
|
+ ['entity_id' => 5, 'name' => 'Lace Wigs', 'path' => '1/2/5'],
|
|
|
+ ],
|
|
|
+ 'options' => [
|
|
|
+ [
|
|
|
+ 'title' => 'Hair Length',
|
|
|
+ 'type' => 'drop_down',
|
|
|
+ 'is_require' => 1,
|
|
|
+ 'sort_order' => 1,
|
|
|
+ 'values' => [
|
|
|
+ ['title' => '12"', 'price' => 0, 'sort_order' => 1],
|
|
|
+ ['title' => '14"', 'price' => 10, 'sort_order' => 2],
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'title' => 'Hair Color',
|
|
|
+ 'type' => 'drop_down',
|
|
|
+ 'is_require' => 1,
|
|
|
+ 'sort_order' => 2,
|
|
|
+ 'values' => [
|
|
|
+ ['title' => 'Natural Black', 'price' => 0, 'sort_order' => 1],
|
|
|
+ ['title' => 'Brown', 'price' => 5, 'sort_order' => 2],
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'title' => 'Free Gift',
|
|
|
+ 'type' => 'drop_down',
|
|
|
+ 'is_require' => 0,
|
|
|
+ 'sort_order' => 3,
|
|
|
+ 'values' => [
|
|
|
+ ['title' => 'Yes', 'price' => 0, 'sort_order' => 1],
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+}
|