Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed May 17, 2017
1 parent 9f970cb commit 30c8a82
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 278 deletions.
7 changes: 1 addition & 6 deletions Classes/Service/ViewHelper/Field/FieldContextEntry.php
Expand Up @@ -14,9 +14,8 @@
namespace Romm\Formz\Service\ViewHelper\Field;

use Romm\Formz\Configuration\Form\Field\Field;
use TYPO3\CMS\Core\SingletonInterface;

class FieldContextEntry implements SingletonInterface
class FieldContextEntry
{
/**
* @var Field
Expand All @@ -37,10 +36,6 @@ public function __construct(Field $field)
}

/**
* Returns the current field which was defined by the `FieldViewHelper`.
*
* Returns null if no current field was found.
*
* @return Field|null
*/
public function getField()
Expand Down
33 changes: 17 additions & 16 deletions Classes/Service/ViewHelper/Field/FieldViewHelperService.php
Expand Up @@ -27,6 +27,10 @@
class FieldViewHelperService implements SingletonInterface
{
/**
* Contains all current fields being rendered by FormZ: if a field is
* rendered beneath another field, several entries will be added to this
* property.
*
* @var FieldContextEntry[]
*/
protected $contextEntries;
Expand All @@ -37,7 +41,9 @@ class FieldViewHelperService implements SingletonInterface
protected $view;

/**
* @todo rename ?
* Adds a new context entry to the entries array. The other field-related
* methods will be processed on this entry until the field rendering has
* ended.
*
* @param Field $field
*/
Expand All @@ -47,8 +53,15 @@ public function setCurrentField(Field $field)
}

/**
* Checks that the `FieldViewHelper` has been called. If not, an exception
* is thrown.
* Removes the current field context entry.
*/
public function removeCurrentField()
{
array_pop($this->contextEntries);
}

/**
* Checks that a field context is found.
*
* @return bool
*/
Expand All @@ -58,11 +71,7 @@ public function fieldContextExists()
}

/**
* Returns the current field which was defined by the `FieldViewHelper`.
*
* Returns null if no current field was found.
*
* @return Field|null
* @return Field
*/
public function getCurrentField()
{
Expand Down Expand Up @@ -109,14 +118,6 @@ public function getView(Layout $layout)
return $this->view[$identifier];
}

/**
* Reset every state that can be used by this service.
*/
public function resetState()
{
array_pop($this->contextEntries);
}

/**
* @return FieldContextEntry
*/
Expand Down
20 changes: 9 additions & 11 deletions Classes/Service/ViewHelper/Slot/SlotViewHelperService.php
Expand Up @@ -27,15 +27,21 @@ class SlotViewHelperService implements SingletonInterface
protected $contextEntries = [];

/**
* @todo
*
* @param RenderingContextInterface $renderingContext
*/
public function initialize(RenderingContextInterface $renderingContext)
public function activate(RenderingContextInterface $renderingContext)
{
$this->contextEntries[] = GeneralUtility::makeInstance(SlotContextEntry::class, $renderingContext);
}

/**
* Removes the current context entry.
*/
public function resetState()
{
array_pop($this->contextEntries);
}

/**
* @see \Romm\Formz\Service\ViewHelper\Slot\SlotContextEntry::addSlot()
*
Expand Down Expand Up @@ -104,14 +110,6 @@ public function restoreTemplateVariables($slotName)
$this->getCurrentContext()->restoreTemplateVariables($slotName);
}

/**
* Removes the current context entry.
*/
public function resetState()
{
array_pop($this->contextEntries);
}

/**
* @return SlotContextEntry
*/
Expand Down
7 changes: 4 additions & 3 deletions Classes/ViewHelpers/FieldViewHelper.php
Expand Up @@ -105,9 +105,10 @@ public function render()
$this->injectFieldInService($this->arguments['name']);

/*
* @todo
* Activating the slot service, which will be used all along the
* rendering of this very field.
*/
$this->slotService->initialize($this->renderingContext);
$this->slotService->activate($this->renderingContext);

/*
* Calling this here will process every view helper beneath this one,
Expand Down Expand Up @@ -136,7 +137,7 @@ public function render()
/*
* Resetting all services data.
*/
$this->fieldService->resetState();
$this->fieldService->removeCurrentField();
$this->slotService->resetState();

$viewHelperVariableContainer->setView($currentView);
Expand Down
3 changes: 1 addition & 2 deletions Classes/ViewHelpers/FormatMessageViewHelper.php
Expand Up @@ -148,8 +148,7 @@ protected function getFieldName()
if (empty($fieldName)
&& $this->fieldService->fieldContextExists()
) {
$field = $this->fieldService->getCurrentField();
$fieldName = $field->getName();
$fieldName = $this->fieldService->getCurrentField()->getName();
}

if (null === $fieldName) {
Expand Down
52 changes: 52 additions & 0 deletions Tests/Unit/Service/ViewHelper/Field/FieldContentEntryTest.php
@@ -0,0 +1,52 @@
<?php
namespace Romm\Formz\Tests\Unit\Service\ViewHelper\Field;

use Romm\Formz\Configuration\Form\Field\Field;
use Romm\Formz\Service\ViewHelper\Field\FieldContextEntry;
use Romm\Formz\Tests\Unit\AbstractUnitTest;

class FieldContentEntryTest extends AbstractUnitTest
{
/**
* @test
*/
public function initializationDoneProperly()
{
$field = new Field;

$fieldContextEntry = new FieldContextEntry($field);
$this->assertSame($field, $fieldContextEntry->getField());
}

/**
* @test
*/
public function setFieldOptionSetsFieldOption()
{
$fieldContextEntry = new FieldContextEntry(new Field);

$fieldContextEntry->setOption('foo', 'bar');
$this->assertEquals(
['foo' => 'bar'],
$fieldContextEntry->getOptions()
);

$fieldContextEntry->setOption('bar', 'baz');
$this->assertEquals(
[
'foo' => 'bar',
'bar' => 'baz'
],
$fieldContextEntry->getOptions()
);

$fieldContextEntry->setOption('foo', 'baz');
$this->assertEquals(
[
'foo' => 'baz',
'bar' => 'baz'
],
$fieldContextEntry->getOptions()
);
}
}
56 changes: 20 additions & 36 deletions Tests/Unit/Service/ViewHelper/Field/FieldViewHelperServiceTest.php
Expand Up @@ -11,7 +11,7 @@ class FieldViewHelperServiceTest extends AbstractUnitTest
/**
* @test
*/
public function formContextActivatedTwiceThrowsException()
public function setCurrentFieldSetsCurrentField()
{
$fieldService = new FieldViewHelperService;
$field = new Field;
Expand All @@ -25,51 +25,35 @@ public function formContextActivatedTwiceThrowsException()
/**
* @test
*/
public function setFieldOptionSetsFieldOption()
public function resetStateResetsState()
{
$fieldService = new FieldViewHelperService;
$field = new Field;

$this->assertFalse($fieldService->fieldContextExists());
$fieldService->setCurrentField($field);
$this->assertTrue($fieldService->fieldContextExists());

$fieldService->setFieldOption('foo', 'bar');
$this->assertEquals(
['foo' => 'bar'],
$fieldService->getFieldOptions()
);

$fieldService->setFieldOption('bar', 'baz');
$this->assertEquals(
[
'foo' => 'bar',
'bar' => 'baz'
],
$fieldService->getFieldOptions()
);

$fieldService->setFieldOption('foo', 'baz');
$this->assertEquals(
[
'foo' => 'baz',
'bar' => 'baz'
],
$fieldService->getFieldOptions()
);
$fieldService->removeCurrentField();

$this->assertFalse($fieldService->fieldContextExists());
}

/**
* @test
*/
public function resetStateResetsState()
public function nestingFieldsWorks()
{
$fieldService = new FieldViewHelperService;
$field = new Field;

$fieldService->setCurrentField($field);
$fieldService->setFieldOption('foo', 'bar');

$fieldService->resetState();

$this->assertFalse($fieldService->fieldContextExists());
$this->assertNull($fieldService->getCurrentField());
$this->assertEmpty($fieldService->getFieldOptions());
$field1 = new Field;
$field2 = new Field;

$fieldService->setCurrentField($field1);
$this->assertSame($field1, $fieldService->getCurrentField());
$fieldService->setCurrentField($field2);
$this->assertSame($field2, $fieldService->getCurrentField());
$fieldService->removeCurrentField();
$this->assertSame($field1, $fieldService->getCurrentField());
}

/**
Expand Down

0 comments on commit 30c8a82

Please sign in to comment.