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

Commit

Permalink
Re-enable zend-modulemanager, -mvc, -navigation, and -session tests o…
Browse files Browse the repository at this point in the history
…n Travis

Now that each of zend-modulemanager, zend-mvc, zend-navigation, and
zend-session have known-stable, forwards-compatible versions available,
we can re-enable tests against them on Travis.

Doing so exposed some areas where the tests and/or SUTs were not yet
correct. These included:

- `Zend\View\Helper\Navigation\AbstractHelper` relied on having a shared
  event manager composed in its event manager, but when lazy-loaded,
  this does not happen under zend-eventmanager v3. As such, code was
  added to ensure a shared manager is always present.
- The `FlashMessengerFactory` was still pulling the configuration
  services using the v2 `Config` instead of `config`.
- The `HelperPluginManager` was missing an `EventManagerAware`
  initializer; one was added.
- Navigation helper tests were updated to inject a service locator where
  required.
  • Loading branch information
weierophinney committed Mar 2, 2016
1 parent d9ad104 commit 341919c
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 61 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ before_install:
- if [[ $ZEND_EVENTMANAGER_VERSION == '' ]]; then composer require --no-update "zendframework/zend-eventmanager:^3.0" ; fi
- if [[ $ZEND_SERVICEMANAGER_VERSION != '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:$ZEND_SERVICEMANAGER_VERSION" ; fi
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:^3.0.3" ; fi
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer remove --dev --no-update zendframework/zend-modulemanager zendframework/zend-mvc zendframework/zend-session ; fi

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
"zendframework/zend-i18n": "^2.6",
"zendframework/zend-json": "^2.6.1",
"zendframework/zend-log": "^2.7",
"zendframework/zend-modulemanager": "^2.5",
"zendframework/zend-mvc": "^2.6.1",
"zendframework/zend-modulemanager": "^2.7.1",
"zendframework/zend-mvc": "^2.7",
"zendframework/zend-navigation": "^2.5",
"zendframework/zend-paginator": "^2.5",
"zendframework/zend-permissions-acl": "^2.6",
"zendframework/zend-serializer": "^2.6.1",
"zendframework/zend-session": "^2.5",
"zendframework/zend-session": "^2.6.2",
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
"zendframework/zend-uri": "^2.5",
"fabpot/php-cs-fixer": "1.7.*",
Expand Down
22 changes: 21 additions & 1 deletion src/Helper/Navigation/AbstractHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

use Interop\Container\ContainerInterface;
use RecursiveIteratorIterator;
use ReflectionClass;
use ReflectionProperty;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\SharedEventManager;
use Zend\I18n\Translator\TranslatorInterface as Translator;
use Zend\I18n\Translator\TranslatorAwareInterface;
use Zend\Navigation;
Expand Down Expand Up @@ -524,7 +526,7 @@ public function setEventManager(EventManagerInterface $events)
public function getEventManager()
{
if (null === $this->events) {
$this->setEventManager(new EventManager());
$this->setEventManager($this->createEventManager());
}

return $this->events;
Expand Down Expand Up @@ -960,4 +962,22 @@ protected function setDefaultListeners()
['Zend\View\Helper\Navigation\Listener\AclListener', 'accept']
);
}

/**
* Create and return an event manager instance.
*
* Ensures that the returned event manager has a shared manager
* composed.
*
* @return EventManager
*/
private function createEventManager()
{
$r = new ReflectionClass(EventManager::class);
if ($r->hasMethod('setSharedManager')) {
return new EventManager();
}

return new EventManager(new SharedEventManager());
}
}
14 changes: 8 additions & 6 deletions src/Helper/Service/FlashMessengerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __invoke(ContainerInterface $container, $name, array $options =

$helper->setPluginFlashMessenger($flashMessenger);

$config = $container->get('Config');
$config = $container->get('config');
if (isset($config['view_helper_config']['flashmessenger'])) {
$configHelper = $config['view_helper_config']['flashmessenger'];
if (isset($configHelper['message_open_format'])) {
Expand All @@ -54,13 +54,15 @@ public function __invoke(ContainerInterface $container, $name, array $options =
}

/**
* Create service
* Create service (v2)
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
* @param ServiceLocatorInterface $container
* @param string $normalizedName
* @param string $requestedName
* @return FlashMessenger
*/
public function createService(ServiceLocatorInterface $serviceLocator, $rName = null, $cName = null)
public function createService(ServiceLocatorInterface $container, $normalizedName = null, $requestedName = null)
{
return $this($serviceLocator, $cName);
return $this($container, $requestedName);
}
}
34 changes: 34 additions & 0 deletions src/HelperPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Zend\View;

use Interop\Container\ContainerInterface;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\SharedEventManagerInterface;
use Zend\I18n\Translator\TranslatorAwareInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Exception\InvalidServiceException;
Expand Down Expand Up @@ -243,6 +245,7 @@ public function __construct($configOrContainerInstance = null, array $v3config =
{
$this->initializers[] = [$this, 'injectRenderer'];
$this->initializers[] = [$this, 'injectTranslator'];
$this->initializers[] = [$this, 'injectEventManager'];

parent::__construct($configOrContainerInstance, $v3config);
}
Expand Down Expand Up @@ -340,6 +343,37 @@ public function injectTranslator($first, $second)
}
}

/**
* Inject a helper instance with the registered event manager
*
* @param ContainerInterface|Helper\HelperInterface $first helper instance
* under zend-servicemanager v2, ContainerInterface under v3.
* @param ContainerInterface|Helper\HelperInterface $second
* ContainerInterface under zend-servicemanager v3, helper instance
* under v2. Ignored regardless.
*/
public function injectEventManager($first, $second)
{
if ($first instanceof ContainerInterface) {
// v3 usage
$container = $first;
$helper = $second;
} else {
// v2 usage; grab the parent container
$container = $second->getServiceLocator();
$helper = $first;
}

if (! $helper instanceof EventManagerAwareInterface) {
return;
}

$events = $helper->getEventManager();
if (! $events || ! $events->getSharedManager() instanceof SharedEventManagerInterface) {
$helper->setEventManager($container->get('EventManager'));
}
}

/**
* Validate the plugin is of the expected type (v3).
*
Expand Down
8 changes: 0 additions & 8 deletions test/Helper/Navigation/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ abstract class AbstractTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
if (! class_exists(ServiceManagerConfig::class)) {
$this->markTestSkipped(
'Skipping zend-mvc-related tests until that component is updated '
. 'to be forwards-compatible with zend-eventmanager, zend-stdlib, '
. 'and zend-servicemanager v3.'
);
}

$cwd = __DIR__;

// read navigation config
Expand Down
3 changes: 1 addition & 2 deletions test/Helper/Navigation/BreadcrumbsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public function testHelperEntryPointWithContainerParam()

public function testHelperEntryPointWithContainerStringParam()
{
$pm = new \Zend\View\HelperPluginManager();
$pm->setServiceLocator($this->serviceManager);
$pm = new \Zend\View\HelperPluginManager($this->serviceManager);
$this->_helper->setServiceLocator($pm);

$returned = $this->_helper->__invoke('nav1');
Expand Down
13 changes: 12 additions & 1 deletion test/Helper/Navigation/NavigationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function testAcceptAclShouldReturnGracefullyWithUnknownResource()
public function testShouldProxyToMenuHelperByDefault()
{
$this->_helper->setContainer($this->_nav1);
$this->_helper->setServiceLocator(new ServiceManager());

// result
$expected = $this->_getExpected('menu/default1.html');
Expand All @@ -95,6 +96,7 @@ public function testInjectingContainer()
{
// setup
$this->_helper->setContainer($this->_nav2);
$this->_helper->setServiceLocator(new ServiceManager());
$expected = [
'menu' => $this->_getExpected('menu/default2.html'),
'breadcrumbs' => $this->_getExpected('bc/default.html')
Expand All @@ -112,6 +114,7 @@ public function testInjectingContainer()
public function testDisablingContainerInjection()
{
// setup
$this->_helper->setServiceLocator(new ServiceManager());
$this->_helper->setInjectContainer(false);
$this->_helper->menu()->setContainer(null);
$this->_helper->breadcrumbs()->setContainer(null);
Expand All @@ -132,6 +135,7 @@ public function testDisablingContainerInjection()

public function testMultipleNavigationsAndOneMenuDisplayedTwoTimes()
{
$this->_helper->setServiceLocator(new ServiceManager());
$expected = $this->_helper->setContainer($this->_nav1)->menu()->getContainer();
$this->_helper->setContainer($this->_nav2)->menu()->getContainer();
$actual = $this->_helper->setContainer($this->_nav1)->menu()->getContainer();
Expand Down Expand Up @@ -161,6 +165,7 @@ public function testInjectingAcl()
$acl = $this->_getAcl();
$this->_helper->setAcl($acl['acl']);
$this->_helper->setRole($acl['role']);
$this->_helper->setServiceLocator(new ServiceManager());

$expected = $this->_getExpected('menu/acl.html');
$actual = $this->_helper->render();
Expand All @@ -175,6 +180,7 @@ public function testDisablingAclInjection()
$this->_helper->setAcl($acl['acl']);
$this->_helper->setRole($acl['role']);
$this->_helper->setInjectAcl(false);
$this->_helper->setServiceLocator(new ServiceManager());

$expected = $this->_getExpected('menu/default1.html');
$actual = $this->_helper->render();
Expand All @@ -189,6 +195,7 @@ public function testInjectingTranslator()
}

$this->_helper->setTranslator($this->_getTranslator());
$this->_helper->setServiceLocator(new ServiceManager());

$expected = $this->_getExpected('menu/translated.html');
$actual = $this->_helper->render();
Expand All @@ -200,6 +207,7 @@ public function testDisablingTranslatorInjection()
{
$this->_helper->setTranslator($this->_getTranslator());
$this->_helper->setInjectTranslator(false);
$this->_helper->setServiceLocator(new ServiceManager());

$expected = $this->_getExpected('menu/default1.html');
$actual = $this->_helper->render();
Expand Down Expand Up @@ -230,6 +238,7 @@ public function testSpecifyingDefaultProxy()
$actual = [];

// result
$this->_helper->setServiceLocator(new ServiceManager());
$this->_helper->setDefaultProxy('breadcrumbs');
$actual['breadcrumbs'] = $this->_helper->render($this->_nav1);
$this->_helper->setDefaultProxy('menu');
Expand Down Expand Up @@ -416,6 +425,7 @@ public function testPageIdShouldBeNormalized()
. ' </li>' . $nl
. '</ul>';

$this->_helper->setServiceLocator(new ServiceManager());
$actual = $this->_helper->render($container);

$this->assertEquals($expected, $actual);
Expand All @@ -440,6 +450,7 @@ public function testRenderInvisibleItem()
]
]);

$this->_helper->setServiceLocator(new ServiceManager());
$render = $this->_helper->menu()->render($container);

$this->assertNotContains('p2', $render);
Expand Down Expand Up @@ -558,7 +569,7 @@ public function testMultipleNavigationsWithSameHelperAndSameContainer()

public function testSetPluginManagerAndView()
{
$pluginManager = new Navigation\PluginManager();
$pluginManager = new Navigation\PluginManager(new ServiceManager());
$view = new PhpRenderer();

$helper = new $this->_helperName;
Expand Down
21 changes: 6 additions & 15 deletions test/Helper/UrlIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ class UrlIntegrationTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (! class_exists(PluginFlashMessenger::class)) {
$this->markTestSkipped(
'Skipping zend-mvc-related tests until that component is updated '
. 'to be forwards-compatible with zend-eventmanager, zend-stdlib, '
. 'and zend-servicemanager v3.'
);
}


$config = [
'router' => [
'routes' => [
Expand Down Expand Up @@ -66,12 +57,12 @@ protected function setUp()

$serviceConfig = $this->readAttribute(new ServiceListenerFactory, 'defaultServiceConfig');

$this->serviceManager = new ServiceManager(new ServiceManagerConfig($serviceConfig));
$this->serviceManager
->setAllowOverride(true)
->setService('Config', $config)
->setAlias('Configuration', 'Config')
->setAllowOverride(false);
$this->serviceManager = new ServiceManager();
(new ServiceManagerConfig($serviceConfig))->configureServiceManager($this->serviceManager);
$this->serviceManager->setAllowOverride(true);
$this->serviceManager->setService('config', $config);
$this->serviceManager->setAlias('Configure', 'config');
$this->serviceManager->setAllowOverride(false);
}

public function testUrlHelperWorksUnderNormalHttpParadigms()
Expand Down
8 changes: 0 additions & 8 deletions test/Helper/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
if (! class_exists(PluginFlashMessenger::class)) {
$this->markTestSkipped(
'Skipping zend-mvc-related tests until that component is updated '
. 'to be forwards-compatible with zend-eventmanager, zend-stdlib, '
. 'and zend-servicemanager v3.'
);
}

$router = new Router();
$router->addRoute('home', [
'type' => 'Zend\Mvc\Router\Http\Literal',
Expand Down
16 changes: 0 additions & 16 deletions test/HelperPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,6 @@ public function testIdentityFactoryCanInjectAuthenticationServiceIfInParentServi

public function testIfHelperIsTranslatorAwareAndMvcTranslatorIsAvailableItWillInjectTheMvcTranslator()
{
if (! class_exists(PluginFlashMessenger::class)) {
$this->markTestSkipped(
'Skipping zend-mvc-related tests until that component is updated '
. 'to be forwards-compatible with zend-eventmanager, zend-stdlib, '
. 'and zend-servicemanager v3.'
);
}

$translator = new MvcTranslator($this->getMock('Zend\I18n\Translator\TranslatorInterface'));
$config = new Config(['services' => [
'MvcTranslator' => $translator,
Expand All @@ -144,14 +136,6 @@ public function testIfHelperIsTranslatorAwareAndMvcTranslatorIsAvailableItWillIn

public function testIfHelperIsTranslatorAwareAndMvcTranslatorIsUnavailableAndTranslatorIsAvailableItWillInjectTheTranslator()
{
if (! class_exists(PluginFlashMessenger::class)) {
$this->markTestSkipped(
'Skipping zend-mvc-related tests until that component is updated '
. 'to be forwards-compatible with zend-eventmanager, zend-stdlib, '
. 'and zend-servicemanager v3.'
);
}

$translator = new Translator();
$config = new Config(['services' => [
'Translator' => $translator,
Expand Down

0 comments on commit 341919c

Please sign in to comment.