Skip to content

Commit

Permalink
Updated CI and fixed CS
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Nov 16, 2023
1 parent 2dec6d0 commit d7e68ce
Show file tree
Hide file tree
Showing 24 changed files with 77 additions and 2,265 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/vendor/
/tools/*/vendor
/composer.lock
/.php-cs-fixer.cache
/tools/phpunit/.phpunit.result.cache
/.phpunit.result.cache
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,20 @@
"contao-manager-plugin": "Terminal42\\MultipageFormsBundle\\ContaoManager\\Plugin"
},
"scripts": {
"cs-fixer": "@php tools/ecs/vendor/bin/ecs check config/ contao/ src/ tests/ --config tools/ecs/config.php --fix --ansi",
"unit-tests": "@php tools/phpunit/vendor/bin/phpunit -c tools/phpunit/phpunit.xml.dist"
"unit-tests": "@php vendor/bin/phpunit"
},
"config": {
"allow-plugins": {
"contao-components/installer": false,
"contao/manager-plugin": false
"contao/manager-plugin": false,
"php-http/discovery": true,
"terminal42/contao-build-tools": true
}
},
"require-dev": {
"contao/manager-plugin": "^2.12"
"contao/manager-plugin": "^2.12",
"terminal42/contao-build-tools": "@dev",
"phpunit/phpunit": "^9.6",
"contao/test-case": "^5.0"
}
}
4 changes: 2 additions & 2 deletions tools/phpunit/phpunit.xml.dist → phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
bootstrap="../../vendor/autoload.php"
bootstrap="vendor/autoload.php"
colors="true"
>
<php>
Expand All @@ -12,7 +12,7 @@

<testsuites>
<testsuite name="MultipageForms Test Suite">
<directory>../../tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
6 changes: 4 additions & 2 deletions src/Controller/FrontendModule/StepsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
#[AsFrontendModule('mp_form_steps', template: 'mod_mp_form_steps')]
class StepsController extends AbstractFrontendModuleController
{
public function __construct(private ContaoFramework $contaoFramework, private FormManagerFactoryInterface $formManagerFactory)
{
public function __construct(
private readonly ContaoFramework $contaoFramework,
private readonly FormManagerFactoryInterface $formManagerFactory,
) {
}

protected function getResponse(Template $template, ModuleModel $model, Request $request): Response
Expand Down
6 changes: 4 additions & 2 deletions src/EventListener/CompileFormFieldsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
#[AsHook('compileFormFields')]
class CompileFormFieldsListener
{
public function __construct(private FormManagerFactoryInterface $formManagerFactory, private RequestStack $requestStack)
{
public function __construct(
private readonly FormManagerFactoryInterface $formManagerFactory,
private readonly RequestStack $requestStack,
) {
}

/**
Expand Down
16 changes: 6 additions & 10 deletions src/EventListener/InsertTagsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#[AsHook('replaceInsertTags')]
class InsertTagsListener
{
public function __construct(private FormManagerFactoryInterface $formManagerFactory)
public function __construct(private readonly FormManagerFactoryInterface $formManagerFactory)
{
}

Expand All @@ -27,15 +27,11 @@ public function __invoke(string $tag)
$value = $chunks[3] ?? '';

$manager = $this->formManagerFactory->forFormId((int) $formId);

switch ($type) {
case 'step':
return $this->getStepValue($manager, $value);
case 'field_value':
return $manager->getDataOfAllSteps()->getAllSubmitted()[$value] ?? '';
}

return '';
return match ($type) {
'step' => $this->getStepValue($manager, $value),
'field_value' => $manager->getDataOfAllSteps()->getAllSubmitted()[$value] ?? '',
default => '',
};
}

private function getStepValue(FormManager $manager, string $value): string
Expand Down
4 changes: 2 additions & 2 deletions src/EventListener/LoadFormFieldListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#[AsHook('loadFormField')]
class LoadFormFieldListener
{
public function __construct(private FormManagerFactoryInterface $formManagerFactory)
public function __construct(private readonly FormManagerFactoryInterface $formManagerFactory)
{
}

Expand All @@ -38,7 +38,7 @@ public function __invoke(Widget $widget, string $formId, array $formData, Form $
// 3. The widget default value itself
if (!$postData->has($widget->name)) {
$widget->value = $stepData->getSubmitted()->get(
$widget->name, $stepData->getOriginalPostData()->get($widget->name, $widget->value)
$widget->name, $stepData->getOriginalPostData()->get($widget->name, $widget->value),
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/EventListener/PrepareFomDataListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#[AsHook('prepareFormData')]
class PrepareFomDataListener
{
public function __construct(private FormManagerFactoryInterface $formManagerFactory, private RequestStack $reqestStack)
{
public function __construct(
private readonly FormManagerFactoryInterface $formManagerFactory,
private readonly RequestStack $reqestStack,
) {
}

/**
Expand Down Expand Up @@ -77,7 +79,7 @@ public function __invoke(array &$submitted, array &$labels, array $fields, Form
private function getUploadedFiles($hook = []): FileParameterBag
{
// Contao 5
if (0 !== \count($hook)) {
if (0 !== (is_countable($hook) ? \count($hook) : 0)) {
return new FileParameterBag($hook);
}

Expand Down
23 changes: 13 additions & 10 deletions src/FormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
class FormManager
{
private FormModel $formModel;

private string $sessionRef;

private string $storageIdentifier;

private bool $prepared = false;

private bool $preparing = false;

/**
Expand All @@ -40,13 +43,13 @@ class FormManager
private bool $isValidFormFieldCombination = true;

public function __construct(
private int $formId,
private Request $request,
private ContaoFramework $contaoFramework,
private StorageInterface $storage,
private StorageIdentifierGeneratorInterface $storageIdentifierGenerator,
private SessionReferenceGeneratorInterface $sessionReferenceGenerator,
private UrlParser $urlParser,
private readonly int $formId,
private readonly Request $request,
private readonly ContaoFramework $contaoFramework,
private readonly StorageInterface $storage,
private readonly StorageIdentifierGeneratorInterface $storageIdentifierGenerator,
private readonly SessionReferenceGeneratorInterface $sessionReferenceGenerator,
private readonly UrlParser $urlParser,
) {
}

Expand Down Expand Up @@ -230,7 +233,7 @@ public function getGetParamForSessionReference()
/**
* @throws RedirectResponseException
*/
public function redirectToStep(int $step): void
public function redirectToStep(int $step): never
{
$this->prepare();
$this->validateStep($step);
Expand Down Expand Up @@ -354,7 +357,7 @@ private function loadFormFieldModels(): void
// Ignore the name of form fields which do not use a name (see contao/core-bundle #1268)
if (
$formFieldModel->name && isset($GLOBALS['TL_DCA']['tl_form_field']['palettes'][$formFieldModel->type])
&& preg_match('/[,;]name[,;]/', $GLOBALS['TL_DCA']['tl_form_field']['palettes'][$formFieldModel->type])
&& preg_match('/[,;]name[,;]/', (string) $GLOBALS['TL_DCA']['tl_form_field']['palettes'][$formFieldModel->type])
) {
$formFieldModels[$formFieldModel->name] = $formFieldModel;
} else {
Expand Down Expand Up @@ -467,7 +470,7 @@ private function initSessionReference(bool $forceNew = false): void

$this->sessionRef = $this->request->query->get(
$this->getGetParamForSessionReference(),
$this->sessionReferenceGenerator->generate($this)
$this->sessionReferenceGenerator->generate($this),
);
}
}
10 changes: 6 additions & 4 deletions src/FormManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
class FormManagerFactory implements FormManagerFactoryInterface
{
private StorageInterface|null $storage = null;

private StorageIdentifierGeneratorInterface|null $storageIdentifierGenerator = null;

private SessionReferenceGeneratorInterface|null $sessionReferenceGenerator = null;

/**
Expand All @@ -28,9 +30,9 @@ class FormManagerFactory implements FormManagerFactoryInterface
private array $managers = [];

public function __construct(
private ContaoFramework $contaoFramework,
private RequestStack $requestStack,
private UrlParser $urlParser,
private readonly ContaoFramework $contaoFramework,
private readonly RequestStack $requestStack,
private readonly UrlParser $urlParser,
) {
}

Expand Down Expand Up @@ -78,7 +80,7 @@ public function forFormId(int $id): FormManager
$storage,
$storageIdentifierGenerator,
$sessionReferenceGenerator,
$this->urlParser
$this->urlParser,
);

if ($storage instanceof FormManagerAwareInterface) {
Expand Down
7 changes: 4 additions & 3 deletions src/Step/FileParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class FileParameterBag extends ParameterBag
*/
public function set(string $name, mixed $value): self
{
if (\is_array($value) &&
\array_key_exists('tmp_name', $value) &&
\is_string($value['tmp_name'])
if (
\is_array($value)
&& \array_key_exists('tmp_name', $value)
&& \is_string($value['tmp_name'])
&& is_uploaded_file($value['tmp_name'])
) {
$target = (new Filesystem())->tempnam(sys_get_temp_dir(), 'nc');
Expand Down
9 changes: 7 additions & 2 deletions src/Step/StepData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@

class StepData
{
private function __construct(private int $step, private ParameterBag $submitted, private ParameterBag $labels, private ParameterBag $files, private ParameterBag $originalPostData)
{
private function __construct(
private readonly int $step,
private ParameterBag $submitted,
private ParameterBag $labels,
private ParameterBag $files,
private ParameterBag $originalPostData,
) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class FixedSessionReferenceGenerator implements SessionReferenceGeneratorInterface
{
public function __construct(private string $identifier)
public function __construct(private readonly string $identifier)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Storage/SessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

class SessionStorage implements StorageInterface
{
public const SESSION_KEY = 'contao.mp_forms';
final public const SESSION_KEY = 'contao.mp_forms';

public function __construct(private Request $request)
public function __construct(private readonly Request $request)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class FixedStorageIdentifierGenerator implements StorageIdentifierGeneratorInterface
{
public function __construct(private string $identifier)
public function __construct(private readonly string $identifier)
{
}

Expand Down
5 changes: 3 additions & 2 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
abstract class AbstractTestCase extends ContaoTestCase
{
protected const STORAGE_IDENTIFIER = 'storage-identifier';

protected const SESSION_IDENTIFIER = 'session-identifier';

protected function createFormFieldsForValidConfiguration(): Collection
Expand Down Expand Up @@ -71,7 +72,7 @@ protected function createFormFieldsForValidConfiguration(): Collection
return new Collection($formFieldsModels, 'tl_form_field');
}

protected function createStorage(StepDataCollection $initialData = null): InMemoryStorage
protected function createStorage(StepDataCollection|null $initialData = null): InMemoryStorage
{
$storage = new InMemoryStorage();

Expand Down Expand Up @@ -116,7 +117,7 @@ protected function createFactory(FormModel $formModel, Collection $formFields, S
$factory = new FormManagerFactory(
$framework,
$stack,
new UrlParser()
new UrlParser(),
);

$factory->setStorage($storage);
Expand Down
6 changes: 3 additions & 3 deletions tests/EventListener/PrepareFormDataListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testDataIsStoredProperlyAndDoesNotAdjustHookParametersIfNotOnLas
$this->mockClassWithProperties(FormModel::class, ['id' => 42]),
$this->createFormFieldsForValidConfiguration(),
$storage,
1 // This mocks step=1 (page 2)
1, // This mocks step=1 (page 2)
);

$listener = new PrepareFomDataListener($factory, $this->createMock(RequestStack::class));
Expand All @@ -45,7 +45,7 @@ public function testDataIsStoredProperlyAndDoesNotAdjustHookParametersIfNotOnLas
} catch (RedirectResponseException $exception) {
$this->assertSame(
'https://www.example.com/form?step=2&ref='.self::SESSION_IDENTIFIER,
$exception->getResponse()->headers->get('Location')
$exception->getResponse()->headers->get('Location'),
);
}

Expand Down Expand Up @@ -74,7 +74,7 @@ public function testDataIsStoredProperlyAndDoesAdjustHookParametersOnLastStep():
$this->mockClassWithProperties(FormModel::class, ['id' => 42]),
$this->createFormFieldsForValidConfiguration(),
$storage,
2 // This mocks step=2 (page 3 - last page)
2, // This mocks step=2 (page 3 - last page)
);

$listener = new PrepareFomDataListener($factory, $this->createMock(RequestStack::class));
Expand Down
6 changes: 3 additions & 3 deletions tests/FormManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testKeepsSameInstance(): void
$factory = new FormManagerFactory(
$this->createMock(ContaoFramework::class),
$requestStack,
$this->createMock(UrlParser::class)
$this->createMock(UrlParser::class),
);

$manager = $factory->forFormId(42);
Expand All @@ -46,7 +46,7 @@ public function testCannotCreateManagerIfNoRequest(): void
$factory = new FormManagerFactory(
$this->createMock(ContaoFramework::class),
new RequestStack(),
$this->createMock(UrlParser::class)
$this->createMock(UrlParser::class),
);

$factory->forFormId(42);
Expand Down Expand Up @@ -78,7 +78,7 @@ public function getData(string $storageIdentifier): StepDataCollection
$factory = new FormManagerFactory(
$this->createMock(ContaoFramework::class),
$requestStack,
$this->createMock(UrlParser::class)
$this->createMock(UrlParser::class),
);

$factory->setStorage($storage);
Expand Down
3 changes: 2 additions & 1 deletion tests/FormManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function testAccessingAllDataWithoutFormFields(): void
->with(42)
->willReturn($formModel)
;

$formFieldModelAdapter = $this->mockAdapter(['findPublishedByPid']);
$formFieldModelAdapter
->expects($this->once())
Expand All @@ -50,7 +51,7 @@ public function testAccessingAllDataWithoutFormFields(): void
$this->createMock(StorageInterface::class),
$this->createMock(StorageIdentifierGeneratorInterface::class),
$this->createMock(SessionReferenceGeneratorInterface::class),
new UrlParser()
new UrlParser(),
);

$this->assertSame([], $formManager->getDataOfAllSteps()->all());
Expand Down
Loading

0 comments on commit d7e68ce

Please sign in to comment.