Skip to content

Commit

Permalink
MAGETWO-32340: [GITHUB] Add getParams and setParams to App\RequestInt…
Browse files Browse the repository at this point in the history
…erface magento#865 magento#1053

- Remove RequestInterface::getParam from obsolete_methods since it still exists
- Move getCookie to PhpEnvironment\Request
  • Loading branch information
otoolec committed Mar 1, 2015
1 parent 969d2ff commit 77e4220
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 200 deletions.
22 changes: 2 additions & 20 deletions app/code/Magento/Webapi/Controller/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ class Request extends HttpRequest implements RequestInterface
/** @var int */
protected $_consumerId = 0;

/**
* @var CookieReaderInterface
*/
protected $_cookieReader;

/**
* Modify pathInfo: strip down the front name and query parameters.
*
Expand All @@ -32,13 +27,12 @@ class Request extends HttpRequest implements RequestInterface
* @param null|string|\Zend_Uri $uri
*/
public function __construct(
CookieReaderInterface $cookieReader,
AreaList $areaList,
ScopeInterface $configScope,
CookieReaderInterface $cookieReader,
$uri = null
) {
parent::__construct($uri);
$this->_cookieReader = $cookieReader;
parent::__construct($cookieReader, $uri);

$pathInfo = $this->getRequestUri();
/** Remove base url and area from path */
Expand All @@ -48,16 +42,4 @@ public function __construct(
$pathInfo = preg_replace('#\?.*#', '', $pathInfo);
$this->setPathInfo($pathInfo);
}

/**
* Retrieve a value from a cookie.
*
* @param string|null $name
* @param string|null $default The default value to return if no value could be found for the given $name.
* @return string|null
*/
public function getCookie($name = null, $default = null)
{
return $this->_cookieReader->getCookie($name, $default);
}
}
6 changes: 3 additions & 3 deletions app/code/Magento/Webapi/Controller/Rest/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ class Request extends \Magento\Webapi\Controller\Request
/**
* Initialize dependencies
*
* @param \Magento\Framework\Stdlib\Cookie\CookieReaderInterface $cookieReader
* @param \Magento\Framework\App\AreaList $areaList
* @param \Magento\Framework\Config\ScopeInterface $configScope
* @param \Magento\Framework\Stdlib\Cookie\CookieReaderInterface $cookieReader
* @param \Magento\Webapi\Controller\Rest\Request\Deserializer\Factory $deserializerFactory
* @param null|string $uri
*/
public function __construct(
\Magento\Framework\Stdlib\Cookie\CookieReaderInterface $cookieReader,
\Magento\Framework\App\AreaList $areaList,
\Magento\Framework\Config\ScopeInterface $configScope,
\Magento\Framework\Stdlib\Cookie\CookieReaderInterface $cookieReader,
\Magento\Webapi\Controller\Rest\Request\Deserializer\Factory $deserializerFactory,
$uri = null
) {
parent::__construct($areaList, $configScope, $cookieReader, $uri);
parent::__construct($cookieReader, $areaList, $configScope, $uri);
$this->_deserializerFactory = $deserializerFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class RequestTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->_model = new \Magento\TestFramework\Request(
$this->getMock('Magento\Framework\App\Route\ConfigInterface\Proxy', [], [], '', false),
$this->getMock('Magento\Framework\App\Request\PathInfoProcessorInterface', [], [], '', false),
$this->getMock('Magento\Framework\Stdlib\Cookie\CookieReaderInterface'),
$this->getMock('Magento\Framework\App\Route\ConfigInterface\Proxy', [], [], '', false),
$this->getMock('Magento\Framework\App\Request\PathInfoProcessorInterface'),
$this->getMock('Magento\Framework\ObjectManagerInterface')
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ protected function setUp()
{
$this->messageManager = $this->getMock('\Magento\Framework\Message\Manager', [], [], '', false);
$request = new \Magento\TestFramework\Request(
$this->getMock('Magento\Framework\App\Route\ConfigInterface\Proxy', [], [], '', false),
$this->getMock('Magento\Framework\App\Request\PathInfoProcessorInterface', [], [], '', false),
$this->getMock('Magento\Framework\Stdlib\Cookie\CookieReaderInterface'),
$this->getMock('Magento\Framework\App\Route\ConfigInterface\Proxy', [], [], '', false),
$this->getMock('Magento\Framework\App\Request\PathInfoProcessorInterface'),
$this->getMock('Magento\Framework\ObjectManagerInterface')
);
$response = new \Magento\TestFramework\Response(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2084,5 +2084,4 @@
['getFiles', 'Magento\Framework\App\Request\Http'],
['getAlias', 'Magento\Framework\App\Request\Http', 'Magento\Framework\HTTP\PhpEnvironment\Request'],
['setAlias', 'Magento\Framework\App\Request\Http', 'Magento\Framework\HTTP\PhpEnvironment\Request'],
['getParam', 'Magento\Framework\App\RequestInterface'],
];
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ public function testMatch(
) {
$this->_model = $this->_prepareMocksForTestMatch($request, $isVde, $isLoggedIn, $routers);
$this->assertEquals($matchedValue, $this->_model->match($request));
if ($isVde && $isLoggedIn) {
$this->assertEquals(self::TEST_PATH, $request->getPathInfo());
}
}

/**
Expand All @@ -79,30 +76,26 @@ public function testMatch(
*/
public function matchDataProvider()
{
$objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface');
$this->_cookieReaderMock = $this->getMock('Magento\Framework\Stdlib\Cookie\CookieReaderInterface');
$uri = self::TEST_HOST . '/' . self::VDE_FRONT_NAME . self::TEST_PATH;
$notVdeUrl = self::TEST_HOST . self::TEST_PATH;
$vdePath = '/' . self::VDE_FRONT_NAME . self::TEST_PATH;
$notVdePath = self::TEST_PATH;

$excludedRouters = ['admin' => 'admin router', 'vde' => 'vde router'];

$routerListMock = $this->getMock(
'\Magento\Framework\App\Route\ConfigInterface\Proxy',
[],
[],
'',
false
);

$infoProcessorMock = $this->getMock('Magento\Framework\App\Request\PathInfoProcessorInterface');
$infoProcessorMock->expects($this->any())->method('process')->will($this->returnArgument(1));

// test data to verify routers match logic
$matchedRequest = $this->getMock(
'Magento\Framework\App\Request\Http',
['_isFrontArea'],
[$routerListMock, $infoProcessorMock, $this->_cookieReaderMock, $objectManagerMock, $uri]
);
$notVdeRequest = $this->getMockBuilder('Magento\Framework\App\Request\Http')
->disableOriginalConstructor()->getMock();
$notVdeRequest->expects($this->any())
->method('getPathInfo')
->willReturn($vdePath);
$vdeRequest = $this->getMockBuilder('Magento\Framework\App\Request\Http')
->disableOriginalConstructor()->getMock();
$vdeRequest->expects($this->any())
->method('getPathInfo')
->willReturn($notVdePath);

$matchedController = $this->getMockForAbstractClass(
'Magento\Framework\App\Action\AbstractAction',
Expand All @@ -118,13 +111,13 @@ public function matchDataProvider()
)->method(
'match'
)->with(
$matchedRequest
$notVdeRequest
)->will(
$this->returnValue($matchedController)
);
$matchedRouter->expects($this->once())
->method('match')
->with($matchedRequest)
->with($notVdeRequest)
->will($this->returnValue($matchedController));

// method "match" will not be invoked for this router because controller will be found by first router
Expand All @@ -138,39 +131,23 @@ public function matchDataProvider()

$routers = [
'not vde request' => [
'$request' => $this->getMock(
'Magento\Framework\App\Request\Http', ['_isFrontArea'], [
$routerListMock,
$infoProcessorMock,
$this->_cookieReaderMock,
$objectManagerMock,
$notVdeUrl
]
),
'$request' => $vdeRequest,
'$isVde' => false,
'$isLoggedIn' => true,
],
'not logged as admin' => [
'$request' => $this->getMock(
'Magento\Framework\App\Request\Http',
['_isFrontArea'],
[$routerListMock, $infoProcessorMock, $this->_cookieReaderMock, $objectManagerMock, $uri]
),
'$request' => $notVdeRequest,
'$isVde' => true,
'$isLoggedIn' => false,
],
'no matched routers' => [
'$request' => $this->getMock(
'Magento\Framework\App\Request\Http',
['_isFrontArea'],
[$routerListMock, $infoProcessorMock, $this->_cookieReaderMock, $objectManagerMock, $uri]
),
'$request' => $notVdeRequest,
'$isVde' => true,
'$isLoggedIn' => true,
'$routers' => $excludedRouters
],
'matched routers' => [
'$request' => $matchedRequest,
'$request' => $notVdeRequest,
'$isVde' => true,
'$isLoggedIn' => true,
'$routers' => $matchedRouters,
Expand Down
66 changes: 12 additions & 54 deletions dev/tests/unit/testsuite/Magento/Framework/App/Request/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class HttpTest extends \PHPUnit_Framework_TestCase
*/
protected $_infoProcessorMock;

/**
* @var \Magento\Framework\Stdlib\Cookie\CookieReaderInterface | \PHPUnit_Framework_MockObject_MockObject
*/
protected $cookieReaderMock;

/**
* @var \Magento\TestFramework\Helper\ObjectManager | \PHPUnit_Framework_MockObject_MockObject
*/
Expand All @@ -55,7 +50,6 @@ protected function setUp()
);
$this->_infoProcessorMock = $this->getMock('Magento\Framework\App\Request\PathInfoProcessorInterface');
$this->_infoProcessorMock->expects($this->any())->method('process')->will($this->returnArgument(1));
$this->cookieReaderMock = $this->getMock('Magento\Framework\Stdlib\Cookie\CookieReaderInterface');
$this->objectManager = $this->getMock('Magento\Framework\ObjectManagerInterface');

// Stash the $_SERVER array to protect it from modification in test
Expand All @@ -67,14 +61,20 @@ public function tearDown()
$_SERVER = $this->serverArray;
}

/**
* @return \Magento\Framework\App\Request\Http
*/
private function getModel($uri = null)
{
return new \Magento\Framework\App\Request\Http(
$this->_routerListMock,
$this->_infoProcessorMock,
$this->cookieReaderMock,
$this->objectManager,
$uri
$testFrameworkObjectManager = new \Magento\TestFramework\Helper\ObjectManager($this);
return $testFrameworkObjectManager->getObject(
'Magento\Framework\App\Request\Http',
[
'routeConfig' => $this->_routerListMock,
'pathInfoProcessor' => $this->_infoProcessorMock,
'objectManager' => $this->objectManager,
'uri' => $uri,
]
);
}

Expand Down Expand Up @@ -255,48 +255,6 @@ public function getDistroBaseUrlPathDataProvider()
];
}

public function testGetCookieDefault()
{
$key = "cookieName";
$default = "defaultValue";

$this->cookieReaderMock
->expects($this->once())
->method('getCookie')
->with($key, $default)
->will($this->returnValue($default));

$this->assertEquals($default, $this->getModel()->getCookie($key, $default));
}

public function testGetCookieNameExists()
{
$key = "cookieName";
$default = "defaultValue";
$value = "cookieValue";

$this->cookieReaderMock
->expects($this->once())
->method('getCookie')
->with($key, $default)
->will($this->returnValue($value));

$this->assertEquals($value, $this->getModel()->getCookie($key, $default));
}

public function testGetCookieNullName()
{
$nullKey = null;
$default = "defaultValue";

$this->cookieReaderMock
->expects($this->once())
->method('getCookie')
->with($nullKey, $default)
->will($this->returnValue($default));

$this->assertEquals($default, $this->getModel()->getCookie($nullKey, $default));
}

public function serverVariablesProvider()
{
Expand Down
Loading

0 comments on commit 77e4220

Please sign in to comment.