1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Webapi\Test\Unit\Controller\Rest;
class RouterTest extends \PHPUnit\Framework\TestCase
{
/** @var \Magento\Webapi\Controller\Rest\Router\Route */
protected $_routeMock;
/** @var \Magento\Framework\Webapi\Rest\Request */
protected $_request;
/** @var \Magento\Webapi\Model\Rest\Config */
protected $_apiConfigMock;
/** @var \Magento\Webapi\Controller\Rest\Router */
protected $_router;
protected function setUp()
{
/** Prepare mocks for SUT constructor. */
$this->_apiConfigMock = $this->getMockBuilder(
\Magento\Webapi\Model\Rest\Config::class
)->disableOriginalConstructor()->getMock();
$this->_routeMock = $this->getMockBuilder(
\Magento\Webapi\Controller\Rest\Router\Route::class
)->disableOriginalConstructor()->setMethods(
['match']
)->getMock();
$areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
$areaListMock->expects($this->once())
->method('getFrontName')
->will($this->returnValue('rest'));
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->_request = $objectManager->getObject(
\Magento\Framework\Webapi\Rest\Request::class,
[
'areaList' => $areaListMock,
]
);
/** Initialize SUT. */
$this->_router = $objectManager->getObject(
\Magento\Webapi\Controller\Rest\Router::class,
[
'apiConfig' => $this->_apiConfigMock
]
);
}
protected function tearDown()
{
unset($this->_routeMock);
unset($this->_request);
unset($this->_apiConfigMock);
unset($this->_router);
parent::tearDown();
}
public function testMatch()
{
$this->_apiConfigMock->expects(
$this->once()
)->method(
'getRestRoutes'
)->will(
$this->returnValue([$this->_routeMock])
);
$this->_routeMock->expects(
$this->once()
)->method(
'match'
)->with(
$this->_request
)->will(
$this->returnValue([])
);
$matchedRoute = $this->_router->match($this->_request);
$this->assertEquals($this->_routeMock, $matchedRoute);
}
/**
* @expectedException \Magento\Framework\Webapi\Exception
*/
public function testNotMatch()
{
$this->_apiConfigMock->expects(
$this->once()
)->method(
'getRestRoutes'
)->will(
$this->returnValue([$this->_routeMock])
);
$this->_routeMock->expects(
$this->once()
)->method(
'match'
)->with(
$this->_request
)->will(
$this->returnValue(false)
);
$this->_router->match($this->_request);
}
}