PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/Validation/UserValidationTest.php

https://github.com/flaaam/UserBundle
PHP | 80 lines | 69 code | 11 blank | 0 comment | 3 complexity | 37644ded8b133e828a1adcd63ec4bcde MD5 | raw file
  1. <?php
  2. namespace FOS\UserBundle\Tests\Validation;
  3. use FOS\UserBundle\Test\WebTestCase;
  4. class UserValidationTest extends WebTestCase
  5. {
  6. public function testBlankUsernameFail()
  7. {
  8. $user = $this->getService('fos_user.user_manager')->createUser();
  9. $violations = $this->getService('validator')->validate($user, 'Registration');
  10. $this->assertTrue($this->hasViolationForPropertyPath($violations, 'username'));
  11. }
  12. public function testGoodUsernameValid()
  13. {
  14. $user = $this->getService('fos_user.user_manager')->createUser();
  15. $user->setUsername(uniqid());
  16. $violations = $this->getService('validator')->validate($user, 'Registration');
  17. $this->assertFalse($this->hasViolationForPropertyPath($violations, 'username'));
  18. }
  19. public function testDuplicatedUsernameFail()
  20. {
  21. $username = uniqid();
  22. $userManager = $this->getService('fos_user.user_manager');
  23. $user1 = $userManager->createUser();
  24. $user1->setUsername($username);
  25. $user1->setEmail(uniqid().'@mail.org');
  26. $user1->setPlainPassword(uniqid());
  27. $violations = $this->getService('validator')->validate($user1, 'Registration');
  28. $this->assertFalse($this->hasViolationForPropertyPath($violations, 'username'));
  29. $userManager->updateUser($user1);
  30. $user2 = $userManager->createUser();
  31. $user2->setUsername($username);
  32. $user1->setEmail(uniqid().'@mail.org');
  33. $user1->setPlainPassword(uniqid());
  34. $violations = $this->getService('validator')->validate($user2, 'Registration');
  35. $this->assertTrue($this->hasViolationForPropertyPath($violations, 'username'));
  36. $userManager->deleteUser($user1);
  37. }
  38. public function testDuplicatedEmailFail()
  39. {
  40. $email = uniqid().'@email.org';
  41. $userManager = $this->getService('fos_user.user_manager');
  42. $user1 = $userManager->createUser();
  43. $user1->setUsername(uniqid());
  44. $user1->setPlainPassword(uniqid());
  45. $user1->setEmail($email);
  46. $violations = $this->getService('validator')->validate($user1, 'Registration');
  47. $this->assertFalse($this->hasViolationForPropertyPath($violations, 'email'));
  48. $userManager->updateUser($user1);
  49. $user2 = $userManager->createUser();
  50. $user2->setUsername(uniqid());
  51. $user2->setPlainPassword(uniqid());
  52. $user2->setEmail($email);
  53. $violations = $this->getService('validator')->validate($user2, 'Registration');
  54. $this->assertTrue($this->hasViolationForPropertyPath($violations, 'email'));
  55. $userManager->deleteUser($user1);
  56. }
  57. protected function hasViolationForPropertyPath($violations, $propertyPath)
  58. {
  59. if (!is_object($violations)) {
  60. return false;
  61. }
  62. foreach ($violations as $violation) {
  63. if ($violation->getPropertyPath() == $propertyPath) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. }