|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\AdminAnalytics\Test\Unit\Controller\Adminhtml\Config; |
| 10 | + |
| 11 | +use Magento\AdminAnalytics\Controller\Adminhtml\Config\EnableAdminUsage; |
| 12 | +use Magento\AdminAnalytics\Model\ResourceModel\Viewer\Logger as NotificationLogger; |
| 13 | +use Magento\Config\Model\Config; |
| 14 | +use Magento\Config\Model\Config\Factory as ConfigFactory; |
| 15 | +use Magento\Framework\App\ProductMetadataInterface; |
| 16 | +use Magento\Framework\Controller\Result\Json as JsonResult; |
| 17 | +use Magento\Framework\Controller\ResultFactory; |
| 18 | +use Magento\Framework\Controller\ResultInterface; |
| 19 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | + |
| 22 | +/** |
| 23 | + * @covers \Magento\AdminAnalytics\Controller\Adminhtml\Config\EnableAdminUsage |
| 24 | + */ |
| 25 | +class EnableAdminUsageTest extends \PHPUnit\Framework\TestCase |
| 26 | +{ |
| 27 | + private const STUB_PRODUCT_VERSION = 'Product Version'; |
| 28 | + |
| 29 | + /** @var EnableAdminUsage */ |
| 30 | + private $controller; |
| 31 | + |
| 32 | + /** @var MockObject|Config */ |
| 33 | + private $configMock; |
| 34 | + |
| 35 | + /** @var MockObject|ProductMetadataInterface */ |
| 36 | + private $productMetadataMock; |
| 37 | + |
| 38 | + /** @var MockObject|NotificationLogger */ |
| 39 | + private $notificationLoggerMock; |
| 40 | + |
| 41 | + /** @var MockObject|ResultFactory */ |
| 42 | + private $resultFactoryMock; |
| 43 | + |
| 44 | + /** @var JsonResult|MockObject */ |
| 45 | + private $resultMock; |
| 46 | + |
| 47 | + protected function setUp(): void |
| 48 | + { |
| 49 | + $objectManager = new ObjectManager($this); |
| 50 | + |
| 51 | + $this->configMock = $this->getMockBuilder(Config::class) |
| 52 | + ->disableOriginalConstructor() |
| 53 | + ->onlyMethods(['setDataByPath', 'save']) |
| 54 | + ->getMock(); |
| 55 | + |
| 56 | + $configFactory = $this->getMockBuilder(ConfigFactory::class) |
| 57 | + ->disableOriginalConstructor() |
| 58 | + ->onlyMethods(['create']) |
| 59 | + ->getMock(); |
| 60 | + |
| 61 | + $configFactory->method('create') |
| 62 | + ->willReturn($this->configMock); |
| 63 | + |
| 64 | + $this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class) |
| 65 | + ->onlyMethods(['getVersion']) |
| 66 | + ->getMockForAbstractClass(); |
| 67 | + |
| 68 | + $this->productMetadataMock->method('getVersion') |
| 69 | + ->willReturn(self::STUB_PRODUCT_VERSION); |
| 70 | + |
| 71 | + $this->notificationLoggerMock = $this->getMockBuilder(NotificationLogger::class) |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->onlyMethods(['log']) |
| 74 | + ->getMock(); |
| 75 | + |
| 76 | + $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->onlyMethods(['create']) |
| 79 | + ->getMock(); |
| 80 | + |
| 81 | + $this->resultMock = $this->getMockBuilder(JsonResult::class) |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->onlyMethods(['setData']) |
| 84 | + ->getMock(); |
| 85 | + |
| 86 | + $this->resultFactoryMock->method('create') |
| 87 | + ->with(ResultFactory::TYPE_JSON) |
| 88 | + ->willReturn($this->resultMock); |
| 89 | + |
| 90 | + $this->controller = $objectManager->getObject(EnableAdminUsage::class, [ |
| 91 | + 'configFactory' => $configFactory, |
| 92 | + 'productMetadata' => $this->productMetadataMock, |
| 93 | + 'notificationLogger' => $this->notificationLoggerMock, |
| 94 | + 'resultFactory' => $this->resultFactoryMock |
| 95 | + ]); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * If Controller returns `null`, no data is passed to the browser |
| 100 | + */ |
| 101 | + public function testResponseAfterAdminUsageChange() |
| 102 | + { |
| 103 | + // Given |
| 104 | + $this->resultMock->method('setData')->willReturnSelf(); |
| 105 | + |
| 106 | + // When |
| 107 | + $response = $this->controller->execute(); |
| 108 | + |
| 109 | + // Then |
| 110 | + $this->assertInstanceOf(ResultInterface::class, $response); |
| 111 | + } |
| 112 | + |
| 113 | + public function testResponseWhenExceptionThrown() |
| 114 | + { |
| 115 | + $this->markTestSkipped('magento/magento2#31393 Lack of exception handling'); |
| 116 | + |
| 117 | + $this->configMock->method('setDataByPath') |
| 118 | + ->willThrowException( |
| 119 | + new \Exception('System Exception') |
| 120 | + ); |
| 121 | + |
| 122 | + // When |
| 123 | + $response = $this->controller->execute(); |
| 124 | + |
| 125 | + // Then |
| 126 | + $this->assertInstanceOf(ResultInterface::class, $response); |
| 127 | + } |
| 128 | +} |
0 commit comments