[TESTS] Raise App\Util\Common test coverage to 100%

This commit is contained in:
Hugo Sales 2021-05-02 20:42:25 +00:00
parent 3fab198c04
commit abda73b8e3
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 88 additions and 10 deletions

View File

@ -94,7 +94,7 @@ abstract class Common
return self::ensureLoggedIn()->getNickname(); return self::ensureLoggedIn()->getNickname();
} }
public static function userId(): ?string public static function userId(): ?int
{ {
return self::ensureLoggedIn()->getId(); return self::ensureLoggedIn()->getId();
} }

View File

@ -19,16 +19,28 @@
namespace App\Tests\Util; namespace App\Tests\Util;
use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Core\Security;
use App\Entity\GSActor;
use App\Entity\LocalUser;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\NoLoggedInUser;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Jchook\AssertThrows\AssertThrows;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security as SSecurity;
class CommonTest extends WebTestCase class CommonTest extends WebTestCase
{ {
use AssertThrows;
public function testSetConfig() public function testSetConfig()
{ {
$conf = ['test' => ['hydrogen' => 'helium']]; $conf = ['test' => ['hydrogen' => 'helium']];
@ -47,6 +59,7 @@ class CommonTest extends WebTestCase
static::assertSame('helium', Common::config('test', 'hydrogen')); static::assertSame('helium', Common::config('test', 'hydrogen'));
Common::setConfig('test', 'hydrogen', 'lithium'); Common::setConfig('test', 'hydrogen', 'lithium');
static::assertSame('lithium', Common::config('test', 'hydrogen')); static::assertSame('lithium', Common::config('test', 'hydrogen'));
static::assertSame($conf, Common::getConfigDefaults());
unlink(INSTALLDIR . '/social.local.yaml.back'); unlink(INSTALLDIR . '/social.local.yaml.back');
if ($exists) { if ($exists) {
@ -54,6 +67,47 @@ class CommonTest extends WebTestCase
} }
} }
/**
* Test Common::user, Common::actor and such. Requires a lot of setup
*/
public function testUserAndActorGetters()
{
$client = static::createClient();
$sec = $this->getMockBuilder(SSecurity::class)->setConstructorArgs([self::$kernel->getContainer()])->getMock();
Security::setHelper($sec, null);
static::assertNull(Common::user());
static::assertThrows(NoLoggedInUser::class, fn () => Common::ensureLoggedIn());
static::assertFalse(Common::isLoggedIn());
$metadata = $this->createMock(ClassMetadataInfo::class);
$metadata->method('getTableName')->willReturn('gsactor');
$metadata->method('getMetadataValue')->willReturn('App\Entity\GSActor');
$factory = $this->createMock(ClassMetadataFactory::class);
$factory->method('getAllMetadata')->willReturn([$metadata]);
$actor = GSActor::create(['nickname' => 'nick']);
$actor->setId(0);
$em = $this->createMock(EntityManager::class);
$em->method('find')->willReturn($actor);
$em->method('getMetadataFactory')->willReturn($factory);
DB::setManager($em);
DB::initTableMap();
$user = LocalUser::create(['nickname' => 'nick']);
$user->setId(0);
$sec = $this->getMockBuilder(SSecurity::class)->setConstructorArgs([self::$kernel->getContainer()])->getMock();
$sec->method('getUser')->willReturn($user);
Security::setHelper($sec, null);
// $cookies = $client->loginUser($user)->getCookieJar();
// $cookies->get('MOCKSESSID')->getValue();
static::assertSame($user, Common::user());
static::assertSame($actor, Common::actor());
static::assertSame('nick', Common::userNickname());
static::assertSame(0, Common::userId());
static::assertSame($user, Common::ensureLoggedIn());
static::assertTrue(Common::isLoggedIn());
}
public function testIsSystemPath() public function testIsSystemPath()
{ {
static::bootKernel(); static::bootKernel();
@ -91,15 +145,29 @@ class CommonTest extends WebTestCase
public function testSizeStrToInt() public function testSizeStrToInt()
{ {
static::assertSame(pow(1024, 0), Common::sizeStrToInt('1')); static::assertSame(pow(1024, 0), Common::sizeStrToInt('1'));
static::assertSame(pow(1024, 1), Common::sizeStrToInt('1K')); static::assertSame(pow(1024, 1), Common::sizeStrToInt('1K'));
static::assertSame(pow(1024, 2), Common::sizeStrToInt('1M')); static::assertSame(pow(1024, 2), Common::sizeStrToInt('1M'));
static::assertSame(pow(1024, 3), Common::sizeStrToInt('1G')); static::assertSame(3 * pow(1024, 2), Common::sizeStrToInt(''));
static::assertSame(pow(1024, 4), Common::sizeStrToInt('1T')); static::assertSame(pow(1024, 3), Common::sizeStrToInt('1G'));
static::assertSame(pow(1024, 5), Common::sizeStrToInt('1P')); static::assertSame(pow(1024, 4), Common::sizeStrToInt('1T'));
static::assertSame(128, Common::sizeStrToInt('128')); static::assertSame(pow(1024, 5), Common::sizeStrToInt('1P'));
static::assertSame(128 * 1024, Common::sizeStrToInt('128K')); static::assertSame(128, Common::sizeStrToInt('128'));
static::assertSame(128 * 1024, Common::sizeStrToInt('128.5K')); static::assertSame(128 * 1024, Common::sizeStrToInt('128K'));
static::assertSame(128 * 1024, Common::sizeStrToInt('128.5K'));
}
public function testGetPreferredPhpUploadLimit()
{
$post_max_size = ini_set('post_max_size', 1);
$upload_max_filesize = ini_set('upload_max_filesize', 1);
$memory_limit = ini_set('memory_limit', 1);
static::assertSame(1, Common::getPreferredPhpUploadLimit());
ini_set('post_max_size', $post_max_size);
ini_set('upload_max_filesize', $upload_max_filesize);
ini_set('memory_limit', $memory_limit);
} }
public function testClamp() public function testClamp()
@ -108,5 +176,15 @@ class CommonTest extends WebTestCase
static::assertSame(2, Common::clamp(value: 2, min: 2, max: 3)); static::assertSame(2, Common::clamp(value: 2, min: 2, max: 3));
static::assertSame(1, Common::clamp(value: 2, min: 0, max: 1)); static::assertSame(1, Common::clamp(value: 2, min: 0, max: 1));
static::assertSame(3, Common::clamp(value: 2, min: 3, max: 5)); static::assertSame(3, Common::clamp(value: 2, min: 3, max: 5));
static::assertSame(3.5, Common::clamp(value: 2.75, min: 3.5, max: 5.1));
}
public function testIsValidHttpUrl()
{
static::assertFalse(Common::isValidHttpUrl(''));
static::assertTrue(Common::isValidHttpUrl('http://gnu.org'));
static::assertFalse(Common::isValidHttpUrl('http://gnu.org', ensure_secure: true));
static::assertTrue(Common::isValidHttpUrl('https://gnu.org'));
static::assertTrue(Common::isValidHttpUrl('https://gnu.org', ensure_secure: true));
} }
} }