Skip to content

Commit

Permalink
ENGCOM-7243: PhpUnit 8 Migration - AdminNotification #27521
Browse files Browse the repository at this point in the history
 - Merge Pull Request #27521 from ihor-sviziev/magento2:phpunit-7-part2
 - Merged commits:
   1. fa5dadc
   2. 0b2cf65
   3. dbbd826
   4. 7e29a89
  • Loading branch information
magento-engcom-team committed Apr 2, 2020
2 parents def8a07 + 7e29a89 commit 3bb1b22
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Magento\Framework\Escaper;
use Magento\Framework\Url\Helper\Data;
use Magento\Framework\UrlInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ActionsTest extends TestCase
Expand All @@ -32,20 +33,20 @@ protected function setUp() : void
{
parent::setUp();

/** @var Escaper | \PHPUnit_Framework_MockObject_MockObject $escaperMock */
/** @var Escaper|MockObject $escaperMock */
$escaperMock = $this->getMockBuilder(Escaper::class)->disableOriginalConstructor()->getMock();
$escaperMock->expects($this->once())->method('escapeUrl')->willReturn('https://magento.com');

/** @var UrlInterface | \PHPUnit_Framework_MockObject_MockObject $urlBuilder */
/** @var UrlInterface|MockObject $urlBuilder */
$urlBuilder = $this->getMockBuilder(UrlInterface::class)->getMock();
$urlBuilder->expects($this->once())->method('getUrl')->willReturn('http://magento.com');

/** @var Context | \PHPUnit_Framework_MockObject_MockObject $contextMock */
/** @var Context|MockObject $contextMock */
$contextMock = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
$contextMock->expects($this->once())->method('getEscaper')->willReturn($escaperMock);
$contextMock->expects($this->once())->method('getUrlBuilder')->willReturn($urlBuilder);

/** @var Data | \PHPUnit_Framework_MockObject_MockObject $urlHelperMock */
/** @var Data|MockObject $urlHelperMock */
$urlHelperMock = $this->getMockBuilder(Data::class)->disableOriginalConstructor()->getMock();
$urlHelperMock->expects($this->once())->method('getEncodedUrl')->willReturn('http://magento.com');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\Framework\DataObject;
use Magento\Framework\Escaper;
use Magento\Backend\Block\Context;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class NoticeTest extends TestCase
Expand All @@ -30,11 +31,11 @@ protected function setUp() : void
{
parent::setUp();

/** @var Escaper | \PHPUnit_Framework_MockObject_MockObject $escaperMock */
/** @var Escaper|MockObject $escaperMock */
$escaperMock = $this->getMockBuilder(Escaper::class)->disableOriginalConstructor()->getMock();
$escaperMock->expects($this->exactly(2))->method('escapeHtml')->willReturn('<div>Some random html</div>');

/** @var Context | \PHPUnit_Framework_MockObject_MockObject $contextMock */
/** @var Context|MockObject $contextMock */
$contextMock = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
$contextMock->expects($this->once())->method('getEscaper')->willReturn($escaperMock);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Backend\Block\Widget\Grid\Column;
use Magento\Framework\DataObject;
use Magento\Framework\Escaper;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class SeverityTest extends TestCase
Expand All @@ -32,18 +33,18 @@ protected function setUp() : void
{
parent::setUp();

/** @var Inbox |\PHPUnit_Framework_MockObject_MockObject $inboxMock */
/** @var Inbox|MockObject $inboxMock */
$inboxMock = $this->getMockBuilder(Inbox::class)->disableOriginalConstructor()->getMock();

/** @var Context | \PHPUnit_Framework_MockObject_MockObject $contextMock */
/** @var Context|MockObject $contextMock */
$contextMock = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();

$this->sut = new Severity($contextMock, $inboxMock);
}

public function testShouldRenderSeverity() : void
{
/** @var Column | \PHPUnit_Framework_MockObject_MockObject $columnMock */
/** @var Column|MockObject $columnMock */
$columnMock = $this->getMockBuilder(Column::class)
->disableOriginalConstructor()
->setMethods(['getIndex'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@
*/
namespace Magento\AdminNotification\Test\Unit\Block;

class ToolbarEntryTest extends \PHPUnit\Framework\TestCase
use Magento\AdminNotification\Block\ToolbarEntry;
use Magento\AdminNotification\Model\ResourceModel\Inbox\Collection\Unread;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\TestCase;

class ToolbarEntryTest extends TestCase
{
/**
* Retrieve toolbar entry block instance
*
* @param int $unreadNotifications number of unread notifications
* @return \Magento\AdminNotification\Block\ToolbarEntry
* @return ToolbarEntry
*/
protected function _getBlockInstance($unreadNotifications)
{
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$objectManagerHelper = new ObjectManager($this);
// mock collection of unread notifications
$notificationList = $this->createPartialMock(
\Magento\AdminNotification\Model\ResourceModel\Inbox\Collection\Unread::class,
Unread::class,
['getSize', 'setCurPage', 'setPageSize']
);
$notificationList->expects($this->any())->method('getSize')->will($this->returnValue($unreadNotifications));

$block = $objectManagerHelper->getObject(
\Magento\AdminNotification\Block\ToolbarEntry::class,
ToolbarEntry::class,
['notificationList' => $notificationList]
);

Expand All @@ -44,25 +49,23 @@ public function testGetUnreadNotificationCount()

public function testGetLatestUnreadNotifications()
{
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$helper = new ObjectManager($this);

// 1. Create mocks
$notificationList = $this->getMockBuilder(
\Magento\AdminNotification\Model\ResourceModel\Inbox\Collection\Unread::class
)
$notificationList = $this->getMockBuilder(Unread::class)
->disableOriginalConstructor()
->getMock();

/** @var \Magento\AdminNotification\Block\ToolbarEntry $model */
/** @var ToolbarEntry $model */
$model = $helper->getObject(
\Magento\AdminNotification\Block\ToolbarEntry::class,
ToolbarEntry::class,
['notificationList' => $notificationList]
);

// 2. Set expectations
$notificationList->expects($this->atLeastOnce())
->method('setPageSize')
->with(\Magento\AdminNotification\Block\ToolbarEntry::NOTIFICATIONS_NUMBER)
->with(ToolbarEntry::NOTIFICATIONS_NUMBER)
->will($this->returnSelf());

// 3. Run tested method
Expand Down
61 changes: 37 additions & 24 deletions app/code/Magento/AdminNotification/Test/Unit/Model/FeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,87 @@

namespace Magento\AdminNotification\Test\Unit\Model;

use Magento\AdminNotification\Model\Feed;
use Magento\AdminNotification\Model\Inbox;
use Magento\AdminNotification\Model\InboxFactory;
use Magento\Backend\App\ConfigInterface;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\ProductMetadata;
use Magento\Framework\App\State;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Framework\HTTP\Adapter\Curl;
use Magento\Framework\HTTP\Adapter\CurlFactory;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Framework\UrlInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class FeedTest extends \PHPUnit\Framework\TestCase
class FeedTest extends TestCase
{
/** @var \Magento\AdminNotification\Model\Feed */
/** @var Feed */
protected $feed;

/** @var ObjectManagerHelper */
protected $objectManagerHelper;

/** @var \Magento\AdminNotification\Model\InboxFactory|\PHPUnit_Framework_MockObject_MockObject */
/** @var InboxFactory|MockObject */
protected $inboxFactory;

/** @var \Magento\AdminNotification\Model\Inbox|\PHPUnit_Framework_MockObject_MockObject */
/** @var Inbox|MockObject */
protected $inboxModel;

/** @var \Magento\Framework\HTTP\Adapter\CurlFactory|\PHPUnit_Framework_MockObject_MockObject */
/** @var CurlFactory|MockObject */
protected $curlFactory;

/** @var \Magento\Framework\HTTP\Adapter\Curl|\PHPUnit_Framework_MockObject_MockObject */
/** @var Curl|MockObject */
protected $curl;

/** @var \Magento\Backend\App\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
/** @var ConfigInterface|MockObject */
protected $backendConfig;

/** @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */
/** @var CacheInterface|MockObject */
protected $cacheManager;

/** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */
/** @var State|MockObject */
protected $appState;

/** @var \Magento\Framework\App\DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject */
/** @var DeploymentConfig|MockObject */
protected $deploymentConfig;

/** @var \Magento\Framework\App\ProductMetadata|\PHPUnit_Framework_MockObject_MockObject */
/** @var ProductMetadata|MockObject */
protected $productMetadata;

/** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
/** @var UrlInterface|MockObject */
protected $urlBuilder;

protected function setUp()
protected function setUp(): void
{
$this->inboxFactory = $this->createPartialMock(
\Magento\AdminNotification\Model\InboxFactory::class,
InboxFactory::class,
['create']
);
$this->curlFactory = $this->createPartialMock(\Magento\Framework\HTTP\Adapter\CurlFactory::class, ['create']);
$this->curl = $this->getMockBuilder(\Magento\Framework\HTTP\Adapter\Curl::class)
$this->curlFactory = $this->createPartialMock(CurlFactory::class, ['create']);
$this->curl = $this->getMockBuilder(Curl::class)
->disableOriginalConstructor()->getMock();
$this->appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['getInstallDate']);
$this->inboxModel = $this->createPartialMock(\Magento\AdminNotification\Model\Inbox::class, [
$this->appState = $this->createPartialMock(State::class, []);
$this->inboxModel = $this->createPartialMock(Inbox::class, [
'__wakeup',
'parse'
]);
$this->backendConfig = $this->createPartialMock(
\Magento\Backend\App\ConfigInterface::class,
ConfigInterface::class,
[
'getValue',
'setValue',
'isSetFlag'
]
);
$this->cacheManager = $this->createPartialMock(
\Magento\Framework\App\CacheInterface::class,
CacheInterface::class,
[
'load',
'getFrontend',
Expand All @@ -83,18 +96,18 @@ protected function setUp()
]
);

$this->deploymentConfig = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
$this->deploymentConfig = $this->getMockBuilder(DeploymentConfig::class)
->disableOriginalConstructor()->getMock();

$this->objectManagerHelper = new ObjectManagerHelper($this);

$this->productMetadata = $this->getMockBuilder(\Magento\Framework\App\ProductMetadata::class)
$this->productMetadata = $this->getMockBuilder(ProductMetadata::class)
->disableOriginalConstructor()->getMock();

$this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
$this->urlBuilder = $this->createMock(UrlInterface::class);

$this->feed = $this->objectManagerHelper->getObject(
\Magento\AdminNotification\Model\Feed::class,
Feed::class,
[
'backendConfig' => $this->backendConfig,
'cacheManager' => $this->cacheManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,46 @@
*/
namespace Magento\AdminNotification\Test\Unit\Model;

class NotificationServiceTest extends \PHPUnit\Framework\TestCase
use Magento\AdminNotification\Model\Inbox;
use Magento\AdminNotification\Model\InboxFactory;
use Magento\AdminNotification\Model\NotificationService;
use Magento\Framework\Exception\LocalizedException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class NotificationServiceTest extends TestCase
{
/**
* Retrieve instance of notification service model
*
* @param $notificationId
* @return \Magento\AdminNotification\Model\NotificationService
* @return NotificationService
*/
protected function _getServiceInstanceForMarkAsReadTest($notificationId)
{
/**
* @var
* $notificationFactory \PHPUnit_Framework_MockObject_MockObject|\Magento\AdminNotification\Model\InboxFactory
* @var $notificationFactory MockObject|InboxFactory
*/
$notificationFactory = $this->createPartialMock(
\Magento\AdminNotification\Model\InboxFactory::class,
InboxFactory::class,
['create']
);
$notification = $this->createPartialMock(
\Magento\AdminNotification\Model\Inbox::class,
['load', 'getId', 'save', 'setIsRead', '__sleep', '__wakeup']
Inbox::class,
['load', 'getId', 'save', 'setData', '__sleep', '__wakeup']
);
$notification->expects($this->once())->method('load')->with($notificationId)->will($this->returnSelf());
$notification->expects($this->once())->method('getId')->will($this->returnValue($notificationId));

// when notification Id is valid, add additional expectations
if ($notificationId) {
$notification->expects($this->once())->method('save')->will($this->returnSelf());
$notification->expects($this->once())->method('setIsRead')->with(1)->will($this->returnSelf());
$notification->expects($this->once())->method('setData')
->with('is_read', 1)->will($this->returnSelf());
}

$notificationFactory->expects($this->once())->method('create')->will($this->returnValue($notification));
return new \Magento\AdminNotification\Model\NotificationService($notificationFactory);
return new NotificationService($notificationFactory);
}

public function testMarkAsRead()
Expand All @@ -51,12 +58,11 @@ public function testMarkAsRead()
$service->markAsRead($notificationId);
}

/**
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage Wrong notification ID specified.
*/
public function testMarkAsReadThrowsExceptionWhenNotificationIdIsInvalid()
{
$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('Wrong notification ID specified.');

$notificationId = null;
$service = $this->_getServiceInstanceForMarkAsReadTest($notificationId);
$service->markAsRead($notificationId);
Expand Down
Loading

0 comments on commit 3bb1b22

Please sign in to comment.