Skip to content

Commit 5dc4ae1

Browse files
ENGCOM-7198: Implement ActionInterface for /version/ #27425
- Merge Pull Request #27425 from lbajsarowicz/magento2:refactor/version-index-index - Merged commits: 1. e849f39 2. a67d063
2 parents 8ec21ff + a67d063 commit 5dc4ae1

File tree

2 files changed

+44
-65
lines changed

2 files changed

+44
-65
lines changed

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
declare(strict_types=1);
87

98
namespace Magento\Version\Controller\Index;
109

11-
use Magento\Framework\App\Action\Action;
12-
use Magento\Framework\App\Action\Context;
13-
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
10+
use Magento\Framework\App\Action\HttpGetActionInterface;
1411
use Magento\Framework\App\ProductMetadataInterface;
12+
use Magento\Framework\Controller\Result\RawFactory as RawResponseFactory;
1513

1614
/**
17-
* Magento Version controller
15+
* Magento Version controller: Sets the response body to ProductName/Major.MinorVersion (Edition).
1816
*/
19-
class Index extends Action implements HttpGetActionInterface
17+
class Index implements HttpGetActionInterface
2018
{
2119
const DEV_PREFIX = 'dev-';
2220

2321
/**
2422
* @var ProductMetadataInterface
2523
*/
26-
protected $productMetadata;
24+
private $productMetadata;
2725

2826
/**
29-
* @param Context $context
27+
* @var RawResponseFactory
28+
*/
29+
private $rawFactory;
30+
31+
/**
32+
* @param RawResponseFactory $rawFactory
3033
* @param ProductMetadataInterface $productMetadata
3134
*/
32-
public function __construct(Context $context, ProductMetadataInterface $productMetadata)
35+
public function __construct(RawResponseFactory $rawFactory, ProductMetadataInterface $productMetadata)
3336
{
37+
$this->rawFactory = $rawFactory;
3438
$this->productMetadata = $productMetadata;
35-
parent::__construct($context);
3639
}
3740

3841
/**
39-
* Sets the response body to ProductName/Major.MinorVersion (Edition).
40-
*
41-
* @return void
42+
* @inheritDoc
4243
*/
43-
public function execute(): void
44+
public function execute()
4445
{
46+
$rawResponse = $this->rawFactory->create();
47+
4548
$version = $this->productMetadata->getVersion();
4649
$versionParts = explode('.', $version);
47-
if ($this->isGitBasedInstallation($version) || !$this->isCorrectVersion($versionParts)) {
48-
return;
50+
if (!$this->isGitBasedInstallation($version) && $this->isCorrectVersion($versionParts)) {
51+
$rawResponse->setContents(
52+
$this->productMetadata->getName() . '/' .
53+
$this->getMajorMinorVersion($versionParts) .
54+
' (' . $this->productMetadata->getEdition() . ')'
55+
);
4956
}
5057

51-
$this->getResponse()->setBody(
52-
$this->productMetadata->getName() . '/' .
53-
$this->getMajorMinorVersion($versionParts) .
54-
' (' . $this->productMetadata->getEdition() . ')'
55-
);
58+
return $rawResponse;
5659
}
5760

5861
/**

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

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,43 @@
77

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

10-
use Magento\Framework\App\Action\Context;
1110
use Magento\Framework\App\ProductMetadataInterface;
1211
use Magento\Framework\App\ResponseInterface;
13-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\Framework\Controller\Result\Raw;
13+
use Magento\Framework\Controller\Result\RawFactory;
1414
use Magento\Version\Controller\Index\Index as VersionIndex;
15+
use PHPUnit\Framework\MockObject\MockObject;
1516
use PHPUnit\Framework\TestCase;
1617

1718
class IndexTest extends TestCase
1819
{
19-
/**
20-
* @var VersionIndex
21-
*/
22-
private $model;
23-
24-
/**
25-
* @var Context
26-
*/
27-
private $contextMock;
20+
/** @var VersionIndex */
21+
private $versionController;
2822

29-
/**
30-
* @var ProductMetadataInterface
31-
*/
23+
/** @var MockObject|ProductMetadataInterface */
3224
private $productMetadataMock;
3325

34-
/**
35-
* @var ResponseInterface
36-
*/
37-
private $responseMock;
26+
/** @var MockObject|RawFactory */
27+
private $rawResponseFactoryMock;
28+
29+
/** @var MockObject|Raw */
30+
private $rawResponseMock;
3831

3932
/**
4033
* Prepare test preconditions
4134
*/
4235
protected function setUp()
4336
{
44-
$this->contextMock = $this->getMockBuilder(Context::class)
45-
->disableOriginalConstructor()
46-
->getMock();
47-
4837
$this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class)
4938
->disableOriginalConstructor()
5039
->setMethods(['getName', 'getEdition', 'getVersion'])
5140
->getMock();
5241

53-
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
54-
->disableOriginalConstructor()
55-
->setMethods(['setBody', 'sendResponse'])
56-
->getMock();
57-
58-
$this->contextMock->expects($this->any())
59-
->method('getResponse')
60-
->willReturn($this->responseMock);
61-
62-
$objectManager = new ObjectManager($this);
42+
$this->rawResponseFactoryMock = $this->createPartialMock(RawFactory::class, ['create']);
43+
$this->rawResponseMock = $this->createPartialMock(Raw::class, ['setContents']);
44+
$this->rawResponseFactoryMock->method('create')->willReturn($this->rawResponseMock);
6345

64-
$this->model = $objectManager->getObject(
65-
VersionIndex::class,
66-
[
67-
'context' => $this->contextMock,
68-
'productMetadata' => $this->productMetadataMock
69-
]
70-
);
46+
$this->versionController = new VersionIndex($this->rawResponseFactoryMock, $this->productMetadataMock);
7147
}
7248

7349
/**
@@ -79,10 +55,10 @@ public function testGitBasedInstallationDoesNotReturnVersion(): void
7955
->method('getVersion')
8056
->willReturn('dev-2.3');
8157

82-
$this->responseMock->expects($this->never())
83-
->method('setBody');
58+
$this->rawResponseMock->expects($this->never())
59+
->method('setContents');
8460

85-
$this->assertNull($this->model->execute());
61+
$this->versionController->execute();
8662
}
8763

8864
/**
@@ -94,10 +70,10 @@ public function testCommunityVersionDisplaysMajorMinorVersionAndEditionName(): v
9470
$this->productMetadataMock->expects($this->any())->method('getEdition')->willReturn('Community');
9571
$this->productMetadataMock->expects($this->any())->method('getName')->willReturn('Magento');
9672

97-
$this->responseMock->expects($this->once())->method('setBody')
73+
$this->rawResponseMock->expects($this->once())->method('setContents')
9874
->with('Magento/2.3 (Community)')
9975
->will($this->returnSelf());
10076

101-
$this->model->execute();
77+
$this->versionController->execute();
10278
}
10379
}

0 commit comments

Comments
 (0)