|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ZFTest\Apigility\Documentation; |
| 4 | + |
| 5 | +use Zend\Mvc\MvcEvent; |
| 6 | +use Zend\View\Helper\BasePath; |
| 7 | +use Zend\View\Helper\ServerUrl; |
| 8 | +use Zend\View\Model\ModelInterface; |
| 9 | +use ZF\Apigility\Documentation\ApiFactory; |
| 10 | +use ZF\Apigility\Documentation\Controller; |
| 11 | + |
| 12 | +class ControllerTest extends \PHPUnit_Framework_TestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var MvcEvent |
| 16 | + */ |
| 17 | + private $event; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var ServerUrl |
| 21 | + */ |
| 22 | + private $serverUrl; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var BasePath |
| 26 | + */ |
| 27 | + private $basePath; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var ApiFactory|\PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + private $apiFactory; |
| 33 | + |
| 34 | + protected function setUp() |
| 35 | + { |
| 36 | + $this->apiFactory = $this->getMockBuilder(ApiFactory::class)->disableOriginalConstructor()->getMock(); |
| 37 | + $this->serverUrl = new ServerUrl(); |
| 38 | + $this->basePath = new BasePath(); |
| 39 | + |
| 40 | + $this->event = new MvcEvent(); |
| 41 | + |
| 42 | + if (class_exists('Zend\Router\RouteMatch', true)) { |
| 43 | + $this->event->setRouteMatch(new \Zend\Router\RouteMatch([])); |
| 44 | + } elseif (class_exists('Zend\Mvc\Router\RouteMatch', true)) { |
| 45 | + $this->event->setRouteMatch(new \Zend\Mvc\Router\RouteMatch([])); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public function testViewModelMissingBasePath() |
| 50 | + { |
| 51 | + $this->serverUrl->setScheme('https'); |
| 52 | + $this->serverUrl->setHost('localhost'); |
| 53 | + $this->basePath->setBasePath('/controller_test'); |
| 54 | + |
| 55 | + $sut = new Controller($this->apiFactory, $this->serverUrl); |
| 56 | + $sut->setEvent($this->event); |
| 57 | + |
| 58 | + /** @var ModelInterface $viewModel */ |
| 59 | + $viewModel = $sut->showAction(); |
| 60 | + self::assertInstanceOf(ModelInterface::class, $viewModel); |
| 61 | + |
| 62 | + self::assertEquals('https://localhost', $viewModel->getVariable('baseUrl')); |
| 63 | + } |
| 64 | + |
| 65 | + public function testSetBaseUrlIntoViewModel() |
| 66 | + { |
| 67 | + $this->serverUrl->setScheme('https'); |
| 68 | + $this->serverUrl->setHost('localhost'); |
| 69 | + $this->basePath->setBasePath('/controller_test'); |
| 70 | + |
| 71 | + $sut = new Controller($this->apiFactory, $this->serverUrl, $this->basePath); |
| 72 | + $sut->setEvent($this->event); |
| 73 | + |
| 74 | + /** @var ModelInterface $viewModel */ |
| 75 | + $viewModel = $sut->showAction(); |
| 76 | + self::assertInstanceOf(ModelInterface::class, $viewModel); |
| 77 | + |
| 78 | + self::assertEquals('https://localhost/controller_test', $viewModel->getVariable('baseUrl')); |
| 79 | + } |
| 80 | +} |
0 commit comments