DBTest.php 3.2 KB

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