View source
<?php
namespace Drupal\Tests\commerce_feeds\Kernel\Processor;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\feeds\Event\FeedsEvents;
use Drupal\feeds\Event\ParseEvent;
use Drupal\Tests\commerce_feeds\Kernel\CommerceFeedsKernelTestBase;
class ProductProcessorTest extends CommerceFeedsKernelTestBase {
protected $feedType;
public function setUp() {
parent::setUp();
$this->feedType = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
'sku' => 'sku',
'store' => 'store',
], [
'processor' => 'entity:commerce_product',
'processor_configuration' => [
'authorize' => FALSE,
'values' => [
'type' => 'default',
],
],
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'variations',
'map' => [
'target_id' => 'sku',
],
'settings' => [
'reference_by' => 'sku',
],
],
]),
]);
}
public function testImportProductsWithStore() {
$store_a = $this
->createStore('Store A', 'a@example.com');
$store_b = $this
->createStore('Store B', 'b@example.com');
$skus = [
'A001',
'B001',
'B002',
];
foreach ($skus as $sku) {
$variation = ProductVariation::create([
'type' => 'default',
'sku' => $sku,
]);
$variation
->save();
}
$this->feedType
->addMapping([
'target' => 'stores',
'map' => [
'target_id' => 'store',
],
'settings' => [
'reference_by' => 'name',
],
]);
$this->feedType
->save();
$this->container
->get('event_dispatcher')
->addListener(FeedsEvents::PARSE, [
$this,
'afterParse',
], FeedsEvents::AFTER);
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/products.csv',
]);
$feed
->import();
$products = Product::loadMultiple();
$this
->assertCount(2, $products);
$expected_per_product = [
1 => [
'variation_ids' => [
1,
],
'store_ids' => [
$store_a
->id(),
],
],
2 => [
'variation_ids' => [
2,
3,
],
'store_ids' => [
$store_b
->id(),
],
],
];
foreach ($expected_per_product as $i => $expected) {
$this
->assertEquals($expected['variation_ids'], $products[$i]
->getVariationIds());
$this
->assertEquals($expected['store_ids'], $products[$i]
->getStoreIds());
}
}
public function afterParse(ParseEvent $event) {
foreach ($event
->getParserResult() as $item) {
$item
->set('sku', explode('|', $item
->get('sku')));
}
}
}