EntityTest.php 2.6 KB

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