|
| 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\Framework\GraphQl\Test\Unit\Query; |
| 10 | + |
| 11 | +use Magento\Framework\GraphQl\Query\Fields; |
| 12 | +use Magento\Framework\GraphQl\Query\QueryParser; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class FieldsTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var QueryParser|MockObject |
| 21 | + */ |
| 22 | + private $queryParser; |
| 23 | + |
| 24 | + public function setQueryDataProvider() |
| 25 | + { |
| 26 | + return [ |
| 27 | + 'mutation without variables' => [ |
| 28 | + 'query' => ['query' => 'mutation { |
| 29 | + addProductsToCart( |
| 30 | + cartId: "8k0Q4MpH2IGahWrTRtqM61YV2MtLPApz" |
| 31 | + cartItems: [ |
| 32 | + { |
| 33 | + quantity: 1 |
| 34 | + sku: "24-MB04" |
| 35 | + } |
| 36 | + ] |
| 37 | + ) { |
| 38 | + cart { |
| 39 | + items { |
| 40 | + product { |
| 41 | + name |
| 42 | + sku |
| 43 | + } |
| 44 | + quantity |
| 45 | + } |
| 46 | + } |
| 47 | + user_errors { |
| 48 | + code |
| 49 | + message |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +'], |
| 54 | + 'variables' => [], |
| 55 | + 'expected' => [ |
| 56 | + 'addProductsToCart' => 'addProductsToCart', |
| 57 | + 'cartId' => 'cartId', |
| 58 | + 'cartItems' => 'cartItems', |
| 59 | + 'quantity' => 'quantity', |
| 60 | + 'sku' => 'sku', |
| 61 | + 'cart' => 'cart', |
| 62 | + 'items' => 'items', |
| 63 | + 'product' => 'product', |
| 64 | + 'name' => 'name', |
| 65 | + 'user_errors' => 'user_errors', |
| 66 | + 'code' => 'code', |
| 67 | + 'message' => 'message' |
| 68 | + ] |
| 69 | + ], |
| 70 | + 'mutation with variables' => [ |
| 71 | + 'query' => ['query' => 'mutation ($cartId: String!, $products: [CartItemInput!]!) { |
| 72 | + addProductsToCart(cartId: $cartId, cartItems: $products) { |
| 73 | + cart { |
| 74 | + id |
| 75 | + items { |
| 76 | + uid |
| 77 | + quantity |
| 78 | + product { |
| 79 | + sku |
| 80 | + name |
| 81 | + thumbnail { |
| 82 | + url |
| 83 | + __typename |
| 84 | + } |
| 85 | + __typename |
| 86 | + } |
| 87 | + prices { |
| 88 | + price { |
| 89 | + value |
| 90 | + currency |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + user_errors { |
| 96 | + code |
| 97 | + message |
| 98 | + } |
| 99 | + } |
| 100 | +}'], |
| 101 | + 'variables' => [ |
| 102 | + 'cartId' => '123', |
| 103 | + 'products' => [ |
| 104 | + [ |
| 105 | + 'sku' => 'sku1', |
| 106 | + 'parent_sku' => 'sku2', |
| 107 | + 'quantity' => 1 |
| 108 | + ] |
| 109 | + ] |
| 110 | + ], |
| 111 | + 'expected' => [ |
| 112 | + 'cartId' => 'cartId', |
| 113 | + 'String' => 'String', |
| 114 | + 'products' => 'products', |
| 115 | + 'CartItemInput' => 'CartItemInput', |
| 116 | + 'addProductsToCart' => 'addProductsToCart', |
| 117 | + 'cartItems' => 'cartItems', |
| 118 | + 'cart' => 'cart', |
| 119 | + 'id' => 'id', |
| 120 | + 'items' => 'items', |
| 121 | + 'uid' => 'uid', |
| 122 | + 'quantity' => 'quantity', |
| 123 | + 'product' => 'product', |
| 124 | + 'sku' => 'sku', |
| 125 | + 'name' => 'name', |
| 126 | + 'thumbnail' => 'thumbnail', |
| 127 | + 'url' => 'url', |
| 128 | + '__typename' => '__typename', |
| 129 | + 'prices' => 'prices', |
| 130 | + 'price' => 'price', |
| 131 | + 'value' => 'value', |
| 132 | + 'currency' => 'currency', |
| 133 | + 'user_errors' => 'user_errors', |
| 134 | + 'code' => 'code', |
| 135 | + 'message' => 'message', |
| 136 | + 'parent_sku' => 'parent_sku' |
| 137 | + ] |
| 138 | + ] |
| 139 | + ]; |
| 140 | + } |
| 141 | + |
| 142 | + protected function setUp(): void |
| 143 | + { |
| 144 | + $this->objectManager = new ObjectManager($this); |
| 145 | + $this->queryParser = $this->objectManager->getObject( |
| 146 | + QueryParser::class |
| 147 | + ); |
| 148 | + |
| 149 | + $this->fields = new Fields( |
| 150 | + $this->queryParser |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @covers \Magento\Framework\GraphQl\Query\Fields::setQuery |
| 156 | + * @return void |
| 157 | + * @dataProvider setQueryDataProvider |
| 158 | + */ |
| 159 | + public function testSetQuery(array $query, array $variables, $expected) |
| 160 | + { |
| 161 | + $this->fields->setQuery($query['query'], $variables); |
| 162 | + $result = $this->fields->getFieldsUsedInQuery(); |
| 163 | + $this->assertEquals($expected, $result); |
| 164 | + } |
| 165 | +} |
0 commit comments