-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit tests for User #8
base: 8.x-2.x
Are you sure you want to change the base?
Conversation
$this->userStorage->expects($this->once()) | ||
->method('loadByProperties') | ||
->with(['user_id' => $this->userId, 'plugin_id' => $this->pluginId]) | ||
->will($this->returnValue(['test'])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should return an instance of \Drupal\Core\Entity\EntityInterface[]
$this->assertEquals(['test'], $this->userManager->getAccountsByUserId($this->userId)); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No tests for other methods such as addRecord, getToken, etc?
/** | ||
* The tested Social Post UserManager. | ||
* | ||
* @var \Drupal\social_post\User\UserManager |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it @var \Drupal\social_post\User\UserManager|\PHPUnit_Framework_MockObject_MockObject
, so editors can pick up the expects
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment was not addressed
$this->socialPost->expects($this->any()) | ||
->method('getUserId') | ||
->will($this->returnValue(97212)); | ||
|
||
$this->currentUser->expects($this->once()) | ||
->method('id') | ||
->will($this->returnValue(456)); | ||
|
||
$this->userStorage->expects($this->once()) | ||
->method('loadByProperties') | ||
->with(['plugin_id' => $this->pluginId, 'provider_user_id' => $this->providerUserId]) | ||
->will($this->returnValue([$this->socialPost])); | ||
|
||
$this->userManager->setPluginId($this->pluginId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to do all these things, just mock checkIfUserExists
to return an integer.
src/User/UserManager.php
Outdated
return TRUE; | ||
|
||
} | ||
catch (\Exception $ex) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try catching for the specific exception EntityStorageException
instead of the global one.
src/User/UserManager.php
Outdated
catch (\Exception $ex) { | ||
$this->loggerFactory | ||
->get($this->getPluginId()) | ||
->error('Failed to add record. Exception: @message', ['@message' => $ex->getMessage()]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can include the user id as well
/** | ||
* The tested Social Post UserManager. | ||
* | ||
* @var \Drupal\social_post\User\UserManager |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment was not addressed
@@ -198,16 +198,38 @@ public function addRecord($name, $provider_user_id, $token, $additional_data = N | |||
]; | |||
|
|||
$user_info = $this->entityTypeManager->getStorage('social_post')->create($values); | |||
$user_info->setToken($token); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know there is an user_id
initialized before the if checking if user exists. Move $user_id = $this->getCurrentUser();
it after the if block.
public function testAddRecordExist() { | ||
$this->prepareAddRecord(); | ||
|
||
$this->userManager->expects($this->once()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do what I mention in the first comment, this become unnecessary.
I've created unit tests for UserManager.php and fixed issue same as in Social Api