From e1a299ac1f1b64b83d5fe9551399d96e20939ccd Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Wed, 18 Sep 2013 09:57:00 +0200 Subject: [PATCH] Add assembling method for query strategy --- src/SlmLocale/Strategy/QueryStrategy.php | 16 +++++++ .../Strategy/QueryStrategyTest.php | 46 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/SlmLocale/Strategy/QueryStrategy.php b/src/SlmLocale/Strategy/QueryStrategy.php index d748c38..3fc39eb 100644 --- a/src/SlmLocale/Strategy/QueryStrategy.php +++ b/src/SlmLocale/Strategy/QueryStrategy.php @@ -100,4 +100,20 @@ public function detect(LocaleEvent $event) return $locale; } + /** + * {@inheritdoc} + */ + public function assemble(LocaleEvent $event) + { + $uri = $event->getUri(); + $locale = $event->getLocale(); + + $query = $uri->getQueryAsArray(); + $key = $this->getQueryKey(); + + $query[$key] = $locale; + + $uri->setQuery($query); + return $uri; + } } diff --git a/tests/SlmLocaleTest/Strategy/QueryStrategyTest.php b/tests/SlmLocaleTest/Strategy/QueryStrategyTest.php index e16b5a1..7f45e9f 100644 --- a/tests/SlmLocaleTest/Strategy/QueryStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/QueryStrategyTest.php @@ -46,6 +46,7 @@ use Zend\Http\Request as HttpRequest; use Zend\Http\Response as HttpResponse; +use Zend\Uri\Uri; class QueryStrategyTest extends TestCase { @@ -125,4 +126,49 @@ public function testQueryKeyCanBeModifiedAndHaveLocaleReturned() $this->assertEquals('locale', $locale); } + public function testAssemblingAddsQueryParameter() + { + $strategy = $this->strategy; + $event = $this->event; + $uri = new Uri('/'); + + $event->setLocale('en-US'); + $event->setUri($uri); + $strategy->assemble($event); + + $query = $event->getUri()->getQuery(); + $expected = 'lang=en-US'; + $this->assertEquals($expected, $query); + } + + public function testAssemblingReplacesExistingQueryParameter() + { + $strategy = $this->strategy; + $event = $this->event; + $uri = new Uri('/?lang=nl-NL'); + + $event->setLocale('en-US'); + $event->setUri($uri); + $strategy->assemble($event); + + $query = $event->getUri()->getQuery(); + $expected = 'lang=en-US'; + $this->assertEquals($expected, $query); + } + + public function testAsssemblingUsesQueryKeyParamter() + { + $strategy = $this->strategy; + $event = $this->event; + $uri = new Uri('/'); + + $event->setLocale('en-US'); + $event->setUri($uri); + $strategy->setOptions(array('query_key' => 'language')); + $strategy->assemble($event); + + $query = $event->getUri()->getQuery(); + $expected = 'language=en-US'; + $this->assertEquals($expected, $query); + } }