[TESTS] Add tests of Common and fix small oddities that pop up
This commit is contained in:
parent
b387ea9aa0
commit
dc2a453e94
|
@ -72,6 +72,7 @@ abstract class Router
|
||||||
return self::$url_gen->generate($id, $args, $type);
|
return self::$url_gen->generate($id, $args, $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** function match($url) throws Symfony\Component\Routing\Exception\ResourceNotFoundException */
|
||||||
public static function __callStatic(string $name, array $args)
|
public static function __callStatic(string $name, array $args)
|
||||||
{
|
{
|
||||||
return self::$router->{$name}(...$args);
|
return self::$router->{$name}(...$args);
|
||||||
|
|
|
@ -37,9 +37,9 @@ use App\Core\Security;
|
||||||
use App\Entity\GSActor;
|
use App\Entity\GSActor;
|
||||||
use App\Entity\LocalUser;
|
use App\Entity\LocalUser;
|
||||||
use App\Util\Exception\NoLoggedInUser;
|
use App\Util\Exception\NoLoggedInUser;
|
||||||
use Exception;
|
|
||||||
use Functional as F;
|
use Functional as F;
|
||||||
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
|
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
|
||||||
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
||||||
use Symfony\Component\Yaml;
|
use Symfony\Component\Yaml;
|
||||||
|
|
||||||
abstract class Common
|
abstract class Common
|
||||||
|
@ -69,7 +69,7 @@ abstract class Common
|
||||||
{
|
{
|
||||||
self::$config[$section][$setting] = $value;
|
self::$config[$section][$setting] = $value;
|
||||||
$diff = self::array_diff_recursive(self::$config, self::$defaults);
|
$diff = self::array_diff_recursive(self::$config, self::$defaults);
|
||||||
$yaml = (new Yaml\Dumper(2))->dump(['parameters' => ['gnusocial' => $diff]], Yaml\Yaml::DUMP_OBJECT_AS_MAP);
|
$yaml = (new Yaml\Dumper(indentation: 2))->dump(['parameters' => ['gnusocial' => $diff]], Yaml\Yaml::DUMP_OBJECT_AS_MAP);
|
||||||
rename(INSTALLDIR . '/social.local.yaml', INSTALLDIR . '/social.local.yaml.back');
|
rename(INSTALLDIR . '/social.local.yaml', INSTALLDIR . '/social.local.yaml.back');
|
||||||
file_put_contents(INSTALLDIR . '/social.local.yaml', $yaml);
|
file_put_contents(INSTALLDIR . '/social.local.yaml', $yaml);
|
||||||
}
|
}
|
||||||
|
@ -132,32 +132,37 @@ abstract class Common
|
||||||
try {
|
try {
|
||||||
Router::match('/' . $str);
|
Router::match('/' . $str);
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception $e) {
|
} catch (ResourceNotFoundException $e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A recursive `array_diff`, while PHP itself doesn't provide one
|
* A recursive `array_diff`, while PHP itself doesn't provide one
|
||||||
|
*
|
||||||
|
* @param mixed $array1
|
||||||
|
* @param mixed $array2
|
||||||
*/
|
*/
|
||||||
public function array_diff_recursive(array $array1, array $array2)
|
public static function array_diff_recursive($array1, $array2): array
|
||||||
{
|
{
|
||||||
$difference = [];
|
$diff = [];
|
||||||
foreach ($array1 as $key => $value) {
|
foreach ($array1 as $key => $value) {
|
||||||
if (is_array($value)) {
|
if (array_key_exists($key, $array2)) {
|
||||||
if (!isset($array2[$key]) || !is_array($array2[$key])) {
|
if (is_array($value)) {
|
||||||
$difference[$key] = $value;
|
$recursive_diff = static::array_diff_recursive($value, $array2[$key]);
|
||||||
|
if (count($recursive_diff)) {
|
||||||
|
$diff[$key] = $recursive_diff;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$new_diff = self::array_diff_recursive($value, $array2[$key]);
|
if ($value != $array2[$key]) {
|
||||||
if (!empty($new_diff)) {
|
$diff[$key] = $value;
|
||||||
$difference[$key] = $new_diff;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif ((!isset($array2[$key]) || $array2[$key] != $value) && !($array2[$key] === null && $value === null)) {
|
} else {
|
||||||
$difference[$key] = $value;
|
$diff[$key] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $difference ?? false;
|
return $diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -223,14 +228,10 @@ abstract class Common
|
||||||
/**
|
/**
|
||||||
* Clamps a value between 2 numbers
|
* Clamps a value between 2 numbers
|
||||||
*
|
*
|
||||||
* @param int $current
|
* @return float|int clamped value
|
||||||
* @param int $min
|
|
||||||
* @param int $max
|
|
||||||
*
|
|
||||||
* @return int clamped value
|
|
||||||
*/
|
*/
|
||||||
public static function clamp(int $current,int $min,int $max): int
|
public static function clamp(int | float $value, int | float $min, int | float $max): int | float
|
||||||
{
|
{
|
||||||
return min(max($current,$min),$max);
|
return min(max($value, $min), $max);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
<?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\Util\Common;
|
|
||||||
|
|
||||||
use App\Util\Common;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
||||||
|
|
||||||
class CommonTest extends WebTestCase
|
|
||||||
{
|
|
||||||
public function testClamp()
|
|
||||||
{
|
|
||||||
static::assertSame(2, Common::clamp(2,0,3));
|
|
||||||
static::assertSame(2, Common::clamp(2,2,3));
|
|
||||||
static::assertSame(1, Common::clamp(2,0,1));
|
|
||||||
static::assertSame(3, Common::clamp(2,3,5));
|
|
||||||
}
|
|
||||||
}
|
|
93
tests/Util/CommonTest.php
Normal file
93
tests/Util/CommonTest.php
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
<?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\Util\Common;
|
||||||
|
|
||||||
|
use App\Core\Event;
|
||||||
|
use App\Core\Router\Router;
|
||||||
|
use App\Util\Common;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
|
||||||
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||||
|
|
||||||
|
class CommonTest extends WebTestCase
|
||||||
|
{
|
||||||
|
public function testClamp()
|
||||||
|
{
|
||||||
|
static::assertSame(2, Common::clamp(value: 2, min: 0, 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(3, Common::clamp(value: 2, min: 3, max: 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetConfig()
|
||||||
|
{
|
||||||
|
$conf = ['test' => ['hydrogen' => 'helium']];
|
||||||
|
$cb = $this->createMock(ContainerBagInterface::class);
|
||||||
|
static::assertTrue($cb instanceof ContainerBagInterface);
|
||||||
|
$cb->method('get')
|
||||||
|
->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
|
||||||
|
Common::setupConfig($cb);
|
||||||
|
|
||||||
|
if ($exists = file_exists(INSTALLDIR . '/social.local.yaml')) {
|
||||||
|
copy(INSTALLDIR . '/social.local.yaml', INSTALLDIR . '/social.local.yaml.back_test');
|
||||||
|
} else {
|
||||||
|
touch(INSTALLDIR . '/social.local.yaml');
|
||||||
|
}
|
||||||
|
|
||||||
|
static::assertSame('helium', Common::config('test', 'hydrogen'));
|
||||||
|
Common::setConfig('test', 'hydrogen', 'lithium');
|
||||||
|
static::assertSame('lithium', Common::config('test', 'hydrogen'));
|
||||||
|
|
||||||
|
unlink(INSTALLDIR . '/social.local.yaml.back');
|
||||||
|
if ($exists) {
|
||||||
|
rename(INSTALLDIR . '/social.local.yaml.back_test', INSTALLDIR . '/social.local.yaml');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsSystemPath()
|
||||||
|
{
|
||||||
|
static::bootKernel();
|
||||||
|
|
||||||
|
$router = static::$container->get('router');
|
||||||
|
$url_gen = static::$container->get(UrlGeneratorInterface::class);
|
||||||
|
$event_dispatcher = static::$container->get(EventDispatcherInterface::class);
|
||||||
|
Router::setRouter($router, $url_gen);
|
||||||
|
Event::setDispatcher($event_dispatcher);
|
||||||
|
|
||||||
|
static::assertTrue(Common::isSystemPath('login'));
|
||||||
|
static::assertFalse(Common::isSystemPath('non-existent-path'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayDiffRecursive()
|
||||||
|
{
|
||||||
|
static::assertSame(['foo'], Common::array_diff_recursive(['foo'], ['bar']));
|
||||||
|
static::assertSame([], Common::array_diff_recursive(['foo'], ['foo']));
|
||||||
|
// array_diff(['foo' => []], ['foo' => 'bar']) >>> Array to string conversion
|
||||||
|
static::assertSame([], Common::array_diff_recursive(['foo' => []], ['foo' => 'bar']));
|
||||||
|
static::assertSame([], Common::array_diff_recursive(['foo' => ['bar']], ['foo' => ['bar']]));
|
||||||
|
static::assertSame(['foo' => [1 => 'quux']], Common::array_diff_recursive(['foo' => ['bar', 'quux']], ['foo' => ['bar']]));
|
||||||
|
static::assertSame([], Common::array_diff_recursive(['hydrogen' => ['helium' => ['lithium'], 'boron' => 'carbon']],
|
||||||
|
['hydrogen' => ['helium' => ['lithium'], 'boron' => 'carbon']]));
|
||||||
|
static::assertSame(['hydrogen' => ['helium' => ['lithium']]],
|
||||||
|
Common::array_diff_recursive(['hydrogen' => ['helium' => ['lithium'], 'boron' => 'carbon']],
|
||||||
|
['hydrogen' => ['helium' => ['beryllium'], 'boron' => 'carbon']]));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user