Skip to content

Commit 669865f

Browse files
ENGCOM-6505: Refactor Magento Version module (+ Unit Tests) #26128
2 parents 416ebed + 294095c commit 669865f

File tree

4 files changed

+70
-42
lines changed

4 files changed

+70
-42
lines changed

app/code/Magento/Version/Controller/Index/Index.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
declare(strict_types=1);
8+
79
namespace Magento\Version\Controller\Index;
810

9-
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
1011
use Magento\Framework\App\Action\Action;
1112
use Magento\Framework\App\Action\Context;
13+
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
1214
use Magento\Framework\App\ProductMetadataInterface;
1315

1416
/**
1517
* Magento Version controller
1618
*/
1719
class Index extends Action implements HttpGetActionInterface
1820
{
21+
const DEV_PREFIX = 'dev-';
22+
1923
/**
2024
* @var ProductMetadataInterface
2125
*/
@@ -32,25 +36,22 @@ public function __construct(Context $context, ProductMetadataInterface $productM
3236
}
3337

3438
/**
35-
* Sets the response body to ProductName/Major.MinorVersion (Edition). E.g.: Magento/0.42 (Community). Omits patch
36-
* version from response
39+
* Sets the response body to ProductName/Major.MinorVersion (Edition).
3740
*
3841
* @return void
3942
*/
40-
public function execute()
43+
public function execute(): void
4144
{
4245
$version = $this->productMetadata->getVersion();
4346
$versionParts = explode('.', $version);
44-
if ((!isset($versionParts[0]) || !isset($versionParts[1]))
45-
|| $this->isGitBasedInstallation($version)
46-
) {
47+
if ($this->isGitBasedInstallation($version) || !$this->isCorrectVersion($versionParts)) {
4748
return;
4849
}
49-
$majorMinorVersion = $versionParts[0] . '.' . $versionParts[1];
50+
5051
$this->getResponse()->setBody(
5152
$this->productMetadata->getName() . '/' .
52-
$majorMinorVersion . ' (' .
53-
$this->productMetadata->getEdition() . ')'
53+
$this->getMajorMinorVersion($versionParts) .
54+
' (' . $this->productMetadata->getEdition() . ')'
5455
);
5556
}
5657

@@ -60,9 +61,30 @@ public function execute()
6061
* @param string $fullVersion
6162
* @return bool
6263
*/
63-
private function isGitBasedInstallation($fullVersion)
64+
private function isGitBasedInstallation($fullVersion): bool
65+
{
66+
return 0 === strpos($fullVersion, self::DEV_PREFIX);
67+
}
68+
69+
/**
70+
* Verifies if the Magento version is correct
71+
*
72+
* @param array $versionParts
73+
* @return bool
74+
*/
75+
private function isCorrectVersion(array $versionParts): bool
76+
{
77+
return isset($versionParts[0]) && isset($versionParts[1]);
78+
}
79+
80+
/**
81+
* Returns string only with Major and Minor version number
82+
*
83+
* @param array $versionParts
84+
* @return string
85+
*/
86+
private function getMajorMinorVersion(array $versionParts): string
6487
{
65-
$versionParts = explode('-', $fullVersion);
66-
return (isset($versionParts[0]) && $versionParts[0] == 'dev');
88+
return $versionParts[0] . '.' . $versionParts[1];
6789
}
6890
}

app/code/Magento/Version/Test/Unit/Controller/Index/IndexTest.php

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77

88
namespace Magento\Version\Test\Unit\Controller\Index;
99

10-
use Magento\Version\Controller\Index\Index as VersionIndex;
1110
use Magento\Framework\App\Action\Context;
1211
use Magento\Framework\App\ProductMetadataInterface;
1312
use Magento\Framework\App\ResponseInterface;
1413
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\Version\Controller\Index\Index as VersionIndex;
15+
use PHPUnit\Framework\TestCase;
1516

16-
/**
17-
* Class \Magento\Version\Test\Unit\Controller\Index\IndexTest
18-
*/
19-
class IndexTest extends \PHPUnit\Framework\TestCase
17+
class IndexTest extends TestCase
2018
{
2119
/**
2220
* @var VersionIndex
@@ -26,72 +24,80 @@ class IndexTest extends \PHPUnit\Framework\TestCase
2624
/**
2725
* @var Context
2826
*/
29-
private $context;
27+
private $contextMock;
3028

3129
/**
3230
* @var ProductMetadataInterface
3331
*/
34-
private $productMetadata;
32+
private $productMetadataMock;
3533

3634
/**
3735
* @var ResponseInterface
3836
*/
39-
private $response;
37+
private $responseMock;
4038

4139
/**
4240
* Prepare test preconditions
4341
*/
4442
protected function setUp()
4543
{
46-
$this->context = $this->getMockBuilder(Context::class)
44+
$this->contextMock = $this->getMockBuilder(Context::class)
4745
->disableOriginalConstructor()
4846
->getMock();
4947

50-
$this->productMetadata = $this->getMockBuilder(ProductMetadataInterface::class)
48+
$this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class)
5149
->disableOriginalConstructor()
5250
->setMethods(['getName', 'getEdition', 'getVersion'])
5351
->getMock();
5452

55-
$this->response = $this->getMockBuilder(ResponseInterface::class)
53+
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
5654
->disableOriginalConstructor()
5755
->setMethods(['setBody', 'sendResponse'])
5856
->getMock();
5957

60-
$this->context->expects($this->any())
58+
$this->contextMock->expects($this->any())
6159
->method('getResponse')
62-
->willReturn($this->response);
60+
->willReturn($this->responseMock);
6361

64-
$helper = new ObjectManager($this);
62+
$objectManager = new ObjectManager($this);
6563

66-
$this->model = $helper->getObject(
67-
'Magento\Version\Controller\Index\Index',
64+
$this->model = $objectManager->getObject(
65+
VersionIndex::class,
6866
[
69-
'context' => $this->context,
70-
'productMetadata' => $this->productMetadata
67+
'context' => $this->contextMock,
68+
'productMetadata' => $this->productMetadataMock
7169
]
7270
);
7371
}
7472

7573
/**
76-
* Test with Git Base version
74+
* Git Base version does not return information about version
7775
*/
78-
public function testExecuteWithGitBase()
76+
public function testGitBasedInstallationDoesNotReturnVersion(): void
7977
{
80-
$this->productMetadata->expects($this->any())->method('getVersion')->willReturn('dev-2.3');
78+
$this->productMetadataMock->expects($this->any())
79+
->method('getVersion')
80+
->willReturn('dev-2.3');
81+
82+
$this->responseMock->expects($this->never())
83+
->method('setBody');
84+
8185
$this->assertNull($this->model->execute());
8286
}
8387

8488
/**
85-
* Test with Community Version
89+
* Magento Community returns information about major and minor version of product
8690
*/
87-
public function testExecuteWithCommunityVersion()
91+
public function testCommunityVersionDisplaysMajorMinorVersionAndEditionName(): void
8892
{
89-
$this->productMetadata->expects($this->any())->method('getVersion')->willReturn('2.3.3');
90-
$this->productMetadata->expects($this->any())->method('getEdition')->willReturn('Community');
91-
$this->productMetadata->expects($this->any())->method('getName')->willReturn('Magento');
92-
$this->response->expects($this->once())->method('setBody')
93+
$this->productMetadataMock->expects($this->any())->method('getVersion')->willReturn('2.3.3');
94+
$this->productMetadataMock->expects($this->any())->method('getEdition')->willReturn('Community');
95+
$this->productMetadataMock->expects($this->any())->method('getName')->willReturn('Magento');
96+
97+
$this->responseMock->expects($this->once())->method('setBody')
9398
->with('Magento/2.3 (Community)')
9499
->will($this->returnSelf());
100+
95101
$this->model->execute();
96102
}
97103
}

app/code/Magento/Version/etc/frontend/routes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
99
<router id="standard">
1010
<route id="magento_version" frontName="magento_version">
11-
<module name="Magento_Version" />
11+
<module name="Magento_Version"/>
1212
</route>
1313
</router>
1414
</config>

app/code/Magento/Version/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_Version" >
9+
<module name="Magento_Version">
1010
</module>
1111
</config>

0 commit comments

Comments
 (0)