Skip to content

Commit

Permalink
Improve/modernize code.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Dec 17, 2024
1 parent f6c9323 commit afdc906
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace VuFind\I18n\Translator;

use Laminas\Translator\TranslatorInterface;
use Laminas\I18n\Translator\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
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\Translator\TranslatorInterface;
use Laminas\I18n\Translator\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
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 8 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\I18n\Translator\Translator;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Trait for tests involving Laminas Translator.
*
Expand All @@ -46,21 +49,16 @@ trait TranslatorTrait
* @param array $translations Key => value translation map.
* @param string $locale Locale, default to 'en'
*
* @return \Laminas\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\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->createMock(Translator::class);
$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 @@ -32,7 +32,6 @@
use DateTime;
use Laminas\Http\Request;
use Laminas\Session\SessionManager;
use Laminas\Translator\TranslatorInterface;
use Laminas\View\Renderer\PhpRenderer;
use PHPUnit\Event\NoPreviousThrowableException;
use PHPUnit\Framework\InvalidArgumentException;
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

0 comments on commit afdc906

Please sign in to comment.