|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * NOTICE: All information contained herein is, and remains |
| 7 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 8 | + * and technical concepts contained herein are proprietary to Adobe |
| 9 | + * and its suppliers and are protected by all applicable intellectual |
| 10 | + * property laws, including trade secret and copyright laws. |
| 11 | + * Dissemination of this information or reproduction of this material |
| 12 | + * is strictly forbidden unless prior written permission is obtained from |
| 13 | + * Adobe. |
| 14 | + */ |
| 15 | +declare(strict_types=1); |
| 16 | + |
| 17 | +namespace Magento\Wishlist\Test\Fixture; |
| 18 | + |
| 19 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 20 | +use Magento\Framework\DataObject; |
| 21 | +use Magento\Framework\Exception\LocalizedException; |
| 22 | +use Magento\Wishlist\Model\ResourceModel\Wishlist; |
| 23 | +use Magento\Wishlist\Model\WishlistFactory; |
| 24 | +use Magento\TestFramework\Fixture\DataFixtureInterface; |
| 25 | + |
| 26 | +class AddProductToWishlist implements DataFixtureInterface |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var WishlistFactory |
| 30 | + */ |
| 31 | + private WishlistFactory $wishlistFactory; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ProductRepositoryInterface |
| 35 | + */ |
| 36 | + private ProductRepositoryInterface $productRepository; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Wishlist |
| 40 | + */ |
| 41 | + private Wishlist $wishlistResource; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param WishlistFactory $wishlistFactory |
| 45 | + * @param ProductRepositoryInterface $productRepository |
| 46 | + * @param Wishlist $wishlistResource |
| 47 | + */ |
| 48 | + public function __construct( |
| 49 | + WishlistFactory $wishlistFactory, |
| 50 | + ProductRepositoryInterface $productRepository, |
| 51 | + Wishlist $wishlistResource, |
| 52 | + ) { |
| 53 | + $this->wishlistFactory = $wishlistFactory; |
| 54 | + $this->productRepository = $productRepository; |
| 55 | + $this->wishlistResource = $wishlistResource; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + * @param array $data Parameters |
| 61 | + * <pre> |
| 62 | + * $data = [ |
| 63 | + * 'customer_id' => (int) Customer ID. Required. |
| 64 | + * 'product_ids' => (array) Product IDs. Optional. Default: []. |
| 65 | + * 'name' => (string) name. Optional. Default: 'Wish List'. |
| 66 | + * ] |
| 67 | + * </pre> |
| 68 | + */ |
| 69 | + public function apply(array $data = []): ?DataObject |
| 70 | + { |
| 71 | + $name = $data['name'] ?? 'Wish List'; |
| 72 | + $wishlist = $this->wishlistFactory->create(); |
| 73 | + $wishlist->setCustomerId($data['customer_id']) |
| 74 | + ->setName($name) |
| 75 | + ->setVisibility(1); |
| 76 | + $this->wishlistResource->save($wishlist); |
| 77 | + if (isset($data['product_ids'])) { |
| 78 | + foreach ($data['product_ids'] as $productId) { |
| 79 | + $product = $this->productRepository->getById($productId); |
| 80 | + $wishlist->addNewItem($product); |
| 81 | + } |
| 82 | + } |
| 83 | + if (is_string($wishlist)) { |
| 84 | + throw new LocalizedException(__($wishlist)); |
| 85 | + } |
| 86 | + return $wishlist; |
| 87 | + } |
| 88 | +} |
0 commit comments