Skip to content

Commit ad741c8

Browse files
MCLOUD-5688: Replace usages of PHPUnit_Framework_MockObject_MockObject with MockObject (magento#713)
1 parent 422eb72 commit ad741c8

File tree

83 files changed

+1212
-713
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1212
-713
lines changed

config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<service id="Magento\MagentoCloud\Service\ServiceMismatchException" autowire="false" />
5252
<service id="Magento\MagentoCloud\Shell\Process" autowire="false" />
5353
<service id="Magento\MagentoCloud\Shell\ProcessException" autowire="false" />
54+
<service id="Magento\MagentoCloud\App\LoggerException" autowire="false" />
5455
<service id="Symfony\Component\Yaml\Parser" autowire="false" />
5556
<service id="Symfony\Component\Yaml\Dumper" autowire="false" />
5657
<!-- Service argument configuration -->

src/App/Logger.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\MagentoCloud\Filesystem\Driver\File;
1313
use Magento\MagentoCloud\App\Logger\Pool;
1414
use Magento\MagentoCloud\App\Logger\Processor\SanitizeProcessor;
15+
use Magento\MagentoCloud\Filesystem\FileSystemException;
16+
use Magento\MagentoCloud\Package\UndefinedPackageException;
1517

1618
/**
1719
* @inheritdoc
@@ -39,6 +41,8 @@ class Logger extends \Monolog\Logger
3941
* @param FileList $fileList
4042
* @param Pool $pool
4143
* @param SanitizeProcessor $sanitizeProcessor
44+
*
45+
* @throws LoggerException
4246
*/
4347
public function __construct(
4448
File $file,
@@ -59,25 +63,32 @@ public function __construct(
5963
/**
6064
* Prepares the deploy log for further use.
6165
*
62-
* @return void
66+
* @throws LoggerException
6367
*/
64-
private function prepare()
68+
private function prepare(): void
6569
{
66-
$deployLogPath = $this->fileList->getCloudLog();
67-
$buildPhaseLogPath = $this->fileList->getInitCloudLog();
68-
$deployLogFileExists = $this->file->isExists($deployLogPath);
69-
$buildLogFileExists = $this->file->isExists($buildPhaseLogPath);
70-
$buildPhaseLogContent = $buildLogFileExists ? $this->file->fileGetContents($buildPhaseLogPath) : '';
71-
72-
$this->file->createDirectory($this->directoryList->getLog());
70+
try {
71+
$deployLogPath = $this->fileList->getCloudLog();
72+
$buildPhaseLogPath = $this->fileList->getInitCloudLog();
73+
$deployLogFileExists = $this->file->isExists($deployLogPath);
74+
$buildLogFileExists = $this->file->isExists($buildPhaseLogPath);
75+
$buildPhaseLogContent = $buildLogFileExists ? $this->file->fileGetContents($buildPhaseLogPath) : '';
7376

74-
if ($deployLogFileExists
75-
&& $buildPhaseLogContent
76-
&& !$this->isBuildLogApplied($deployLogPath, $buildPhaseLogContent)
77-
) {
78-
$this->file->filePutContents($deployLogPath, $buildPhaseLogContent, FILE_APPEND);
79-
} elseif (!$deployLogFileExists && $buildLogFileExists) {
80-
$this->file->copy($buildPhaseLogPath, $deployLogPath);
77+
$this->file->createDirectory($this->directoryList->getLog());
78+
if ($deployLogFileExists
79+
&& $buildPhaseLogContent
80+
&& !$this->isBuildLogApplied($deployLogPath, $buildPhaseLogContent)
81+
) {
82+
$this->file->filePutContents($deployLogPath, $buildPhaseLogContent, FILE_APPEND);
83+
} elseif (!$deployLogFileExists && $buildLogFileExists) {
84+
$this->file->copy($buildPhaseLogPath, $deployLogPath);
85+
}
86+
} catch (FileSystemException | UndefinedPackageException $exception) {
87+
throw new LoggerException(
88+
$exception->getMessage(),
89+
$exception->getCode(),
90+
$exception
91+
);
8192
}
8293
}
8394

@@ -87,6 +98,8 @@ private function prepare()
8798
* @param string $deployLogPath deploy log path
8899
* @param string $buildPhaseLogContent build log content
89100
* @return bool
101+
*
102+
* @throws FileSystemException
90103
*/
91104
private function isBuildLogApplied(string $deployLogPath, string $buildPhaseLogContent): bool
92105
{

src/App/Logger/Gelf/HandlerFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Illuminate\Contracts\Config\Repository;
1111
use Gelf\Publisher;
12+
use Magento\MagentoCloud\App\LoggerException;
1213

1314
/**
1415
* Creates instance of Gelf handler.
@@ -34,13 +35,14 @@ public function __construct(TransportFactory $transportFactory)
3435
* @param Repository $configuration
3536
* @param int $minLevel
3637
* @return Handler
37-
* @throws \Exception
38+
* @throws LoggerException
3839
*/
3940
public function create(Repository $configuration, int $minLevel): Handler
4041
{
4142
$this->increaseSocketTimeout();
4243

4344
$publisher = new Publisher();
45+
4446
foreach ($configuration->get('transport') as $transportType => $transportConfig) {
4547
$publisher->addTransport(
4648
$this->transportFactory->create($transportType, $transportConfig)
@@ -64,7 +66,7 @@ public function create(Repository $configuration, int $minLevel): Handler
6466
*
6567
* @return void
6668
*/
67-
private function increaseSocketTimeout()
69+
private function increaseSocketTimeout(): void
6870
{
6971
ini_set('default_socket_timeout', '3600');
7072
}

src/App/Logger/Gelf/TransportFactory.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@
1111
use Gelf\Transport\HttpTransport;
1212
use Gelf\Transport\TcpTransport;
1313
use Gelf\Transport\UdpTransport;
14+
use Magento\MagentoCloud\App\LoggerException;
1415

1516
/**
1617
* Factory for creating gelf transport instances.
1718
*/
1819
class TransportFactory
1920
{
20-
const TRANSPORT_HTTP = 'http';
21-
const TRANSPORT_UDP = 'udp';
22-
const TRANSPORT_TCP = 'tcp';
21+
public const TRANSPORT_HTTP = 'http';
22+
public const TRANSPORT_UDP = 'udp';
23+
public const TRANSPORT_TCP = 'tcp';
2324

2425
/**
2526
* @param string $type
2627
* @param array $config
2728
* @return AbstractTransport
28-
* @throws \Exception
29+
*
30+
* @throws LoggerException
2931
*/
3032
public function create(string $type, array $config): AbstractTransport
3133
{
@@ -57,7 +59,7 @@ public function create(string $type, array $config): AbstractTransport
5759
);
5860
break;
5961
default:
60-
throw new \Exception('Unknown transport type: ' . $type);
62+
throw new LoggerException('Unknown transport type: ' . $type);
6163
}
6264

6365
return $transport;

src/App/Logger/Pool.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
namespace Magento\MagentoCloud\App\Logger;
99

10+
use Magento\MagentoCloud\App\LoggerException;
1011
use Monolog\Handler\HandlerInterface;
1112
use Magento\MagentoCloud\Config\Log as LogConfig;
13+
use Exception;
1214

1315
/**
1416
* The pool of handlers.
@@ -52,7 +54,7 @@ public function __construct(
5254

5355
/**
5456
* @return HandlerInterface[]
55-
* @throws \Exception If can't create handlers from config.
57+
* @throws LoggerException If can't create handlers from config.
5658
*/
5759
public function getHandlers(): array
5860
{
@@ -62,14 +64,22 @@ public function getHandlers(): array
6264

6365
$this->handlers = [];
6466

65-
foreach ($this->logConfig->getHandlers() as $handlerName => $handlerConfig) {
66-
$handler = $this->handlerFactory->create($handlerName);
67+
try {
68+
foreach ($this->logConfig->getHandlers() as $handlerName => $handlerConfig) {
69+
$handler = $this->handlerFactory->create($handlerName);
6770

68-
if (empty($handlerConfig['use_default_formatter'])) {
69-
$handler->setFormatter($this->lineFormatterFactory->create());
70-
}
71+
if (empty($handlerConfig['use_default_formatter'])) {
72+
$handler->setFormatter($this->lineFormatterFactory->create());
73+
}
7174

72-
$this->handlers[] = $handler;
75+
$this->handlers[] = $handler;
76+
}
77+
} catch (Exception $exception) {
78+
throw new LoggerException(
79+
$exception->getMessage(),
80+
$exception->getCode(),
81+
$exception
82+
);
7383
}
7484

7585
return $this->handlers;

src/App/LoggerException.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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\App;
9+
10+
/**
11+
* Generic logging exception;
12+
*/
13+
class LoggerException extends GenericException
14+
{
15+
}

src/Command/Dev/UpdateComposer/ComposerGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private function getBaseComposer(array $repoOptions): array
214214
* @param array $repoOptions
215215
* @param array $composer
216216
* @return array
217-
* @throws FilesystemException
217+
* @throws FileSystemException
218218
*
219219
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
220220
*

src/Step/Build/DeployStaticContent/Generate.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\MagentoCloud\Step\Build\DeployStaticContent;
99

10+
use Magento\MagentoCloud\Config\ConfigException;
1011
use Magento\MagentoCloud\Config\Stage\BuildInterface;
1112
use Magento\MagentoCloud\Step\StepException;
1213
use Magento\MagentoCloud\Step\StepInterface;
@@ -70,7 +71,7 @@ public function __construct(
7071
/**
7172
* @inheritdoc
7273
*/
73-
public function execute()
74+
public function execute(): void
7475
{
7576
$locales = $this->buildOption->getLocales();
7677
$threadCount = $this->buildOption->getThreadCount();
@@ -83,16 +84,16 @@ public function execute()
8384

8485
$this->logger->info($logMessage);
8586

86-
$commands = $this->commandFactory->matrix(
87-
$this->buildOption,
88-
$this->buildConfig->get(BuildInterface::VAR_SCD_MATRIX)
89-
);
90-
9187
try {
88+
$commands = $this->commandFactory->matrix(
89+
$this->buildOption,
90+
$this->buildConfig->get(BuildInterface::VAR_SCD_MATRIX)
91+
);
92+
9293
foreach ($commands as $command) {
9394
$this->shell->execute($command);
9495
}
95-
} catch (ShellException $exception) {
96+
} catch (ConfigException|ShellException $exception) {
9697
throw new StepException($exception->getMessage(), $exception->getCode(), $exception);
9798
}
9899
}

src/Test/Unit/App/Logger/Gelf/HandlerFactoryTest.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
use Gelf\Transport\HttpTransport;
1111
use Gelf\Transport\TcpTransport;
1212
use Illuminate\Config\Repository;
13-
use Magento\MagentoCloud\App\Logger\Gelf\Handler;
1413
use Magento\MagentoCloud\App\Logger\Gelf\HandlerFactory;
1514
use Magento\MagentoCloud\App\Logger\Gelf\TransportFactory;
1615
use Monolog\Logger;
16+
use PHPUnit\Framework\MockObject\MockObject;
1717
use PHPUnit\Framework\TestCase;
18-
use PHPUnit_Framework_MockObject_MockObject as Mock;
1918

2019
/**
2120
* @inheritdoc
@@ -28,12 +27,12 @@ class HandlerFactoryTest extends TestCase
2827
private $handlerFactory;
2928

3029
/**
31-
* @var TransportFactory|Mock
30+
* @var TransportFactory|MockObject
3231
*/
3332
private $transportFactoryMock;
3433

3534
/**
36-
* @var Repository|Mock
35+
* @var Repository|MockObject
3736
*/
3837
private $repositoryMock;
3938

@@ -44,17 +43,20 @@ protected function setUp()
4443
{
4544
$this->transportFactoryMock = $this->createMock(TransportFactory::class);
4645
$this->repositoryMock = $this->createMock(Repository::class);
47-
46+
4847
$this->handlerFactory = new HandlerFactory(
4948
$this->transportFactoryMock
5049
);
5150
}
5251

53-
public function testCreate()
52+
/**
53+
* @throws \Exception
54+
*/
55+
public function testCreate(): void
5456
{
5557
$httpTransportMock = $this->createMock(HttpTransport::class);
5658
$tcpTransportMock = $this->createMock(TcpTransport::class);
57-
59+
5860
$this->repositoryMock->expects($this->exactly(2))
5961
->method('get')
6062
->withConsecutive(
@@ -85,9 +87,6 @@ public function testCreate()
8587
$tcpTransportMock
8688
);
8789

88-
$this->assertInstanceOf(
89-
Handler::class,
90-
$this->handlerFactory->create($this->repositoryMock, Logger::INFO)
91-
);
90+
$this->handlerFactory->create($this->repositoryMock, Logger::INFO);
9291
}
9392
}

src/Test/Unit/App/LoggerTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,42 @@
88
namespace Magento\MagentoCloud\Test\Unit\App;
99

1010
use Magento\MagentoCloud\App\Logger;
11+
use Magento\MagentoCloud\App\LoggerException;
1112
use Magento\MagentoCloud\Filesystem\DirectoryList;
1213
use Magento\MagentoCloud\Filesystem\FileList;
1314
use Magento\MagentoCloud\Filesystem\Driver\File;
1415
use Magento\MagentoCloud\App\Logger\Pool;
16+
use PHPUnit\Framework\MockObject\MockObject;
1517
use PHPUnit\Framework\TestCase;
1618
use Magento\MagentoCloud\App\Logger\Processor\SanitizeProcessor;
17-
use PHPUnit_Framework_MockObject_MockObject as Mock;
1819

1920
/**
2021
* @inheritdoc
2122
*/
2223
class LoggerTest extends TestCase
2324
{
2425
/**
25-
* @var File|Mock
26+
* @var File|MockObject
2627
*/
2728
private $fileMock;
2829

2930
/**
30-
* @var DirectoryList|Mock
31+
* @var DirectoryList|MockObject
3132
*/
3233
private $directoryListMock;
3334

3435
/**
35-
* @var FileList|Mock
36+
* @var FileList|MockObject
3637
*/
3738
private $fileListMock;
3839

3940
/**
40-
* @var Pool|Mock
41+
* @var Pool|MockObject
4142
*/
4243
private $poolMock;
4344

4445
/**
45-
* @var SanitizeProcessor|Mock
46+
* @var SanitizeProcessor|MockObject
4647
*/
4748
private $sanitizeProcessorMock;
4849

@@ -67,6 +68,8 @@ protected function setUp()
6768
* @param int $fileMockFilePutContentsExpects
6869
* @param int $fileMockCopyExpects
6970
* @dataProvider executeDataProvider
71+
*
72+
* @throws LoggerException
7073
*/
7174
public function testExecute(
7275
$fileMockFileGetContentsExpects,
@@ -76,7 +79,7 @@ public function testExecute(
7679
$deployLogFileExists,
7780
$fileMockFilePutContentsExpects,
7881
$fileMockCopyExpects
79-
) {
82+
): void {
8083
$magentoRoot = 'magento_root';
8184
$deployLogPath = $magentoRoot . '/var/log/cloud.log';
8285
$buildPhaseLogPath = $magentoRoot . '/init/var/log/cloud.log';

0 commit comments

Comments
 (0)