Unit tests for User#8
Unit tests for User#8agr0chal wants to merge 10 commits intodrupalsocialinitiative:8.x-2.xfrom agr0chal:agrochal
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.
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.
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.
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.
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.
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.
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.
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.
This comment was not addressed
| ]; | ||
|
|
||
| $user_info = $this->entityTypeManager->getStorage('social_post')->create($values); | ||
| $user_info->setToken($token); |
There was a problem hiding this comment.
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.
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