Skip to content

Commit dd5fb66

Browse files
committed
Changes to Magento Version module and Unit Tests
1 parent 9400c1e commit dd5fb66

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-33
lines changed

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
78
namespace Magento\Version\Controller\Index;
89

910
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
@@ -16,6 +17,8 @@
1617
*/
1718
class Index extends Action implements HttpGetActionInterface
1819
{
20+
const DEV_PREFIX = 'dev-';
21+
1922
/**
2023
* @var ProductMetadataInterface
2124
*/
@@ -41,16 +44,14 @@ public function execute()
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

@@ -62,7 +63,26 @@ public function execute()
6263
*/
6364
private function isGitBasedInstallation($fullVersion)
6465
{
65-
$versionParts = explode('-', $fullVersion);
66-
return (isset($versionParts[0]) && $versionParts[0] == 'dev');
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+
* @param array $versionParts
82+
* @return string
83+
*/
84+
private function getMajorMinorVersion(array $versionParts): string
85+
{
86+
return $versionParts[0] . '.' . $versionParts[1];
6787
}
6888
}

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

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
use Magento\Framework\App\ProductMetadataInterface;
1313
use Magento\Framework\App\ResponseInterface;
1414
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
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(
64+
$this->model = $objectManager->getObject(
6765
'Magento\Version\Controller\Index\Index',
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()
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()
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
}

0 commit comments

Comments
 (0)