[TESTS] Fix tests

This commit is contained in:
Hugo Sales 2022-10-19 22:39:17 +01:00
parent 2fd46ca886
commit 053bc38792
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
12 changed files with 89 additions and 50 deletions

View File

@ -33,10 +33,6 @@ class PersonSettingsTest extends GNUsocialTestCase
{
use AssertThrows;
/**
* @covers \App\Controller\PersonSettings::allSettings
* @covers \App\Controller\PersonSettings::personalInfo
*/
public function testPersonalInfo()
{
$client = static::createClient();
@ -64,10 +60,6 @@ class PersonSettingsTest extends GNUsocialTestCase
// static::assertSame('908555842', $changed_user->getPhoneNumber()->getNationalNumber());
}
/**
* @covers \App\Controller\PersonSettings::account
* @covers \App\Controller\PersonSettings::allSettings
*/
public function testEmail()
{
$client = static::createClient();
@ -86,10 +78,6 @@ class PersonSettingsTest extends GNUsocialTestCase
static::assertSame($changed_user->getIncomingEmail(), 'incoming@provider.any');
}
/**
* @covers \App\Controller\PersonSettings::account
* @covers \App\Controller\PersonSettings::allSettings
*/
public function testCorrectPassword()
{
$client = static::createClient();
@ -108,10 +96,6 @@ class PersonSettingsTest extends GNUsocialTestCase
static::assertTrue($changed_user->checkPassword('this is some test password'));
}
/**
* @covers \App\Controller\PersonSettings::account
* @covers \App\Controller\PersonSettings::allSettings
*/
public function testAccountWrongPassword()
{
$client = static::createClient();
@ -129,11 +113,7 @@ class PersonSettingsTest extends GNUsocialTestCase
$this->assertSelectorTextContains('.stacktrace', 'AuthenticationException');
}
// TODO: First actually implement this functionality
// /**
// * @covers \App\Controller\PersonSettings::allSettings
// * @covers \App\Controller\PersonSettings::notifications
// */
// TODO: First actually implement this functionality
// public function testNotifications()
// {
// $client = static::createClient();

View File

@ -33,6 +33,7 @@ use DateTime;
use DateTimeInterface;
use Exception;
use Functional as F;
use InvalidArgumentException;
/**
* Base class to all entities, with some utilities
@ -68,6 +69,8 @@ abstract class Entity
* properties of $obj with the associative array $args. Doesn't
* persist the result
*
* @throws InvalidArgumentException
*
* @return static
*/
public static function create(array $args, bool $_delegated_call = false): self

View File

@ -101,7 +101,7 @@ abstract class Form
$name = $form[array_key_last($form)][0];
$r = Common::getRequest();
$form[] = ['_next', HiddenType::class, ['data' => $r->get('next') ?? $r->get('_next') ?? $r->getRequestUri()]];
$form[] = ['_next', HiddenType::class, ['data' => $r?->get('next') ?? $r?->get('_next') ?? $r?->getRequestUri()]];
$fb = self::$form_factory->createNamedBuilder($name, $type, data: null, options: array_merge($form_options, ['translation_domain' => false]));
foreach ($form as [$key, $class, $options]) {

View File

@ -67,7 +67,7 @@ abstract class Common
self::$request = $req;
}
public static function getRequest(): Request
public static function getRequest(): ?Request
{
return self::$request;
}
@ -185,12 +185,9 @@ abstract class Common
/**
* A recursive `array_diff`, while PHP itself doesn't provide one
*
* @param mixed[] $array1
* @param mixed[] $array2
*
* @return mixed[]
*/
public static function arrayDiffRecursive(array $array1, array $array2): array
public static function arrayDiffRecursive(mixed $array1, mixed $array2): array
{
$diff = [];
foreach ($array1 as $key => $value) {
@ -232,7 +229,7 @@ abstract class Common
{
// `memory_limit` can be -1 and `post_max_size` can be 0
// for unlimited. Consistency.
if (!isset($size) || $size === '-1' || $size === '0') {
if (!isset($size) || $size === '' || $size === '-1' || $size === '0') {
$size = '3M';
}

View File

@ -117,7 +117,7 @@ class ActorForms
}
// Delete related cache
$cache_keys = array_intersect(Actor::cacheKeys($target->getId()), array_flip(['id', 'nickname', 'fullname']));
$cache_keys = array_intersect_key(Actor::cacheKeys($target->getId()), array_flip(['id', 'nickname', 'fullname']));
foreach ($cache_keys as $key) {
Cache::delete($key);
}

View File

@ -209,8 +209,12 @@ abstract class Formatting
*
* @param static::SPLIT_BY_BOTH|static::SPLIT_BY_COMMA|static::SPLIT_BY_SPACE $split_type
*/
public static function toArray(string $input, array &$output, string $split_type = self::SPLIT_BY_COMMA): bool
public static function toArray(string $input, mixed &$output, string $split_type = self::SPLIT_BY_COMMA): bool
{
if (!\in_array($split_type, [static::SPLIT_BY_BOTH, static::SPLIT_BY_COMMA, static::SPLIT_BY_SPACE])) {
throw new InvalidArgumentException;
}
if ($input == '') {
$output = [];
return true;

View File

@ -0,0 +1,46 @@
<?php
declare(strict_types = 1);
// {{{ 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\Test\Controller;
use App\Core\DB;
use App\Util\GNUsocialTestCase;
class NoteTest extends GNUsocialTestCase
{
public function testNoteView()
{
$client = static::createClient();
$id = DB::findBy('note', ['neq' => ['content' => null]])[0]->getId();
dd(DB::findBy('note', ['neq' => ['content' => null]])[0]);
$crawler = $client->request('GET', '/object/note/{$id}');
$this->assertResponseIsSuccessful();
// $form = $crawler->selectButton('Sign in')->form();
$crawler = $client->submitForm('Sign in', [
'_username' => $nickname,
'_password' => $password,
]);
$this->assertResponseStatusCodeSame(302);
$crawler = $client->followRedirect();
}
}

View File

@ -97,7 +97,7 @@ class SecurityTest extends GNUsocialTestCase
public function testRegisterSuccess()
{
[$client,] = self::testRegister('new_nickname', 'new_email@provider.any', 'foobar');
[$client] = self::testRegister('new_nickname', 'new_email@provider.any', 'foobar');
$this->assertResponseStatusCodeSame(302);
$crawler = $client->followRedirect();
$this->assertRouteSame('security_login');
@ -122,7 +122,7 @@ class SecurityTest extends GNUsocialTestCase
private function testRegisterPasswordLength(string $password, string $error)
{
[$client, ] = self::testRegister('new_nickname', 'email@provider.any', $password);
[$client] = self::testRegister('new_nickname', 'email@provider.any', $password);
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('.form-error', $error);
$this->assertRouteSame('security_register');
@ -145,7 +145,7 @@ class SecurityTest extends GNUsocialTestCase
private function testRegisterNoEmail()
{
[$client, ] = self::testRegister('new_nickname', '', 'foobar');
[$client] = self::testRegister('new_nickname', '', 'foobar');
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('.form-error', 'Please enter an email');
$this->assertRouteSame('security_register');
@ -153,7 +153,7 @@ class SecurityTest extends GNUsocialTestCase
private function testRegisterNicknameLength(string $nickname, string $error)
{
[$client, ] = self::testRegister($nickname, 'email@provider.any', 'foobar');
[$client] = self::testRegister($nickname, 'email@provider.any', 'foobar');
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('.form-error', $error);
$this->assertRouteSame('security_register');
@ -171,13 +171,19 @@ class SecurityTest extends GNUsocialTestCase
public function testRegisterExistingNickname()
{
[$client, ] = self::testRegister('taken_user', 'new_new_email@provider.any', 'foobar');
[$client] = self::testRegister('taken_user', 'new_new_email@provider.any', 'foobar');
$this->assertSelectorTextContains('.stacktrace', 'App\Util\Exception\NicknameTakenException');
}
public function testRegisterExistingEmail()
{
[$client, ] = self::testRegister('other_new_nickname', 'taken_user@provider.any', 'foobar');
[$client] = self::testRegister('other_new_nickname', 'taken_user@provider.any', 'foobar');
$this->assertSelectorTextContains('.stacktrace', 'App\Util\Exception\EmailTakenException');
}
public function testInvalidEmail()
{
[$client] = self::testRegister('nicknameec1038', 'userec1038', 'foobar');
$this->assertSelectorTextContains('.stacktrace', 'App\Util\Exception\EmailException');
}
}

View File

@ -32,9 +32,10 @@ use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
class CacheTest extends GNUsocialTestCase
{
// Needs to return something to conform to the interface
private function notCalled(mixed $i): array
public function notCalled(mixed $i): array
{
static::assertFalse('should not be called');
static::markTestIncomplete('This should not be called.');
return [];
}

View File

@ -25,9 +25,9 @@ namespace App\Test\Core;
use App\Core\DB;
use App\Entity\LocalUser;
use App\Util\Exception\BugFoundException;
use App\Util\GNUsocialTestCase;
use BadMethodCallException;
use InvalidArgumentException;
use Jchook\AssertThrows\AssertThrows;
class EntityTest extends GNUsocialTestCase
@ -46,7 +46,7 @@ class EntityTest extends GNUsocialTestCase
{
$user = LocalUser::create(['nickname' => 'foo']);
static::assertSame('foo', $user->getNickname());
static::assertThrows(InvalidArgumentException::class, fn () => LocalUser::create(['non_existant_property' => 'bar']));
static::assertThrows(BugFoundException::class, fn () => LocalUser::create(['non_existant_property' => 'bar']));
}
public function testCreateOrUpdate()

View File

@ -61,20 +61,22 @@ class FormTest extends GNUsocialTestCase
$form_class = $config->getType()->getInnerType();
$found = false;
foreach ($form_array as [$array_name, $array_class, $options]) {
if ($name === $array_name) {
$found = true;
static::assertSame(\get_class($form_class), $array_class);
foreach (['label', 'attr', 'data'] as $field) {
if (isset($options[$field])) {
static::assertSame($form_options[$field], $options[$field]);
if ($name !== '_next') {
$found = false;
foreach ($form_array as [$array_name, $array_class, $options]) {
if ($name === $array_name) {
$found = true;
static::assertSame(\get_class($form_class), $array_class);
foreach (['label', 'attr', 'data'] as $field) {
if (isset($options[$field])) {
static::assertSame($form_options[$field], $options[$field]);
}
}
break;
}
break;
}
static::assertTrue($found);
}
static::assertTrue($found);
static::assertSame(\get_class($f->getParent()), 'Symfony\\Component\\Form\\Form');
}

View File

@ -185,7 +185,7 @@ class FormattingTest extends WebTestCase
public function testToArray()
{
static::assertThrows(Exception::class, fn () => Formatting::toArray('foo', $a, '')); // @phpstan-ignore-line
static::assertThrows(InvalidArgumentException::class, fn () => Formatting::toArray('foo', $a, '')); // @phpstan-ignore-line
static::assertTrue(Formatting::toArray('', $a));
static::assertSame([], $a);