Skip to content

Commit

Permalink
Merge branch '2.4-develop' of github.com:magento-gl/magento2ce into A…
Browse files Browse the repository at this point in the history
…CQE-6168
  • Loading branch information
Keerthana81187 committed Mar 21, 2024
2 parents 45bd79c + a98a64a commit b2e6217
Show file tree
Hide file tree
Showing 1,340 changed files with 28,639 additions and 19,557 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private function markUserNotified(): ResultInterface
public function execute()
{
$this->enableAdminUsage();
$this->markUserNotified();
return $this->markUserNotified();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\AdminAnalytics\Test\Unit\Controller\Adminhtml\Config;

use Magento\AdminAnalytics\Controller\Adminhtml\Config\EnableAdminUsage;
use Magento\AdminAnalytics\Model\ResourceModel\Viewer\Logger as NotificationLogger;
use Magento\Config\Model\Config;
use Magento\Config\Model\Config\Factory as ConfigFactory;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Controller\Result\Json as JsonResult;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @covers \Magento\AdminAnalytics\Controller\Adminhtml\Config\EnableAdminUsage
*/
class EnableAdminUsageTest extends \PHPUnit\Framework\TestCase
{
private const STUB_PRODUCT_VERSION = 'Product Version';

/** @var EnableAdminUsage */
private $controller;

/** @var MockObject|Config */
private $configMock;

/** @var MockObject|ProductMetadataInterface */
private $productMetadataMock;

/** @var MockObject|NotificationLogger */
private $notificationLoggerMock;

/** @var MockObject|ResultFactory */
private $resultFactoryMock;

/** @var JsonResult|MockObject */
private $resultMock;

protected function setUp(): void
{
$objectManager = new ObjectManager($this);

$this->configMock = $this->getMockBuilder(Config::class)
->disableOriginalConstructor()
->onlyMethods(['setDataByPath', 'save'])
->getMock();

$configFactory = $this->getMockBuilder(ConfigFactory::class)
->disableOriginalConstructor()
->onlyMethods(['create'])
->getMock();

$configFactory->method('create')
->willReturn($this->configMock);

$this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class)
->onlyMethods(['getVersion'])
->getMockForAbstractClass();

$this->productMetadataMock->method('getVersion')
->willReturn(self::STUB_PRODUCT_VERSION);

$this->notificationLoggerMock = $this->getMockBuilder(NotificationLogger::class)
->disableOriginalConstructor()
->onlyMethods(['log'])
->getMock();

$this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
->disableOriginalConstructor()
->onlyMethods(['create'])
->getMock();

$this->resultMock = $this->getMockBuilder(JsonResult::class)
->disableOriginalConstructor()
->onlyMethods(['setData'])
->getMock();

$this->resultFactoryMock->method('create')
->with(ResultFactory::TYPE_JSON)
->willReturn($this->resultMock);

$this->controller = $objectManager->getObject(EnableAdminUsage::class, [
'configFactory' => $configFactory,
'productMetadata' => $this->productMetadataMock,
'notificationLogger' => $this->notificationLoggerMock,
'resultFactory' => $this->resultFactoryMock
]);
}

/**
* If Controller returns `null`, no data is passed to the browser
*/
public function testResponseAfterAdminUsageChange()
{
// Given
$this->resultMock->method('setData')->willReturnSelf();

// When
$response = $this->controller->execute();

// Then
$this->assertInstanceOf(ResultInterface::class, $response);
}

public function testResponseWhenExceptionThrown()
{
$this->markTestSkipped('magento/magento2#31393 Lack of exception handling');

$this->configMock->method('setDataByPath')
->willThrowException(
new \Exception('System Exception')
);

// When
$response = $this->controller->execute();

// Then
$this->assertInstanceOf(ResultInterface::class, $response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testShouldRenderSeverity() : void
/** @var Column|MockObject $columnMock */
$columnMock = $this->getMockBuilder(Column::class)
->disableOriginalConstructor()
->setMethods(['getIndex'])
->addMethods(['getIndex'])
->getMock();
$columnMock->expects($this->exactly(5))->method('getIndex')->willReturn('index');
$this->sut->setColumn($columnMock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use Magento\AdminNotification\Block\ToolbarEntry;
use Magento\AdminNotification\Model\ResourceModel\Inbox\Collection\Unread;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Framework\Json\Helper\Data as JsonHelper;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\TestCase;

Expand All @@ -26,6 +28,17 @@ class ToolbarEntryTest extends TestCase
protected function _getBlockInstance($unreadNotifications)
{
$objectManagerHelper = new ObjectManager($this);
$objects = [
[
JsonHelper::class,
$this->createMock(JsonHelper::class)
],
[
DirectoryHelper::class,
$this->createMock(DirectoryHelper::class)
]
];
$objectManagerHelper->prepareObjectManager($objects);
// mock collection of unread notifications
$notificationList = $this->createPartialMock(
Unread::class,
Expand All @@ -52,6 +65,18 @@ public function testGetLatestUnreadNotifications()
{
$helper = new ObjectManager($this);

$objects = [
[
JsonHelper::class,
$this->createMock(JsonHelper::class)
],
[
DirectoryHelper::class,
$this->createMock(DirectoryHelper::class)
]
];
$helper->prepareObjectManager($objects);

// 1. Create mocks
$notificationList = $this->createMock(Unread::class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,15 @@ protected function setUp(): void
'initWebsites',
'initCategories'
];
$mockAddMethods = [
'_headerColumns'
];
$mockMethods = array_merge($constructorMethods, [
'_customHeadersMapping',
'_prepareEntityCollection',
'_getEntityCollection',
'getWriter',
'getExportData',
'_headerColumns',
'_customFieldsMapping',
'getItemsPerPage',
'paginateCollection',
Expand All @@ -243,7 +245,8 @@ protected function setUp(): void
$this->advancedPricing = $this->getMockBuilder(
AdvancedPricing::class
)
->setMethods($mockMethods)
->addMethods($mockAddMethods)
->onlyMethods($mockMethods)
->disableOriginalConstructor()
->getMock();
foreach ($constructorMethods as $method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ public function testCreate(): void
$clientOptionsMock = $this->getMockForAbstractClass(ClientOptionsInterface::class);

$this->objectManager->expects($this->exactly(2))->method('create')
->withConsecutive(
[$this->equalTo('engineFactoryClass')],
[$this->equalTo('engineOptionClass')]
)
->willReturnOnConsecutiveCalls(
$factoryMock,
$clientOptionsMock
);
->willReturnCallback(function ($className) use ($factoryMock, $clientOptionsMock) {
if ($className == 'engineFactoryClass') {
return $factoryMock;
} elseif ($className == 'engineOptionClass') {
return $clientOptionsMock;
}
});

$clientOptionsMock->expects($this->once())->method('prepareClientOptions')
->with([])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ protected function setUp(): void
$this->objectManager = new ObjectManager($this);
$this->connectionValidatorMock = $this->getMockBuilder(ConnectionValidator::class)
->disableOriginalConstructor()
->setMethods([])
->getMock();

$this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
->disableOriginalConstructor()
->setMethods([])
->onlyMethods([])
->getMock();

$this->model = $this->objectManager->getObject(
Expand Down Expand Up @@ -171,7 +170,7 @@ public function testValidateNoOptions()
/**
* @return array
*/
public function getCreateConfigDataProvider()
public static function getCreateConfigDataProvider()
{
return [
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ protected function setUp(): void
$this->subscriptionStatusProviderMock = $this->createMock(SubscriptionStatusProvider::class);
$this->contextMock = $this->createMock(Context::class);
$this->abstractElementMock = $this->getMockBuilder(AbstractElement::class)
->setMethods(['getComment', 'getElementHtml'])
->addMethods(['getComment'])
->onlyMethods(['getElementHtml'])
->disableOriginalConstructor()
->getMock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class VerticalTest extends TestCase
protected function setUp(): void
{
$this->abstractElementMock = $this->getMockBuilder(AbstractElement::class)
->setMethods(['getComment', 'getLabel', 'getHint', 'getElementHtml'])
->addMethods(['getComment', 'getLabel', 'getHint'])
->onlyMethods(['getElementHtml'])
->disableOriginalConstructor()
->getMock();

Expand Down
12 changes: 6 additions & 6 deletions app/code/Magento/Analytics/Test/Unit/Cron/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ private function addFinalOutputAsserts(bool $isExecuted = true)
$this->flagManagerMock
->expects($this->exactly(2 * $isExecuted))
->method('deleteFlag')
->withConsecutive(
[SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE],
[SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE]
);
->willReturnCallback(fn($param) => match ([$param]) {
[SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE] => $this->flagManagerMock,
[SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE] => $this->flagManagerMock
});
$this->configWriterMock
->expects($this->exactly((int)$isExecuted))
->method('delete')
Expand Down Expand Up @@ -143,7 +143,7 @@ public function testExecuteWithEmptyReverseCounter($counterData)
*
* @return array
*/
public function executeWithEmptyReverseCounterDataProvider()
public static function executeWithEmptyReverseCounterDataProvider()
{
return [
[null],
Expand Down Expand Up @@ -189,7 +189,7 @@ public function testExecuteRegularScenario(
/**
* @return array
*/
public function executeRegularScenarioDataProvider()
public static function executeRegularScenarioDataProvider()
{
return [
'The last attempt with command execution result False' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,15 @@ public function testTokenAndPreviousBaseUrlExist()
$this->flagManagerMock
->expects($this->once())
->method('saveFlag')
->withConsecutive(
[SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE, $this->attemptsInitValue],
[SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, $url]
);
->willReturnCallback(function ($arg1, $arg2) use ($url) {
if ($arg1 == SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE
&& $arg2 == $this->attemptsInitValue) {
return true;
} elseif ($arg1 == SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE && $arg2 == $url) {
return true;
}
});

$this->configWriterMock
->expects($this->once())
->method('save')
Expand Down Expand Up @@ -160,10 +165,14 @@ public function testTokenExistAndWithoutPreviousBaseUrl()
$this->flagManagerMock
->expects($this->exactly(2))
->method('saveFlag')
->withConsecutive(
[SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, $url],
[SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE, $this->attemptsInitValue]
);
->willReturnCallback(function ($arg1, $arg2) use ($url) {
if ($arg1 == SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE
&& $arg2 == $this->attemptsInitValue) {
return true;
} elseif ($arg1 == SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE && $arg2 == $url) {
return true;
}
});
$this->configWriterMock
->expects($this->once())
->method('save')
Expand Down
Loading

0 comments on commit b2e6217

Please sign in to comment.