|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\NewRelicReporting\Test\Unit\Plugin; |
| 9 | + |
| 10 | +use Magento\Framework\App\State; |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 12 | +use Magento\NewRelicReporting\Model\Config as NewRelicConfig; |
| 13 | +use Magento\NewRelicReporting\Model\NewRelicWrapper; |
| 14 | +use Magento\NewRelicReporting\Plugin\StatePlugin; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject as MockObject; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test coverage for \Magento\NewRelicReporting\Plugin\StatePlugin |
| 21 | + */ |
| 22 | +class StatePluginTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + private const STUB_APP_NAME = 'app_name'; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var StatePlugin |
| 31 | + */ |
| 32 | + private $statePlugin; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var NewRelicConfig|MockObject |
| 36 | + */ |
| 37 | + private $configMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var NewRelicWrapper|MockObject |
| 41 | + */ |
| 42 | + private $newRelicWrapperMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var LoggerInterface|MockObject |
| 46 | + */ |
| 47 | + private $loggerMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var State|MockObject |
| 51 | + */ |
| 52 | + private $stateMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * Set Up |
| 56 | + */ |
| 57 | + public function setUp(): void |
| 58 | + { |
| 59 | + $objectManager = new ObjectManager($this); |
| 60 | + $this->configMock = $this->getMockBuilder(NewRelicConfig::class)->disableOriginalConstructor() |
| 61 | + ->getMock(); |
| 62 | + $this->newRelicWrapperMock = $this->createMock(NewRelicWrapper::class); |
| 63 | + $this->loggerMock = $this->createMock(LoggerInterface::class); |
| 64 | + $this->stateMock = $this->createMock(State::class); |
| 65 | + |
| 66 | + $this->statePlugin = $objectManager->getObject( |
| 67 | + StatePlugin::class, |
| 68 | + [ |
| 69 | + 'config' => $this->configMock, |
| 70 | + 'newRelicWrapper' => $this->newRelicWrapperMock, |
| 71 | + 'logger' => $this->loggerMock, |
| 72 | + ] |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Tests setting the new relic app name |
| 78 | + */ |
| 79 | + public function testSuccessfullySettingAppName(): void |
| 80 | + { |
| 81 | + $this->configMock->expects($this->once())->method('isSeparateApps')->willReturn(true); |
| 82 | + $this->configMock->expects($this->any())->method('getNewRelicAppName') |
| 83 | + ->willReturn(static::STUB_APP_NAME); |
| 84 | + $this->configMock->expects($this->once())->method('isNewRelicEnabled')->willReturn(true); |
| 85 | + $this->stateMock->expects($this->once())->method('getAreaCode')->willReturn('frontend'); |
| 86 | + $this->newRelicWrapperMock->expects($this->once())->method('setAppName'); |
| 87 | + |
| 88 | + $this->statePlugin->afterSetAreaCode($this->stateMock, static::STUB_APP_NAME); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Tests not being able to set the New Relic app name |
| 93 | + * |
| 94 | + * @param bool $isSeparateApps |
| 95 | + * @param string $newRelicAppName |
| 96 | + * @param bool $enabled |
| 97 | + * |
| 98 | + * @dataProvider newRelicConfigDataProvider |
| 99 | + */ |
| 100 | + public function testSuccessfullySettingAreaCode(bool $isSeparateApps, string $newRelicAppName, bool $enabled): void |
| 101 | + { |
| 102 | + $this->configMock->expects($this->any())->method('isSeparateApps')->willReturn($isSeparateApps); |
| 103 | + $this->configMock->expects($this->any())->method('getNewRelicAppName')->willReturn($newRelicAppName); |
| 104 | + $this->configMock->expects($this->any())->method('isNewRelicEnabled')->willReturn($enabled); |
| 105 | + $this->newRelicWrapperMock->expects($this->never())->method('setAppName'); |
| 106 | + |
| 107 | + $this->statePlugin->afterSetAreaCode($this->stateMock, static::STUB_APP_NAME); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * New relic configuration data provider |
| 112 | + * |
| 113 | + * @return array |
| 114 | + */ |
| 115 | + public function newRelicConfigDataProvider(): array |
| 116 | + { |
| 117 | + return [ |
| 118 | + 'Separate apps config is disabled' => [ |
| 119 | + false, |
| 120 | + static::STUB_APP_NAME, |
| 121 | + true |
| 122 | + ], |
| 123 | + 'Application name is not configured' => [ |
| 124 | + true, |
| 125 | + '', |
| 126 | + true |
| 127 | + ], |
| 128 | + 'New Relic is disabled' => [ |
| 129 | + true, |
| 130 | + static::STUB_APP_NAME, |
| 131 | + false |
| 132 | + ] |
| 133 | + ]; |
| 134 | + } |
| 135 | +} |
0 commit comments