Skip to content

Commit

Permalink
Merge pull request #261 from gsteel/Integer-Label-BC-Break
Browse files Browse the repository at this point in the history
Add failing test case for integer element labels
  • Loading branch information
Xerkus authored Jan 11, 2024
2 parents ee4a3b6 + cdeb149 commit c6f6c68
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@
<code>translate</code>
<code>translate</code>
</PossiblyNullReference>
<RedundantCastGivenDocblockType>
<code>(string) $label</code>
</RedundantCastGivenDocblockType>
</file>
<file src="src/View/Helper/Form.php">
<DeprecatedMethod>
Expand Down
9 changes: 7 additions & 2 deletions src/View/Helper/AbstractHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,17 @@ protected function hasAllowedPrefix(string $attribute): bool
}

/**
* translate the label
* Translate the label
*
* @internal
*
* @todo Reduce argument to only string in the next major
* @param string $label
*/
protected function translateLabel(string $label): string
protected function translateLabel(int|string|float|bool $label): string
{
$label = (string) $label;

return $this->getTranslator()?->translate($label, $this->getTranslatorTextDomain()) ?? $label;
}

Expand Down
21 changes: 21 additions & 0 deletions test/View/Helper/AbstractHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
namespace LaminasTest\Form\View\Helper;

use Laminas\Escaper\Escaper;
use Laminas\Form\Element\Select;
use Laminas\Form\Exception\InvalidArgumentException;
use Laminas\Form\View\Helper\AbstractHelper;
use Laminas\Form\View\Helper\FormSelect;
use Laminas\I18n\Translator\Translator;

use function range;

/**
* Tests for {@see \Laminas\Form\View\Helper\AbstractHelper}
*
Expand Down Expand Up @@ -236,4 +240,21 @@ public function testNullValueForBooleanAttributeDisablesIt(): void
$this->helper->createAttributesString(['disabled' => null])
);
}

/**
* @deprecated This test should be removed in 4.0 and string should become a hard requirement for labels
*
* @covers \Laminas\Form\View\Helper\AbstractHelper::translateLabel
*/
public function testThatAnIntegerElementLabelWillBeCastToAString(): void
{
$select = new Select('some-name');
$select->setValueOptions(range(0, 10));

$helper = new FormSelect();

$markup = $helper->render($select);

self::assertStringContainsString('<option value="1">1</option>', $markup);
}
}

0 comments on commit c6f6c68

Please sign in to comment.