|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Customer\Test\Fixture; |
| 10 | + |
| 11 | +use Magento\Customer\Api\Data\GroupInterface; |
| 12 | +use Magento\Customer\Api\GroupRepositoryInterface; |
| 13 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 14 | +use Magento\Framework\DataObject; |
| 15 | +use Magento\Framework\EntityManager\Hydrator; |
| 16 | +use Magento\Framework\Exception\LocalizedException; |
| 17 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 18 | +use Magento\Tax\Api\TaxClassRepositoryInterface; |
| 19 | +use Magento\TestFramework\Fixture\Api\ServiceFactory; |
| 20 | +use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Data fixture for customer group |
| 24 | + */ |
| 25 | +class CustomerGroup implements RevertibleDataFixtureInterface |
| 26 | +{ |
| 27 | + private const DEFAULT_DATA = [ |
| 28 | + GroupInterface::CODE => 'Customergroup%uniqid%', |
| 29 | + GroupInterface::TAX_CLASS_ID => 3, |
| 30 | + ]; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ServiceFactory |
| 34 | + */ |
| 35 | + private ServiceFactory $serviceFactory; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var TaxClassRepositoryInterface |
| 39 | + */ |
| 40 | + private TaxClassRepositoryInterface $taxClassRepository; |
| 41 | + |
| 42 | + /** @var Hydrator */ |
| 43 | + private Hydrator $hydrator; |
| 44 | + |
| 45 | + /** |
| 46 | + * @param ServiceFactory $serviceFactory |
| 47 | + * @param TaxClassRepositoryInterface $taxClassRepository |
| 48 | + * @param SearchCriteriaBuilder $searchCriteriaBuilder |
| 49 | + * @param Hydrator $hydrator |
| 50 | + */ |
| 51 | + public function __construct( |
| 52 | + ServiceFactory $serviceFactory, |
| 53 | + TaxClassRepositoryInterface $taxClassRepository, |
| 54 | + Hydrator $hydrator |
| 55 | + ) { |
| 56 | + $this->serviceFactory = $serviceFactory; |
| 57 | + $this->taxClassRepository = $taxClassRepository; |
| 58 | + $this->hydrator = $hydrator; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * {@inheritdoc} |
| 63 | + * @param array $data Parameters. Same format as Customer::DEFAULT_DATA. |
| 64 | + * @return DataObject|null |
| 65 | + * @throws LocalizedException |
| 66 | + * @throws NoSuchEntityException |
| 67 | + */ |
| 68 | + public function apply(array $data = []): ?DataObject |
| 69 | + { |
| 70 | + $customerGroupSaveService = $this->serviceFactory->create( |
| 71 | + GroupRepositoryInterface::class, |
| 72 | + 'save' |
| 73 | + ); |
| 74 | + $data = self::DEFAULT_DATA; |
| 75 | + if (!empty($data['tax_class_id'])) { |
| 76 | + $data[GroupInterface::TAX_CLASS_ID] = $this->taxClassRepository->get($data['tax_class_id'])->getClassId(); |
| 77 | + } |
| 78 | + |
| 79 | + $customerGroup = $customerGroupSaveService->execute( |
| 80 | + [ |
| 81 | + 'group' => $data, |
| 82 | + ] |
| 83 | + ); |
| 84 | + |
| 85 | + return new DataObject($this->hydrator->extract($customerGroup)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @inheritdoc |
| 90 | + */ |
| 91 | + public function revert(DataObject $data): void |
| 92 | + { |
| 93 | + $service = $this->serviceFactory->create(GroupRepositoryInterface::class, 'deleteById'); |
| 94 | + $service->execute( |
| 95 | + [ |
| 96 | + 'id' => $data->getId() |
| 97 | + ] |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
0 commit comments