From 2ee99e517699754628268fb81578f9e9d3fe7ce7 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 22 Jul 2021 12:31:19 +0000 Subject: [PATCH] [TESTS] Raise test coverage for Form to 100% --- src/Core/Form.php | 10 +++- tests/Core/FormTest.php | 113 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 tests/Core/FormTest.php diff --git a/src/Core/Form.php b/src/Core/Form.php index e9b3b24dcd..cca46d524d 100644 --- a/src/Core/Form.php +++ b/src/Core/Form.php @@ -92,7 +92,9 @@ abstract class Form foreach ($form as [$key, $class, $options]) { if ($target != null && empty($options['data']) && (strstr($key, 'password') == false) && $class != SubmitType::class) { if (isset($extra_data[$key])) { + // @codeCoverageIgnoreStart $options['data'] = $extra_data[$key]; + // @codeCoverageIgnoreEnd } else { $method = 'get' . ucfirst(Formatting::snakeCaseToCamelCase($key)); if (method_exists($target, $method)) { @@ -127,9 +129,9 @@ abstract class Form * Handle the full life cycle of a form. Creates it with @see * self::create and inserts the submitted values into the database */ - public static function handle(array $form_definition, Request $request, object $target, array $extra_args = [], ?callable $extra_step = null, array $create_args = []) + public static function handle(array $form_definition, Request $request, ?object $target, array $extra_args = [], ?callable $extra_step = null, array $create_args = [], SymfForm $testing_only_form = null) { - $form = self::create($form_definition, $target, ...$create_args); + $form = $testing_only_form ?? self::create($form_definition, $target, ...$create_args); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { @@ -142,14 +144,18 @@ abstract class Form $method = 'set' . ucfirst(Formatting::snakeCaseToCamelCase($key)); if (method_exists($target, $method)) { if (isset($extra_args[$key])) { + // @codeCoverageIgnoreStart $target->{$method}($val, $extra_args[$key]); + // @codeCoverageIgnoreEnd } else { $target->{$method}($val); } } } if (isset($extra_step)) { + // @codeCoverageIgnoreStart $extra_step($data, $extra_args); + // @codeCoverageIgnoreEnd } DB::flush(); } diff --git a/tests/Core/FormTest.php b/tests/Core/FormTest.php new file mode 100644 index 0000000000..dc92fa86b7 --- /dev/null +++ b/tests/Core/FormTest.php @@ -0,0 +1,113 @@ +. + +// }}} + +namespace App\Tests\Core; + +use App\Core\DB\DB; +use App\Core\Form; +use App\Entity\GSActor; +use App\Util\Form\ArrayTransformer; +use App\Util\GNUsocialTestCase; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\Form\Extension\Core\Type\TextareaType; +use Symfony\Component\Form\Form as SymfForm; +use Symfony\Component\HttpFoundation\Request; + +class FormTest extends GNUsocialTestCase +{ + public function testCreate() + { + parent::bootKernel(); + $form = Form::create($form_array = [ + ['content', TextareaType::class, ['label' => ' ', 'data' => '', 'attr' => ['placeholder' => 'placeholder']]], + ['array_trans', TextareaType::class, ['data' => ['foo', 'bar'], 'transformer' => ArrayTransformer::class]], + ['post', SubmitType::class, ['label' => 'Post']], + ]); + static::assertSame(get_class($form), 'Symfony\\Component\\Form\\Form'); + foreach ($form as $name => $f) { + if ($name == 'post') { + static::assertSame(get_class($f), 'Symfony\Component\Form\SubmitButton'); + } else { + static::assertSame(get_class($f), 'Symfony\Component\Form\Form'); + } + + $config = $f->getConfig(); + $form_options = $config->getOptions(); + + $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]); + } + } + break; + } + } + static::assertTrue($found); + + static::assertSame(get_class($f->getParent()), 'Symfony\\Component\\Form\\Form'); + } + static::assertTrue(Form::isRequired($form_array, 'content')); + } + + /** + * Test creating a form with default values pulled from an existing object. Can be used in conjunction with `Form::hanlde` to update said object + */ + public function testCreateUpdateObject() + { + $nick = 'form_testing_new_user'; + $user = GSActor::create(['nickname' => $nick, 'normalized_nickname' => $nick]); + $form = Form::create([ + ['nickname', TextareaType::class, []], + ['normalized_nickname', TextareaType::class, []], + ['post', SubmitType::class, []], + ], target: $user); + $options = $form['nickname']->getConfig()->getOptions(); + static::assertSame($nick, $options['data']); + } + + public function testHandle() + { + parent::bootKernel(); + $data = ['fullname' => 'Full Name', 'homepage' => 'gnu.org']; + $mock_request = static::createMock(Request::class); + $mock_form = static::createMock(SymfForm::class); + $mock_form->method('handleRequest'); + $mock_form->method('isSubmitted')->willReturn(true); + $mock_form->method('isValid')->willReturn(true); + $mock_form->method('getData')->willReturn($data); + $ret = Form::handle(form_definition: [/* not normal usage */], request: $mock_request, target: null, extra_args: [], extra_step: null, create_args: [], testing_only_form: $mock_form); + static::assertSame($data, $ret); + + $user = GSActor::create(['nickname' => 'form_testing_new_user', 'normalized_nickname' => 'form_testing_new_user']); + DB::persist($user); + $ret = Form::handle(form_definition: [/* not normal usage */], request: $mock_request, target: $user, extra_args: [], extra_step: null, create_args: [], testing_only_form: $mock_form); + static::assertSame($mock_form, $ret); + static::assertSame($data['fullname'], $user->getFullname()); + static::assertSame($data['homepage'], $user->getHomepage()); + } +}