Skip to content

Commit

Permalink
L10n: Turn L10nTrait into an abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Jan 22, 2024
1 parent ef7a764 commit 383d1bf
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 36 deletions.
25 changes: 24 additions & 1 deletion src/Lunr/L10n/L10nTrait.php → src/Lunr/L10n/AbstractL10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Localization support trait.
*/
trait L10nTrait
abstract class AbstractL10n
{

/**
Expand All @@ -39,6 +39,29 @@ trait L10nTrait
*/
protected $logger;

/**
* Constructor.
*
* @param LoggerInterface $logger Shared instance of a Logger class.
*/
public function __construct($logger)
{
$this->logger = $logger;

$this->default_language = 'en_US';
$this->locales_location = dirname($_SERVER['PHP_SELF']) . '/l10n';
}

/**
* Destructor.
*/
public function __destruct()
{
unset($this->logger);
unset($this->default_language);
unset($this->locales_location);
}

/**
* Set the default language.
*
Expand Down
13 changes: 5 additions & 8 deletions src/Lunr/L10n/L10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
/**
* Localization support class
*/
class L10n
class L10n extends AbstractL10n
{

use L10nTrait;

/**
* Shared instance of a FilesystemAccessObject class.
* @var FilesystemAccessObjectInterface
Expand All @@ -42,20 +40,19 @@ class L10n
*/
public function __construct($logger, $fao)
{
$this->logger = $logger;
$this->fao = $fao;
parent::__construct($logger);

$this->default_language = 'en_US';
$this->locales_location = dirname($_SERVER['PHP_SELF']) . '/l10n';
$this->fao = $fao;
}

/**
* Destructor.
*/
public function __destruct()
{
unset($this->logger);
unset($this->fao);

parent::__destruct();
}

/**
Expand Down
15 changes: 5 additions & 10 deletions src/Lunr/L10n/L10nProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
/**
* Abstract Localization Provider class
*/
abstract class L10nProvider
abstract class L10nProvider extends AbstractL10n
{

use L10nTrait;

/**
* The language the provider has been initialized with
* @var string
Expand All @@ -44,12 +42,10 @@ abstract class L10nProvider
*/
public function __construct($language, $domain, $logger)
{
parent::__construct($logger);

$this->language = $language;
$this->domain = $domain;
$this->logger = $logger;

$this->default_language = 'en_US';
$this->locales_location = dirname($_SERVER['PHP_SELF']) . '/l10n';
}

/**
Expand All @@ -58,10 +54,9 @@ public function __construct($language, $domain, $logger)
public function __destruct()
{
unset($this->language);
unset($this->default_language);
unset($this->locales_location);
unset($this->domain);
unset($this->logger);

parent::__destruct();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* This file contains the L10nTraitBaseTest class.
* This file contains the AbstractL10nBaseTest class.
*
* SPDX-FileCopyrightText: Copyright 2012 M2mobi B.V., Amsterdam, The Netherlands
* SPDX-FileCopyrightText: Copyright 2022 Move Agency Group B.V., Zwolle, The Netherlands
Expand All @@ -10,18 +10,41 @@

namespace Lunr\L10n\Tests;

use Lunr\Halo\PropertyTraits\PsrLoggerTestTrait;

/**
* This class contains test methods for the L10n class.
*
* @covers Lunr\L10n\L10nTrait
* @covers Lunr\L10n\AbstractL10n
*/
class L10nTraitBaseTest extends L10nTraitTest
class AbstractL10nBaseTest extends AbstractL10nTest
{

use PsrLoggerTestTrait;

/**
* Test that the language is correctly stored in the object.
*/
public function testDefaultLanguageSetCorrectly(): void
{
$this->assertPropertyEquals('default_language', 'en_US');
}

/**
* Test that the language is correctly stored in the object.
*/
public function testLocaleLocationSetCorrectly(): void
{
// /usr/bin/l10n by default
$default_location = dirname($_SERVER['PHP_SELF']) . '/l10n';

$this->assertPropertyEquals('locales_location', $default_location);
}

/**
* Test that setting a valid default language stores it in the object.
*
* @covers Lunr\L10n\L10nTrait::set_default_language
* @covers Lunr\L10n\AbstractL10n::set_default_language
*/
public function testSetValidDefaultLanguage(): void
{
Expand All @@ -33,7 +56,7 @@ public function testSetValidDefaultLanguage(): void
/**
* Test that setting an invalid default language doesn't store it in the object.
*
* @covers Lunr\L10n\L10nTrait::set_default_language
* @covers Lunr\L10n\AbstractL10n::set_default_language
*/
public function testSetInvalidDefaultLanguage(): void
{
Expand All @@ -43,13 +66,13 @@ public function testSetInvalidDefaultLanguage(): void

$this->class->set_default_language('Whatever');

$this->assertNull($this->get_reflection_property_value('default_language'));
$this->assertEquals('en_US', $this->get_reflection_property_value('default_language'));
}

/**
* Test that setting a valid default language doesn't alter the currently set locale.
*
* @covers Lunr\L10n\L10nTrait::set_default_language
* @covers Lunr\L10n\AbstractL10n::set_default_language
*/
public function testSetValidDefaultLanguageDoesNotAlterCurrentLocale(): void
{
Expand All @@ -63,7 +86,7 @@ public function testSetValidDefaultLanguageDoesNotAlterCurrentLocale(): void
/**
* Test that setting an invalid default language doesn't alter the currently set locale.
*
* @covers Lunr\L10n\L10nTrait::set_default_language
* @covers Lunr\L10n\AbstractL10n::set_default_language
*/
public function testSetInvalidDefaultLanguageDoesNotAlterCurrentLocale(): void
{
Expand All @@ -81,7 +104,7 @@ public function testSetInvalidDefaultLanguageDoesNotAlterCurrentLocale(): void
/**
* Test that setting a valid locales location stores it in the object.
*
* @covers Lunr\L10n\L10nTrait::set_locales_location
* @covers Lunr\L10n\AbstractL10n::set_locales_location
*/
public function testSetValidLocalesLocation(): void
{
Expand All @@ -95,10 +118,13 @@ public function testSetValidLocalesLocation(): void
/**
* Test that setting an invalid locales location doesn't store it in the object.
*
* @covers Lunr\L10n\L10nTrait::set_locales_location
* @covers Lunr\L10n\AbstractL10n::set_locales_location
*/
public function testSetInvalidLocalesLocation(): void
{
// /usr/bin/l10n by default
$default_location = dirname($_SERVER['PHP_SELF']) . '/l10n';

$location = TEST_STATICS . '/../l10n';

$this->logger->expects($this->once())
Expand All @@ -107,7 +133,7 @@ public function testSetInvalidLocalesLocation(): void

$this->class->set_locales_location($location);

$this->assertNull($this->get_reflection_property_value('locales_location'));
$this->assertEquals($default_location, $this->get_reflection_property_value('locales_location'));
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* This file contains the L10nTraitTest class.
* This file contains the AbstractL10nTest class.
*
* SPDX-FileCopyrightText: Copyright 2012 M2mobi B.V., Amsterdam, The Netherlands
* SPDX-FileCopyrightText: Copyright 2022 Move Agency Group B.V., Zwolle, The Netherlands
Expand All @@ -10,17 +10,17 @@

namespace Lunr\L10n\Tests;

use Lunr\L10n\L10nTrait;
use Lunr\L10n\AbstractL10n;
use Lunr\Halo\LunrBaseTest;
use Psr\Log\LoggerInterface;
use ReflectionClass;

/**
* This class contains test methods for the L10n class.
*
* @covers Lunr\L10n\L10nTrait
* @covers Lunr\L10n\AbstractL10n
*/
class L10nTraitTest extends LunrBaseTest
class AbstractL10nTest extends LunrBaseTest
{

/**
Expand All @@ -46,9 +46,12 @@ class L10nTraitTest extends LunrBaseTest
*/
public function setUp(): void
{
$this->class = $this->getObjectForTrait('Lunr\L10n\L10nTrait');
$this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();

$this->class = $this->getMockBuilder('Lunr\L10n\AbstractL10n')
->setConstructorArgs([ $this->logger ])
->getMockForAbstractClass();

parent::baseSetUp($this->class);

$this->set_reflection_property_value('logger', $this->logger);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
</testsuite>
<testsuite name="L10n">
<file>../tests/system/EnvironmentTest.php</file>
<file>../src/Lunr/L10n/Tests/L10nTraitBaseTest.php</file>
<file>../src/Lunr/L10n/Tests/AbstractL10nBaseTest.php</file>
<file>../src/Lunr/L10n/Tests/L10nBaseTest.php</file>
<file>../src/Lunr/L10n/Tests/L10nProviderBaseTest.php</file>
<file>../src/Lunr/L10n/Tests/GettextL10nProviderBaseTest.php</file>
Expand Down

0 comments on commit 383d1bf

Please sign in to comment.