You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuUsersTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. use Luticate\Auth\Business\LuUsersBusiness;
  3. use Luticate\Auth\Controller\LuUsersController;
  4. use Luticate\Auth\Dbo\Users\LuUsersAddDbo;
  5. use Luticate\Auth\Dbo\Users\LuUsersLoginDbo;
  6. use Luticate\Utils\Business\LuBusinessException;
  7. use Luticate\Utils\Controller\LuticateApplication;
  8. use Luticate\Utils\DataAccess\LuDataAccess;
  9. /**
  10. * Created by PhpStorm.
  11. * User: robin
  12. * Date: 9/18/16
  13. * Time: 1:02 AM
  14. */
  15. class Db extends LuDataAccess
  16. {
  17. protected static $_connection = "luticatedb";
  18. }
  19. class LuUsersTest extends \PHPUnit_Framework_TestCase
  20. {
  21. protected function getCtrl()
  22. {
  23. return new LuUsersController();
  24. }
  25. public static function setUpBeforeClass()
  26. {
  27. Db::getPdo()->query("DELETE FROM lu_users WHERE username LIKE '_test_%'");
  28. }
  29. public static function tearDownAfterClass()
  30. {
  31. static::setUpBeforeClass();
  32. }
  33. public function testUserAdd1()
  34. {
  35. $ctrl = static::getCtrl();
  36. $newUser = new LuUsersAddDbo();
  37. $newUser->setUsername("_test_user1");
  38. $newUser->setPassword("test42");
  39. $newUser->setFirstname("Test");
  40. $newUser->setLastname("user1");
  41. $newUser->setEmail("test.user1@example.com");
  42. $newUser->setExternalAuth(null);
  43. $newUser->setProfileId(null);
  44. $user = $ctrl->add($newUser);
  45. $this->assertNotNull($user);
  46. $this->assertNotNull($user->getId());
  47. $this->assertSame("_test_user1", $user->getUsername());
  48. $this->assertSame("Test", $user->getFirstname());
  49. $this->assertSame("user1", $user->getLastname());
  50. $this->assertSame("test.user1@example.com", $user->getEmail());
  51. $this->assertNull($user->getExternalAuth());
  52. $this->assertNull($user->getProfileId());
  53. }
  54. public function testUserAdd2()
  55. {
  56. $ctrl = static::getCtrl();
  57. $newUser = new LuUsersAddDbo();
  58. $newUser->setUsername("_test_user1");
  59. $newUser->setPassword("test42");
  60. $newUser->setFirstname("Test");
  61. $newUser->setLastname("user1");
  62. $newUser->setEmail("test.user1@example.com");
  63. $newUser->setExternalAuth(null);
  64. $newUser->setProfileId(null);
  65. $this->expectException(LuBusinessException::class);
  66. $ctrl->add($newUser);
  67. }
  68. public function testUserAdd3()
  69. {
  70. $ctrl = static::getCtrl();
  71. $newUser = new LuUsersAddDbo();
  72. $newUser->setUsername("_test_user2");
  73. $newUser->setPassword("test42");
  74. $newUser->setFirstname("Test");
  75. $newUser->setLastname("user1");
  76. $newUser->setEmail("test.user1@example.com");
  77. $newUser->setExternalAuth(null);
  78. $newUser->setProfileId(null);
  79. $this->expectException(LuBusinessException::class);
  80. $ctrl->add($newUser);
  81. }
  82. public function testUserAdd4()
  83. {
  84. $ctrl = static::getCtrl();
  85. $newUser = new LuUsersAddDbo();
  86. $newUser->setUsername("_test_user2");
  87. $newUser->setPassword("test24");
  88. $newUser->setFirstname("Test");
  89. $newUser->setLastname("user2");
  90. $newUser->setEmail("test.user2@example.com");
  91. $newUser->setExternalAuth(null);
  92. $newUser->setProfileId(null);
  93. $user = $ctrl->add($newUser);
  94. $this->assertNotNull($user);
  95. $this->assertNotNull($user->getId());
  96. $this->assertSame("_test_user2", $user->getUsername());
  97. $this->assertSame("Test", $user->getFirstname());
  98. $this->assertSame("user2", $user->getLastname());
  99. $this->assertSame("test.user2@example.com", $user->getEmail());
  100. $this->assertNull($user->getExternalAuth());
  101. $this->assertNull($user->getProfileId());
  102. }
  103. public function testUserLogin1()
  104. {
  105. $ctrl = static::getCtrl();
  106. $loginDbo = new LuUsersLoginDbo();
  107. $loginDbo->setUsername("_test_user1");
  108. $loginDbo->setPassword("test42");
  109. $loginResult = $ctrl->login($loginDbo);
  110. $this->assertNotNull($loginResult);
  111. $this->assertSame("test.user1@example.com", $loginResult->getEmail());
  112. $loggedUser = LuUsersBusiness::getUserFromToken($loginResult->getToken());
  113. $this->assertSame("test.user1@example.com", $loggedUser->getEmail());
  114. }
  115. public function testUserLogin2()
  116. {
  117. $ctrl = static::getCtrl();
  118. $loginDbo = new LuUsersLoginDbo();
  119. $loginDbo->setUsername("_test_user2");
  120. $loginDbo->setPassword("test24");
  121. $loginResult = $ctrl->login($loginDbo);
  122. $this->assertNotNull($loginResult);
  123. $this->assertSame("test.user2@example.com", $loginResult->getEmail());
  124. $loggedUser = LuUsersBusiness::getUserFromToken($loginResult->getToken());
  125. $this->assertSame("test.user2@example.com", $loggedUser->getEmail());
  126. }
  127. }