Skip to content

Commit

Permalink
Merge pull request #37 from hostnet/fix-tests
Browse files Browse the repository at this point in the history
Fix failing unit tests
  • Loading branch information
JasperS2307 authored Sep 21, 2023
2 parents 595023c + bf246df commit 56174e5
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 41 deletions.
30 changes: 17 additions & 13 deletions src/Form/Simple/SimpleFormProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,23 @@ public function handle(Request $request, FormHandlerInterface $handler, FormInte
if (null !== $form) {
$handler->setForm($form);
} elseif (null === ($form = $handler->getForm())) {
if ($handler instanceof NamedFormHandlerInterface && null !== ($name = $handler->getName())) {
$form = $this->form_factory->createNamed(
$name,
$handler->getType(),
$handler->getData(),
$handler->getOptions()
);
} else {
$form = $this->form_factory->create(
$handler->getType(),
$handler->getData(),
$handler->getOptions()
);
try {
if ($handler instanceof NamedFormHandlerInterface && null !== ($name = $handler->getName())) {
$form = $this->form_factory->createNamed(
$name,
$handler->getType(),
$handler->getData(),
$handler->getOptions()
);
} else {
$form = $this->form_factory->create(
$handler->getType(),
$handler->getData(),
$handler->getOptions()
);
}
} catch (\InvalidArgumentException $e) {
$form = null;
}

if (!$form instanceof FormInterface) {
Expand Down
4 changes: 4 additions & 0 deletions test/Form/Simple/SimpleFormProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ public function testNoFormNotFound(): void
->method('getType')
->willReturn(FormType::class);

$this->factory
->method('create')
->willThrowException(new \InvalidArgumentException());

$provider = new SimpleFormProvider($this->factory);

$this->expectException(FormNotFoundException::class);
Expand Down
31 changes: 19 additions & 12 deletions test/FormHandler/FormSubmitProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ public function testFailureWithArrayCallables(): void
[$this, 'callbackFailure']
);

$request = Request::create('/', 'POST');
$request = Request::create('/', 'POST');
$form_interface = $this->prophesize(FormInterface::class);

$this->form->handleRequest($request)->shouldBeCalled();
$this->form->handleRequest($request)->shouldBeCalled()->willReturn($form_interface);
$this->form->isSubmitted()->willReturn(true);
$this->form->isValid()->willReturn(false);
$this->form->getData()->willReturn([]);
Expand All @@ -84,9 +85,10 @@ public function testSuccessWithArrayCallables(): void
[$this, 'callbackFailure']
);

$request = Request::create('/', 'POST');
$request = Request::create('/', 'POST');
$form_interface = $this->prophesize(FormInterface::class);

$this->form->handleRequest($request)->shouldBeCalled();
$this->form->handleRequest($request)->shouldBeCalled()->willReturn($form_interface);
$this->form->isSubmitted()->willReturn(true);
$this->form->isValid()->willReturn(true);
$this->form->getData()->willReturn([]);
Expand Down Expand Up @@ -120,19 +122,21 @@ public function testGetForm(): void

public function testSubmitNotSubmitted(): void
{
$request = Request::create('/', 'POST');
$request = Request::create('/', 'POST');
$form_interface = $this->prophesize(FormInterface::class);

$this->form->handleRequest($request)->shouldBeCalled();
$this->form->handleRequest($request)->shouldBeCalled()->willReturn($form_interface);
$this->form->isSubmitted()->willReturn(false);

self::assertNull($this->form_submit_processor->process($request));
}

public function testSubmitValid(): void
{
$request = Request::create('/', 'POST');
$request = Request::create('/', 'POST');
$form_interface = $this->prophesize(FormInterface::class);

$this->form->handleRequest($request)->shouldBeCalled();
$this->form->handleRequest($request)->shouldBeCalled()->willReturn($form_interface);
$this->form->isSubmitted()->willReturn(true);
$this->form->isValid()->willReturn(true);
$this->form->getData()->willReturn([]);
Expand All @@ -144,8 +148,9 @@ public function testSubmitValidNoHandler(): void
{
$request = Request::create('/', 'POST');
$form_submit_processor = new FormSubmitProcessor($this->form->reveal());
$form_interface = $this->prophesize(FormInterface::class);

$this->form->handleRequest($request)->shouldBeCalled();
$this->form->handleRequest($request)->shouldBeCalled()->willReturn($form_interface);
$this->form->isSubmitted()->willReturn(true);
$this->form->isValid()->willReturn(true);
$this->form->getData()->willReturn([]);
Expand All @@ -155,9 +160,10 @@ public function testSubmitValidNoHandler(): void

public function testSubmitInvalid(): void
{
$request = Request::create('/', 'POST');
$request = Request::create('/', 'POST');
$form_interface = $this->prophesize(FormInterface::class);

$this->form->handleRequest($request)->shouldBeCalled();
$this->form->handleRequest($request)->shouldBeCalled()->willReturn($form_interface);
$this->form->isSubmitted()->willReturn(true);
$this->form->isValid()->willReturn(false);
$this->form->getData()->willReturn([]);
Expand All @@ -169,8 +175,9 @@ public function testSubmitInvalidNoHandler(): void
{
$request = Request::create('/', 'POST');
$form_submit_processor = new FormSubmitProcessor($this->form->reveal());
$form_interface = $this->prophesize(FormInterface::class);

$this->form->handleRequest($request)->shouldBeCalled();
$this->form->handleRequest($request)->shouldBeCalled()->willReturn($form_interface);
$this->form->isSubmitted()->willReturn(true);
$this->form->isValid()->willReturn(false);
$this->form->getData()->willReturn([]);
Expand Down
10 changes: 8 additions & 2 deletions test/FormHandler/HandlerBackwardsCompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function testGet(): void

$form_factory->create(TestType::class, Argument::type(TestData::class), [])->willReturn($form);

$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(false);

self::assertNull($handler_factory->create(LegacyFormHandler::class)->handle($request, $data));
}

Expand All @@ -62,6 +65,9 @@ public function testGetOptions(): void
->create(TestType::class, Argument::type(TestData::class), ['attr' => ['class' => 'foobar']])
->willReturn($form);

$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(false);

self::assertNull($handler_factory->create(LegacyFormVariableOptionsHandler::class)->handle($request, $data));
}

Expand Down Expand Up @@ -90,7 +96,7 @@ public function testPostSuccess(): void
$data->test = 'foobar';

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);
$form->getData()->willReturn($data);
Expand All @@ -115,7 +121,7 @@ public function testPostFailure(): void
$data->test = 'foobar';

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(false);
$form->getData()->willReturn($data);
Expand Down
14 changes: 7 additions & 7 deletions test/FormHandler/HandlerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testSetName(): void
$form_factory = $this->prophesize(FormFactoryInterface::class);

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);
$form->getData()->willReturn(null);
Expand All @@ -53,7 +53,7 @@ public function testSetOptions(): void
$form_factory = $this->prophesize(FormFactoryInterface::class);

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);
$form->getData()->willReturn(null);
Expand All @@ -74,7 +74,7 @@ public function testSetOptionsCallback(): void
$form_factory = $this->prophesize(FormFactoryInterface::class);

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);
$form->getData()->willReturn(null);
Expand All @@ -99,7 +99,7 @@ public function testBuildSuccess(): void
$form_factory = $this->prophesize(FormFactoryInterface::class);

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);
$form->getData()->willReturn(null);
Expand All @@ -125,7 +125,7 @@ public function testBuildFailure(): void
$form_factory = $this->prophesize(FormFactoryInterface::class);

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(false);
$form->getData()->willReturn(null);
Expand All @@ -152,7 +152,7 @@ public function testBuildSuccessCallback(): void
$form_factory = $this->prophesize(FormFactoryInterface::class);

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);
$form->getData()->willReturn(null);
Expand Down Expand Up @@ -184,7 +184,7 @@ public function testBuildFailureCallback(): void
$form_factory = $this->prophesize(FormFactoryInterface::class);

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(false);
$form->getData()->willReturn(null);
Expand Down
10 changes: 5 additions & 5 deletions test/FormHandler/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testGetForm(): void
$data = new TestData();

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(false);

$form_factory->create(TestType::class, Argument::type(TestData::class), [])->willReturn($form);
Expand Down Expand Up @@ -107,7 +107,7 @@ public function testSimple(): void
$data = new TestData();

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);
$form->getData()->willReturn($data);
Expand All @@ -131,7 +131,7 @@ public function testPostSuccess(): void
$data = new TestData();

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);
$form->getData()->willReturn($data);
Expand Down Expand Up @@ -163,7 +163,7 @@ public function testPostSuccessSyncData(): void
$data->test = 'foobar';

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);
$form->getData()->willReturn($data);
Expand Down Expand Up @@ -191,7 +191,7 @@ public function testPostFailure(): void
$data = new TestData();

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(false);
$form->getData()->willReturn($data);
Expand Down
6 changes: 4 additions & 2 deletions test/FormHandler/LegacyHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function testGet(): void
$handler = new LegacyFormHandler();

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(false);

$form_factory->create(TestType::class, Argument::type(TestData::class), [])->willReturn($form);

Expand All @@ -47,7 +49,7 @@ public function testPostSuccess(): void
$handler = new LegacyFormHandler();

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);

Expand All @@ -67,7 +69,7 @@ public function testPostFailure(): void
$handler = new LegacyFormHandler();

$form = $this->prophesize(FormInterface::class);
$form->handleRequest($request)->shouldBeCalled();
$form->handleRequest($request)->shouldBeCalled()->willReturn($form);
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(false);

Expand Down

0 comments on commit 56174e5

Please sign in to comment.