Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VirtualProductViewTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 
17 {
23  public function testQueryAllFieldsVirtualProduct()
24  {
25  $productSku = 'virtual-product';
26 
27  $query
28  = <<<QUERY
29 {
30  products(filter: {sku: {eq: "$productSku"}})
31  {
32  items{
33  id
34  attribute_set_id
35  created_at
36  name
37  sku
38  type_id
39  updated_at
40  ... on PhysicalProductInterface {
41  weight
42  }
43  ... on VirtualProduct {
44  attribute_set_id
45  name
46  id
47  sku
48 
49  }
50  }
51  }
52 }
53 QUERY;
54 
55  $response = $this->graphQlQuery($query);
56 
58  $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
59  $product = $productRepository->get($productSku, false, null, true);
61  $metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
62  $product->setId(
63  $product->getData($metadataPool->getMetadata(ProductInterface::class)->getLinkField())
64  );
65  $this->assertArrayHasKey('products', $response);
66  $this->assertArrayHasKey('items', $response['products']);
67  $this->assertEquals(1, count($response['products']['items']));
68  $this->assertArrayHasKey(0, $response['products']['items']);
69  $this->assertBaseFields($product, $response['products']['items'][0]);
70  $this->assertArrayNotHasKey(
71  'weight',
72  $response['products']['items'][0],
73  "response does contain the key weight"
74  );
75  }
76 
83  {
84  $productSku = 'virtual-product';
85 
86  $query
87  = <<<QUERY
88 {
89  products(filter: {sku: {eq: "$productSku"}})
90  {
91  items{
92  id
93  attribute_set_id
94  created_at
95  name
96  sku
97  type_id
98  updated_at
99  ... on PhysicalProductInterface {
100  weight
101  }
102  ... on VirtualProduct {
103  attribute_set_id
104  name
105  weight
106  id
107  sku
108  }
109  }
110  }
111 }
112 QUERY;
113 
114  $this->expectException(\Exception::class);
115  $this->expectExceptionMessage(
116  'GraphQL response contains errors: Cannot query field "weight" on type "VirtualProduct"'
117  );
118  $this->graphQlQuery($query);
119  }
120 
125  private function assertBaseFields($product, $actualResponse)
126  {
127  $assertionMap = [
128  ['response_field' => 'attribute_set_id', 'expected_value' => $product->getAttributeSetId()],
129  ['response_field' => 'id', 'expected_value' => $product->getId()],
130  ['response_field' => 'name', 'expected_value' => $product->getName()],
131  ['response_field' => 'sku', 'expected_value' => $product->getSku()],
132  ['response_field' => 'type_id', 'expected_value' => $product->getTypeId()]
133  ];
134 
135  $this->assertResponseFields($actualResponse, $assertionMap);
136  }
137 }
$response
Definition: 404.php:11
assertResponseFields($actualResponse, $assertionMap)
taxRateField this edit on("click.mselect-delete", ".mselect-delete", function() { if(!confirm('<?=/*@escapeNotVerified */__( 'Do you really want to delete this tax rate?') ?>')) { return;} var that=$(this), select=that.closest('.mselect-list').prev(), rateValue=that.parent().find( 'input[type="checkbox"]').val();$( 'body').trigger( 'processStart');var ajaxOptions={ type:'POST', data:{ tax_calculation_rate_id:rateValue, form_key:$( 'input[name="form_key"]').val() }, dataType:'json', url:'<?=/*@escapeNotVerified */$block->getTaxRateDeleteUrl() ?>', success:function(result, status) { $( 'body').trigger( 'processStop');if(result.success) { that.parent().remove();select.find( 'option').each(function() { if(this.value===rateValue) { $(this).remove();} });select.trigger( 'change.hiddenSelect');} else { if(result.error_message) alert({ content:result.error_message });else alert({ content:'<?=/*@escapeNotVerified */__( 'An error occurred') ?>' });} }, error:function() { $( 'body').trigger( 'processStop');alert({ content:'<?=/*@escapeNotVerified */__( 'An error occurred') ?>' });} };$.ajax(ajaxOptions);}) .on( 'click.mselectAdd'
Definition: edit.phtml:164
graphQlQuery(string $query, array $variables=[], string $operationName='', array $headers=[])