RouterTest.php 3.0 KB

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