Skip to content

Commit c306c2c

Browse files
author
Oleksii Korshenko
committed
MAGETWO-52571: [Github][PR] Add HttpInterface methods and add up-casts for type safety #3746
- Merge branch 'httpinterface-methods' of https://github.com/Vinai/magento2 into Vinai-httpinterface-methods
2 parents cf003bb + c930932 commit c306c2c

File tree

13 files changed

+137
-83
lines changed

13 files changed

+137
-83
lines changed

app/code/Magento/Backend/Model/View/Result/Redirect.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Backend\Model\UrlInterface;
1111
use Magento\Framework\App;
1212
use Magento\Framework\App\ActionFlag;
13+
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
1314

1415
class Redirect extends \Magento\Framework\Controller\Result\Redirect
1516
{
@@ -56,7 +57,7 @@ public function setRefererOrBaseUrl()
5657
/**
5758
* {@inheritdoc}
5859
*/
59-
protected function render(App\ResponseInterface $response)
60+
protected function render(HttpResponseInterface $response)
6061
{
6162
$this->session->setIsUrlNotice($this->actionFlag->get('', AbstractAction::FLAG_IS_URLS_CHECKED));
6263
return parent::render($response);

lib/internal/Magento/Framework/App/Response/HttpInterface.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,88 @@ interface HttpInterface extends \Magento\Framework\App\ResponseInterface
1616
* @return void
1717
*/
1818
public function setHttpResponseCode($code);
19+
20+
/**
21+
* Get HTTP response code
22+
*
23+
* @return int
24+
*/
25+
public function getHttpResponseCode();
26+
27+
/**
28+
* Set a header
29+
*
30+
* If $replace is true, replaces any headers already defined with that $name.
31+
*
32+
* @param string $name
33+
* @param string $value
34+
* @param boolean $replace
35+
* @return self
36+
*/
37+
public function setHeader($name, $value, $replace = false);
38+
39+
/**
40+
* Get header value by name
41+
*
42+
* Returns first found header by passed name.
43+
* If header with specified name was not found returns false.
44+
*
45+
* @param string $name
46+
* @return \Zend\Http\Header\HeaderInterface|bool
47+
*/
48+
public function getHeader($name);
49+
50+
/**
51+
* Remove header by name from header stack
52+
*
53+
* @param string $name
54+
* @return self
55+
*/
56+
public function clearHeader($name);
57+
58+
/**
59+
* Allow granular setting of HTTP response status code, version and phrase
60+
*
61+
* For example, a HTTP response as the following:
62+
* HTTP 200 1.1 Your response has been served
63+
* Can be set with the arguments
64+
* $httpCode = 200
65+
* $version = 1.1
66+
* $phrase = 'Your response has been served'
67+
*
68+
* @param int|string $httpCode
69+
* @param null|int|string $version
70+
* @param null|string $phrase
71+
* @return self
72+
*/
73+
public function setStatusHeader($httpCode, $version = null, $phrase = null);
74+
75+
/**
76+
* Append the given string to the response body
77+
*
78+
* @param string $value
79+
* @return self
80+
*/
81+
public function appendBody($value);
82+
83+
/**
84+
* Set the response body to the given value
85+
*
86+
* Any previously set contents will be replaced by the new content.
87+
*
88+
* @param string $value
89+
* @return self
90+
*/
91+
public function setBody($value);
92+
93+
/**
94+
* Set redirect URL
95+
*
96+
* Sets Location header and response code. Forces replacement of any prior redirects.
97+
*
98+
* @param string $url
99+
* @param int $code
100+
* @return self
101+
*/
102+
public function setRedirect($url, $code = 302);
19103
}

lib/internal/Magento/Framework/Controller/AbstractResult.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Framework\Controller;
88

99
use Magento\Framework\App\ResponseInterface;
10+
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
1011

1112
abstract class AbstractResult implements ResultInterface
1213
{
@@ -83,10 +84,10 @@ public function setStatusHeader($httpCode, $version = null, $phrase = null)
8384
}
8485

8586
/**
86-
* @param ResponseInterface $response
87+
* @param HttpResponseInterface $response
8788
* @return $this
8889
*/
89-
protected function applyHttpHeaders(ResponseInterface $response)
90+
protected function applyHttpHeaders(HttpResponseInterface $response)
9091
{
9192
if (!empty($this->httpResponseCode)) {
9293
$response->setHttpResponseCode($this->httpResponseCode);
@@ -105,17 +106,17 @@ protected function applyHttpHeaders(ResponseInterface $response)
105106
}
106107
return $this;
107108
}
108-
109+
109110
/**
110-
* @param ResponseInterface $response
111+
* @param HttpResponseInterface $response
111112
* @return $this
112113
*/
113-
abstract protected function render(ResponseInterface $response);
114+
abstract protected function render(HttpResponseInterface $response);
114115

115116
/**
116117
* Render content
117118
*
118-
* @param ResponseInterface $response
119+
* @param HttpResponseInterface|ResponseInterface $response
119120
* @return $this
120121
*/
121122
public function renderResult(ResponseInterface $response)

lib/internal/Magento/Framework/Controller/Result/Forward.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Magento\Framework\Controller\Result;
88

99
use Magento\Framework\App\RequestInterface;
10-
use Magento\Framework\App\ResponseInterface;
10+
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
1111
use Magento\Framework\Controller\AbstractResult;
1212

1313
class Forward extends AbstractResult
@@ -99,7 +99,7 @@ public function forward($action)
9999
/**
100100
* {@inheritdoc}
101101
*/
102-
protected function render(ResponseInterface $response)
102+
protected function render(HttpResponseInterface $response)
103103
{
104104
return $this;
105105
}

lib/internal/Magento/Framework/Controller/Result/Json.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Magento\Framework\Controller\Result;
88

9-
use Magento\Framework\App\ResponseInterface;
9+
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
1010
use Magento\Framework\Controller\AbstractResult;
1111
use Magento\Framework\Translate\InlineInterface;
1212

@@ -61,10 +61,11 @@ public function setJsonData($jsonData)
6161
/**
6262
* {@inheritdoc}
6363
*/
64-
protected function render(ResponseInterface $response)
64+
protected function render(HttpResponseInterface $response)
6565
{
6666
$this->translateInline->processResponseBody($this->json, true);
67-
$response->representJson($this->json);
67+
$response->setHeader('Content-Type', 'application/json', true);
68+
$response->setBody($this->json);
6869
return $this;
6970
}
7071
}

lib/internal/Magento/Framework/Controller/Result/Raw.php

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

77
namespace Magento\Framework\Controller\Result;
88

9-
use Magento\Framework\App\ResponseInterface;
9+
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
1010
use Magento\Framework\Controller\AbstractResult;
1111

1212
/**
@@ -33,7 +33,7 @@ public function setContents($contents)
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
protected function render(ResponseInterface $response)
36+
protected function render(HttpResponseInterface $response)
3737
{
3838
$response->setBody($this->contents);
3939
return $this;

lib/internal/Magento/Framework/Controller/Result/Redirect.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Framework\Controller\Result;
88

99
use Magento\Framework\App;
10+
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
1011
use Magento\Framework\Controller\AbstractResult;
1112

1213
/**
@@ -92,7 +93,7 @@ public function setPath($path, array $params = [])
9293
/**
9394
* {@inheritdoc}
9495
*/
95-
protected function render(App\ResponseInterface $response)
96+
protected function render(HttpResponseInterface $response)
9697
{
9798
$response->setRedirect($this->url);
9899
return $this;

lib/internal/Magento/Framework/Controller/Test/Unit/Result/JsonTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ public function testRenderResult()
2424
/** @var \Magento\Framework\Translate\InlineInterface|\PHPUnit_Framework_MockObject_MockObject
2525
* $translateInline
2626
*/
27-
$translateInline = $this->getMock(\Magento\Framework\Translate\InlineInterface::class, [], [], '', false);
27+
$translateInline = $this->getMock(\Magento\Framework\Translate\InlineInterface::class);
2828
$translateInline->expects($this->any())->method('processResponseBody')->with($json, true)->will(
2929
$this->returnValue($translatedJson)
3030
);
3131

32-
$response = $this->getMock(\Magento\Framework\App\Response\Http::class, ['representJson'], [], '', false);
33-
$response->expects($this->atLeastOnce())->method('representJson')->with($json)->will($this->returnSelf());
32+
$response = $this->getMock(\Magento\Framework\App\Response\HttpInterface::class);
33+
$response->expects($this->atLeastOnce())->method('setHeader')->with('Content-Type', 'application/json', true);
34+
$response->expects($this->atLeastOnce())->method('setBody')->with($json);
3435

3536
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
3637
$resultJson = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))

lib/internal/Magento/Framework/Controller/Test/Unit/Result/RawTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66

77
namespace Magento\Framework\Controller\Test\Unit\Result;
88

9+
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
910
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1011

1112
class RawTest extends \PHPUnit_Framework_TestCase
1213
{
1314
/** @var \Magento\Framework\Controller\Result\Raw */
1415
protected $raw;
1516

16-
/** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject*/
17+
/** @var HttpResponseInterface|\PHPUnit_Framework_MockObject_MockObject*/
1718
protected $response;
1819

1920
/** @var ObjectManagerHelper */
@@ -24,15 +25,13 @@ protected function setUp()
2425
$this->objectManagerHelper = new ObjectManagerHelper($this);
2526

2627
$this->response = $this->getMock(
27-
\Magento\Framework\App\ResponseInterface::class,
28-
['setBody', 'sendResponse'],
28+
HttpResponseInterface::class,
29+
[],
2930
[],
3031
'',
3132
false
3233
);
33-
$this->raw = $this->objectManagerHelper->getObject(
34-
\Magento\Framework\Controller\Result\Raw::class
35-
);
34+
$this->raw = $this->objectManagerHelper->getObject(\Magento\Framework\Controller\Result\Raw::class);
3635
}
3736

3837
public function testSetContents()

lib/internal/Magento/Framework/Controller/Test/Unit/Result/RedirectTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Framework\Controller\Test\Unit\Result;
88

9+
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
910
use \Magento\Framework\Controller\Result\Redirect;
1011

1112
class RedirectTest extends \PHPUnit_Framework_TestCase
@@ -22,7 +23,7 @@ class RedirectTest extends \PHPUnit_Framework_TestCase
2223
/** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
2324
protected $urlInterface;
2425

25-
/** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
26+
/** @var HttpResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
2627
protected $response;
2728

2829
protected function setUp()
@@ -49,8 +50,8 @@ protected function setUp()
4950
false
5051
);
5152
$this->response = $this->getMock(
52-
\Magento\Framework\App\ResponseInterface::class,
53-
['setRedirect', 'sendResponse'],
53+
HttpResponseInterface::class,
54+
[],
5455
[],
5556
'',
5657
false

0 commit comments

Comments
 (0)