-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5172 from magento-engcom/2.4-develop-forward-port-2
[Magento Community Engineering] Community Contributions - GraphQL - 2.4-develop forward-port
- Loading branch information
Showing
14 changed files
with
673 additions
and
16 deletions.
There are no files selected for viewing
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
66 changes: 66 additions & 0 deletions
66
...agento/SwatchesGraphQl/Model/Resolver/Product/Options/DataProvider/SwatchDataProvider.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,66 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\SwatchesGraphQl\Model\Resolver\Product\Options\DataProvider; | ||
|
||
use Magento\Swatches\Helper\Data as SwatchData; | ||
use Magento\Swatches\Helper\Media as SwatchesMedia; | ||
use Magento\Swatches\Model\Swatch; | ||
|
||
/** | ||
* Data provider for options swatches. | ||
*/ | ||
class SwatchDataProvider | ||
{ | ||
/** | ||
* @var SwatchData | ||
*/ | ||
private $swatchHelper; | ||
|
||
/** | ||
* @var SwatchesMedia | ||
*/ | ||
private $swatchMediaHelper; | ||
|
||
/** | ||
* SwatchDataProvider constructor. | ||
* | ||
* @param SwatchData $swatchHelper | ||
* @param SwatchesMedia $swatchMediaHelper | ||
*/ | ||
public function __construct( | ||
SwatchData $swatchHelper, | ||
SwatchesMedia $swatchMediaHelper | ||
) { | ||
$this->swatchHelper = $swatchHelper; | ||
$this->swatchMediaHelper = $swatchMediaHelper; | ||
} | ||
|
||
/** | ||
* Returns swatch data by option ID. | ||
* | ||
* @param string $optionId | ||
* @return array|null | ||
*/ | ||
public function getData(string $optionId): ?array | ||
{ | ||
$swatches = $this->swatchHelper->getSwatchesByOptionsId([$optionId]); | ||
if (!isset($swatches[$optionId]['type'], $swatches[$optionId]['value'])) { | ||
return null; | ||
} | ||
$type = (int)$swatches[$optionId]['type']; | ||
$value = $swatches[$optionId]['value']; | ||
$data = ['value' => $value, 'type' => $type]; | ||
if ($type === Swatch::SWATCH_TYPE_VISUAL_IMAGE) { | ||
$data['thumbnail'] = $this->swatchMediaHelper->getSwatchAttributeImage( | ||
Swatch::SWATCH_THUMBNAIL_NAME, | ||
$value | ||
); | ||
} | ||
return $data; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
app/code/Magento/SwatchesGraphQl/Model/Resolver/Product/Options/SwatchData.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,50 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\SwatchesGraphQl\Model\Resolver\Product\Options; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\SwatchesGraphQl\Model\Resolver\Product\Options\DataProvider\SwatchDataProvider; | ||
|
||
/** | ||
* Class SwatchData | ||
* | ||
* Product swatch data resolver, used for GraphQL request processing | ||
*/ | ||
class SwatchData implements ResolverInterface | ||
{ | ||
/** | ||
* @var SwatchDataProvider | ||
*/ | ||
private $swatchDataProvider; | ||
|
||
/** | ||
* SwatchData constructor. | ||
* | ||
* @param SwatchDataProvider $swatchDataProvider | ||
*/ | ||
public function __construct( | ||
SwatchDataProvider $swatchDataProvider | ||
) { | ||
$this->swatchDataProvider = $swatchDataProvider; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
return $this->swatchDataProvider->getData($value['value_index']); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/code/Magento/SwatchesGraphQl/Model/Resolver/Product/Options/SwatchDataTypeResolver.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,35 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\SwatchesGraphQl\Model\Resolver\Product\Options; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface; | ||
use Magento\Swatches\Model\Swatch; | ||
|
||
/** | ||
* Resolver for swatch data interface. | ||
*/ | ||
class SwatchDataTypeResolver implements TypeResolverInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolveType(array $data): string | ||
{ | ||
switch ($data['type']) { | ||
case Swatch::SWATCH_TYPE_TEXTUAL: | ||
return 'TextSwatchData'; | ||
case Swatch::SWATCH_TYPE_VISUAL_COLOR: | ||
return 'ColorSwatchData'; | ||
case Swatch::SWATCH_TYPE_VISUAL_IMAGE: | ||
return 'ImageSwatchData'; | ||
default: | ||
throw new LocalizedException(__('Unsupported swatch type')); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -16,4 +16,4 @@ | |
</argument> | ||
</arguments> | ||
</type> | ||
</config> | ||
</config> |
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 |
---|---|---|
|
@@ -172,24 +172,25 @@ public function testCreateCustomerIfEmailMissed() | |
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage "Email" is not a valid email address. | ||
* @dataProvider invalidEmailAddressDataProvider | ||
* | ||
* @param string $email | ||
* @throws \Exception | ||
*/ | ||
public function testCreateCustomerIfEmailIsNotValid() | ||
public function testCreateCustomerIfEmailIsNotValid(string $email) | ||
{ | ||
$newFirstname = 'Richard'; | ||
$newLastname = 'Rowe'; | ||
$currentPassword = 'test123#'; | ||
$newEmail = 'email'; | ||
$firstname = 'Richard'; | ||
$lastname = 'Rowe'; | ||
$password = 'test123#'; | ||
|
||
$query = <<<QUERY | ||
mutation { | ||
createCustomer( | ||
input: { | ||
firstname: "{$newFirstname}" | ||
lastname: "{$newLastname}" | ||
email: "{$newEmail}" | ||
password: "{$currentPassword}" | ||
firstname: "{$firstname}" | ||
lastname: "{$lastname}" | ||
email: "{$email}" | ||
password: "{$password}" | ||
is_subscribed: true | ||
} | ||
) { | ||
|
@@ -203,9 +204,29 @@ public function testCreateCustomerIfEmailIsNotValid() | |
} | ||
} | ||
QUERY; | ||
$this->expectExceptionMessage('"' . $email . '" is not a valid email address.'); | ||
$this->graphQlMutation($query); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function invalidEmailAddressDataProvider(): array | ||
{ | ||
return [ | ||
['plainaddress'], | ||
['jØ[email protected]'], | ||
['#@%^%#$@#$@#.com'], | ||
['@example.com'], | ||
['Joe Smith <[email protected]>'], | ||
['email.example.com'], | ||
['email@[email protected]'], | ||
['[email protected] (Joe Smith)'], | ||
['email@example'], | ||
['“email”@example.com'], | ||
]; | ||
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage Field "test123" is not defined by type CustomerInput. | ||
|
Oops, something went wrong.