/** * Perform the actual handleKernelSiteRequest method test */ protected function performHandleKernelRequestTest($url) { $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'); $request = SiteRequest::create($url); $event = new GetResponseEvent($kernel, $request, 'master'); $siteManager = $this->getMock('Sonata\\PageBundle\\Model\\SiteManagerInterface'); $decoratorStrategy = $this->getMock('Sonata\\PageBundle\\CmsManager\\DecoratorStrategyInterface'); $seoPage = $this->getMock('Sonata\\SeoBundle\\Seo\\SeoPageInterface'); $siteSelector = new HostPathSiteSelector($siteManager, $decoratorStrategy, $seoPage); // Stash the request object in the siteSelector request property $ref = new \ReflectionObject($siteSelector); $property = $ref->getProperty('request'); $property->setAccessible(true); $property->setValue($siteSelector, $request); $property = $ref->getProperty('request'); $property->setAccessible(false); // Look for the first site matched that is enabled, has started, and has not expired. // localhost is a possible match, but only if no other sites match. $siteSelector->handleKernelRequest($event); // The site found is stored in the protected property "site", so we use Reflection to access it. $ref = new \ReflectionObject($siteSelector); $property = $ref->getProperty('site'); $property->setAccessible(true); $site = $property->getValue($siteSelector); return array($site, $event); }
/** * Tests handleKernelRequest method selects the site /fr */ public function testHandleKernelRequestSelectsFr() { $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'); $request = SiteRequest::create('http://www.example.com', 'GET', array(), array(), array(), array('HTTP_ACCEPT_LANGUAGE' => 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4')); // Ensure request locale is null $this->assertNull($request->attributes->get('_locale')); $event = new GetResponseEvent($kernel, $request, 'master'); $this->siteSelector->expects($this->once())->method('getSites')->with($request)->will($this->returnValue($this->getSites())); $this->siteSelector->handleKernelRequest($event); // Ensure request locale is fr $this->assertEquals('fr', $request->attributes->get('_locale')); $site = $this->getSite(); // Ensure we retrieved the site "/fr" $this->assertEquals('/fr', $site->getRelativePath()); }
/** * Perform the actual handleKernelSiteRequest method test. */ protected function performHandleKernelRequestTest($url) { $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'); $request = SiteRequest::create($url); // Ensure request locale is null $this->assertNull($request->attributes->get('_locale')); $event = new GetResponseEvent($kernel, $request, 'master'); $siteManager = $this->getMock('Sonata\\PageBundle\\Model\\SiteManagerInterface'); $decoratorStrategy = $this->getMock('Sonata\\PageBundle\\CmsManager\\DecoratorStrategyInterface'); $seoPage = $this->getMock('Sonata\\SeoBundle\\Seo\\SeoPageInterface'); $siteSelector = new HostPathSiteSelector($siteManager, $decoratorStrategy, $seoPage); // Look for the first site matched that is enabled, has started, and has not expired. // localhost is a possible match, but only if no other sites match. $siteSelector->handleKernelRequest($event); $site = $siteSelector->retrieve(); return array($site, $event); }
/** * Tests handleKernelRequest method redirects to /fr. */ public function testHandleKernelRequestRedirectsToFr() { $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'); $request = SiteRequest::create('http://www.example.com', 'GET', array(), array(), array(), array('HTTP_ACCEPT_LANGUAGE' => 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4')); // Ensure request locale is null $this->assertNull($request->attributes->get('_locale')); $event = new GetResponseEvent($kernel, $request, 'master'); $this->siteSelector->expects($this->once())->method('getSites')->with($request)->will($this->returnValue($this->getSites())); $this->siteSelector->handleKernelRequest($event); // Ensure request locale is still null $this->assertNull($request->attributes->get('_locale')); $site = $this->getSite(); // Ensure no site was retrieved $this->assertNull($site); // Retrieve the event's response object $response = $event->getResponse(); // Ensure the response was a redirect to the default site $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $response); // Ensure the redirect url is for "/fr" $this->assertEquals('/fr', $response->getTargetUrl()); }