DBTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\DB;
  20. use App\Core\ActorLocalRoles;
  21. use App\Core\DB\DB;
  22. use App\Entity\Actor;
  23. use App\Entity\LocalUser;
  24. use App\Util\Exception\DuplicateFoundException;
  25. use App\Util\Exception\NotFoundException;
  26. use App\Util\GNUsocialTestCase;
  27. use Jchook\AssertThrows\AssertThrows;
  28. class DBTest extends GNUsocialTestCase
  29. {
  30. use AssertThrows;
  31. public function testDql()
  32. {
  33. static::bootKernel();
  34. $actor = DB::dql('select a from actor a where a.nickname = :nickname', ['nickname' => 'taken_user']);
  35. static::assertIsArray($actor);
  36. static::assertTrue($actor[0] instanceof Actor);
  37. }
  38. public function testSql()
  39. {
  40. static::bootKernel();
  41. $actor = DB::sql('select {select} from actor a where a.nickname = :nickname', ['nickname' => 'taken_user']);
  42. static::assertIsArray($actor);
  43. static::assertTrue($actor[0] instanceof Actor);
  44. }
  45. public function testFindBy()
  46. {
  47. static::bootKernel();
  48. $actor = DB::findBy('actor', ['nickname' => 'taken_user']);
  49. static::assertIsArray($actor);
  50. static::assertTrue($actor[0] instanceof Actor);
  51. $actor = DB::findBy('actor', ['and' => ['is_null' => 'bio', 'or' => ['nickname' => 'user does not exist', 'gte' => ['id' => 0]]]]);
  52. static::assertIsArray($actor);
  53. static::assertTrue($actor[0] instanceof Actor);
  54. }
  55. public function testFindOneBy()
  56. {
  57. static::bootKernel();
  58. $actor = DB::findOneBy('actor', ['nickname' => 'taken_user']);
  59. static::assertTrue($actor instanceof Actor);
  60. static::assertThrows(DuplicateFoundException::class, fn () => DB::findOneBy('actor', ['is_null' => 'bio']));
  61. static::assertThrows(NotFoundException::class, fn () => DB::findOneBy('actor', ['nickname' => 'nickname_not_in_use']));
  62. }
  63. public function testCount()
  64. {
  65. static::bootKernel();
  66. static::assertTrue(DB::count('actor', ['nickname' => 'taken_user']) == 1);
  67. static::assertTrue(DB::count('actor', []) != 1);
  68. }
  69. public function testPersistWithSameId()
  70. {
  71. $actor = Actor::create(['nickname' => 'test', 'bio' => 'some very interesting bio', 'is_local' => false, 'roles' => ActorLocalRoles::NONE, 'type' => Actor::PERSON]);
  72. $user = LocalUser::create(['nickname' => 'test']);
  73. $id = DB::persistWithSameId($actor, $user, fn ($id) => $id);
  74. static::assertTrue($id != 0);
  75. static::assertTrue($actor->getId() == $id);
  76. static::assertTrue($user->getId() == $id);
  77. }
  78. }