Skip to content

Commit

Permalink
Replace deprecated TranslatorInterface with new version.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Dec 17, 2024
1 parent a91c46b commit 4cc44bd
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 79 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"laminas/laminas-text": "2.11.0",
"laminas/laminas-validator": "2.64.2",
"laminas/laminas-view": "2.27.0",
"laminas/laminas-translator": "^1",
"league/commonmark": "2.6.0",
"league/oauth2-client": "^2.7",
"league/oauth2-server": "8.5.4",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion module/VuFind/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@
'Laminas\Session\SessionManager' => 'VuFind\Session\ManagerFactory',
],
'delegators' => [
'Laminas\I18n\Translator\TranslatorInterface' => [
'Laminas\Mvc\I18n\Translator' => [
'VuFind\I18n\Translator\TranslatorFactory',
],
'SlmLocale\Locale\Detector' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace VuFind\I18n\Translator;

use Laminas\I18n\Translator\TranslatorInterface;
use Laminas\Mvc\I18n\Translator;
use VuFind\Config\PathResolver;
use VuFind\I18n\Locale\LocaleSettings;

Expand Down Expand Up @@ -101,14 +101,14 @@ protected function getTextDomains(): array
/**
* Configure a translator to support the requested language.
*
* @param TranslatorInterface $translator Translator
* @param LocaleSettings $settings Locale settings
* @param string $language Language to set up
* @param Translator $translator Translator
* @param LocaleSettings $settings Locale settings
* @param string $language Language to set up
*
* @return void
*/
protected function addLanguageToTranslator(
TranslatorInterface $translator,
Translator $translator,
LocaleSettings $settings,
string $language
): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace VuFind\I18n\Translator;

use Laminas\I18n\Translator\TranslatorInterface;
use Laminas\Translator\TranslatorInterface;

/**
* Lightweight translator aware marker interface (used as an alternative to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace VuFind\I18n\Translator;

use Laminas\I18n\Translator\TranslatorInterface;
use Laminas\Translator\TranslatorInterface;

use function count;
use function is_array;
Expand All @@ -50,7 +50,7 @@ trait TranslatorAwareTrait
/**
* Translator
*
* @var \Laminas\I18n\Translator\TranslatorInterface
* @var TranslatorInterface
*/
protected $translator = null;

Expand All @@ -70,7 +70,7 @@ public function setTranslator(TranslatorInterface $translator)
/**
* Get translator object.
*
* @return \Laminas\I18n\Translator\TranslatorInterface
* @return TranslatorInterface
*/
public function getTranslator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace VuFind\I18n\Translator;

use Laminas\I18n\Translator\TranslatorInterface;
use Laminas\Mvc\I18n\Translator;
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\ServiceManager\Factory\DelegatorFactoryInterface;
Expand Down Expand Up @@ -94,13 +94,13 @@ public function __invoke(
/**
* Add caching to a translator object
*
* @param TranslatorInterface $translator Translator object
* @param ContainerInterface $container Service manager
* @param Translator $translator Translator object
* @param ContainerInterface $container Service manager
*
* @return void
*/
protected function enableCaching(
TranslatorInterface $translator,
Translator $translator,
ContainerInterface $container
): void {
// Set up language caching for better performance:
Expand Down
34 changes: 5 additions & 29 deletions module/VuFind/src/VuFind/ILS/Driver/Aleph.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

namespace VuFind\ILS\Driver;

use Laminas\I18n\Translator\TranslatorInterface;
use Laminas\Mvc\I18n\Translator;
use VuFind\Date\DateException;
use VuFind\Exception\ILS as ILSException;

Expand Down Expand Up @@ -78,27 +78,6 @@ class Aleph extends AbstractBase implements
*/
protected $alephTranslator = false;

/**
* Cache manager
*
* @var \VuFind\Cache\Manager
*/
protected $cacheManager;

/**
* Translator
*
* @var TranslatorInterface
*/
protected $translator;

/**
* Date converter object
*
* @var \VuFind\Date\Converter
*/
protected $dateConverter = null;

/**
* The base URL, where the REST DLF API is running
*
Expand Down Expand Up @@ -240,16 +219,13 @@ class Aleph extends AbstractBase implements
*
* @param \VuFind\Date\Converter $dateConverter Date converter
* @param ?\VuFind\Cache\Manager $cacheManager Cache manager (optional)
* @param ?TranslatorInterface $translator Translator (optional)
* @param ?Translator $translator Translator (optional)
*/
public function __construct(
\VuFind\Date\Converter $dateConverter,
?\VuFind\Cache\Manager $cacheManager = null,
?TranslatorInterface $translator = null
protected \VuFind\Date\Converter $dateConverter,
protected ?\VuFind\Cache\Manager $cacheManager = null,
protected ?Translator $translator = null
) {
$this->dateConverter = $dateConverter;
$this->cacheManager = $cacheManager;
$this->translator = $translator;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion module/VuFind/src/VuFind/Recommend/AuthorInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace VuFind\Recommend;

use Exception;
use Laminas\I18n\Translator\TranslatorInterface;
use Laminas\Translator\TranslatorInterface;
use VuFind\Connection\Wikipedia;
use VuFind\I18n\Translator\TranslatorAwareInterface;
use VuFindSearch\Query\Query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace VuFind\View\Helper\Root;

use Laminas\I18n\Translator\TranslatorInterface;
use Laminas\Translator\TranslatorInterface;

/**
* DisplayLanguageOption view helper
Expand All @@ -42,21 +42,13 @@
*/
class DisplayLanguageOption extends \Laminas\View\Helper\AbstractHelper
{
/**
* Translator (or null if unavailable)
*
* @var TranslatorInterface
*/
protected $translator = null;

/**
* Constructor
*
* @param TranslatorInterface $translator Main VuFind translator
* @param TranslatorInterface $translator Translator
*/
public function __construct(TranslatorInterface $translator)
public function __construct(protected TranslatorInterface $translator)
{
$this->translator = $translator;
}

/**
Expand Down
22 changes: 12 additions & 10 deletions module/VuFind/src/VuFindTest/Feature/TranslatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

namespace VuFindTest\Feature;

use Laminas\Mvc\I18n\Translator;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Trait for tests involving Laminas Translator.
*
Expand All @@ -46,21 +49,20 @@ trait TranslatorTrait
* @param array $translations Key => value translation map.
* @param string $locale Locale, default to 'en'
*
* @return \Laminas\I18n\Translator\TranslatorInterface
* @return MockObject&Translator
*/
protected function getMockTranslator(array $translations, string $locale = 'en')
protected function getMockTranslator(array $translations, string $locale = 'en'): MockObject&Translator
{
$callback = function ($str, $domain) use ($translations) {
return $translations[$domain][$str] ?? $str;
};
$translator
= $this->getMockBuilder(\Laminas\I18n\Translator\TranslatorInterface::class)
->addMethods(['getLocale'])
->getMockForAbstractClass();
$translator->expects($this->any())->method('translate')
->will($this->returnCallback($callback));
$translator->expects($this->any())->method('getLocale')
->will($this->returnValue($locale));
$translator = $this->getMockBuilder(Translator::class)
->disableOriginalConstructor()
->onlyMethods(['translate'])
->addMethods(['getLocale'])
->getMock();
$translator->expects($this->any())->method('translate')->willReturnCallback($callback);
$translator->expects($this->any())->method('getLocale')->willReturn($locale);
return $translator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

use DateTime;
use Laminas\Http\Request;
use Laminas\I18n\Translator\TranslatorInterface;
use Laminas\Session\SessionManager;
use Laminas\View\Renderer\PhpRenderer;
use PHPUnit\Event\NoPreviousThrowableException;
Expand All @@ -44,6 +43,7 @@
use VuFind\Mailer\Mailer;
use VuFind\Net\UserIpReader;
use VuFind\Validator\CsrfInterface;
use VuFindTest\Feature\TranslatorTrait;

/**
* Email Authenticator Manager Test Class
Expand All @@ -56,6 +56,8 @@
*/
class EmailAuthenticatorTest extends \PHPUnit\Framework\TestCase
{
use TranslatorTrait;

/**
* Get an EmailAuthenticator to test.
*
Expand Down Expand Up @@ -90,13 +92,7 @@ protected function getEmailAuthenticator(
new Config($config),
$authHashService ?? $this->createMock(AuthHashServiceInterface::class)
);
$mockTranslator = $this->createMock(TranslatorInterface::class);
$mockTranslator->method('translate')->willReturnCallback(
function ($str) {
return $str;
}
);
$authenticator->setTranslator($mockTranslator);
$authenticator->setTranslator($this->getMockTranslator([]));
return $authenticator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,7 @@ public function testLocaleWithoutTranslator(): void
public function testLocaleWithTranslator(): void
{
$translate = new Translate();
$translator = $this->createMock(\Laminas\I18n\Translator\Translator::class);
$translator->expects($this->once())->method('getLocale')
->will($this->returnValue('foo'));
$translate->setTranslator($translator);
$translate->setTranslator($this->getMockTranslator([], 'foo'));
$this->assertEquals('foo', $translate->getTranslatorLocale());
}

Expand All @@ -472,7 +469,7 @@ public function testLocaleWithTranslator(): void
public function testGetTranslator(): void
{
$translate = new Translate();
$translator = $this->createMock(\Laminas\I18n\Translator\TranslatorInterface::class);
$translator = $this->createMock(\Laminas\Translator\TranslatorInterface::class);
$translate->setTranslator($translator);
$this->assertEquals($translator, $translate->getTranslator());
}
Expand Down

0 comments on commit 4cc44bd

Please sign in to comment.