Skip to content

Commit 2cf7b3a

Browse files
committed
ACP2E-894: wip adding parametrized data fixtures
1 parent 84fab3e commit 2cf7b3a

File tree

2 files changed

+352
-0
lines changed

2 files changed

+352
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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\AddressRepositoryInterface;
12+
use Magento\Customer\Api\CustomerRepositoryInterface;
13+
use Magento\Customer\Api\Data\AddressInterface;
14+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
15+
use Magento\Customer\Api\Data\CustomerInterface;
16+
use Magento\Customer\Model\CustomerFactory;
17+
use Magento\Framework\DataObject;
18+
use Magento\Framework\Exception\LocalizedException;
19+
use Magento\Framework\Exception\NoSuchEntityException;
20+
use Magento\TestFramework\Fixture\Api\DataMerger;
21+
use Magento\TestFramework\Fixture\Api\ServiceFactory;
22+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
23+
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
24+
25+
/**
26+
* Data fixture for customer
27+
*/
28+
class Customer implements RevertibleDataFixtureInterface
29+
{
30+
private const DEFAULT_DATA = [
31+
'password' => null,
32+
CustomerInterface::ID => null,
33+
CustomerInterface::CONFIRMATION => null,
34+
CustomerInterface::CREATED_AT => null,
35+
CustomerInterface::UPDATED_AT => null,
36+
CustomerInterface::CREATED_IN => null,
37+
CustomerInterface::DOB => null,
38+
CustomerInterface::EMAIL => 'customer%uniqid%@mail.com',
39+
CustomerInterface::FIRSTNAME => 'Firstname %uniqid%',
40+
CustomerInterface::GENDER => null,
41+
CustomerInterface::GROUP_ID => null,
42+
CustomerInterface::LASTNAME => 'Lastname %uniqid%',
43+
CustomerInterface::MIDDLENAME => null,
44+
CustomerInterface::PREFIX => null,
45+
CustomerInterface::STORE_ID => null,
46+
CustomerInterface::SUFFIX => null,
47+
CustomerInterface::TAXVAT => null,
48+
CustomerInterface::WEBSITE_ID => null,
49+
CustomerInterface::DEFAULT_BILLING => null,
50+
CustomerInterface::DEFAULT_SHIPPING => null,
51+
CustomerInterface::KEY_ADDRESSES => [],
52+
CustomerInterface::DISABLE_AUTO_GROUP_CHANGE => null
53+
];
54+
55+
private const DEFAULT_DATA_ADDRESS = [
56+
AddressInterface::ID => null,
57+
AddressInterface::CUSTOMER_ID => null,
58+
AddressInterface::REGION => null,
59+
AddressInterface::REGION_ID => null,
60+
AddressInterface::COUNTRY_ID => null,
61+
AddressInterface::STREET => null,
62+
AddressInterface::COMPANY => null,
63+
AddressInterface::TELEPHONE => null,
64+
AddressInterface::FAX => null,
65+
AddressInterface::POSTCODE => null,
66+
AddressInterface::CITY => null,
67+
AddressInterface::FIRSTNAME => 'Firstname %uniqid%',
68+
AddressInterface::LASTNAME => 'Lastname %uniqid%',
69+
AddressInterface::MIDDLENAME => null,
70+
AddressInterface::PREFIX => null,
71+
AddressInterface::SUFFIX => null,
72+
AddressInterface::VAT_ID => null,
73+
AddressInterface::DEFAULT_BILLING => null,
74+
AddressInterface::DEFAULT_SHIPPING => null
75+
];
76+
77+
/**
78+
* @var ServiceFactory
79+
*/
80+
private $serviceFactory;
81+
82+
/**
83+
* @var CustomerRepositoryInterface
84+
*/
85+
private $customerRepository;
86+
87+
/**
88+
* @var CustomerFactory
89+
*/
90+
private $customerFactory;
91+
92+
/**
93+
* @var AddressRepositoryInterface
94+
*/
95+
private $addressRepository;
96+
97+
/**
98+
* @var AddressInterfaceFactory
99+
*/
100+
private $addressDataFactory;
101+
102+
/**
103+
* @var ProcessorInterface
104+
*/
105+
private $dataProcessor;
106+
107+
/**
108+
* @var DataMerger
109+
*/
110+
private $dataMerger;
111+
112+
/**
113+
* @var null
114+
*/
115+
private $customer;
116+
117+
/**
118+
* @param ServiceFactory $serviceFactory
119+
* @param CustomerRepositoryInterface $customerRepository
120+
* @param CustomerFactory $customerFactory
121+
* @param AddressRepositoryInterface $addressRepository
122+
* @param AddressInterfaceFactory $addressDataFactory
123+
* @param ProcessorInterface $dataProcessor
124+
* @param DataMerger $dataMerger
125+
*/
126+
public function __construct(
127+
ServiceFactory $serviceFactory,
128+
CustomerRepositoryInterface $customerRepository,
129+
CustomerFactory $customerFactory,
130+
AddressRepositoryInterface $addressRepository,
131+
AddressInterfaceFactory $addressDataFactory,
132+
ProcessorInterface $dataProcessor,
133+
DataMerger $dataMerger,
134+
) {
135+
$this->serviceFactory = $serviceFactory;
136+
$this->customerRepository = $customerRepository;
137+
$this->customerFactory = $customerFactory;
138+
$this->addressRepository = $addressRepository;
139+
$this->addressDataFactory = $addressDataFactory;
140+
$this->dataProcessor = $dataProcessor;
141+
$this->dataMerger = $dataMerger;
142+
$this->customer = null;
143+
}
144+
145+
/**
146+
* {@inheritdoc}
147+
* @param array $data Parameters. Same format as Customer::DEFAULT_DATA.
148+
* @return DataObject|null
149+
* @throws LocalizedException
150+
* @throws NoSuchEntityException
151+
*/
152+
public function apply(array $data = []): ?DataObject
153+
{
154+
$customerSaveService = $this->serviceFactory->create(CustomerRepositoryInterface::class, 'save');
155+
$data = $this->prepareData($data);
156+
if (count($data[CustomerInterface::KEY_ADDRESSES])) {
157+
$addresses = $this->prepareCustomerAddress($data[CustomerInterface::KEY_ADDRESSES]);
158+
$data[CustomerInterface::KEY_ADDRESSES] = $addresses;
159+
}
160+
return $customerSaveService->execute(
161+
['customer' => $data, 'passwordHash' => $this->customer->getPasswordHash()]
162+
);
163+
}
164+
165+
/**
166+
* @inheritdoc
167+
*/
168+
public function revert(DataObject $data): void
169+
{
170+
$service = $this->serviceFactory->create(CustomerRepositoryInterface::class, 'deleteById');
171+
$service->execute(
172+
[
173+
'id' => $data->getId()
174+
]
175+
);
176+
}
177+
178+
/**
179+
* Prepare customer data
180+
*
181+
* @param array $data
182+
* @return array
183+
*/
184+
private function prepareData(array $data): array
185+
{
186+
$data = $this->dataMerger->merge(self::DEFAULT_DATA, $data, false);
187+
188+
$this->customer = $this->customerFactory->create()->setData($data);
189+
$this->customer->setPassword($data['password']);
190+
if (isset($data['password'])) {
191+
unset($data['password']);
192+
}
193+
194+
return $this->dataProcessor->process($this, $data);
195+
}
196+
197+
/**
198+
* Prepare customer address
199+
*
200+
* @param array $data
201+
* @return array
202+
*/
203+
private function prepareCustomerAddress(array $data): array
204+
{
205+
$addresses = [];
206+
foreach ($data as $dataAddress) {
207+
$dataAddress = $this->dataMerger->merge(self::DEFAULT_DATA_ADDRESS, $dataAddress, false);
208+
$addresses[] = $this->dataProcessor->process($this, $dataAddress);
209+
}
210+
211+
return $addresses;
212+
}
213+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Sales;
9+
10+
use Magento\Catalog\Helper\Data;
11+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
12+
use Magento\Customer\Api\CustomerRepositoryInterface;
13+
use Magento\Customer\Model\CustomerAuthUpdate;
14+
use Magento\Customer\Model\CustomerRegistry;
15+
use Magento\Customer\Test\Fixture\Customer;
16+
use Magento\Framework\ObjectManagerInterface;
17+
use Magento\Integration\Api\CustomerTokenServiceInterface;
18+
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
19+
use Magento\Store\Test\Fixture\Store as StoreFixture;
20+
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
21+
use Magento\TestFramework\Fixture\Config;
22+
use Magento\TestFramework\Fixture\DataFixture;
23+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
24+
use Magento\TestFramework\Helper\Bootstrap;
25+
use Magento\TestFramework\TestCase\GraphQlAbstract;
26+
27+
/**
28+
* GraphQl tests for @see \Magento\SalesGraphQl\Model\Resolver\CustomerOrders.
29+
*/
30+
class GetCustomerOrdersTest extends GraphQlAbstract
31+
{
32+
/**
33+
* @var CustomerTokenServiceInterface
34+
*/
35+
private $customerTokenService;
36+
37+
/**
38+
* @var CustomerRegistry
39+
*/
40+
private $customerRegistry;
41+
42+
/**
43+
* @var CustomerAuthUpdate
44+
*/
45+
private $customerAuthUpdate;
46+
47+
/**
48+
* @var CustomerRepositoryInterface
49+
*/
50+
private $customerRepository;
51+
52+
/**
53+
* @var ObjectManagerInterface
54+
*/
55+
private $objectManager;
56+
57+
/**
58+
* @inheridoc
59+
*/
60+
protected function setUp(): void
61+
{
62+
parent::setUp();
63+
64+
$this->objectManager = Bootstrap::getObjectManager();
65+
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
66+
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
67+
$this->customerRegistry = $this->objectManager->get(CustomerRegistry::class);
68+
$this->customerAuthUpdate = $this->objectManager->get(CustomerAuthUpdate::class);
69+
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
70+
}
71+
72+
#[
73+
Config(Data::XML_PATH_PRICE_SCOPE, Data::PRICE_SCOPE_WEBSITE),
74+
DataFixture(WebsiteFixture::class, as: 'website2'),
75+
DataFixture(StoreGroupFixture::class, ['website_id' => '$website2.id$'], 'store_group2'),
76+
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store2'),
77+
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store3'),
78+
DataFixture(ProductFixture::class, ['website_ids' => [1, '$website2.id$' ]], as: 'product'),
79+
DataFixture(
80+
Customer::class,
81+
[
82+
'email' => '[email protected]',
83+
'password' => 'password',
84+
'store_id' => '$store2.id$',
85+
'website_id' => '$website2.id$',
86+
'addresses' => [
87+
[
88+
'country_id' => 'US',
89+
'region_id' => 32,
90+
'city' => 'Boston',
91+
'street' => ['10 Milk Street'],
92+
'postcode' => '02108',
93+
'telephone' => '1234567890',
94+
'default_billing' => true,
95+
'default_shipping' => true
96+
]
97+
]
98+
],
99+
as: 'customer'
100+
)
101+
]
102+
public function testGetCustomerOrders()
103+
{
104+
$currentEmail = '[email protected]';
105+
$currentPassword = 'password';
106+
107+
$query = <<<QUERY
108+
query {
109+
customer {
110+
orders(
111+
pageSize: 20,
112+
scope: STORE
113+
) {
114+
items {
115+
id
116+
order_number
117+
order_date
118+
total {
119+
grand_total
120+
{ value currency }}
121+
status
122+
}
123+
}
124+
}
125+
}
126+
QUERY;
127+
$response = $this->graphQlQuery(
128+
$query,
129+
[],
130+
'',
131+
$this->getCustomerAuthHeaders($currentEmail, $currentPassword)
132+
);
133+
134+
$this->assertNull($response['customer']['id']);
135+
$this->assertEquals('John', $response['customer']['firstname']);
136+
$this->assertEquals('Smith', $response['customer']['lastname']);
137+
$this->assertEquals($currentEmail, $response['customer']['email']);
138+
}
139+
}

0 commit comments

Comments
 (0)