IndexTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Swagger\Test\Unit\Block;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. use Magento\Framework\View\Element\Template\Context;
  11. use Magento\Swagger\Api\Data\SchemaTypeInterface;
  12. use Magento\Swagger\Block\Index;
  13. use Magento\SwaggerWebapi\Model\SchemaType\Rest;
  14. class IndexTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var SchemaTypeInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $schemaTypeMock;
  20. /**
  21. * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $requestMock;
  24. /**
  25. * @var Index
  26. */
  27. private $index;
  28. /**
  29. * @inheritdoc
  30. */
  31. protected function setUp()
  32. {
  33. $this->requestMock = $this->getMockBuilder(RequestInterface::class)->getMock();
  34. $this->schemaTypeMock = $this->getMockBuilder(SchemaTypeInterface::class)->getMock();
  35. $this->index = (new ObjectManager($this))->getObject(
  36. Index::class,
  37. [
  38. 'context' => (new ObjectManager($this))->getObject(
  39. Context::class,
  40. [
  41. 'request' => $this->requestMock,
  42. ]
  43. ),
  44. 'data' => [
  45. 'schema_types' => [
  46. 'test' => $this->schemaTypeMock
  47. ]
  48. ]
  49. ]
  50. );
  51. }
  52. /**
  53. * Test that the passed URL parameter is used when it is a valid schema type.
  54. *
  55. * @covers \Magento\Swagger\Block\Index::getSchemaUrl()
  56. */
  57. public function testGetSchemaUrlValidType()
  58. {
  59. $this->requestMock->expects($this->atLeastOnce())
  60. ->method('getParam')
  61. ->willReturn('test');
  62. $this->schemaTypeMock->expects($this->any())
  63. ->method('getCode')->willReturn('test');
  64. $this->schemaTypeMock->expects($this->once())
  65. ->method('getSchemaUrlPath')
  66. ->willReturn('/test');
  67. $this->assertEquals('/test', $this->index->getSchemaUrl());
  68. }
  69. /**
  70. * Test that Swagger UI throws an exception if an invalid schema type is supplied.
  71. *
  72. * @covers \Magento\Swagger\Block\Index::getSchemaUrl()
  73. */
  74. public function testGetSchemaUrlInvalidType()
  75. {
  76. $this->requestMock->expects($this->atLeastOnce())
  77. ->method('getParam')
  78. ->willReturn('invalid');
  79. $this->schemaTypeMock->expects($this->any())
  80. ->method('getCode')->willReturn('test');
  81. $this->expectException(\UnexpectedValueException::class);
  82. $this->index->getSchemaUrl();
  83. }
  84. }