Skip to content

Commit 208630c

Browse files
committed
Merge branch 'feature/62' into develop
Close #62
2 parents 26cf4e0 + fac7c94 commit 208630c

File tree

6 files changed

+138
-19
lines changed

6 files changed

+138
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,11 @@ All notable changes to this project will be documented in this file, in reverse
88

99
- Nothing.
1010

11-
### Deprecated
12-
13-
- Nothing.
14-
15-
### Removed
16-
17-
- Nothing.
11+
### Changed
1812

19-
### Fixed
20-
21-
- Nothing.
22-
23-
## 1.2.4 - TBD
24-
25-
### Added
26-
27-
- Nothing.
13+
- [#62](https://github.com/zfcampus/zf-apigility-documentation/pull/62) updates the `ZF\Apigility\Documentation\Controller` to accept a `BasePath`
14+
view helper as a construction aregument; this is then used to prefix any generated links with
15+
the currently detected/configured base path to the application.
2816

2917
### Deprecated
3018

phpunit.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44
<directory>./test</directory>
55
</testsuite>
66
</testsuites>
7+
<filter>
8+
<whitelist>
9+
<directory>src</directory>
10+
</whitelist>
11+
</filter>
712
</phpunit>

src/Controller.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace ZF\Apigility\Documentation;
88

99
use Zend\Mvc\Controller\AbstractActionController;
10+
use Zend\View\Helper\BasePath;
1011
use Zend\View\Helper\ServerUrl;
1112
use ZF\ContentNegotiation\ViewModel;
1213

@@ -22,14 +23,21 @@ class Controller extends AbstractActionController
2223
*/
2324
protected $serverUrlViewHelper;
2425

26+
/**
27+
* @var BasePath|null
28+
*/
29+
private $basePath;
30+
2531
/**
2632
* @param ApiFactory $apiFactory
2733
* @param ServerUrl $serverUrlViewHelper
34+
* @param BasePath $basePath
2835
*/
29-
public function __construct(ApiFactory $apiFactory, ServerUrl $serverUrlViewHelper)
36+
public function __construct(ApiFactory $apiFactory, ServerUrl $serverUrlViewHelper, BasePath $basePath = null)
3037
{
3138
$this->apiFactory = $apiFactory;
3239
$this->serverUrlViewHelper = $serverUrlViewHelper;
40+
$this->basePath = $basePath;
3341
}
3442

3543
/**
@@ -42,13 +50,15 @@ public function __construct(ApiFactory $apiFactory, ServerUrl $serverUrlViewHelp
4250
*/
4351
public function showAction()
4452
{
53+
$basePath = $this->basePath ? $this->basePath->__invoke() : null;
54+
4555
$apiName = $this->normalizeApiName($this->params()->fromRoute('api'));
4656
$apiVersion = $this->params()->fromRoute('version', '1');
4757
$serviceName = $this->params()->fromRoute('service');
4858

4959
$viewModel = new ViewModel();
5060
$viewModel->setTemplate('zf-apigility-documentation/show');
51-
$viewModel->setVariable('baseUrl', $this->serverUrlViewHelper->__invoke());
61+
$viewModel->setVariable('baseUrl', $this->serverUrlViewHelper->__invoke($basePath));
5262

5363
if (!$apiName) {
5464
$apiList = $this->apiFactory->createApiList();

src/ControllerFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ class ControllerFactory implements FactoryInterface
2121
*/
2222
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
2323
{
24+
$viewHelpers = $container->get('ViewHelperManager');
25+
2426
return new Controller(
2527
$container->get(ApiFactory::class),
26-
$container->get('ViewHelperManager')->get('ServerUrl')
28+
$viewHelpers->get('ServerUrl'),
29+
$viewHelpers->get('BasePath')
2730
);
2831
}
2932

test/ControllerFactoryTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace ZFTest\Apigility\Documentation;
4+
5+
use Zend\ServiceManager\ServiceManager;
6+
use Zend\View\Helper\BasePath;
7+
use Zend\View\Helper\ServerUrl;
8+
use ZF\Apigility\Documentation\ApiFactory;
9+
use ZF\Apigility\Documentation\Controller;
10+
use ZF\Apigility\Documentation\ControllerFactory;
11+
12+
class ControllerFactoryTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function testCreateController()
15+
{
16+
$apiFactory = $this->getMockBuilder(ApiFactory::class)->disableOriginalConstructor()->getMock();
17+
18+
$viewHelpers = new ServiceManager();
19+
$viewHelpers->setService('ServerUrl', new ServerUrl());
20+
$viewHelpers->setService('BasePath', new BasePath());
21+
22+
$container = new ServiceManager();
23+
$container->setService('ViewHelperManager', $viewHelpers);
24+
$container->setService(
25+
ApiFactory::class,
26+
$apiFactory
27+
);
28+
29+
$controller = (new ControllerFactory())->createService($container);
30+
31+
self::assertInstanceOf(Controller::class, $controller);
32+
}
33+
}

test/ControllerTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)