|
| 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\MagentoCloud\Test\Unit\Step\Build; |
| 9 | + |
| 10 | +use Magento\MagentoCloud\App\Error; |
| 11 | +use Magento\MagentoCloud\Config\GlobalSection; |
| 12 | +use Magento\MagentoCloud\Config\StageConfigInterface; |
| 13 | +use Magento\MagentoCloud\Shell\MagentoShell; |
| 14 | +use Magento\MagentoCloud\Shell\ShellException; |
| 15 | +use Magento\MagentoCloud\Shell\ShellFactory; |
| 16 | +use Magento\MagentoCloud\Step\Build\EnableWebhooks; |
| 17 | +use Magento\MagentoCloud\Step\StepException; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Psr\Log\LoggerInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * @inheritDoc |
| 24 | + */ |
| 25 | +class EnableWebhooksTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var EnableWebhooks |
| 29 | + */ |
| 30 | + private $step; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var LoggerInterface|MockObject |
| 34 | + */ |
| 35 | + private $loggerMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var MagentoShell|MockObject |
| 39 | + */ |
| 40 | + private $magentoShellMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var GlobalSection|MockObject |
| 44 | + */ |
| 45 | + private $globalConfigMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritdoc |
| 49 | + */ |
| 50 | + protected function setUp(): void |
| 51 | + { |
| 52 | + $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); |
| 53 | + $this->magentoShellMock = $this->createMock(MagentoShell::class); |
| 54 | + /** @var ShellFactory|MockObject $shellFactoryMock */ |
| 55 | + $shellFactoryMock = $this->createMock(ShellFactory::class); |
| 56 | + $shellFactoryMock->expects($this->once()) |
| 57 | + ->method('createMagento') |
| 58 | + ->willReturn($this->magentoShellMock); |
| 59 | + $this->globalConfigMock = $this->createMock(GlobalSection::class); |
| 60 | + |
| 61 | + $this->step = new EnableWebhooks( |
| 62 | + $this->loggerMock, |
| 63 | + $shellFactoryMock, |
| 64 | + $this->globalConfigMock |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return void |
| 70 | + * @throws StepException |
| 71 | + */ |
| 72 | + public function testExecuteWebhooksNotEnabled() |
| 73 | + { |
| 74 | + $this->globalConfigMock->expects(self::once()) |
| 75 | + ->method('get') |
| 76 | + ->with(StageConfigInterface::VAR_ENABLE_WEBHOOKS) |
| 77 | + ->willReturn(false); |
| 78 | + |
| 79 | + $this->magentoShellMock->expects(self::never()) |
| 80 | + ->method('execute'); |
| 81 | + $this->loggerMock->expects(self::never()) |
| 82 | + ->method('notice'); |
| 83 | + |
| 84 | + $this->step->execute(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return void |
| 89 | + * @throws StepException |
| 90 | + */ |
| 91 | + public function testExecuteGenerateCommandFailed() |
| 92 | + { |
| 93 | + $this->expectException(StepException::class); |
| 94 | + $this->expectExceptionMessage('error during module generation'); |
| 95 | + $this->expectExceptionCode(Error::GLOBAL_WEBHOOKS_MODULE_GENERATE_FAILED); |
| 96 | + |
| 97 | + $this->globalConfigMock->expects(self::once()) |
| 98 | + ->method('get') |
| 99 | + ->with(StageConfigInterface::VAR_ENABLE_WEBHOOKS) |
| 100 | + ->willReturn(true); |
| 101 | + $this->magentoShellMock->expects(self::once()) |
| 102 | + ->method('execute') |
| 103 | + ->with('webhooks:generate:module') |
| 104 | + ->willThrowException(new ShellException('error during module generation')); |
| 105 | + $this->loggerMock->expects(self::once()) |
| 106 | + ->method('notice'); |
| 107 | + $this->loggerMock->expects(self::once()) |
| 108 | + ->method('error'); |
| 109 | + |
| 110 | + $this->step->execute(); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return void |
| 115 | + * @throws StepException |
| 116 | + */ |
| 117 | + public function testExecuteEnableModuleCommandFailed() |
| 118 | + { |
| 119 | + $this->expectException(StepException::class); |
| 120 | + $this->expectExceptionMessage('error during module enablement'); |
| 121 | + $this->expectExceptionCode(Error::GLOBAL_WEBHOOKS_MODULE_ENABLEMENT_FAILED); |
| 122 | + |
| 123 | + $this->globalConfigMock->expects(self::once()) |
| 124 | + ->method('get') |
| 125 | + ->with(StageConfigInterface::VAR_ENABLE_WEBHOOKS) |
| 126 | + ->willReturn(true); |
| 127 | + $this->magentoShellMock->expects(self::at(0)) |
| 128 | + ->method('execute') |
| 129 | + ->with('webhooks:generate:module'); |
| 130 | + $this->magentoShellMock->expects(self::at(1)) |
| 131 | + ->method('execute') |
| 132 | + ->with('module:enable Magento_AdobeCommerceWebhookPlugins') |
| 133 | + ->willThrowException(new ShellException('error during module enablement')); |
| 134 | + $this->loggerMock->expects(self::exactly(2)) |
| 135 | + ->method('notice'); |
| 136 | + $this->loggerMock->expects(self::once()) |
| 137 | + ->method('error'); |
| 138 | + |
| 139 | + $this->step->execute(); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @return void |
| 144 | + * @throws StepException |
| 145 | + */ |
| 146 | + public function testExecuteSuccess() |
| 147 | + { |
| 148 | + $this->globalConfigMock->expects(self::once()) |
| 149 | + ->method('get') |
| 150 | + ->with(StageConfigInterface::VAR_ENABLE_WEBHOOKS) |
| 151 | + ->willReturn(true); |
| 152 | + $this->magentoShellMock->expects(self::at(0)) |
| 153 | + ->method('execute') |
| 154 | + ->with('webhooks:generate:module'); |
| 155 | + $this->magentoShellMock->expects(self::at(1)) |
| 156 | + ->method('execute') |
| 157 | + ->with('module:enable Magento_AdobeCommerceWebhookPlugins'); |
| 158 | + $this->loggerMock->expects(self::exactly(2)) |
| 159 | + ->method('notice'); |
| 160 | + $this->loggerMock->expects(self::never()) |
| 161 | + ->method('error'); |
| 162 | + |
| 163 | + $this->step->execute(); |
| 164 | + } |
| 165 | +} |
0 commit comments