DBTest.php 3.2 KB

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