. // }}} namespace App\Test\Core; use App\Core\DB; use App\Entity\LocalUser; use App\Util\GNUsocialTestCase; use BadMethodCallException; use InvalidArgumentException; use Jchook\AssertThrows\AssertThrows; class EntityTest extends GNUsocialTestCase { use AssertThrows; public function testHasMethod() { $user = LocalUser::create(['nickname' => 'foo']); static::assertTrue($user->hasNickname()); static::assertFalse($user->hasPassword()); static::assertThrows(BadMethodCallException::class, fn () => $user->nonExistantMethod()); } public function testCreate() { $user = LocalUser::create(['nickname' => 'foo']); static::assertSame('foo', $user->getNickname()); static::assertThrows(InvalidArgumentException::class, fn () => LocalUser::create(['non_existant_property' => 'bar'])); } public function testCreateOrUpdate() { [$user, $is_update] = LocalUser::checkExistingAndCreateOrUpdate(['nickname' => 'taken_user']); static::assertNotNull($user); static::assertTrue($is_update); [, $is_update] = LocalUser::checkExistingAndCreateOrUpdate(['nickname' => 'taken_user', 'outgoing_email' => 'foo@bar']); static::assertFalse($is_update); [$user, $is_update] = LocalUser::checkExistingAndCreateOrUpdate(['nickname' => 'taken_user', 'outgoing_email' => 'foo@bar'], find_by_keys: ['nickname']); static::assertSame('foo@bar', $user->getOutgoingEmail()); static::assertTrue($is_update); } public function testGetByPK() { $user = DB::findOneBy('local_user', ['nickname' => 'taken_user']); $user_with_pk = LocalUser::getByPK($user->getId()); static::assertSame($user, $user_with_pk); $user_with_pk = LocalUser::getByPK(['id' => $user->getId()]); static::assertSame($user, $user_with_pk); static::assertNull(LocalUser::getByPK(0)); } }