Я новичок в друпале 8 php модульный тест. Я написал специальный сервис, который будет принимать идентификатор пользователя и выдает массив пользовательских данных. Я попытался выполнить модульное тестирование этой функции, но получил ошибку в функции $ user-> get ().
Вот мой модуль / custom / common / src / CommonService. php
namespace Drupal\common;
use Drupal\user\Entity\User;
use Drupal\common\UserWrapper;
class CommonService {
public function __construct(UserWrapper $userWrapper) {
$this->userWrapper = $userWrapper;
}
public function getUserData($uid) {
// @todo fetch current user information.
$user_data = $this->userWrapper->load($uid);
if (!empty($user_data)) {
$address = $user_data->get('field_user_address')->getValue();
return array(
'first_name' => isset($user_data->get('first_name')->value) ? $user_data->get('first_name')->value : '',
'middle_name' => isset($user_data->get('field_middle_name')->value) ? $user_data->get('field_middle_name')->value : '',
'last_name' => isset($user_data->get('last_name')->value) ? $user_data->get('last_name')->value : '',
'email_address' => isset($user_data->get('mail')->value) ? $user_data->get('mail')->value : '',
'telephone_number' => isset($user_data->get('field_telephone_number')->value) ? $user_data->get('field_telephone_number')->value : '',
'mobile_number' => isset($user_data->get('field_mobile_number')->value) ? $user_data->get('field_mobile_number')->value : '',
'country' => isset($address[0]['country_code']) ? $address[0]['country_code'] : '',
'uidnew' => $uid,
'state_province' => isset($address[0]['administrative_area']) ? $address[0]['administrative_area'] : '',
'address_one' => isset($address[0]['address_line1']) ? $address[0]['address_line1'] : '',
'address_two' => isset($address[0]['address_line2']) ? $address[0]['address_line2'] : '',
'city' => isset($address[0]['locality']) ? $address[0]['locality'] : '',
'zip_postal_code' => isset($address[0]['postal_code']) ? $address[0]['postal_code'] : '',
);
} else {
return NULL;
}
}
}
Вот мой модуль / custom / common / src / UserWrapper. php class,
namespace Drupal\common;
use Drupal\user\Entity\User;
class UserWrapper {
public function load($uid) {
return User::load($uid);
}
}
Вот этот модуль / custom / common / tests / src / Unit / CommonServiceTest. php
namespace Drupal\Tests\common\Unit;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\common\CommonService;
use Drupal\user\Entity\User;
use PHPUnit_Framework_MockObject_MockObject;
use Drupal\common\UserWrapper;
/**
* A test service class.
*/
class CommonServiceTest extends UnitTestCase {
/** @var PHPUnit_Framework_MockObject_MockObject $user */
private $user;
public function setUp() {
parent::setUp();
$user = $this->getMock(UserWrapper::class);
$this->user = $user;
$container = new ContainerBuilder();
$container->set('user', $user);
\Drupal::setContainer($container);
}
public function testGetUserData () {
$user_data = (object)[
'first_name' => (object)[
'value' => 'test'
],
'field_middle_name' => (object)[
'value' => 'test'
],
'last_name' => (object)[
'value' => 'test'
]
];
$this->user
->method('load')
->willReturn($user_data);
$expected = [
'first_name' => 'test',
'middle_name' => 'test',
'last_name' => 'test',
];
$service = new CommonService($this->user);
$this->assertEquals($expected, $service->getUserData(1));
}
}