|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Catalog\Test\Fixture\Product; |
| 8 | + |
| 9 | +use Magento\Mtf\Fixture\DataSource; |
| 10 | +use Magento\Mtf\Fixture\FixtureFactory; |
| 11 | +use Magento\Customer\Test\Fixture\CustomerGroup; |
| 12 | +use Magento\Mtf\Repository\RepositoryFactory; |
| 13 | + |
| 14 | +/** |
| 15 | + * TierPrice data source. |
| 16 | + * |
| 17 | + * Data keys: |
| 18 | + * - dataset |
| 19 | + */ |
| 20 | +class TierPrice extends DataSource |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Customer group fixture array. |
| 24 | + * |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + private $customerGroups; |
| 28 | + |
| 29 | + /** |
| 30 | + * Repository Factory instance. |
| 31 | + * |
| 32 | + * @var RepositoryFactory |
| 33 | + */ |
| 34 | + private $repositoryFactory; |
| 35 | + |
| 36 | + /** |
| 37 | + * Fixture Factory instance. |
| 38 | + * |
| 39 | + * @var FixtureFactory |
| 40 | + */ |
| 41 | + private $fixtureFactory; |
| 42 | + |
| 43 | + /** |
| 44 | + * Rought fixture field data. |
| 45 | + * |
| 46 | + * @var array |
| 47 | + */ |
| 48 | + private $fixtureData = null; |
| 49 | + |
| 50 | + /** |
| 51 | + * @constructor |
| 52 | + * @param RepositoryFactory $repositoryFactory |
| 53 | + * @param FixtureFactory $fixtureFactory |
| 54 | + * @param array $params |
| 55 | + * @param array $data |
| 56 | + * @throws \Exception |
| 57 | + */ |
| 58 | + public function __construct( |
| 59 | + RepositoryFactory $repositoryFactory, |
| 60 | + FixtureFactory $fixtureFactory, |
| 61 | + array $params, |
| 62 | + $data = [] |
| 63 | + ) { |
| 64 | + $this->repositoryFactory = $repositoryFactory; |
| 65 | + $this->fixtureFactory = $fixtureFactory; |
| 66 | + $this->params = $params; |
| 67 | + $this->fixtureData = $data; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Return prepared data set. |
| 72 | + * |
| 73 | + * @param string $key [optional] |
| 74 | + * @return mixed |
| 75 | + * @throws \Exception |
| 76 | + */ |
| 77 | + public function getData($key = null) |
| 78 | + { |
| 79 | + if (!isset($this->fixtureData['dataset'])) { |
| 80 | + throw new \Exception("Data must be set"); |
| 81 | + } |
| 82 | + $this->data = $this->repositoryFactory->get($this->params['repository'])->get($this->fixtureData['dataset']); |
| 83 | + foreach ($this->data as $key => $item) { |
| 84 | + /** @var CustomerGroup $customerGroup */ |
| 85 | + $customerGroup = $this->fixtureFactory->createByCode( |
| 86 | + 'customerGroup', |
| 87 | + ['dataset' => $item['customer_group']['dataset']] |
| 88 | + ); |
| 89 | + if (!$customerGroup->hasData('customer_group_id')) { |
| 90 | + $customerGroup->persist(); |
| 91 | + } |
| 92 | + $this->data[$key]['customer_group'] = $customerGroup->getCustomerGroupCode(); |
| 93 | + $this->customerGroups[$key] = $customerGroup; |
| 94 | + } |
| 95 | + |
| 96 | + return parent::getData($key); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Return customer group fixture. |
| 101 | + * |
| 102 | + * @return array |
| 103 | + */ |
| 104 | + public function getCustomerGroups() |
| 105 | + { |
| 106 | + return $this->customerGroups; |
| 107 | + } |
| 108 | +} |
0 commit comments