Commit 715abb52 authored by Tino Goratsch's avatar Tino Goratsch

- refactored the Country class, moved it to the ACP3\Core\I18n namespace and...

- refactored the Country class, moved it to the ACP3\Core\I18n namespace and renamed it to CountryList
- covered the CountryList class with unit tests
parent b56f4b39
<?php
namespace ACP3\Core\Helpers;
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
class Country
namespace ACP3\Core\I18n;
class CountryList
{
/**
* @var Translator
*/
private $translator;
/**
* Country constructor.
* @param Translator $translator
*/
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
/**
* Returns an array with all earth countries
*
* @param string $locale
* @return array
*/
public static function worldCountries($locale = 'en_US')
public function worldCountries()
{
$path = ACP3_ROOT_DIR . 'vendor/umpirsky/country-list/data/' . $locale . '/country.json';
$path = ACP3_ROOT_DIR . 'vendor/umpirsky/country-list/data/' . $this->translator->getLocale() . '/country.json';
if (preg_match('/^[a-z]{2}_[A-Z]{2}/', $locale) && is_file($path)) {
if (preg_match('/^[a-z]{2}_[A-Z]{2}/', $this->translator->getLocale()) && is_file($path)) {
$countries = file_get_contents($path);
return json_decode($countries, true);
......
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Test\I18n;
use ACP3\Core\I18n\CountryList;
use ACP3\Core\I18n\Translator;
class CountryListTest extends \PHPUnit_Framework_TestCase
{
/**
* @var CountryList
*/
private $countryList;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $translatorMock;
protected function setUp()
{
$this->translatorMock = $this->getMockBuilder(Translator::class)
->disableOriginalConstructor()
->setMethods(['getLocale'])
->getMock();
$this->countryList = new CountryList($this->translatorMock);
}
public function testValidLocale()
{
$this->translatorMock->expects($this->exactly(2))
->method('getLocale')
->willReturn('en_US');
$actual = $this->countryList->worldCountries();
$this->assertTrue(is_array($actual));
$this->assertNotEmpty($actual);
}
public function testInvalidLocaleByPath()
{
$this->translatorMock->expects($this->exactly(2))
->method('getLocale')
->willReturn('xx_ZZ');
$actual = $this->countryList->worldCountries();
$this->assertTrue(is_array($actual));
$this->assertEmpty($actual);
}
public function testInvalidLocaleByCharacters()
{
$this->translatorMock->expects($this->exactly(2))
->method('getLocale')
->willReturn('2390');
$actual = $this->countryList->worldCountries();
$this->assertTrue(is_array($actual));
$this->assertEmpty($actual);
}
}
......@@ -12,3 +12,8 @@ services:
- '@core.cache.system'
- '@core.environment.application_path'
- '@core.modules.vendors'
core.i18n.country_list:
class: ACP3\Core\I18n\CountryList
arguments:
- '@core.lang'
<?php
namespace ACP3\Modules\ACP3\Users\Helpers;
use ACP3\Core\Helpers\Country;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\I18n\CountryList;
use ACP3\Core\I18n\Translator;
class Forms
......@@ -19,20 +19,27 @@ class Forms
* @var \ACP3\Core\Helpers\Forms
*/
protected $formsHelpers;
/**
* @var CountryList
*/
private $country;
/**
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\I18n\Translator $translator
* @param CountryList $countryList
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\Helpers\Forms $formsHelpers
* @param \ACP3\Core\Helpers\Forms $formsHelpers
*/
public function __construct(
Translator $translator,
CountryList $countryList,
RequestInterface $request,
\ACP3\Core\Helpers\Forms $formsHelpers
) {
$this->translator = $translator;
$this->request = $request;
$this->formsHelpers = $formsHelpers;
$this->country = $countryList;
}
/**
......@@ -86,7 +93,7 @@ class Forms
{
return $this->formsHelpers->choicesGenerator(
'country',
Country::worldCountries($this->translator->getLocale()),
$this->country->worldCountries(),
$defaultValue
);
}
......
......@@ -6,7 +6,7 @@
namespace ACP3\Modules\ACP3\Users\Model;
use ACP3\Core\Helpers\Country;
use ACP3\Core\I18n\CountryList;
use ACP3\Core\I18n\Translator;
use ACP3\Modules\ACP3\Users;
......@@ -38,19 +38,26 @@ class UserModel
* @var Translator
*/
private $translator;
/**
* @var CountryList
*/
private $countryList;
/**
* UserModel constructor.
*
* @param Translator $translator
* @param CountryList $countryList
* @param \ACP3\Modules\ACP3\Users\Model\Repository\UserRepository $userRepository
*/
public function __construct(
Translator $translator,
CountryList $countryList,
Users\Model\Repository\UserRepository $userRepository
) {
$this->userRepository = $userRepository;
$this->translator = $translator;
$this->countryList = $countryList;
}
/**
......@@ -69,10 +76,10 @@ class UserModel
$userId = (int)$userId;
if (empty($this->userInfo[$userId])) {
$countries = Country::worldCountries($this->translator->getLocale());
$countries = $this->countryList->worldCountries();
$info = $this->userRepository->getOneById($userId);
if (!empty($info)) {
$info['country_formatted'] = !empty($info['country']) && isset($countries[$info['country']]) ? $countries[$info['country']] : '';
$info['country_formatted'] = isset($countries[$info['country']]) ? $countries[$info['country']] : '';
$this->userInfo[$userId] = $info;
}
}
......
......@@ -14,6 +14,7 @@ services:
class: ACP3\Modules\ACP3\Users\Model\UserModel
arguments:
- '@core.lang'
- '@core.i18n.country_list'
- '@users.model.userrepository'
users.model.users_model:
......
......@@ -18,6 +18,7 @@ services:
class: ACP3\Modules\ACP3\Users\Helpers\Forms
arguments:
- '@core.lang'
- '@core.i18n.country_list'
- '@core.http.request'
- '@core.helpers.forms'
......
<?php
require_once __DIR__ . '/../vendor/autoload.php';
define('ACP3_ROOT_DIR', realpath(__DIR__ . '/../') . '/');
\ No newline at end of file
define('ACP3_ROOT_DIR', realpath(__DIR__ . '/../') . '/');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment