Skip to content

Commit

Permalink
Fixed uninitialized properties when no form fields are available (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar authored Feb 15, 2023
1 parent 991e0e2 commit fd37d75
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/FormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ private function prepare(): void

$this->formModel = $formModel;

// Set current session reference from request or generate a new one
$this->initSessionReference();

// Set storage identifier for storage implementations to work with
$this->storageIdentifier = $this->storageIdentifierGenerator->generate($this);

$this->loadFormFieldModels();

if (0 === \count($this->formFieldModels)) {
Expand Down Expand Up @@ -409,12 +415,6 @@ private function prepare(): void
}
}

// Set current session reference from request or generate a new one
$this->initSessionReference();

// Set storage identifier for storage implementations to work with
$this->storageIdentifier = $this->storageIdentifierGenerator->generate($this);

$this->prepared = true;
$this->preparing = false;
}
Expand Down
58 changes: 58 additions & 0 deletions tests/FormManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Terminal42\MultipageFormsBundle\Test;

use Codefog\HasteBundle\UrlParser;
use Contao\Config;
use Contao\FormFieldModel;
use Contao\FormModel;
use Contao\System;
use Contao\TestCase\ContaoTestCase;
use Symfony\Component\HttpFoundation\Request;
use Terminal42\MultipageFormsBundle\FormManager;
use Terminal42\MultipageFormsBundle\Storage\SessionReferenceGenerator\SessionReferenceGeneratorInterface;
use Terminal42\MultipageFormsBundle\Storage\StorageIdentifierGenerator\StorageIdentifierGeneratorInterface;
use Terminal42\MultipageFormsBundle\Storage\StorageInterface;

class FormManagerTest extends ContaoTestCase
{
public function testAccessingAllDataWithoutFormFields(): void
{
$formModel = $this->mockClassWithProperties(FormModel::class, ['id' => 42]);
$formModelAdapter = $this->mockAdapter(['findByPk']);
$formModelAdapter
->expects($this->once())
->method('findByPk')
->with(42)
->willReturn($formModel)
;
$formFieldModelAdapter = $this->mockAdapter(['findPublishedByPid']);
$formFieldModelAdapter
->expects($this->once())
->method('findPublishedByPid')
->with(42)
->willReturn([])
;

$framework = $this->mockContaoFramework([
Config::class => $this->mockAdapter(['isComplete']),
System::class => $this->mockAdapter(['importStatic']),
FormModel::class => $formModelAdapter,
FormFieldModel::class => $formFieldModelAdapter,
]);

$formManager = new FormManager(
42,
new Request(),
$framework,
$this->createMock(StorageInterface::class),
$this->createMock(StorageIdentifierGeneratorInterface::class),
$this->createMock(SessionReferenceGeneratorInterface::class),
new UrlParser()
);

$this->assertSame([], $formManager->getDataOfAllSteps()->all());
}
}

0 comments on commit fd37d75

Please # to comment.