EntityTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace App\Tests\Core;
  19. use App\Core\DB\DB;
  20. use App\Entity\LocalUser;
  21. use App\Util\GNUsocialTestCase;
  22. use Jchook\AssertThrows\AssertThrows;
  23. class EntityTest extends GNUsocialTestCase
  24. {
  25. use AssertThrows;
  26. public function testHasMethod()
  27. {
  28. $user = LocalUser::create(['nickname' => 'foo']);
  29. static::assertTrue($user->hasNickname());
  30. static::assertFalse($user->hasPassword());
  31. static::assertThrows(\BadMethodCallException::class, fn () => $user->nonExistantMethod());
  32. }
  33. public function testCreate()
  34. {
  35. $user = LocalUser::create(['nickname' => 'foo']);
  36. static::assertSame('foo', $user->getNickname());
  37. static::assertThrows(\InvalidArgumentException::class, fn () => LocalUser::create(['non_existant_property' => 'bar']));
  38. }
  39. public function testCreateOrUpdate()
  40. {
  41. [$user, $is_update] = LocalUser::createOrUpdate(['nickname' => 'taken_user']);
  42. static::assertNotNull($user);
  43. static::assertTrue($is_update);
  44. [, $is_update] = LocalUser::createOrUpdate(['nickname' => 'taken_user', 'outgoing_email' => 'foo@bar']);
  45. static::assertFalse($is_update);
  46. [$user, $is_update] = LocalUser::createOrUpdate(['nickname' => 'taken_user', 'outgoing_email' => 'foo@bar'], find_by_keys: ['nickname']);
  47. static::assertSame('foo@bar', $user->getOutgoingEmail());
  48. static::assertTrue($is_update);
  49. }
  50. public function testGetWithPK()
  51. {
  52. $user = DB::findOneBy('local_user', ['nickname' => 'taken_user']);
  53. $user_with_pk = LocalUser::getWithPK($user->getId());
  54. static::assertSame($user, $user_with_pk);
  55. $user_with_pk = LocalUser::getWithPK(['id' => $user->getId()]);
  56. static::assertSame($user, $user_with_pk);
  57. static::assertNull(LocalUser::getWithPK(0));
  58. }
  59. }