From 9cb74fe521355543efc26c5322c9ff50d5d973ac Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Wed, 15 Mar 2017 10:50:14 +0100 Subject: [PATCH 1/3] replace strings with ::class notation --- config/module.config.php | 22 +++++--- src/SlmLocale/Service/DetectorFactory.php | 3 +- .../Service/LocaleMenuViewHelperFactory.php | 3 +- .../Service/LocaleUrlViewHelperFactory.php | 3 +- .../Strategy/StrategyPluginManager.php | 31 ++++++----- tests/SlmLocaleTest/Locale/DetectorTest.php | 14 ++--- .../Service/DetectFactoryTest.php | 53 ++++++++++--------- .../Strategy/HostStrategyTest.php | 7 +-- 8 files changed, 78 insertions(+), 58 deletions(-) diff --git a/config/module.config.php b/config/module.config.php index bcf02d5..cdb79a6 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,4 +1,14 @@ [ 'factories' => [ - 'SlmLocale\Strategy\StrategyPluginManager' => 'SlmLocale\Strategy\Factory\StrategyPluginManagerFactory', - 'SlmLocale\Locale\Detector' => 'SlmLocale\Service\DetectorFactory', + StrategyPluginManager::class => StrategyPluginManagerFactory::class, + Detector::class => DetectorFactory::class, ], ], 'view_helpers' => [ 'aliases' => [ - 'localeUrl' => 'SlmLocale\View\Helper\LocaleUrl', - 'localeMenu' => 'SlmLocale\View\Helper\LocaleMenu', + 'localeUrl' => LocaleUrl::class, + 'localeMenu' => LocaleMenu::class, ], 'factories' => [ - 'SlmLocale\View\Helper\LocaleUrl' => 'SlmLocale\Service\LocaleUrlViewHelperFactory', - 'SlmLocale\View\Helper\LocaleMenu' => 'SlmLocale\Service\LocaleMenuViewHelperFactory', + LocaleUrl::class => LocaleUrlViewHelperFactory::class, + LocaleMenu::class => LocaleMenuViewHelperFactory::class, ], ], ]; diff --git a/src/SlmLocale/Service/DetectorFactory.php b/src/SlmLocale/Service/DetectorFactory.php index 1f35382..e39b711 100644 --- a/src/SlmLocale/Service/DetectorFactory.php +++ b/src/SlmLocale/Service/DetectorFactory.php @@ -42,6 +42,7 @@ use Interop\Container\ContainerInterface; use SlmLocale\Locale\Detector; +use SlmLocale\Strategy\StrategyPluginManager; class DetectorFactory { @@ -74,7 +75,7 @@ public function __invoke(ContainerInterface $container) protected function addStrategies(Detector $detector, array $strategies, ContainerInterface $container) { - $plugins = $container->get('SlmLocale\Strategy\StrategyPluginManager'); + $plugins = $container->get(StrategyPluginManager::class); foreach ($strategies as $strategy) { if (is_string($strategy)) { diff --git a/src/SlmLocale/Service/LocaleMenuViewHelperFactory.php b/src/SlmLocale/Service/LocaleMenuViewHelperFactory.php index 108c01f..32751fc 100644 --- a/src/SlmLocale/Service/LocaleMenuViewHelperFactory.php +++ b/src/SlmLocale/Service/LocaleMenuViewHelperFactory.php @@ -42,6 +42,7 @@ use Interop\Container\ContainerInterface; use Interop\Container\Exception\ContainerException; +use SlmLocale\Locale\Detector; use SlmLocale\View\Helper\LocaleMenu; use Zend\ServiceManager\Exception\ServiceNotCreatedException; use Zend\ServiceManager\Exception\ServiceNotFoundException; @@ -65,7 +66,7 @@ class LocaleMenuViewHelperFactory implements FactoryInterface */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $detector = $container->get('SlmLocale\Locale\Detector'); + $detector = $container->get(Detector::class); $helper = new LocaleMenu(); $helper->setDetector($detector); diff --git a/src/SlmLocale/Service/LocaleUrlViewHelperFactory.php b/src/SlmLocale/Service/LocaleUrlViewHelperFactory.php index 6ed591f..7b8b19c 100644 --- a/src/SlmLocale/Service/LocaleUrlViewHelperFactory.php +++ b/src/SlmLocale/Service/LocaleUrlViewHelperFactory.php @@ -42,6 +42,7 @@ use Interop\Container\ContainerInterface; use Interop\Container\Exception\ContainerException; +use SlmLocale\Locale\Detector; use SlmLocale\View\Helper\LocaleUrl; use Zend\ServiceManager\Exception\ServiceNotCreatedException; use Zend\ServiceManager\Exception\ServiceNotFoundException; @@ -65,7 +66,7 @@ class LocaleUrlViewHelperFactory implements FactoryInterface */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $detector = $container->get('SlmLocale\Locale\Detector'); + $detector = $container->get(Detector::class); $request = $container->get('Request'); $app = $container->get('Application'); diff --git a/src/SlmLocale/Strategy/StrategyPluginManager.php b/src/SlmLocale/Strategy/StrategyPluginManager.php index 143c380..a368ab0 100644 --- a/src/SlmLocale/Strategy/StrategyPluginManager.php +++ b/src/SlmLocale/Strategy/StrategyPluginManager.php @@ -51,28 +51,27 @@ class StrategyPluginManager extends AbstractPluginManager * {@inheritdoc} */ protected $aliases = [ - 'cookie' => 'SlmLocale\Strategy\CookieStrategy', - 'host' => 'SlmLocale\Strategy\HostStrategy', - 'acceptlanguage' => 'SlmLocale\Strategy\HttpAcceptLanguageStrategy', - 'query' => 'SlmLocale\Strategy\QueryStrategy', - 'uripath' => 'SlmLocale\Strategy\UriPathStrategy', + 'cookie' => CookieStrategy::class, + 'host' => HostStrategy::class, + 'acceptlanguage' => HttpAcceptLanguageStrategy::class, + 'query' => QueryStrategy::class, + 'uripath' => UriPathStrategy::class, ]; /** * {@inheritdoc} */ protected $factories = [ - 'SlmLocale\Strategy\CookieStrategy' => InvokableFactory::class, - 'SlmLocale\Strategy\HostStrategy' => InvokableFactory::class, - 'SlmLocale\Strategy\HttpAcceptLanguageStrategy' => InvokableFactory::class, - 'SlmLocale\Strategy\QueryStrategy' => InvokableFactory::class, - 'SlmLocale\Strategy\UriPathStrategy' => InvokableFactory::class, - 'slmlocalestrategycookiestrategy' => InvokableFactory::class, - 'slmlocalestrategyhoststrategy' => InvokableFactory::class, - 'slmlocalestrategyhttpacceptlanguagestrategy' => InvokableFactory::class, - 'slmlocalestrategyquerystrategy' => InvokableFactory::class, - 'slmlocalestrategyuripathstrategy' => InvokableFactory::class, - + CookieStrategy::class => InvokableFactory::class, + HostStrategy::class => InvokableFactory::class, + HttpAcceptLanguageStrategy::class => InvokableFactory::class, + QueryStrategy::class => InvokableFactory::class, + UriPathStrategy::class => InvokableFactory::class, + 'slmlocalestrategycookiestrategy' => InvokableFactory::class, + 'slmlocalestrategyhoststrategy' => InvokableFactory::class, + 'slmlocalestrategyhttpacceptlanguagestrategy' => InvokableFactory::class, + 'slmlocalestrategyquerystrategy' => InvokableFactory::class, + 'slmlocalestrategyuripathstrategy' => InvokableFactory::class, ]; /** diff --git a/tests/SlmLocaleTest/Locale/DetectorTest.php b/tests/SlmLocaleTest/Locale/DetectorTest.php index f49302f..bea9262 100644 --- a/tests/SlmLocaleTest/Locale/DetectorTest.php +++ b/tests/SlmLocaleTest/Locale/DetectorTest.php @@ -43,6 +43,8 @@ use SlmLocale\Locale\Detector; use SlmLocale\LocaleEvent; +use SlmLocale\Strategy\AbstractStrategy; +use SlmLocale\Strategy\StrategyInterface; use Zend\EventManager\EventManager; use Zend\Stdlib\Request; use Zend\Stdlib\Response; @@ -55,7 +57,7 @@ public function testDetectEventUsesLocaleEventObject() $self = $this; $this->setEventManager($detector, LocaleEvent::EVENT_DETECT, function ($e) use ($self) { - $self->assertInstanceOf('SlmLocale\LocaleEvent', $e); + $self->assertInstanceOf(LocaleEvent::class, $e); }); $detector->detect(new Request(), new Response()); @@ -169,8 +171,8 @@ public function testEmptySupportedListIndicatesNoSupportedList() public function testStrategyAttachesToEventManager() { $detector = new Detector(); - $events = $this->getMock('Zend\EventManager\EventManager'); - $strategy = $this->getMock('SlmLocale\Strategy\StrategyInterface'); + $events = $this->getMock(EventManager::class); + $strategy = $this->getMock(StrategyInterface::class); $strategy->expects($this->once()) ->method('attach') ->with($events); @@ -184,12 +186,12 @@ public function testStrategyWithHighestPriorityWins() $detector = new Detector(); $this->setEventManager($detector); - $strategy1 = $this->getMock('SlmLocale\Strategy\AbstractStrategy', ['detect']); + $strategy1 = $this->getMock(AbstractStrategy::class, ['detect']); $strategy1->expects($this->once()) ->method('detect') ->will($this->returnValue('Foo')); - $strategy2 = $this->getMock('SlmLocale\Strategy\AbstractStrategy', ['detect']); + $strategy2 = $this->getMock(AbstractStrategy::class, ['detect']); $strategy2->expects($this->never()) ->method('detect'); @@ -206,7 +208,7 @@ public function testFoundEventUsesLocaleEventObject() $self = $this; $this->setEventManager($detector, LocaleEvent::EVENT_FOUND, function ($e) use ($self) { - $self->assertInstanceOf('SlmLocale\LocaleEvent', $e); + $self->assertInstanceOf(LocaleEvent::class, $e); }); $detector->detect(new Request(), new Response()); diff --git a/tests/SlmLocaleTest/Service/DetectFactoryTest.php b/tests/SlmLocaleTest/Service/DetectFactoryTest.php index da9cb49..dc2f1ab 100644 --- a/tests/SlmLocaleTest/Service/DetectFactoryTest.php +++ b/tests/SlmLocaleTest/Service/DetectFactoryTest.php @@ -41,6 +41,11 @@ use PHPUnit_Framework_TestCase as TestCase; +use SlmLocale\Locale\Detector; +use SlmLocale\Service\DetectorFactory; +use SlmLocale\Strategy\Factory\StrategyPluginManagerFactory; +use SlmLocale\Strategy\StrategyInterface; +use SlmLocale\Strategy\StrategyPluginManager; use Zend\EventManager\EventManager; use Zend\ServiceManager\ServiceManager; @@ -49,15 +54,15 @@ class DetectFactoryTest extends TestCase public function testFactoryInstantiatesDetector() { $sl = $this->getServiceLocator(); - $detector = $sl->get('SlmLocale\Locale\Detector'); + $detector = $sl->get(Detector::class); - $this->assertInstanceOf('SlmLocale\Locale\Detector', $detector); + $this->assertInstanceOf(Detector::class, $detector); } public function testDefaultLocaleIsOptional() { $sl = $this->getServiceLocator(); - $detector = $sl->get('SlmLocale\Locale\Detector'); + $detector = $sl->get(Detector::class); $this->assertNull($detector->getDefault()); } @@ -67,7 +72,7 @@ public function testDefaultLocaleIsSet() $sl = $this->getServiceLocator([ 'default' => 'Foo', ]); - $detector = $sl->get('SlmLocale\Locale\Detector'); + $detector = $sl->get(Detector::class); $this->assertEquals('Foo', $detector->getDefault()); } @@ -75,7 +80,7 @@ public function testDefaultLocaleIsSet() public function testSupportedLocalesAreOptional() { $sl = $this->getServiceLocator(); - $detector = $sl->get('SlmLocale\Locale\Detector'); + $detector = $sl->get(Detector::class); $this->assertNull($detector->getSupported()); } @@ -85,7 +90,7 @@ public function testSupportedLocalesAreSet() $sl = $this->getServiceLocator([ 'supported' => ['Foo', 'Bar'], ]); - $detector = $sl->get('SlmLocale\Locale\Detector'); + $detector = $sl->get(Detector::class); $this->assertEquals(['Foo', 'Bar'], $detector->getSupported()); } @@ -98,14 +103,14 @@ public function testUseServiceLocatorToInstantiateStrategy() $self = $this; $called = false; - $plugins = $sl->get('SlmLocale\Strategy\StrategyPluginManager'); + $plugins = $sl->get(StrategyPluginManager::class); $plugins->setFactory('TestStrategy', function () use ($self, &$called) { $called = true; - return $self->getMock('SlmLocale\Strategy\StrategyInterface'); + return $self->getMock(StrategyInterface::class); }); - $detector = $sl->get('SlmLocale\Locale\Detector'); + $detector = $sl->get(Detector::class); $this->assertTrue($called); } @@ -117,21 +122,21 @@ public function testConfigurationCanHoldMultipleStrategies() $self = $this; $called1 = false; - $plugins = $sl->get('SlmLocale\Strategy\StrategyPluginManager'); + $plugins = $sl->get(StrategyPluginManager::class); $plugins->setFactory('TestStrategy1', function () use ($self, &$called1) { $called1 = true; - return $self->getMock('SlmLocale\Strategy\StrategyInterface'); + return $self->getMock(StrategyInterface::class); }); $called2 = false; $plugins->setFactory('TestStrategy2', function () use ($self, &$called2) { $called2 = true; - return $self->getMock('SlmLocale\Strategy\StrategyInterface'); + return $self->getMock(StrategyInterface::class); }); - $detector = $sl->get('SlmLocale\Locale\Detector'); + $detector = $sl->get(Detector::class); $this->assertTrue($called1); $this->assertTrue($called2); } @@ -146,14 +151,14 @@ public function testStrategyConfigurationCanBeAnArray() $self = $this; $called = false; - $plugins = $sl->get('SlmLocale\Strategy\StrategyPluginManager'); + $plugins = $sl->get(StrategyPluginManager::class); $plugins->setFactory('TestStrategy', function () use ($self, &$called) { $called = true; - return $self->getMock('SlmLocale\Strategy\StrategyInterface'); + return $self->getMock(StrategyInterface::class); }); - $detector = $sl->get('SlmLocale\Locale\Detector'); + $detector = $sl->get(Detector::class); $this->assertTrue($called); } @@ -166,14 +171,14 @@ public function testStrategyCanBeAttachedWithPriorities() ]); $em = $sl->get('EventManager'); - $strategy = $this->getMock('SlmLocale\Strategy\StrategyInterface', ['attach', 'detach']); + $strategy = $this->getMock(StrategyInterface::class, ['attach', 'detach']); $strategy->expects($this->once()) ->method('attach') ->with($em, 100); - $plugins = $sl->get('SlmLocale\Strategy\StrategyPluginManager'); + $plugins = $sl->get(StrategyPluginManager::class); $plugins->setService('TestStrategy', $strategy); - $detector = $sl->get('SlmLocale\Locale\Detector'); + $detector = $sl->get(Detector::class); } public function testStrategyCanBeInstantiatedWithOptions() @@ -183,14 +188,14 @@ public function testStrategyCanBeInstantiatedWithOptions() ['name' => 'TestStrategy', 'options' => 'Foo'], ], ]); - $strategy = $this->getMock('SlmLocale\Strategy\StrategyInterface', ['attach', 'detach', 'setOptions']); + $strategy = $this->getMock(StrategyInterface::class, ['attach', 'detach', 'setOptions']); $strategy->expects($this->once()) ->method('setOptions') ->with('Foo'); - $plugins = $sl->get('SlmLocale\Strategy\StrategyPluginManager'); + $plugins = $sl->get(StrategyPluginManager::class); $plugins->setService('TestStrategy', $strategy); - $detector = $sl->get('SlmLocale\Locale\Detector'); + $detector = $sl->get(Detector::class); } public function getServiceLocator(array $config = []) @@ -201,8 +206,8 @@ public function getServiceLocator(array $config = []) ], ]; $serviceLocator = new ServiceManager(); - $serviceLocator->setFactory('SlmLocale\Locale\Detector', 'SlmLocale\Service\DetectorFactory'); - $serviceLocator->setFactory('SlmLocale\Strategy\StrategyPluginManager', 'SlmLocale\Strategy\Factory\StrategyPluginManagerFactory'); + $serviceLocator->setFactory(Detector::class, DetectorFactory::class); + $serviceLocator->setFactory(StrategyPluginManager::class, StrategyPluginManagerFactory::class); $serviceLocator->setService('EventManager', new EventManager()); $serviceLocator->setService('config', $config); diff --git a/tests/SlmLocaleTest/Strategy/HostStrategyTest.php b/tests/SlmLocaleTest/Strategy/HostStrategyTest.php index b3bfa3e..a54e22d 100644 --- a/tests/SlmLocaleTest/Strategy/HostStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/HostStrategyTest.php @@ -7,6 +7,7 @@ use SlmLocale\Strategy\HostStrategy; use Zend\Http\PhpEnvironment\Request; use Zend\Stdlib\Parameters; +use Zend\Stdlib\RequestInterface; use Zend\Uri\Uri; class HostStrategyTest extends TestCase @@ -14,7 +15,7 @@ class HostStrategyTest extends TestCase public function testDetectNonHttpRequestReturnsNull() { $event = new LocaleEvent(); - $event->setRequest($this->getMockForAbstractClass('Zend\Stdlib\RequestInterface')); + $event->setRequest($this->getMockForAbstractClass(RequestInterface::class)); $strategy = new HostStrategy(); $this->assertNull($strategy->detect($event)); @@ -23,7 +24,7 @@ public function testDetectNonHttpRequestReturnsNull() public function testDetectWithoutSupportedReturnsNull() { $event = new LocaleEvent(); - $event->setRequest($this->getMockForAbstractClass('Zend\Http\Request')); + $event->setRequest($this->getMockForAbstractClass(\Zend\Http\Request::class)); $event->setSupported([]); $strategy = new HostStrategy(); @@ -36,7 +37,7 @@ public function testDetectWithoutSupportedReturnsNull() public function testDetectWithoutDomainThrowsInvalidArgumentException() { $event = new LocaleEvent(); - $event->setRequest($this->getMockForAbstractClass('Zend\Http\Request')); + $event->setRequest($this->getMockForAbstractClass(\Zend\Http\Request::class)); $event->setSupported(['en_GB', 'de_DE']); $strategy = new HostStrategy(); From 0461d3192599695d5feff5875f66b67b42c2e3e2 Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Wed, 15 Mar 2017 10:54:34 +0100 Subject: [PATCH 2/3] module too --- Module.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Module.php b/Module.php index b7e43fc..185ec54 100644 --- a/Module.php +++ b/Module.php @@ -43,6 +43,7 @@ use Locale; use Zend\EventManager\EventInterface; +use Zend\Loader\StandardAutoloader; use Zend\ModuleManager\Feature; use Zend\Mvc\MvcEvent; use Zend\Stdlib\ResponseInterface; @@ -55,7 +56,7 @@ class Module implements public function getAutoloaderConfig() { return [ - 'Zend\Loader\StandardAutoloader' => [ + StandardAutoloader::class => [ 'namespaces' => [ __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, ], @@ -73,7 +74,7 @@ public function onBootstrap(EventInterface $e) $app = $e->getApplication(); $sm = $app->getServiceManager(); - $detector = $sm->get('SlmLocale\Locale\Detector'); + $detector = $sm->get(Locale\Detector::class); $result = $detector->detect($app->getRequest(), $app->getResponse()); if ($result instanceof ResponseInterface) { From eb75e46997a874fd5600d471ba14442689386528 Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Wed, 15 Mar 2017 11:33:53 +0100 Subject: [PATCH 3/3] incorrect import --- Module.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Module.php b/Module.php index 185ec54..606687f 100644 --- a/Module.php +++ b/Module.php @@ -42,6 +42,7 @@ use Locale; +use SlmLocale\Locale\Detector; use Zend\EventManager\EventInterface; use Zend\Loader\StandardAutoloader; use Zend\ModuleManager\Feature; @@ -74,7 +75,7 @@ public function onBootstrap(EventInterface $e) $app = $e->getApplication(); $sm = $app->getServiceManager(); - $detector = $sm->get(Locale\Detector::class); + $detector = $sm->get(Detector::class); $result = $detector->detect($app->getRequest(), $app->getResponse()); if ($result instanceof ResponseInterface) {