Skip to content

Commit f35485f

Browse files
committed
B2B-2658: Implement GraphQL Resolver Cache for Customer query
- is_subscribed cache test
1 parent 319054b commit f35485f

File tree

1 file changed

+52
-18
lines changed
  • dev/tests/api-functional/testsuite/Magento/GraphQl/CustomerGraphQl/Model/Resolver

1 file changed

+52
-18
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/CustomerGraphQl/Model/Resolver/CustomerTest.php

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\Registry;
1818
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator\ProviderInterface;
1919
use Magento\GraphQlResolverCache\Model\Resolver\Result\Type as GraphQlResolverCache;
20+
use Magento\Newsletter\Model\SubscriptionManagerInterface;
2021
use Magento\Store\Api\WebsiteRepositoryInterface;
2122
use Magento\Store\Model\StoreManagerInterface;
2223
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
@@ -137,11 +138,14 @@ public function testCustomerResolverCacheAndInvalidation(callable $invalidationM
137138
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
138139
* @magentoApiDataFixture Magento/Store/_files/second_store.php
139140
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
140-
* @dataProvider invalidationMechanismProvider
141141
*/
142142
public function testCustomerIsSubscribedResolverCacheAndInvalidation()
143143
{
144+
/** @var SubscriptionManagerInterface $subscriptionManager */
145+
$subscriptionManager = $this->objectManager->get(SubscriptionManagerInterface::class);
144146
$customer = $this->customerRepository->get('[email protected]');
147+
// unsubscribe customer to initialize state
148+
$subscriptionManager->unsubscribeCustomer((int)$customer->getId(), (int)$customer->getStoreId());
145149

146150
$query = $this->getCustomerQuery();
147151

@@ -154,69 +158,99 @@ public function testCustomerIsSubscribedResolverCacheAndInvalidation()
154158
'',
155159
['Authorization' => 'Bearer ' . $token]
156160
);
157-
161+
$this->assertFalse($response['body']['customer']['is_subscribed']);
158162
$this->assertCurrentCustomerCacheRecordExists($customer);
159-
$this->assertIsSubscribedRecordExists($customer);
163+
$this->assertIsSubscribedRecordExists($customer, false);
160164

161165
// call query again to ensure no errors are thrown
162-
$this->graphQlQueryWithResponseHeaders(
166+
$response = $this->graphQlQueryWithResponseHeaders(
163167
$query,
164168
[],
165169
'',
166170
['Authorization' => 'Bearer ' . $token]
167171
);
168172

173+
$this->assertFalse($response['body']['customer']['is_subscribed']);
174+
169175
// change customer subscription
176+
$subscriptionManager->subscribeCustomer((int)$customer->getId(), (int)$customer->getStoreId());
177+
$this->assertIsSubscribedRecordNotExists($customer);
178+
$this->assertCurrentCustomerCacheRecordExists($customer);
170179

171-
// assert that cache entry is invalidated
172-
// $this->assertCurrentCustomerCacheRecordDoesNotExist();
180+
// query customer again so that subscription record
181+
$response = $this->graphQlQueryWithResponseHeaders(
182+
$query,
183+
[],
184+
'',
185+
['Authorization' => 'Bearer ' . $token]
186+
);
187+
$this->assertTrue($response['body']['customer']['is_subscribed']);
188+
189+
$this->assertIsSubscribedRecordExists($customer, true);
190+
// unsubscribe customer to restore original state
191+
$subscriptionManager->unsubscribeCustomer((int)$customer->getId(), (int)$customer->getStoreId());
192+
$this->assertIsSubscribedRecordNotExists($customer);
173193
}
174194

195+
/**
196+
* Prepare cache key for subscription flag cache record.
197+
*
198+
* @param CustomerInterface $customer
199+
* @return string
200+
*/
175201
private function getCacheKeyForIsSubscribedResolver(CustomerInterface $customer): string
176202
{
177-
$resolverMock = $this->getMockBuilder(IsSubscribed::class)
178-
->disableOriginalConstructor()
179-
->getMock();
180-
203+
$resolverMock = $this->getMockBuilder(IsSubscribed::class)->disableOriginalConstructor()->getMock();
181204
/** @var ProviderInterface $cacheKeyCalculatorProvider */
182205
$cacheKeyCalculatorProvider = Bootstrap::getObjectManager()->get(ProviderInterface::class);
183-
184206
$cacheKeyFactor = $cacheKeyCalculatorProvider
185207
->getKeyCalculatorForResolver($resolverMock)
186208
->calculateCacheKey(
187209
['model' => $customer]
188210
);
189-
190211
$cacheKeyQueryPayloadMetadata = IsSubscribed::class . '\Interceptor[]';
191-
192212
$cacheKeyParts = [
193213
GraphQlResolverCache::CACHE_TAG,
194214
$cacheKeyFactor,
195215
sha1($cacheKeyQueryPayloadMetadata)
196216
];
197-
198217
// strtoupper is called in \Magento\Framework\Cache\Frontend\Adapter\Zend::_unifyId
199218
return strtoupper(implode('_', $cacheKeyParts));
200219
}
201220

202221
/**
203-
* Assert that cache record exists for the given customer.
222+
* Assert subscription cache record exists for the given customer.
204223
*
205224
* @param CustomerInterface $customer
225+
* @param bool $expectedValue
206226
* @return void
207227
*/
208-
private function assertIsSubscribedRecordExists(CustomerInterface $customer)
228+
private function assertIsSubscribedRecordExists(CustomerInterface $customer, bool $expectedValue)
209229
{
210230
$cacheKey = $this->getCacheKeyForIsSubscribedResolver($customer);
211231
$cacheEntry = Bootstrap::getObjectManager()->get(GraphQlResolverCache::class)->load($cacheKey);
232+
$this->assertIsString($cacheEntry);
212233
$cacheEntryDecoded = json_decode($cacheEntry, true);
213234

214235
$this->assertEquals(
215-
$customer->getEmail(),
216-
$cacheEntryDecoded['email']
236+
$expectedValue,
237+
$cacheEntryDecoded
217238
);
218239
}
219240

241+
/**
242+
* Assert subscription cache record does not exist for the given customer.
243+
*
244+
* @param CustomerInterface $customer
245+
* @return void
246+
*/
247+
private function assertIsSubscribedRecordNotExists(CustomerInterface $customer)
248+
{
249+
$cacheKey = $this->getCacheKeyForIsSubscribedResolver($customer);
250+
$cacheEntry = Bootstrap::getObjectManager()->get(GraphQlResolverCache::class)->load($cacheKey);
251+
$this->assertFalse($cacheEntry);
252+
}
253+
220254
/**
221255
* @magentoApiDataFixture Magento/Customer/_files/customer.php
222256
* @magentoApiDataFixture Magento/Store/_files/second_store.php

0 commit comments

Comments
 (0)