Skip to content
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

Open
wants to merge 10 commits into
base: 8.x-2.x
Choose a base branch
from
Open

Unit tests for User #8

wants to merge 10 commits into from

Conversation

agr0chal
Copy link
Member

I've created unit tests for UserManager.php and fixed issue same as in Social Api

$this->userStorage->expects($this->once())
->method('loadByProperties')
->with(['user_id' => $this->userId, 'plugin_id' => $this->pluginId])
->will($this->returnValue(['test']));
Copy link
Contributor

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));
}

}
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor

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

Comment on lines 273 to 286
$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);
Copy link
Contributor

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.

return TRUE;

}
catch (\Exception $ex) {
Copy link
Contributor

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.

catch (\Exception $ex) {
$this->loggerFactory
->get($this->getPluginId())
->error('Failed to add record. Exception: @message', ['@message' => $ex->getMessage()]);
Copy link
Contributor

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
Copy link
Contributor

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);
Copy link
Contributor

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())
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants