forked from GNUsocial/gnu-social
[TESTS] Raise App\Core\DB\DB test coverage to 100% and fix issues found
This commit is contained in:
parent
ce98e80836
commit
f8107c86c5
@ -36,6 +36,7 @@ use App\Util\Exception\DuplicateFoundException;
|
|||||||
use App\Util\Exception\NotFoundException;
|
use App\Util\Exception\NotFoundException;
|
||||||
use App\Util\Formatting;
|
use App\Util\Formatting;
|
||||||
use Doctrine\Common\Collections\Criteria;
|
use Doctrine\Common\Collections\Criteria;
|
||||||
|
use Doctrine\Common\Collections\Expr\Expression;
|
||||||
use Doctrine\Common\Collections\ExpressionBuilder;
|
use Doctrine\Common\Collections\ExpressionBuilder;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\ORM\Query;
|
use Doctrine\ORM\Query;
|
||||||
@ -109,7 +110,7 @@ abstract class DB
|
|||||||
*
|
*
|
||||||
* @see self::findBy for the syntax
|
* @see self::findBy for the syntax
|
||||||
*/
|
*/
|
||||||
private static function buildExpression(ExpressionBuilder $eb, array $criteria)
|
private static function buildExpression(ExpressionBuilder $eb, array $criteria): array
|
||||||
{
|
{
|
||||||
$expressions = [];
|
$expressions = [];
|
||||||
foreach ($criteria as $op => $exp) {
|
foreach ($criteria as $op => $exp) {
|
||||||
@ -117,16 +118,17 @@ abstract class DB
|
|||||||
$method = "{$op}X";
|
$method = "{$op}X";
|
||||||
$expr = self::buildExpression($eb, $exp);
|
$expr = self::buildExpression($eb, $exp);
|
||||||
if (is_array($expr)) {
|
if (is_array($expr)) {
|
||||||
return $eb->{$method}(...$expr);
|
$expressions[] = $eb->{$method}(...$expr);
|
||||||
} else {
|
} else {
|
||||||
return $eb->{$method}($expr);
|
$expressions[] = $eb->{$method}($expr);
|
||||||
}
|
}
|
||||||
} elseif ($op == 'is_null') {
|
} elseif ($op == 'is_null') {
|
||||||
$expressions[] = $eb->isNull($exp);
|
$expressions[] = $eb->isNull($exp);
|
||||||
} else {
|
} else {
|
||||||
if (in_array($op, self::$find_by_ops)) {
|
if (in_array($op, self::$find_by_ops)) {
|
||||||
$method = Formatting::snakeCaseToCamelCase($op);
|
foreach ($exp as $field => $value) {
|
||||||
$expressions[] = $eb->{$method}(...$exp);
|
$expressions[] = $eb->{$op}($field, $value);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$expressions[] = $eb->eq($op, $exp);
|
$expressions[] = $eb->eq($op, $exp);
|
||||||
}
|
}
|
||||||
@ -151,7 +153,8 @@ abstract class DB
|
|||||||
if (empty($ops)) {
|
if (empty($ops)) {
|
||||||
return $repo->findBy($criteria, $orderBy, $limit, $offset);
|
return $repo->findBy($criteria, $orderBy, $limit, $offset);
|
||||||
} else {
|
} else {
|
||||||
$criteria = new Criteria(self::buildExpression(Criteria::expr(), $criteria), $orderBy, $offset, $limit);
|
$eb = Criteria::expr();
|
||||||
|
$criteria = new Criteria($eb->andX(...self::buildExpression($eb, $criteria)), $orderBy, $offset, $limit);
|
||||||
return $repo->matching($criteria)->toArray(); // Always work with array or it becomes really complicated
|
return $repo->matching($criteria)->toArray(); // Always work with array or it becomes really complicated
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -161,7 +164,7 @@ abstract class DB
|
|||||||
*/
|
*/
|
||||||
public static function findOneBy(string $table, array $criteria, ?array $orderBy = null, ?int $offset = null)
|
public static function findOneBy(string $table, array $criteria, ?array $orderBy = null, ?int $offset = null)
|
||||||
{
|
{
|
||||||
$res = self::findBy($table, $criteria, $orderBy, 1, $offset);
|
$res = self::findBy($table, $criteria, $orderBy, 2, $offset);
|
||||||
if (count($res) == 1) {
|
if (count($res) == 1) {
|
||||||
return $res[0];
|
return $res[0];
|
||||||
} else {
|
} else {
|
||||||
@ -176,7 +179,7 @@ abstract class DB
|
|||||||
public static function count(string $table, array $criteria)
|
public static function count(string $table, array $criteria)
|
||||||
{
|
{
|
||||||
$repo = self::getRepository($table);
|
$repo = self::getRepository($table);
|
||||||
return $repo->count($table, $criteria);
|
return $repo->count($criteria);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
88
tests/Core/DB/DBTest.php
Normal file
88
tests/Core/DB/DBTest.php
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// {{{ License
|
||||||
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
|
//
|
||||||
|
// GNU social is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// GNU social is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
namespace App\Tests\Core\DB;
|
||||||
|
|
||||||
|
use App\Core\DB\DB;
|
||||||
|
use App\Entity\GSActor;
|
||||||
|
use App\Entity\LocalUser;
|
||||||
|
use App\Util\Exception\DuplicateFoundException;
|
||||||
|
use App\Util\Exception\NotFoundException;
|
||||||
|
use App\Util\GNUsocialTestCase;
|
||||||
|
use Jchook\AssertThrows\AssertThrows;
|
||||||
|
|
||||||
|
class DBTest extends GNUsocialTestCase
|
||||||
|
{
|
||||||
|
use AssertThrows;
|
||||||
|
|
||||||
|
public function testDql()
|
||||||
|
{
|
||||||
|
static::bootKernel();
|
||||||
|
$actor = DB::dql('select a from gsactor a where a.nickname = :nickname', ['nickname' => 'taken_user']);
|
||||||
|
static::assertTrue(is_array($actor));
|
||||||
|
static::assertTrue($actor[0] instanceof GSActor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSql()
|
||||||
|
{
|
||||||
|
static::bootKernel();
|
||||||
|
$actor = DB::sql('select {select} from gsactor a where a.nickname = :nickname', ['a' => 'App\Entity\GSActor'], ['nickname' => 'taken_user']);
|
||||||
|
static::assertTrue(is_array($actor));
|
||||||
|
static::assertTrue($actor[0] instanceof GSActor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindBy()
|
||||||
|
{
|
||||||
|
static::bootKernel();
|
||||||
|
$actor = DB::findBy('gsactor', ['nickname' => 'taken_user']);
|
||||||
|
static::assertTrue(is_array($actor));
|
||||||
|
static::assertTrue($actor[0] instanceof GSActor);
|
||||||
|
|
||||||
|
$actor = DB::findBy('gsactor', ['and' => ['nickname' => 'taken_user', 'is_null' => 'bio', 'gte' => ['id' => 0], 'or' => ['normalized_nickname' => 'takenuser']]]);
|
||||||
|
static::assertTrue(is_array($actor));
|
||||||
|
static::assertTrue($actor[0] instanceof GSActor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindOneBy()
|
||||||
|
{
|
||||||
|
static::bootKernel();
|
||||||
|
$actor = DB::findOneBy('gsactor', ['nickname' => 'taken_user']);
|
||||||
|
static::assertTrue($actor instanceof GSActor);
|
||||||
|
|
||||||
|
static::assertThrows(DuplicateFoundException::class, fn () => DB::findOneBy('gsactor', ['is_null' => 'bio']));
|
||||||
|
static::assertThrows(NotFoundException::class, fn () => DB::findOneBy('gsactor', ['nickname' => 'nickname_not_in_use']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCount()
|
||||||
|
{
|
||||||
|
static::bootKernel();
|
||||||
|
static::assertTrue(DB::count('gsactor', ['nickname' => 'taken_user']) == 1);
|
||||||
|
static::assertTrue(DB::count('gsactor', []) != 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPersistWithSameId()
|
||||||
|
{
|
||||||
|
$actor = GSActor::create(['nickname' => 'test', 'normalized_nickname' => 'test']);
|
||||||
|
$user = LocalUser::create(['nickname' => 'test']);
|
||||||
|
$id = DB::persistWithSameId($actor, $user, fn ($id) => $id);
|
||||||
|
static::assertTrue($id != 0);
|
||||||
|
static::assertTrue($actor->getId() == $id);
|
||||||
|
static::assertTrue($user->getId() == $id);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user