Skip to content

Commit

Permalink
Fix missing coverage since quarantine was removed
Browse files Browse the repository at this point in the history
  • Loading branch information
curry684 committed Jan 20, 2025
1 parent 49e87e2 commit 648a3dc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/EventSubscriber/FormProfileSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public function onPostSetData(FormEvent $event): void
}

/**
* Merge all AntiSpamFormError instances into one if we are running a stealthy operation in this form profile.
* Dispatch aggegrated events and merge all AntiSpamFormError instances into one if we are running a stealthy
* operation in this form profile.
*/
public function onPostSubmit(PostSubmitEvent $event): void
{
Expand Down
6 changes: 6 additions & 0 deletions tests/Fixture/src/EventSubscriber/EventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Tests\Fixture\EventSubscriber;

use Omines\AntiSpamBundle\AntiSpamEvents;
use Omines\AntiSpamBundle\Event\FormProcessedEvent;
use Omines\AntiSpamBundle\Event\FormViolationEvent;
use Omines\AntiSpamBundle\Event\ValidatorViolationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -23,6 +24,7 @@ public static function getSubscribedEvents(): array
{
return [
AntiSpamEvents::VALIDATOR_VIOLATION => 'onValidatorViolation',
AntiSpamEvents::FORM_PROCESSED => 'onFormProcessed',
AntiSpamEvents::FORM_VIOLATION => 'onFormViolation',
];
}
Expand All @@ -35,6 +37,10 @@ public function onValidatorViolation(ValidatorViolationEvent $event): void
}
}

public function onFormProcessed(FormProcessedEvent $event): void
{
}

public function onFormViolation(FormViolationEvent $event): void
{
}
Expand Down
43 changes: 39 additions & 4 deletions tests/Unit/Form/FormTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

namespace Tests\Unit\Form;

use Omines\AntiSpamBundle\AntiSpamEvents;
use Omines\AntiSpamBundle\Event\FormProcessedEvent;
use Omines\AntiSpamBundle\Form\Type\HoneypotType;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Clock\Test\ClockSensitiveTrait;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
Expand All @@ -23,6 +26,7 @@
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Tests\Fixture\Form\Type\KitchenSinkForm;

class FormTypesTest extends KernelTestCase
Expand All @@ -33,10 +37,7 @@ class FormTypesTest extends KernelTestCase

public static function setUpBeforeClass(): void
{
$formFactory = static::getContainer()->get(FormFactoryInterface::class);
assert($formFactory instanceof FormFactoryInterface);

self::$formFactory = $formFactory;
self::$formFactory = static::getContainer()->get(FormFactoryInterface::class);
}

/**
Expand All @@ -47,6 +48,11 @@ private static function createForm(string $class, array $options = []): FormBuil
return self::$formFactory->createBuilder($class, options: $options);
}

private static function getEventDispatcher(): EventDispatcherInterface
{
return static::getContainer()->get(EventDispatcherInterface::class);
}

public function testNonInteractiveFormTypesAreUnmapped(): void
{
$form = $this->createForm(KitchenSinkForm::class)->getForm();
Expand Down Expand Up @@ -122,4 +128,33 @@ public function testNestedProfileResolution(): void
$this->assertNotEmpty($errors = $form->getErrors());
$this->assertStringContainsString('could not be processed', $errors[0]->getMessage());
}

/**
* Run in separate process as we modify global event dispatcher state.
*/
#[RunInSeparateProcess]
public function testEventsAreDispatched(): void
{
$form = $this->createForm(KitchenSinkForm::class, [
'antispam_profile' => 'test1',
])->getForm();

$view = $form->createView();
self::mockTime('+10 seconds');

static::getEventDispatcher()->addListener(AntiSpamEvents::FORM_PROCESSED, function (FormProcessedEvent $event) {
self::assertTrue($event->getResult()->isSpam());
});

$request = Request::create('/', method: 'POST', parameters: [
'kitchen_sink_form' => [
'name' => 'John Doe',
'email' => '[email protected]',
'message' => 'Message for testing',
'timer' => $view['timer']->vars['value'],
],
]);
$form->handleRequest($request);
$form->isValid() && $form->isSubmitted();
}
}

0 comments on commit 648a3dc

Please sign in to comment.