Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Fix #69. Compatibility of legacy SM2 style in PHP 7.2.
Browse files Browse the repository at this point in the history
No need to dynamically request service name, when its known that Navigation::class is one ultimately being requested in __invoke
  • Loading branch information
alextech committed Feb 21, 2018
1 parent 4bf25c9 commit 40b222a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Service/AbstractNavigationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
* @param null|string $requestedName
* @return Navigation
*/
public function createService(ServiceLocatorInterface $container, $name = null, $requestedName = null)
public function createService(ServiceLocatorInterface $container)
{
$requestedName = $requestedName ?: Navigation::class;
return $this($container, $requestedName);
return $this($container, Navigation::class);
}

/**
Expand Down
87 changes: 87 additions & 0 deletions test/Service/AbstractNavigationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@

use PHPUnit_Framework_TestCase as TestCase;
use ReflectionMethod;
use Zend\Mvc\Application;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router as MvcRouter;
use Zend\Navigation\Exception;
use Zend\Navigation\Navigation;
use Zend\Navigation\Service\AbstractNavigationFactory;
use Zend\Router;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceManager;

/**
* @todo Write tests covering full functionality. Tests were introduced to
Expand Down Expand Up @@ -70,4 +76,85 @@ public function testCanInjectComponentsUsingZendMvcRouterClasses()

$this->assertSame([], $pages);
}

public function testCanCreateNavigationInstanceV2()
{
$serviceManager = $this->getServiceManager();

$navigationFactory
= $this->getMockForAbstractClass(AbstractNavigationFactory::class);
$navigationFactory->expects($this->any())
->method('getName')
->willReturn('testStubNavigation');
$navigation = $navigationFactory->createService($serviceManager);

$this->assertInstanceOf(Navigation::class, $navigation);
}

public function getRouterClass()
{
return class_exists(MvcRouter\Http\TreeRouteStack::class)
? MvcRouter\Http\TreeRouteStack::class
: Router\Http\TreeRouteStack::class;
}

public function getRouteMatchClass()
{
return class_exists(MvcRouter\RouteMatch::class)
? MvcRouter\RouteMatch::class
: Router\RouteMatch::class;
}

public function getServiceManager()
{
$routerMatchClass = $this->getRouteMatchClass();
$routerClass = $this->getRouterClass();
$routeMatch = new $routerMatchClass([]);
$router = new $routerClass();

$mvcEventStub = new MvcEvent();
$mvcEventStub->setRouteMatch($routeMatch);
$mvcEventStub->setRouter($router);

$applicationMock = $this->getMockBuilder(Application::class)
->disableOriginalConstructor()
->getMock();

$applicationMock->expects($this->any())
->method('getMvcEvent')
->willReturn($mvcEventStub);

$constructor = new ReflectionMethod(new ServiceManager(), '__construct');
$parameter = $constructor->getParameters();
$parameter = $parameter[0];
$type = $parameter->getType()->getName();

if (strcasecmp($type, 'array') === 0) {
$serviceManager = new ServiceManager(
[
'services' => [
'config' => [
'navigation' => [
'testStubNavigation' => [],
],
],
'Application' => $applicationMock,
],
]
);
} else {
$serviceManager = $this->getMockBuilder(ServiceManager::class)
->disableOriginalConstructor()
->getMock();

$serviceManager->expects($this->any())
->method('get')
->willReturnMap([
['config', ['navigation' => ['testStubNavigation' => []]]],
['Application', $applicationMock]
]);
}

return $serviceManager;
}
}

0 comments on commit 40b222a

Please sign in to comment.