Skip to content

Commit 08bd47e

Browse files
committed
Fixing static tests
1 parent 9e023a4 commit 08bd47e

File tree

3 files changed

+115
-48
lines changed

3 files changed

+115
-48
lines changed

app/code/Magento/WishlistGraphQl/Model/Resolver/AddProductsToWishlist.php

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Query\ResolverInterface;
1414
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1515
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
16+
use Magento\Wishlist\Model\Wishlist;
1617
use Magento\Wishlist\Model\Wishlist\AddProductsToWishlist as AddProductsToWishlistModel;
1718
use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig;
1819
use Magento\Wishlist\Model\Wishlist\Data\Error;
@@ -92,30 +93,19 @@ public function resolve(
9293
throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist'));
9394
}
9495

95-
$wishlistId = $args['wishlist_id'] ?: null;
96-
$wishlistItemsData = $args['wishlist_items'];
97-
$wishlist = $this->wishlistFactory->create();
98-
99-
if ($wishlistId) {
100-
$this->wishlistResource->load($wishlist, $wishlistId);
101-
} elseif ($customerId) {
102-
$wishlist->loadByCustomerId($customerId, true);
103-
}
96+
$wishlistId = ((int) $args['wishlist_id']) ?: null;
97+
$wishlist = $this->getWishlist($wishlistId, $customerId);
10498

10599
if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) {
106100
throw new GraphQlInputException(__('The wishlist was not found.'));
107101
}
108102

109-
$wishlistItems = [];
110-
foreach ($wishlistItemsData as $wishlistItemData) {
111-
$wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData);
112-
}
113-
103+
$wishlistItems = $this->getWishlistItems($args['wishlist_items']);
114104
$wishlistOutput = $this->addProductsToWishlist->execute($wishlist, $wishlistItems);
115105

116106
return [
117107
'wishlist' => $this->wishlistDataMapper->map($wishlistOutput->getWishlist()),
118-
'userInputErrors' => \array_map(
108+
'userInputErrors' => array_map(
119109
function (Error $error) {
120110
return [
121111
'code' => $error->getCode(),
@@ -126,4 +116,43 @@ function (Error $error) {
126116
)
127117
];
128118
}
119+
120+
/**
121+
* Get wishlist items
122+
*
123+
* @param array $wishlistItemsData
124+
*
125+
* @return array
126+
*/
127+
private function getWishlistItems(array $wishlistItemsData): array
128+
{
129+
$wishlistItems = [];
130+
131+
foreach ($wishlistItemsData as $wishlistItemData) {
132+
$wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData);
133+
}
134+
135+
return $wishlistItems;
136+
}
137+
138+
/**
139+
* Get customer wishlist
140+
*
141+
* @param int|null $wishlistId
142+
* @param int|null $customerId
143+
*
144+
* @return Wishlist
145+
*/
146+
private function getWishlist(?int $wishlistId, ?int $customerId): Wishlist
147+
{
148+
$wishlist = $this->wishlistFactory->create();
149+
150+
if ($wishlistId !== null && $wishlistId > 0) {
151+
$this->wishlistResource->load($wishlist, $wishlistId);
152+
} elseif ($customerId !== null) {
153+
$wishlist->loadByCustomerId($customerId, true);
154+
}
155+
156+
return $wishlist;
157+
}
129158
}

app/code/Magento/WishlistGraphQl/Model/Resolver/RemoveProductsFromWishlist.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
use Magento\Framework\GraphQl\Query\ResolverInterface;
1414
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1515
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
16+
use Magento\Wishlist\Model\Wishlist;
1617
use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig;
1718
use Magento\Wishlist\Model\Wishlist\Data\Error;
19+
use Magento\Wishlist\Model\Wishlist\Data\WishlistItemFactory;
1820
use Magento\Wishlist\Model\Wishlist\RemoveProductsFromWishlist as RemoveProductsFromWishlistModel;
1921
use Magento\Wishlist\Model\WishlistFactory;
2022
use Magento\WishlistGraphQl\Mapper\WishlistDataMapper;
@@ -87,22 +89,12 @@ public function resolve(
8789
$customerId = $context->getUserId();
8890

8991
/* Guest checking */
90-
if (null === $customerId || 0 === $customerId) {
92+
if ($customerId === null || 0 === $customerId) {
9193
throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist'));
9294
}
9395

94-
$wishlistId = $args['wishlist_id'] ?: null;
95-
$wishlist = $this->wishlistFactory->create();
96-
97-
if ($wishlistId) {
98-
$this->wishlistResource->load($wishlist, $wishlistId);
99-
} elseif ($customerId) {
100-
$wishlist->loadByCustomerId($customerId, true);
101-
}
102-
103-
if ($wishlistId) {
104-
$this->wishlistResource->load($wishlist, $wishlistId);
105-
}
96+
$wishlistId = ((int) $args['wishlist_id']) ?: null;
97+
$wishlist = $this->getWishlist($wishlistId, $customerId);
10698

10799
if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) {
108100
throw new GraphQlInputException(__('The wishlist was not found.'));
@@ -128,4 +120,25 @@ function (Error $error) {
128120
)
129121
];
130122
}
123+
124+
/**
125+
* Get customer wishlist
126+
*
127+
* @param int|null $wishlistId
128+
* @param int|null $customerId
129+
*
130+
* @return Wishlist
131+
*/
132+
private function getWishlist(?int $wishlistId, ?int $customerId): Wishlist
133+
{
134+
$wishlist = $this->wishlistFactory->create();
135+
136+
if ($wishlistId !== null && $wishlistId > 0) {
137+
$this->wishlistResource->load($wishlist, $wishlistId);
138+
} elseif ($customerId !== null) {
139+
$wishlist->loadByCustomerId($customerId, true);
140+
}
141+
142+
return $wishlist;
143+
}
131144
}

app/code/Magento/WishlistGraphQl/Model/Resolver/UpdateProductsInWishlist.php

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Query\ResolverInterface;
1414
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1515
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
16+
use Magento\Wishlist\Model\Wishlist;
1617
use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig;
1718
use Magento\Wishlist\Model\Wishlist\Data\Error;
1819
use Magento\Wishlist\Model\Wishlist\Data\WishlistItemFactory;
@@ -88,34 +89,19 @@ public function resolve(
8889
$customerId = $context->getUserId();
8990

9091
/* Guest checking */
91-
if (null === $customerId || 0 === $customerId) {
92+
if (null === $customerId || $customerId === 0) {
9293
throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist'));
9394
}
9495

95-
$wishlistId = $args['wishlist_id'] ?: null;
96-
$wishlist = $this->wishlistFactory->create();
97-
98-
if ($wishlistId) {
99-
$this->wishlistResource->load($wishlist, $wishlistId);
100-
} elseif ($customerId) {
101-
$wishlist->loadByCustomerId($customerId, true);
102-
}
103-
104-
if ($wishlistId) {
105-
$this->wishlistResource->load($wishlist, $wishlistId);
106-
}
96+
$wishlistId = ((int) $args['wishlist_id']) ?: null;
97+
$wishlist = $this->getWishlist($wishlistId, $customerId);
10798

10899
if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) {
109100
throw new GraphQlInputException(__('The wishlist was not found.'));
110101
}
111102

112-
$wishlistItems = [];
113-
$wishlistItemsData = $args['wishlist_items'];
114-
115-
foreach ($wishlistItemsData as $wishlistItemData) {
116-
$wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData);
117-
}
118-
103+
$wishlistItems = $args['wishlist_items'];
104+
$wishlistItems = $this->getWishlistItems($wishlistItems);
119105
$wishlistOutput = $this->updateProductsInWishlist->execute($wishlist, $wishlistItems);
120106

121107
if (count($wishlistOutput->getErrors()) !== count($wishlistItems)) {
@@ -135,4 +121,43 @@ function (Error $error) {
135121
)
136122
];
137123
}
124+
125+
/**
126+
* Get DTO wishlist items
127+
*
128+
* @param array $wishlistItemsData
129+
*
130+
* @return array
131+
*/
132+
private function getWishlistItems(array $wishlistItemsData): array
133+
{
134+
$wishlistItems = [];
135+
136+
foreach ($wishlistItemsData as $wishlistItemData) {
137+
$wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData);
138+
}
139+
140+
return $wishlistItems;
141+
}
142+
143+
/**
144+
* Get customer wishlist
145+
*
146+
* @param int|null $wishlistId
147+
* @param int|null $customerId
148+
*
149+
* @return Wishlist
150+
*/
151+
private function getWishlist(?int $wishlistId, ?int $customerId): Wishlist
152+
{
153+
$wishlist = $this->wishlistFactory->create();
154+
155+
if (null !== $wishlistId && 0 < $wishlistId) {
156+
$this->wishlistResource->load($wishlist, $wishlistId);
157+
} elseif ($customerId !== null) {
158+
$wishlist->loadByCustomerId($customerId, true);
159+
}
160+
161+
return $wishlist;
162+
}
138163
}

0 commit comments

Comments
 (0)