diff --git a/.travis.yml b/.travis.yml index c5d76f3..84a7020 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,27 +7,28 @@ cache: matrix: include: - - php: 5.6 + - php: 7.2 env: - DEPENDENCIES="--prefer-stable" - - php: 5.6 + - php: 7.2 env: - DEPENDENCIES="--prefer-lowest --prefer-stable" - - php: 7.0 + + - php: 7.3 env: - DEPENDENCIES="--prefer-stable" - - php: 7.0 + - php: 7.3 env: - DEPENDENCIES="--prefer-lowest --prefer-stable" - - php: 7.1 + + - php: 7.4 env: - DEPENDENCIES="--prefer-stable" - TEST_COVERAGE=true - - php: 7.1 + - php: 7.4 env: - DEPENDENCIES="--prefer-lowest --prefer-stable" - before_script: - phpenv config-rm xdebug.ini - composer self-update diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ca2dd6..2734d8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ Changelog v1.0.0 - TBD --- +v0.5.0 - 2020-09-09 +--- + * [#104](https://github.com/basz/SlmLocale/pull/104) Run tests for php 7.2 and 7.3 + * [#106](https://github.com/basz/SlmLocale/pull/106) Added a view-helper to get the primary-language + * [#105](https://github.com/basz/SlmLocale/pull/105) Fixed locale url change, from default locale to other locale + * [#107](https://github.com/basz/SlmLocale/pull/107) Added Laminas support + * [#108](https://github.com/basz/SlmLocale/pull/108) Added tests for php74 + v0.4.1 --- * [#99](https://github.com/basz/SlmLocale/pull/99) fixed #26 unit testing applications using SlmLocale @@ -34,4 +42,4 @@ First public stable release * Rewrite of internal detector events * Update of all available strategies * Compliant with coding standards - * All covered with unit tests \ No newline at end of file + * All covered with unit tests diff --git a/README.md b/README.md index a8a4633..15dcbad 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Created by Jurian Sluiman Introduction ------------ -SlmLocale is a Zend Framework module to automatically detect a locale for your +SlmLocale is a Laminas module to automatically detect a locale for your application. It uses a variety of pluggable strategies to search for a valid locale. SlmLocale features a default locale, a set of supported locales and locale aliases. @@ -96,7 +96,7 @@ is important! You usually want the `acceptlanguage` as last for a fallback: At this moment, the locale should be detected. The locale is stored inside php's `Locale` object. Retrieve the locale with `Locale::getDefault()`. This is also -automated inside Zend Framework translator objects and i18n view helpers (so +automated inside Laminas translator objects and i18n view helpers (so you do not need to set the locale yourself there). ### Set the locale's language in html @@ -110,7 +110,7 @@ in the `html` tag: Inject the detected language here with the following code: ``` - + ``` ### Disable UriPathStrategy in PHPUNIT diff --git a/composer.json b/composer.json index 4541044..a059ca1 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,10 @@ { "name": "slm/locale", - "description": "Automatic detection of locales for Zend Framework", + "description": "Automatic detection of locales for Laminas", "type": "library", "license": "BSD-3-Clause", "keywords": [ + "laminas", "zf", "zend framework", "i18n", @@ -18,26 +19,26 @@ } ], "require": { - "php": "^5.6 || ^7.0", + "php": "^7.2", "ext-intl": "*", - "zendframework/zend-eventmanager": "^3.1", - "zendframework/zend-http": "^2.7", - "zendframework/zend-modulemanager": "^2.8.2", - "zendframework/zend-router": "^3.0", - "zendframework/zend-servicemanager": "^3.2", - "zendframework/zend-stdlib": "^3.1", - "zendframework/zend-view": "^2.9.0" + "laminas/laminas-eventmanager": "^3.1", + "laminas/laminas-http": "^2.7", + "laminas/laminas-modulemanager": "^2.8.2", + "laminas/laminas-router": "^3.0", + "laminas/laminas-servicemanager": "^3.2", + "laminas/laminas-stdlib": "^3.2.1", + "laminas/laminas-view": "^2.9.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.8.1", - "phpunit/phpunit": "^4.8.36", - "satooshi/php-coveralls": "^1.0.2", - "zendframework/zend-console": "^2.6", - "zendframework/zend-mvc": "^3.1", - "zendframework/zend-mvc-console": "^1.1.9" + "friendsofphp/php-cs-fixer": "^2.16.1", + "phpunit/phpunit": "^8.5.1", + "satooshi/php-coveralls": "^1.1.0", + "laminas/laminas-console": "^2.6", + "laminas/laminas-mvc": "^3.1", + "laminas/laminas-mvc-console": "^1.1.9" }, "suggest": { - "zendframework/zend-mvc": "For using the router in the UriPath strategy" + "laminas/laminas-mvc": "For using the router in the UriPath strategy" }, "autoload": { "psr-4": { diff --git a/config/module.config.php b/config/module.config.php index cdb79a6..24ee54c 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,13 +1,10 @@ [ 'factories' => [ StrategyPluginManager::class => StrategyPluginManagerFactory::class, - Detector::class => DetectorFactory::class, + Detector::class => Service\DetectorFactory::class, ], ], 'view_helpers' => [ 'aliases' => [ - 'localeUrl' => LocaleUrl::class, - 'localeMenu' => LocaleMenu::class, + 'localeUrl' => Helper\LocaleUrl::class, + 'localeMenu' => Helper\LocaleMenu::class, + 'primaryLanguage' => Helper\PrimaryLanguage::class, ], 'factories' => [ - LocaleUrl::class => LocaleUrlViewHelperFactory::class, - LocaleMenu::class => LocaleMenuViewHelperFactory::class, + Helper\LocaleUrl::class => Service\LocaleUrlViewHelperFactory::class, + Helper\LocaleMenu::class => Service\LocaleMenuViewHelperFactory::class, + Helper\PrimaryLanguage::class => Service\PrimaryLanguageHelperFactory::class, ], ], ]; diff --git a/docs/1.Introduction.md b/docs/1.Introduction.md index 798c841..2e12248 100644 --- a/docs/1.Introduction.md +++ b/docs/1.Introduction.md @@ -1,6 +1,6 @@ Introduction === -SlmLocale is a Zend Framework module to automatically detect a locale for your application. It uses a variety of pluggable strategies to search for a valid locale. SlmLocale features a default locale, a set of supported locales and locale aliases. +SlmLocale is a Laminas module to automatically detect a locale for your application. It uses a variety of pluggable strategies to search for a valid locale. SlmLocale features a default locale, a set of supported locales and locale aliases. Strategies --- diff --git a/docs/2.Strategies.md b/docs/2.Strategies.md index 3ab2be5..8b67ccb 100644 --- a/docs/2.Strategies.md +++ b/docs/2.Strategies.md @@ -24,7 +24,7 @@ Path segment of URI --- The path segment uses the first part of the path which does not belong to the base path. This means if your site runs under http://example.com, then `/en-US/` selects the locale en-US. If your site runs under http://example.com/my-app/, then `/my-app/en-US/` selects the locale. -Furthermore, this strategy updates the router's base path with the locale appended. This means all links built with Zend\Navigation or with the `url()` view helper, will have the locale prepended automatically. +Furthermore, this strategy updates the router's base path with the locale appended. This means all links built with Laminas\Navigation or with the `url()` view helper, will have the locale prepended automatically. The strategy supports aliases, so you can use `en` as an alias for en-US and `nl` for nl-NL. However, a few bugs are still present at this moment so it is not advised to use these aliases together with this strategy. diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ff65dc0..c00b5ee 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -4,4 +4,10 @@ ./tests + + + + src + + diff --git a/src/SlmLocale/Locale/Detector.php b/src/SlmLocale/Locale/Detector.php index 45c9227..86f061c 100644 --- a/src/SlmLocale/Locale/Detector.php +++ b/src/SlmLocale/Locale/Detector.php @@ -40,13 +40,13 @@ namespace SlmLocale\Locale; +use Laminas\EventManager\EventManagerAwareInterface; +use Laminas\EventManager\EventManagerInterface; +use Laminas\Stdlib\RequestInterface; +use Laminas\Stdlib\ResponseInterface; +use Laminas\Uri\Uri; use SlmLocale\LocaleEvent; use SlmLocale\Strategy\StrategyInterface; -use Zend\EventManager\EventManagerAwareInterface; -use Zend\EventManager\EventManagerInterface; -use Zend\Stdlib\RequestInterface; -use Zend\Stdlib\ResponseInterface; -use Zend\Uri\Uri; class Detector implements EventManagerAwareInterface { diff --git a/src/SlmLocale/LocaleEvent.php b/src/SlmLocale/LocaleEvent.php index 07e9700..4ebb3ce 100644 --- a/src/SlmLocale/LocaleEvent.php +++ b/src/SlmLocale/LocaleEvent.php @@ -40,10 +40,10 @@ namespace SlmLocale; -use Zend\EventManager\Event; -use Zend\Stdlib\RequestInterface; -use Zend\Stdlib\ResponseInterface; -use Zend\Uri\Uri; +use Laminas\EventManager\Event; +use Laminas\Stdlib\RequestInterface; +use Laminas\Stdlib\ResponseInterface; +use Laminas\Uri\Uri; class LocaleEvent extends Event { diff --git a/src/SlmLocale/Module.php b/src/SlmLocale/Module.php index 1b7c6d1..b4aa787 100644 --- a/src/SlmLocale/Module.php +++ b/src/SlmLocale/Module.php @@ -40,13 +40,13 @@ namespace SlmLocale; -use Locale; +use Laminas\EventManager\EventInterface; +use Laminas\ModuleManager\Feature; +use Laminas\Mvc\MvcEvent; +use Laminas\Stdlib\ResponseInterface; +use Locale; use SlmLocale\Locale\Detector; -use Zend\EventManager\EventInterface; -use Zend\ModuleManager\Feature; -use Zend\Mvc\MvcEvent; -use Zend\Stdlib\ResponseInterface; class Module implements Feature\ConfigProviderInterface, diff --git a/src/SlmLocale/Service/PrimaryLanguageHelperFactory.php b/src/SlmLocale/Service/PrimaryLanguageHelperFactory.php new file mode 100644 index 0000000..89f0844 --- /dev/null +++ b/src/SlmLocale/Service/PrimaryLanguageHelperFactory.php @@ -0,0 +1,59 @@ + + * @copyright 2012-2013 Jurian Sluiman. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://juriansluiman.nl + */ +namespace SlmLocale\Service; + +use Interop\Container\ContainerInterface; +use Laminas\ServiceManager\Factory\FactoryInterface; +use Locale; +use SlmLocale\View\Helper\PrimaryLanguage; + +final class PrimaryLanguageHelperFactory implements FactoryInterface +{ + /** + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options + * @return object|PrimaryLanguage + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + { + return new PrimaryLanguage(new Locale()); + } +} diff --git a/src/SlmLocale/Strategy/AbstractStrategy.php b/src/SlmLocale/Strategy/AbstractStrategy.php index a18f24a..ccd28d6 100644 --- a/src/SlmLocale/Strategy/AbstractStrategy.php +++ b/src/SlmLocale/Strategy/AbstractStrategy.php @@ -40,11 +40,11 @@ namespace SlmLocale\Strategy; +use Laminas\EventManager\AbstractListenerAggregate; +use Laminas\EventManager\EventManagerInterface; +use Laminas\Http\Request as HttpRequest; +use Laminas\Stdlib\RequestInterface; use SlmLocale\LocaleEvent; -use Zend\EventManager\AbstractListenerAggregate; -use Zend\EventManager\EventManagerInterface; -use Zend\Http\Request as HttpRequest; -use Zend\Stdlib\RequestInterface; abstract class AbstractStrategy extends AbstractListenerAggregate implements StrategyInterface { diff --git a/src/SlmLocale/Strategy/CookieStrategy.php b/src/SlmLocale/Strategy/CookieStrategy.php index 844ce58..1f6dddf 100644 --- a/src/SlmLocale/Strategy/CookieStrategy.php +++ b/src/SlmLocale/Strategy/CookieStrategy.php @@ -40,10 +40,10 @@ namespace SlmLocale\Strategy; +use Laminas\Http\Header\Cookie; +use Laminas\Http\Header\SetCookie; use SlmLocale\LocaleEvent; use SlmLocale\Strategy\Exception\InvalidArgumentException; -use Zend\Http\Header\Cookie; -use Zend\Http\Header\SetCookie; class CookieStrategy extends AbstractStrategy { diff --git a/src/SlmLocale/Strategy/StrategyInterface.php b/src/SlmLocale/Strategy/StrategyInterface.php index 5e80d50..5d13833 100644 --- a/src/SlmLocale/Strategy/StrategyInterface.php +++ b/src/SlmLocale/Strategy/StrategyInterface.php @@ -40,7 +40,7 @@ namespace SlmLocale\Strategy; -use Zend\EventManager\ListenerAggregateInterface; +use Laminas\EventManager\ListenerAggregateInterface; interface StrategyInterface extends ListenerAggregateInterface { diff --git a/src/SlmLocale/Strategy/StrategyPluginManager.php b/src/SlmLocale/Strategy/StrategyPluginManager.php index ae9a005..21599bb 100644 --- a/src/SlmLocale/Strategy/StrategyPluginManager.php +++ b/src/SlmLocale/Strategy/StrategyPluginManager.php @@ -40,9 +40,9 @@ namespace SlmLocale\Strategy; +use Laminas\ServiceManager\AbstractPluginManager; +use Laminas\ServiceManager\Factory\InvokableFactory; use SlmLocale\Strategy\Factory\UriPathStrategyFactory; -use Zend\ServiceManager\AbstractPluginManager; -use Zend\ServiceManager\Factory\InvokableFactory; class StrategyPluginManager extends AbstractPluginManager { diff --git a/src/SlmLocale/Strategy/UriPathStrategy.php b/src/SlmLocale/Strategy/UriPathStrategy.php index 4d9fe21..f9a37ee 100644 --- a/src/SlmLocale/Strategy/UriPathStrategy.php +++ b/src/SlmLocale/Strategy/UriPathStrategy.php @@ -40,10 +40,11 @@ namespace SlmLocale\Strategy; +use Laminas\Router\Http\TreeRouteStack; +use Laminas\Router\SimpleRouteStack; +use Laminas\Stdlib\RequestInterface; +use Laminas\Uri\Uri; use SlmLocale\LocaleEvent; -use Zend\Router\Http\TreeRouteStack; -use Zend\Router\SimpleRouteStack; -use Zend\Uri\Uri; class UriPathStrategy extends AbstractStrategy { @@ -103,7 +104,7 @@ public function detect(LocaleEvent $event) return; } - $base = $this->getBasePath(); + $base = $this->getBasePath($request); $locale = $this->getFirstSegmentInPath($request->getUri(), $base); if (! $locale) { return; @@ -144,7 +145,7 @@ public function found(LocaleEvent $event) } } - $base = $this->getBasePath(); + $base = $this->getBasePath($request); $found = $this->getFirstSegmentInPath($request->getUri(), $base); if ($this->router instanceof TreeRouteStack) { @@ -180,7 +181,7 @@ public function found(LocaleEvent $event) public function assemble(LocaleEvent $event) { $uri = $event->getUri(); - $base = $this->getBasePath(); + $base = $this->getBasePath($event->getRequest()); $locale = $event->getLocale(); if (! $this->redirectToCanonical() && null !== $this->getAliases()) { @@ -193,17 +194,34 @@ public function assemble(LocaleEvent $event) $path = $uri->getPath(); // Last part of base is now always locale, remove that - $parts = explode('/', trim($base, '/')); - array_pop($parts); + $parts = explode('/', trim($base, '/')); + $lastElement = count($parts) - 1; + + $removeFirstLocale = true; + if (null !== $this->default && + isset($parts[$lastElement]) && + ! in_array($parts[$lastElement], $event->getSupported(), true) && + $parts[$lastElement] !== $this->default + ) { + $removeFirstLocale = false; + } + + if (true === $removeFirstLocale) { + // Remove first part + array_pop($parts); + } + $base = implode('/', $parts); if ($base) { - $path = substr($path, strlen($base)); + $path = substr(trim($path, '/'), strlen($base)); } - $parts = explode('/', trim($path, '/')); - // Remove first part - array_shift($parts); + $parts = explode('/', trim($path, '/')); + if (true === $removeFirstLocale) { + // Remove first part + array_shift($parts); + } if ($locale === $this->default) { $locale = ''; @@ -211,7 +229,7 @@ public function assemble(LocaleEvent $event) $locale .= '/'; } - $path = $base . '/' . $locale . implode('/', $parts); + $path = ($base ? '/' : '') . trim($base, '/') . '/' . $locale . implode('/', $parts); $uri->setPath($path); return $uri; @@ -240,12 +258,21 @@ protected function getAliasForLocale($locale) } } - protected function getBasePath() + /** + * @param RequestInterface|null $request + * @return string|null + */ + protected function getBasePath(RequestInterface $request = null) { + $result = null; if ($this->router instanceof TreeRouteStack) { - return $this->router->getBaseUrl(); + $result = $this->router->getBaseUrl(); + } + + if (null === $result && null !== $request && method_exists($request, 'getBasePath')) { + $result = $request->getBasePath(); } - return null; + return $result; } } diff --git a/src/SlmLocale/View/Helper/LocaleMenu.php b/src/SlmLocale/View/Helper/LocaleMenu.php index ade280e..384a496 100644 --- a/src/SlmLocale/View/Helper/LocaleMenu.php +++ b/src/SlmLocale/View/Helper/LocaleMenu.php @@ -40,10 +40,10 @@ namespace SlmLocale\View\Helper; +use Laminas\View\Exception\RuntimeException; +use Laminas\View\Helper\AbstractHelper; use Locale; use SlmLocale\Locale\Detector; -use Zend\View\Exception\RuntimeException; -use Zend\View\Helper\AbstractHelper; class LocaleMenu extends AbstractHelper { @@ -230,7 +230,7 @@ public function __invoke() /** * @param array $options * @return string - * @todo implement add way to completely default rendering for maximum flexibility (see Zend\View\Helper\Navigation::renderPartial) + * @todo implement add way to completely default rendering for maximum flexibility (see Laminas\View\Helper\Navigation::renderPartial) */ public function __toString() { diff --git a/src/SlmLocale/View/Helper/LocaleUrl.php b/src/SlmLocale/View/Helper/LocaleUrl.php index e339b76..0e18571 100644 --- a/src/SlmLocale/View/Helper/LocaleUrl.php +++ b/src/SlmLocale/View/Helper/LocaleUrl.php @@ -39,11 +39,11 @@ */ namespace SlmLocale\View\Helper; +use Laminas\Http\Request; +use Laminas\Router\Http\RouteMatch; +use Laminas\View\Exception\RuntimeException; +use Laminas\View\Helper\AbstractHelper; use SlmLocale\Locale\Detector; -use Zend\Http\Request; -use Zend\Router\Http\RouteMatch; -use Zend\View\Exception\RuntimeException; -use Zend\View\Helper\AbstractHelper; class LocaleUrl extends AbstractHelper { @@ -84,7 +84,7 @@ protected function getRequest() /** * Generates an localized url * - * @see \Zend\View\Helper\Url::__invoke() + * @see \Laminas\View\Helper\Url::__invoke() * @param string $locale Locale * @param string $name Name of the route * @param array $params Parameters for the link diff --git a/src/SlmLocale/View/Helper/PrimaryLanguage.php b/src/SlmLocale/View/Helper/PrimaryLanguage.php new file mode 100644 index 0000000..81fbef6 --- /dev/null +++ b/src/SlmLocale/View/Helper/PrimaryLanguage.php @@ -0,0 +1,66 @@ + + * @copyright 2012-2013 Jurian Sluiman. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://juriansluiman.nl + */ +namespace SlmLocale\View\Helper; + +use Laminas\View\Helper\AbstractHelper; +use Locale; + +final class PrimaryLanguage extends AbstractHelper +{ + /** @var Locale */ + protected $locale; + + /** + * PrimaryLanguage constructor. + * @param Locale $locale + */ + public function __construct(Locale $locale) + { + $this->locale = $locale; + } + + /** + * @return string + */ + public function __invoke() + { + return $this->locale->getPrimaryLanguage($this->locale->getDefault()); + } +} diff --git a/tests/SlmLocaleTest/Locale/DetectorTest.php b/tests/SlmLocaleTest/Locale/DetectorTest.php index 289c053..0309f24 100644 --- a/tests/SlmLocaleTest/Locale/DetectorTest.php +++ b/tests/SlmLocaleTest/Locale/DetectorTest.php @@ -39,21 +39,20 @@ */ namespace SlmLocaleTest\Locale; -use PHPUnit_Framework_TestCase as TestCase; +use Laminas\EventManager\EventManager; +use Laminas\Stdlib\Request; +use Laminas\Stdlib\Response; +use PHPUnit\Framework\TestCase; 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; class DetectorTest extends TestCase { private $events; - public function setUp() + public function setUp(): void { $this->events = new EventManager(); } @@ -202,8 +201,8 @@ public function testEmptyMappingsListIndicatesNoMappingsList() public function testStrategyAttachesToEventManager() { $detector = new Detector(); - $events = $this->getMock(EventManager::class); - $strategy = $this->getMock(StrategyInterface::class); + $events = $this->createMock(EventManager::class); + $strategy = $this->createMock(StrategyInterface::class); $strategy->expects($this->once()) ->method('attach') ->with($events); @@ -217,12 +216,18 @@ public function testStrategyWithHighestPriorityWins() $detector = new Detector(); $this->setEventManager($detector); - $strategy1 = $this->getMock(AbstractStrategy::class, ['detect']); + $strategy1 = $this + ->getMockBuilder(AbstractStrategy::class) + ->setMethods(['detect']) + ->getMock(); $strategy1->expects($this->once()) ->method('detect') ->will($this->returnValue('Foo')); - $strategy2 = $this->getMock(AbstractStrategy::class, ['detect']); + $strategy2 = $this + ->getMockBuilder(AbstractStrategy::class) + ->setMethods(['detect']) + ->getMock(); $strategy2->expects($this->never()) ->method('detect'); diff --git a/tests/SlmLocaleTest/ModuleTest.php b/tests/SlmLocaleTest/ModuleTest.php index c1a49f7..1894792 100644 --- a/tests/SlmLocaleTest/ModuleTest.php +++ b/tests/SlmLocaleTest/ModuleTest.php @@ -2,15 +2,15 @@ namespace SlmLocaleTest\Locale; -use PHPUnit_Framework_TestCase as TestCase; +use Laminas\EventManager\EventManager; +use Laminas\Mvc\Application; +use Laminas\Mvc\MvcEvent; +use Laminas\ServiceManager\ServiceManager; +use Laminas\Stdlib\Request; +use Laminas\Stdlib\Response; +use PHPUnit\Framework\TestCase; use SlmLocale\Locale\Detector; use SlmLocale\Module; -use Zend\EventManager\EventManager; -use Zend\Mvc\Application; -use Zend\Mvc\MvcEvent; -use Zend\ServiceManager\ServiceManager; -use Zend\Stdlib\Request; -use Zend\Stdlib\Response; class ModuleTest extends TestCase { diff --git a/tests/SlmLocaleTest/Service/DetectFactoryTest.php b/tests/SlmLocaleTest/Service/DetectFactoryTest.php index ddb3ff8..f115a17 100644 --- a/tests/SlmLocaleTest/Service/DetectFactoryTest.php +++ b/tests/SlmLocaleTest/Service/DetectFactoryTest.php @@ -39,15 +39,14 @@ */ namespace SlmLocaleTest\Locale; -use PHPUnit_Framework_TestCase as TestCase; - +use Laminas\EventManager\EventManager; +use Laminas\ServiceManager\ServiceManager; +use PHPUnit\Framework\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; class DetectFactoryTest extends TestCase { @@ -125,7 +124,7 @@ public function testUseServiceLocatorToInstantiateStrategy() $plugins->setFactory('TestStrategy', function () use ($self, &$called) { $called = true; - return $self->getMock(StrategyInterface::class); + return $self->createMock(StrategyInterface::class); }); $detector = $sl->get(Detector::class); @@ -144,14 +143,14 @@ public function testConfigurationCanHoldMultipleStrategies() $plugins->setFactory('TestStrategy1', function () use ($self, &$called1) { $called1 = true; - return $self->getMock(StrategyInterface::class); + return $self->createMock(StrategyInterface::class); }); $called2 = false; $plugins->setFactory('TestStrategy2', function () use ($self, &$called2) { $called2 = true; - return $self->getMock(StrategyInterface::class); + return $self->createMock(StrategyInterface::class); }); $detector = $sl->get(Detector::class); @@ -173,7 +172,7 @@ public function testStrategyConfigurationCanBeAnArray() $plugins->setFactory('TestStrategy', function () use ($self, &$called) { $called = true; - return $self->getMock(StrategyInterface::class); + return $self->createMock(StrategyInterface::class); }); $detector = $sl->get(Detector::class); @@ -189,7 +188,7 @@ public function testStrategyCanBeAttachedWithPriorities() ]); $em = $sl->get('EventManager'); - $strategy = $this->getMock(StrategyInterface::class, ['attach', 'detach']); + $strategy = $this->createMock(StrategyInterface::class, ['attach', 'detach']); $strategy->expects($this->once()) ->method('attach') ->with($em, 100); @@ -206,7 +205,10 @@ public function testStrategyCanBeInstantiatedWithOptions() ['name' => 'TestStrategy', 'options' => 'Foo'], ], ]); - $strategy = $this->getMock(StrategyInterface::class, ['attach', 'detach', 'setOptions']); + + $strategy = $this->getMockBuilder(StrategyInterface::class) + ->setMethods(['attach', 'detach', 'setOptions']) + ->getMock(); $strategy->expects($this->once()) ->method('setOptions') ->with('Foo'); diff --git a/tests/SlmLocaleTest/Strategy/AssetStrategyTest.php b/tests/SlmLocaleTest/Strategy/AssetStrategyTest.php index c8a5fff..a6d5fb6 100644 --- a/tests/SlmLocaleTest/Strategy/AssetStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/AssetStrategyTest.php @@ -2,15 +2,15 @@ namespace SlmLocaleTest\Strategy; -use PHPUnit_Framework_TestCase as TestCase; +use Laminas\Console\Request as ConsoleRequest; +use Laminas\Console\Response as ConsoleResponse; +use Laminas\EventManager\EventManager; +use Laminas\Http\PhpEnvironment\Request as HttpRequest; +use Laminas\Http\PhpEnvironment\Response as HttpResponse; +use PHPUnit\Framework\TestCase; use SlmLocale\Locale\Detector; use SlmLocale\LocaleEvent; use SlmLocale\Strategy\AssetStrategy; -use Zend\Console\Request as ConsoleRequest; -use Zend\Console\Response as ConsoleResponse; -use Zend\EventManager\EventManager; -use Zend\Http\PhpEnvironment\Request as HttpRequest; -use Zend\Http\PhpEnvironment\Response as HttpResponse; class AssetStrategyTest extends TestCase { @@ -20,7 +20,7 @@ class AssetStrategyTest extends TestCase /** @var LocaleEvent */ private $event; - public function setup() + public function setUp(): void { $this->strategy = new AssetStrategy(); $this->strategy->setOptions(['file_extensions' => ['css', 'js']]); diff --git a/tests/SlmLocaleTest/Strategy/CookieStrategyTest.php b/tests/SlmLocaleTest/Strategy/CookieStrategyTest.php index ca611bd..ca54f34 100644 --- a/tests/SlmLocaleTest/Strategy/CookieStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/CookieStrategyTest.php @@ -39,21 +39,20 @@ */ namespace SlmLocaleTest\Locale; -use PHPUnit_Framework_TestCase as TestCase; - +use Laminas\Http\Header\Cookie; +use Laminas\Http\Request as HttpRequest; +use Laminas\Http\Response as HttpResponse; +use PHPUnit\Framework\TestCase; use SlmLocale\LocaleEvent; use SlmLocale\Strategy\CookieStrategy; - -use Zend\Http\Header\Cookie; -use Zend\Http\Request as HttpRequest; -use Zend\Http\Response as HttpResponse; +use SlmLocale\Strategy\Exception\InvalidArgumentException; class CookieStrategyTest extends TestCase { protected $strategy; protected $event; - public function setUp() + public function setUp(): void { $this->strategy = new CookieStrategy(); $this->event = new LocaleEvent(); @@ -176,12 +175,10 @@ public function testLocaleInCookieIsReturnedIfNameChanged() $this->assertEquals('foo', $locale); } - /** - * @expectedException \SlmLocale\Strategy\Exception\InvalidArgumentException - */ public function testInvalidCookieNameFails() { $strategy = $this->strategy; + $this->expectException(InvalidArgumentException::class); $strategy->setCookieName('$ThisIsAnInvalidCookieName'); } } diff --git a/tests/SlmLocaleTest/Strategy/HostStrategyTest.php b/tests/SlmLocaleTest/Strategy/HostStrategyTest.php index a54e22d..d6f1374 100644 --- a/tests/SlmLocaleTest/Strategy/HostStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/HostStrategyTest.php @@ -2,13 +2,14 @@ namespace SlmLocaleTest\Strategy; -use PHPUnit_Framework_TestCase as TestCase; +use Laminas\Http\PhpEnvironment\Request; +use Laminas\Stdlib\Parameters; +use Laminas\Stdlib\RequestInterface; +use Laminas\Uri\Uri; +use PHPUnit\Framework\TestCase; use SlmLocale\LocaleEvent; +use SlmLocale\Strategy\Exception\InvalidArgumentException; 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 { @@ -24,24 +25,23 @@ public function testDetectNonHttpRequestReturnsNull() public function testDetectWithoutSupportedReturnsNull() { $event = new LocaleEvent(); - $event->setRequest($this->getMockForAbstractClass(\Zend\Http\Request::class)); + $event->setRequest($this->getMockForAbstractClass(\Laminas\Http\Request::class)); $event->setSupported([]); $strategy = new HostStrategy(); $this->assertNull($strategy->detect($event)); } - /** - * @expectedException \SlmLocale\Strategy\Exception\InvalidArgumentException - */ public function testDetectWithoutDomainThrowsInvalidArgumentException() { $event = new LocaleEvent(); - $event->setRequest($this->getMockForAbstractClass(\Zend\Http\Request::class)); + $event->setRequest($this->getMockForAbstractClass(\Laminas\Http\Request::class)); $event->setSupported(['en_GB', 'de_DE']); $strategy = new HostStrategy(); $strategy->setOptions(['domain' => 'test']); + + $this->expectException(InvalidArgumentException::class); $this->assertNull($strategy->detect($event)); } diff --git a/tests/SlmLocaleTest/Strategy/HttpAcceptLanguageStrategyTest.php b/tests/SlmLocaleTest/Strategy/HttpAcceptLanguageStrategyTest.php index f82a8a1..01d3166 100644 --- a/tests/SlmLocaleTest/Strategy/HttpAcceptLanguageStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/HttpAcceptLanguageStrategyTest.php @@ -39,20 +39,18 @@ */ namespace SlmLocaleTest\Locale; -use PHPUnit_Framework_TestCase as TestCase; - +use Laminas\Http\Header\AcceptLanguage; +use Laminas\Http\Request as HttpRequest; +use PHPUnit\Framework\TestCase; use SlmLocale\LocaleEvent; use SlmLocale\Strategy\HttpAcceptLanguageStrategy; -use Zend\Http\Header\AcceptLanguage; -use Zend\Http\Request as HttpRequest; - class HttpAcceptLanguageStrategyTest extends TestCase { protected $strategy; protected $event; - public function setUp() + public function setUp(): void { $this->strategy = new HttpAcceptLanguageStrategy(); $this->event = new LocaleEvent(); diff --git a/tests/SlmLocaleTest/Strategy/QueryStrategyTest.php b/tests/SlmLocaleTest/Strategy/QueryStrategyTest.php index 8747e4f..9f80b47 100644 --- a/tests/SlmLocaleTest/Strategy/QueryStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/QueryStrategyTest.php @@ -39,15 +39,13 @@ */ namespace SlmLocaleTest\Locale; -use PHPUnit_Framework_TestCase as TestCase; - +use Laminas\Http\Request as HttpRequest; +use Laminas\Http\Response as HttpResponse; +use Laminas\Uri\Uri; +use PHPUnit\Framework\TestCase; use SlmLocale\LocaleEvent; use SlmLocale\Strategy\QueryStrategy; -use Zend\Http\Request as HttpRequest; -use Zend\Http\Response as HttpResponse; -use Zend\Uri\Uri; - class QueryStrategyTest extends TestCase { /** @@ -59,7 +57,7 @@ class QueryStrategyTest extends TestCase */ protected $event; - public function setUp() + public function setUp(): void { $this->strategy = new QueryStrategy(); $this->event = new LocaleEvent(); diff --git a/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php b/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php index 886cfd6..4446087 100644 --- a/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php @@ -39,18 +39,18 @@ */ namespace SlmLocaleTest\Locale; -use PHPUnit_Framework_TestCase as TestCase; +use Laminas\Console\Request as ConsoleRequest; +use Laminas\Console\Response as ConsoleResponse; +use Laminas\Http\PhpEnvironment\Request as HttpRequest; +use Laminas\Http\PhpEnvironment\Response as HttpResponse; +use Laminas\Mvc\Console\Router\SimpleRouteStack as ConsoleRouter; +use Laminas\Router\Http\TreeRouteStack as HttpRouter; +use Laminas\ServiceManager\ServiceManager; +use Laminas\Uri\Uri; +use PHPUnit\Framework\TestCase; use SlmLocale\LocaleEvent; use SlmLocale\Strategy\StrategyPluginManager; use SlmLocale\Strategy\UriPathStrategy; -use Zend\Console\Request as ConsoleRequest; -use Zend\Console\Response as ConsoleResponse; -use Zend\Http\PhpEnvironment\Request as HttpRequest; -use Zend\Http\PhpEnvironment\Response as HttpResponse; -use Zend\Mvc\Console\Router\SimpleRouteStack as ConsoleRouter; -use Zend\Router\Http\TreeRouteStack as HttpRouter; -use Zend\ServiceManager\ServiceManager; -use Zend\Uri\Uri; class UriPathStrategyTest extends TestCase { @@ -61,7 +61,7 @@ class UriPathStrategyTest extends TestCase /** @var HttpRouter */ private $router; - public function setup() + public function setUp(): void { $this->router = new HttpRouter(); @@ -127,8 +127,7 @@ public function testDetectWithBaseUrlReturnsRightPartOfPath() $this->event->setResponse(new HttpResponse()); $locale = $this->strategy->detect($this->event); - $expected = 'en'; - $this->assertEquals($expected, $locale); + $this->assertSame('en', $locale); } public function testFoundRedirectsByDefault() @@ -147,7 +146,7 @@ public function testFoundRedirectsByDefault() $header = $this->event->getResponse()->getHeaders()->get('Location'); $expected = 'Location: http://username:password@example.com:8080/en/some/deep/path/some.file?withsomeparam=true'; $this->assertEquals(302, $statusCode); - $this->assertContains($expected, (string) $header); + $this->assertStringContainsString($expected, (string) $header); } public function testFoundShouldRespectDisabledRedirectWhenFound() @@ -169,6 +168,46 @@ public function testFoundShouldRespectDisabledRedirectWhenFound() $this->assertFalse($header); } + public function testFoundRedirectsByDefaultWithBasePath() + { + $uri = 'http://example.com/my-app/public/nl/some/deep/path/some.file?withsomeparam=true'; + $request = new HttpRequest(); + $request->setUri($uri); + $request->setBasePath('/my-app/public'); + + $this->event->setLocale('en'); + $this->event->setRequest($request); + $this->event->setResponse(new HttpResponse()); + + $this->strategy->found($this->event); + + $statusCode = $this->event->getResponse()->getStatusCode(); + $header = $this->event->getResponse()->getHeaders()->get('Location'); + $expected = 'Location: http://example.com/my-app/public/en/some/deep/path/some.file?withsomeparam=true'; + $this->assertEquals(302, $statusCode); + $this->assertStringContainsString($expected, (string) $header); + } + + public function testFoundRedirectsByDefaultWithBasePathDisabledRedirectWhenFound() + { + $this->strategy->setOptions(['redirect_when_found' => false]); + $uri = 'http://example.com/my-app/public/nl/some/deep/path/some.file?withsomeparam=true'; + $request = new HttpRequest(); + $request->setUri($uri); + $request->setBasePath('/my-app/public'); + + $this->event->setLocale('en'); + $this->event->setRequest($request); + $this->event->setResponse(new HttpResponse()); + + $this->strategy->found($this->event); + + $statusCode = $this->event->getResponse()->getStatusCode(); + $header = $this->event->getResponse()->getHeaders()->has('Location'); + $this->assertNotEquals(302, $statusCode); + $this->assertFalse($header); + } + // public function testFoundWithDisabledRedirectWhenFoundOptionLocaleShouldStillBeDirectedAnywayWhenPathContainsNothingFurther() // { // $this->strategy->setOptions(['redirect_when_found' => false]); @@ -186,7 +225,7 @@ public function testFoundShouldRespectDisabledRedirectWhenFound() // $locale = $this->strategy->found($this->event); // $this->assertEquals($this->event->getResponse()->getStatusCode(), 302); - // $this->assertContains($this->event->getResponse()->getHeaders()->toString(), "Location: http://example.com/en/\r\n"); + // $this->assertStringContainsString($this->event->getResponse()->getHeaders()->toString(), "Location: http://example.com/en/\r\n"); // } // public function testFoundWithDisabledRedirectWhenFoundOptionLocaleShouldStillBeDirectedAnyway() @@ -391,6 +430,8 @@ public function testAssembleWithDefault() $this->event->setLocale('en'); $this->event->setUri($uri); + $this->router->setBaseUrl('/nl'); + $this->strategy = new UriPathStrategy($this->router); $this->strategy->setOptions([ 'default' => 'en', ]); @@ -409,15 +450,79 @@ public function testAssembleWithDefaultNotMatching() $this->event->setLocale('en'); $this->event->setUri($uri); + $this->router->setBaseUrl('/nl'); + $this->strategy = new UriPathStrategy($this->router); $this->strategy->setOptions([ 'default' => 'fr', ]); $this->strategy->assemble($this->event); - $expected = '/en/foo/bar/baz'; - $actual = $this->event->getUri()->getPath(); + $this->assertSame('/en/foo/bar/baz', $this->event->getUri()->getPath()); + } - $this->assertSame($expected, $actual); + public function testAssembleWithDefaultWithBasePath() + { + $uri = new Uri('/my-app/nl/foo/bar/baz'); + + $this->event->setLocale('en'); + $this->event->setUri($uri); + + $this->router->setBaseUrl('/my-app/nl'); + $this->strategy = new UriPathStrategy($this->router); + $this->strategy->setOptions([ + 'default' => 'fr', + ]); + $this->strategy->assemble($this->event); + + $this->assertSame('/my-app/en/foo/bar/baz', $this->event->getUri()->getPath()); + } + + public function testAssembleWithDefaultWithBasePathWithMatching() + { + $uri = new Uri('/my-app/foo/bar/baz'); + + $this->event->setLocale('en'); + $this->event->setUri($uri); + + $this->router->setBaseUrl('/my-app'); + $this->strategy = new UriPathStrategy($this->router); + $this->strategy->setOptions([ + 'default' => 'nl', + ]); + $this->strategy->assemble($this->event); + + $this->assertSame('/my-app/en/foo/bar/baz', $this->event->getUri()->getPath()); + } + + public function testAssembleWithDefaultWithBasePathWithMatchingPubic() + { + $uri = new Uri('/my-app/public/foo/bar/baz'); + + $this->event->setLocale('en'); + $this->event->setUri($uri); + + $this->router->setBaseUrl('/my-app/public'); + $this->strategy = new UriPathStrategy($this->router); + $this->strategy->setOptions([ + 'default' => 'nl', + ]); + $this->strategy->assemble($this->event); + + $this->assertSame('/my-app/public/en/foo/bar/baz', $this->event->getUri()->getPath()); + } + + public function testAssembleWithBasePathWithMatchingLanguageName() + { + $uri = new Uri('/my-app/nl/nl/foo/bar/baz'); + + $this->event->setLocale('en'); + $this->event->setUri($uri); + + $this->router->setBaseUrl('/my-app/nl/nl'); + $this->strategy = new UriPathStrategy($this->router); + $this->strategy->assemble($this->event); + + $this->assertSame('/my-app/nl/en/foo/bar/baz', $this->event->getUri()->getPath()); } public function testDisableUriPathStrategyPhpunit() @@ -442,6 +547,21 @@ public function testDisableUriPathStrategyPhpunit() $_SERVER['DISABLE_URIPATHSTRATEGY'] = false; } + public function testAssembleWithDefaultMatchingCurrent() + { + $uri = new Uri('/foo/bar/baz'); + + $this->event->setLocale('en'); + $this->event->setUri($uri); + + $this->strategy->setOptions([ + 'default' => 'fr', + ]); + $this->strategy->assemble($this->event); + + $this->assertSame('/en/foo/bar/baz', $this->event->getUri()->getPath()); + } + protected function getPluginManager($console = false) { $sl = new ServiceManager(); diff --git a/tests/SlmLocaleTest/View/Helper/PrimaryLanguageTest.php b/tests/SlmLocaleTest/View/Helper/PrimaryLanguageTest.php new file mode 100644 index 0000000..8afcc00 --- /dev/null +++ b/tests/SlmLocaleTest/View/Helper/PrimaryLanguageTest.php @@ -0,0 +1,56 @@ + + * @copyright 2012-2013 Jurian Sluiman. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://juriansluiman.nl + */ +namespace SlmLocaleTest\View\Helper; + +use Locale; +use PHPUnit\Framework\TestCase; +use SlmLocale\View\Helper\PrimaryLanguage; + +final class PrimaryLanguageTest extends TestCase +{ + public function testInvoke() + { + $locale = new Locale(); + $locale->setDefault('de-DE'); + $primaryLanguage = new PrimaryLanguage($locale); + + $this->assertSame('de', $primaryLanguage()); + } +}