Skip to content

Commit

Permalink
FRW-1817 Adjusted Dynamic Multistore Test Coverage (#10800)
Browse files Browse the repository at this point in the history
FRW-1817 Adjusted Dynamic Multistore Test Coverage
  • Loading branch information
vol4onok authored Mar 20, 2024
1 parent 104480d commit ae6cb34
Show file tree
Hide file tree
Showing 11 changed files with 614 additions and 33 deletions.
35 changes: 35 additions & 0 deletions src/Spryker/Glue/Locale/LocaleDependencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Spryker\Glue\Kernel\AbstractBundleDependencyProvider;
use Spryker\Glue\Kernel\Container;
use Spryker\Glue\Locale\Dependency\Client\LocaleToStoreClientBridge;
use Spryker\Shared\Kernel\Store;

/**
* @method \Spryker\Glue\Locale\LocaleConfig getConfig()
Expand All @@ -26,6 +27,13 @@ class LocaleDependencyProvider extends AbstractBundleDependencyProvider
*/
public const SERVICE_LOCALE = 'SERVICE_LOCALE';

/**
* @deprecated Will be removed after dynamic multi-store is always enabled.
*
* @var string
*/
public const STORE = 'STORE';

/**
* @param \Spryker\Glue\Kernel\Container $container
*
Expand All @@ -37,6 +45,7 @@ public function provideDependencies(Container $container): Container

$container = $this->addStoreClient($container);
$container = $this->addLocaleService($container);
$container = $this->addStore($container);

return $container;
}
Expand Down Expand Up @@ -70,4 +79,30 @@ protected function addLocaleService(Container $container): Container

return $container;
}

/**
* @deprecated Will be removed after dynamic multi-store is always enabled.
*
* @param \Spryker\Glue\Kernel\Container $container
*
* @return \Spryker\Glue\Kernel\Container
*/
protected function addStore(Container $container): Container
{
$container->set(static::STORE, function () {
return $this->getStore();
});

return $container;
}

/**
* @deprecated Will be removed after dynamic multi-store is always enabled.
*
* @return \Spryker\Shared\Kernel\Store
*/
protected function getStore(): Store
{
return Store::getInstance();
}
}
11 changes: 11 additions & 0 deletions src/Spryker/Glue/Locale/LocaleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Spryker\Glue\Locale\Negotiator\LanguageNegotiator;
use Spryker\Glue\Locale\Negotiator\LanguageNegotiatorInterface;
use Spryker\Service\Locale\LocaleServiceInterface;
use Spryker\Shared\Kernel\Store;

/**
* @method \Spryker\Client\Locale\LocaleClientInterface getClient()
Expand Down Expand Up @@ -46,4 +47,14 @@ public function getLocaleService(): LocaleServiceInterface
{
return $this->getProvidedDependency(LocaleDependencyProvider::SERVICE_LOCALE);
}

/**
* @deprecated Will be removed after dynamic multi-store is always enabled.
*
* @return \Spryker\Shared\Kernel\Store
*/
public function getStore(): Store
{
return $this->getProvidedDependency(LocaleDependencyProvider::STORE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Spryker\Service\Container\ContainerInterface;
use Spryker\Shared\ApplicationExtension\Dependency\Plugin\ApplicationPluginInterface;
use Spryker\Shared\Config\Application\Environment as ApplicationEnvironment;
use Spryker\Shared\Kernel\Store;
use Symfony\Component\HttpFoundation\RequestStack;

/**
Expand Down Expand Up @@ -95,7 +94,7 @@ protected function setStoreCurrentLocale(string $locale): void
return;
}

Store::getInstance()->setCurrentLocale($locale);
$this->getFactory()->getStore()->setCurrentLocale($locale);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Spryker\Glue\Locale\Dependency\Client\LocaleToStoreClientInterface;
use Spryker\Glue\Locale\Plugin\Application\LocaleApplicationPlugin;
use Spryker\Service\Container\ContainerInterface;
use Spryker\Shared\Kernel\Store;
use SprykerTest\Glue\Locale\LocaleGlueTester;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -131,6 +132,34 @@ public function testProvideShouldAddDefaultLocaleToContainerWhileAcceptLanguageG
$this->assertSame(static::LOCALES[static::LOCALE_KEY_DE], $container->get(static::APPLICATION_LOCALE));
}

/**
* @return void
*/
public function testProvideAddsLocaleToContainerForDynamicStore(): void
{
// Arrange
$requestStack = $this->createRequestStack(static::LOCALES[static::LOCALE_KEY_DE]);
$container = $this->createContainer($requestStack);
$localeApplicationPlugin = new LocaleApplicationPlugin();
$storeClientMock = $this->createMock(LocaleToStoreClientInterface::class);
$storeMock = $this->createMock(Store::class);
$storeMock->expects($this->never())->method('setCurrentLocale');

$this->tester->mockFactoryMethod('getClient', $this->createLocaleClientMock(static::LOCALES));
$storeClientMock->method('isDynamicStoreEnabled')->willReturn(true);
$this->tester->mockFactoryMethod('getStoreClient', $storeClientMock);
$this->tester->mockFactoryMethod('getStore', $storeMock);

$localeApplicationPlugin->setFactory($this->tester->getFactory());

// Act
$container = $localeApplicationPlugin->provide($container);

// Assert
$this->assertTrue($container->has(static::APPLICATION_LOCALE));
$this->assertSame(static::LOCALES[static::LOCALE_KEY_DE], $container->get(static::APPLICATION_LOCALE));
}

/**
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerTest\Yves\Locale\Plugin\Application;

use Codeception\Test\Unit;
use Generated\Shared\Transfer\LocaleTransfer;
use Spryker\Shared\Kernel\Container\ContainerProxy;
use Spryker\Shared\Kernel\Store;
use Spryker\Shared\LocaleExtension\Dependency\Plugin\LocalePluginInterface;
use Spryker\Yves\Locale\Dependency\Client\LocaleToStoreClientInterface;
use Spryker\Yves\Locale\Plugin\Application\LocaleApplicationPlugin;
use SprykerTest\Yves\Locale\LocaleBusinessTester;

/**
* Auto-generated group annotations
*
* @group SprykerTest
* @group Yves
* @group Locale
* @group Plugin
* @group Application
* @group LocaleApplicationPluginTest
* Add your own group annotations below this line
*/
class LocaleApplicationPluginTest extends Unit
{
/**
* @var \Spryker\Shared\Kernel\Container\ContainerProxy
*/
protected ContainerProxy $container;

/**
* @var \SprykerTest\Yves\Locale\LocaleBusinessTester
*/
protected LocaleBusinessTester $tester;

/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();

$this->container = $this->tester->getContainer();
}

/**
* @return void
*/
public function testProvidesLocale(): void
{
// Arrange
$localeToStoreClientMock = $this->createMock(LocaleToStoreClientInterface::class);
$localeToStoreClientMock->method('isDynamicStoreEnabled')->willReturn(false);

$localePluginMock = $this->createMock(LocalePluginInterface::class);
$localePluginMock->method('getLocaleTransfer')->willReturn((new LocaleTransfer())->setLocaleName($this->tester::LOCALE_DE));

$storeMock = $this->createMock(Store::class);
$storeMock->method('setCurrentLocale')->with($this->tester::LOCALE_DE);

$this->tester->mockFactoryMethod('getStoreClient', $localeToStoreClientMock);
$this->tester->mockFactoryMethod('getLocalePlugin', $localePluginMock);
$this->tester->mockFactoryMethod('getStore', $storeMock);

$localeApplicationPlugin = new LocaleApplicationPlugin();
$localeApplicationPlugin->setFactory($this->tester->getFactory());
$localeApplicationPlugin->provide($this->container);

// Act
$locale = $this->container->get($this->tester::SERVICE_LOCALE);

// Assert
$this->assertSame($locale, $this->tester::LOCALE_DE);
$this->assertSame($this->container->get($this->tester::BC_FEATURE_FLAG_LOCALE_LISTENER), false);
}

/**
* @return void
*/
public function testProvidesLocaleWhenDynamicStoreEnabled(): void
{
// Arrange
$localeToStoreClientMock = $this->createMock(LocaleToStoreClientInterface::class);
$localeToStoreClientMock->method('isDynamicStoreEnabled')->willReturn(true);

$localePluginMock = $this->createMock(LocalePluginInterface::class);
$localePluginMock->method('getLocaleTransfer')->willReturn((new LocaleTransfer())->setLocaleName($this->tester::LOCALE_DE));

$storeMock = $this->createMock(Store::class);
$storeMock->expects($this->never())->method('setCurrentLocale');

$this->tester->mockFactoryMethod('getStoreClient', $localeToStoreClientMock);
$this->tester->mockFactoryMethod('getLocalePlugin', $localePluginMock);
$this->tester->mockFactoryMethod('getStore', $storeMock);

$localeApplicationPlugin = new LocaleApplicationPlugin();
$localeApplicationPlugin->setFactory($this->tester->getFactory());
$localeApplicationPlugin->provide($this->container);

// Act
$locale = $this->container->get($this->tester::SERVICE_LOCALE);

// Assert
$this->assertSame($locale, $this->tester::LOCALE_DE);
$this->assertSame($this->container->get($this->tester::BC_FEATURE_FLAG_LOCALE_LISTENER), false);
}
}
Loading

0 comments on commit ae6cb34

Please sign in to comment.