Skip to content

Commit

Permalink
Merge pull request #35 from juriansluiman/feature/query-assembling
Browse files Browse the repository at this point in the history
Add assembling method for query strategy
  • Loading branch information
Jurian Sluiman committed Sep 18, 2013
2 parents a9f3fad + e1a299a commit b9d5cfc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/SlmLocale/Strategy/QueryStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
46 changes: 46 additions & 0 deletions tests/SlmLocaleTest/Strategy/QueryStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

use Zend\Http\Request as HttpRequest;
use Zend\Http\Response as HttpResponse;
use Zend\Uri\Uri;

class QueryStrategyTest extends TestCase
{
Expand Down Expand Up @@ -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);
}
}

0 comments on commit b9d5cfc

Please sign in to comment.