forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
magento/graphql-ce:magento#678 Send email to friend
- Loading branch information
1 parent
890bf89
commit a1d87ba
Showing
3 changed files
with
79 additions
and
45 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
app/code/Magento/SendFriendGraphQl/Model/Resolver/ProductService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
|
||
namespace Magento\SendFriendGraphQl\Model\Resolver; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Catalog\Model\Product\Visibility; | ||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
|
||
/** | ||
* Class ProductService | ||
*/ | ||
class ProductService | ||
{ | ||
/** @var ProductRepositoryInterface */ | ||
private $productRepository; | ||
|
||
/** @var Visibility */ | ||
private $visibility; | ||
|
||
/** | ||
* @param ProductRepositoryInterface $productRepository | ||
* @param Visibility $visibility | ||
*/ | ||
public function __construct( | ||
ProductRepositoryInterface $productRepository, | ||
Visibility $visibility | ||
) { | ||
$this->productRepository = $productRepository; | ||
$this->visibility = $visibility; | ||
} | ||
|
||
/** | ||
* Get product | ||
* | ||
* @param int $productId | ||
* @return ProductInterface | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
public function getProduct(int $productId): ProductInterface | ||
{ | ||
try { | ||
$product = $this->productRepository->getById($productId); | ||
|
||
if (!in_array( | ||
$product->getVisibility(), | ||
$this->visibility->getVisibleInCatalogIds() | ||
)) { | ||
throw new GraphQlNoSuchEntityException( | ||
__("The product that was requested doesn't exist. Verify the product and try again.") | ||
); | ||
} | ||
} catch (NoSuchEntityException $e) { | ||
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); | ||
} | ||
return $product; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
use Magento\SendFriend\Model\SendFriend; | ||
use Magento\SendFriend\Model\SendFriendFactory; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
/** | ||
|
@@ -219,15 +220,8 @@ public function testSendProductWithoutVisibility() | |
email:"[email protected]" | ||
}'; | ||
$query = $this->getQuery($productId, $recipients); | ||
|
||
$response = $this->graphQlMutation($query); | ||
self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']); | ||
self::assertEquals('[email protected]', $response['sendEmailToFriend']['sender']['email']); | ||
self::assertEquals('Lorem Ipsum', $response['sendEmailToFriend']['sender']['message']); | ||
self::assertEquals('Recipient Name 1', $response['sendEmailToFriend']['recipients'][0]['name']); | ||
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][0]['email']); | ||
self::assertEquals('Recipient Name 2', $response['sendEmailToFriend']['recipients'][1]['name']); | ||
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][1]['email']); | ||
$this->expectException(ResponseContainsErrorsException::class); | ||
$this->graphQlMutation($query); | ||
} | ||
|
||
/** | ||
|