-
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 #4930 from magento-engcom/2.3-develop-prs
[Magento Community Engineering] Community Contributions - 2.3-develop
- Loading branch information
Showing
36 changed files
with
1,283 additions
and
42 deletions.
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
app/code/Magento/AdvancedSearch/Test/Unit/Model/Recommendations/DataProviderTest.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,189 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\AdvancedSearch\Test\Unit\Model\Recommendations; | ||
|
||
use Magento\AdvancedSearch\Model\Recommendations\DataProvider; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; | ||
use Magento\Catalog\Model\Layer\Resolver; | ||
use Magento\AdvancedSearch\Model\ResourceModel\Recommendations; | ||
use Magento\AdvancedSearch\Model\ResourceModel\RecommendationsFactory; | ||
use Magento\Search\Model\QueryResult; | ||
use Magento\Search\Model\QueryResultFactory; | ||
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; | ||
use Magento\Catalog\Model\Layer as SearchLayer; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Magento\Search\Model\QueryInterface; | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
* | ||
* Class \Magento\AdvancedSearch\Test\Unit\Model\Recommendations\DataProviderTest | ||
*/ | ||
class DataProviderTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var DataProvider; | ||
*/ | ||
private $model; | ||
|
||
/** | ||
* @var ObjectManagerHelper | ||
*/ | ||
private $objectManagerHelper; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|ScopeConfigInterface | ||
*/ | ||
private $scopeConfigMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|Resolver | ||
*/ | ||
private $layerResolverMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|SearchLayer | ||
*/ | ||
private $searchLayerMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|RecommendationsFactory | ||
*/ | ||
private $recommendationsFactoryMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|Recommendations | ||
*/ | ||
private $recommendationsMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|Resolver | ||
*/ | ||
private $queryResultFactory; | ||
|
||
/** | ||
* Set up test environment. | ||
* | ||
* @return void | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class); | ||
$this->layerResolverMock = $this->getMockBuilder(Resolver::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['get']) | ||
->getMock(); | ||
|
||
$this->searchLayerMock = $this->createMock(SearchLayer::class); | ||
|
||
$this->layerResolverMock->expects($this->any()) | ||
->method('get') | ||
->will($this->returnValue($this->searchLayerMock)); | ||
|
||
$this->recommendationsFactoryMock = $this->getMockBuilder(RecommendationsFactory::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['create']) | ||
->getMock(); | ||
|
||
$this->recommendationsMock = $this->createMock(Recommendations::class); | ||
|
||
$this->queryResultFactory = $this->getMockBuilder(QueryResultFactory::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['create']) | ||
->getMock(); | ||
|
||
$this->objectManagerHelper = new ObjectManagerHelper($this); | ||
$this->model = $this->objectManagerHelper->getObject( | ||
DataProvider::class, | ||
[ | ||
'scopeConfig' => $this->scopeConfigMock, | ||
'layerResolver' => $this->layerResolverMock, | ||
'recommendationsFactory' => $this->recommendationsFactoryMock, | ||
'queryResultFactory' => $this->queryResultFactory | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Test testGetItems() when Search Recommendations disabled. | ||
* | ||
* @return void | ||
*/ | ||
public function testGetItemsWhenDisabledSearchRecommendations() | ||
{ | ||
$isEnabledSearchRecommendations = false; | ||
|
||
/** @var $queryInterfaceMock QueryInterface */ | ||
$queryInterfaceMock = $this->createMock(QueryInterface::class); | ||
|
||
$this->scopeConfigMock->expects($this->any()) | ||
->method('isSetFlag') | ||
->with('catalog/search/search_recommendations_enabled', ScopeInterface::SCOPE_STORE) | ||
->willReturn($isEnabledSearchRecommendations); | ||
|
||
$result = $this->model->getItems($queryInterfaceMock); | ||
$this->assertEquals([], $result); | ||
} | ||
|
||
/** | ||
* Test testGetItems() when Search Recommendations enabled. | ||
* | ||
* @return void | ||
*/ | ||
public function testGetItemsWhenEnabledSearchRecommendations() | ||
{ | ||
$storeId = 1; | ||
$searchRecommendationsCountConfig = 2; | ||
$isEnabledSearchRecommendations = true; | ||
$queryText = 'test'; | ||
|
||
/** @var $queryInterfaceMock QueryInterface */ | ||
$queryInterfaceMock = $this->createMock(QueryInterface::class); | ||
$queryInterfaceMock->expects($this->any())->method('getQueryText')->willReturn($queryText); | ||
|
||
$this->scopeConfigMock->expects($this->any()) | ||
->method('isSetFlag') | ||
->with('catalog/search/search_recommendations_enabled', ScopeInterface::SCOPE_STORE) | ||
->willReturn($isEnabledSearchRecommendations); | ||
|
||
$this->scopeConfigMock->expects($this->any()) | ||
->method('getValue') | ||
->with('catalog/search/search_recommendations_count', ScopeInterface::SCOPE_STORE) | ||
->willReturn($searchRecommendationsCountConfig); | ||
|
||
$productCollectionMock = $this->createMock(ProductCollection::class); | ||
$productCollectionMock->expects($this->any())->method('getStoreId')->willReturn($storeId); | ||
|
||
$this->searchLayerMock->expects($this->any())->method('getProductCollection') | ||
->willReturn($productCollectionMock); | ||
|
||
$this->recommendationsFactoryMock->expects($this->any())->method('create') | ||
->willReturn($this->recommendationsMock); | ||
|
||
$this->recommendationsMock->expects($this->any())->method('getRecommendationsByQuery') | ||
->with($queryText, ['store_id' => $storeId], $searchRecommendationsCountConfig) | ||
->willReturn( | ||
[ | ||
[ | ||
'query_text' => 'a', | ||
'num_results' => 3 | ||
], | ||
[ | ||
'query_text' => 'b', | ||
'num_results' => 2 | ||
] | ||
] | ||
); | ||
$queryResultMock = $this->createMock(QueryResult::class); | ||
$this->queryResultFactory->expects($this->any())->method('create')->willReturn($queryResultMock); | ||
|
||
$result = $this->model->getItems($queryInterfaceMock); | ||
$this->assertEquals(2, count($result)); | ||
} | ||
} |
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
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
131 changes: 131 additions & 0 deletions
131
app/code/Magento/CardinalCommerce/Test/Unit/Model/Response/JwtParserTest.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,131 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CardinalCommerce\Test\Unit\Model\Response; | ||
|
||
use Magento\CardinalCommerce\Model\Response\JwtParser; | ||
use Magento\CardinalCommerce\Model\Config; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\CardinalCommerce\Model\JwtManagement; | ||
use Magento\CardinalCommerce\Model\Response\JwtPayloadValidatorInterface; | ||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
/** | ||
* Class \Magento\CardinalCommerce\Test\Unit\Model\Response\JwtParserTest | ||
*/ | ||
class JwtParserTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var JwtParser | ||
*/ | ||
private $model; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | Config | ||
*/ | ||
private $configMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | JwtManagement | ||
*/ | ||
private $jwtManagementMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | JwtPayloadValidatorInterface | ||
*/ | ||
private $jwtPayloadValidatorMock; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->objectManager = new ObjectManager($this); | ||
|
||
$this->configMock = $this->getMockBuilder(Config::class) | ||
->setMethods(['getApiKey', 'isDebugModeEnabled']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->jwtManagementMock = $this->getMockBuilder(JwtManagement::class) | ||
->setMethods(['decode']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->jwtPayloadValidatorMock = $this->getMockBuilder(JwtPayloadValidatorInterface::class) | ||
->setMethods(['validate']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->model = $this->objectManager->getObject( | ||
JwtParser::class, | ||
[ | ||
'jwtManagement' => $this->jwtManagementMock, | ||
'config' => $this->configMock, | ||
'tokenValidator' => $this->jwtPayloadValidatorMock | ||
] | ||
); | ||
|
||
$this->configMock->expects($this->any()) | ||
->method('getApiKey') | ||
->willReturn('API Key'); | ||
|
||
$this->configMock->expects($this->any()) | ||
->method('isDebugModeEnabled') | ||
->willReturn(false); | ||
|
||
$this->jwtManagementMock->expects($this->any()) | ||
->method('decode') | ||
->with('string_to_test', 'API Key') | ||
->willReturn(['mockResult' => 'jwtPayload']); | ||
} | ||
|
||
/** | ||
* Tests Jwt Parser execute with the result and no exception. | ||
*/ | ||
public function testExecuteWithNoException() | ||
{ | ||
/* Validate Success */ | ||
$this->jwtPayloadValidatorMock->expects($this->any()) | ||
->method('validate') | ||
->with(['mockResult' => 'jwtPayload']) | ||
->willReturn(true); | ||
|
||
/* Assert the result of function */ | ||
$jwtPayload = $this->model->execute('string_to_test'); | ||
$this->assertEquals( | ||
['mockResult' => 'jwtPayload'], | ||
$jwtPayload | ||
); | ||
} | ||
|
||
/** | ||
* Tests Jwt Parser execute with exception and no result. | ||
*/ | ||
public function testExecuteWithException() | ||
{ | ||
/* Validate Fail */ | ||
$this->jwtPayloadValidatorMock->expects($this->any()) | ||
->method('validate') | ||
->with(['mockResult' => 'jwtPayload']) | ||
->willReturn(false); | ||
|
||
$this->expectException(LocalizedException::class); | ||
$this->expectExceptionMessage( | ||
'Authentication Failed. Your card issuer cannot authenticate this card. ' . | ||
'Please select another card or form of payment to complete your purchase.' | ||
); | ||
|
||
/* Execute function */ | ||
$this->model->execute('string_to_test'); | ||
} | ||
} |
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
Oops, something went wrong.