Skip to content

Commit 5764ad5

Browse files
committed
fix: improve function names, test cases and validations
[DIS-735]
1 parent c6dc41f commit 5764ad5

File tree

4 files changed

+78
-38
lines changed

4 files changed

+78
-38
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Linio\DynamicFormBundle\Exception;
6+
7+
class InvalidConfigurationException extends DynamicFormException
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Linio\DynamicFormBundle\Exception;
6+
7+
class NumberFormatException extends DynamicFormException
8+
{
9+
}

src/FormlyMapper/FormlyField/BirthdayField.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Linio\DynamicFormBundle\FormlyMapper\FormlyField;
66

7+
use Linio\DynamicFormBundle\Exception\InvalidConfigurationException;
8+
use Linio\DynamicFormBundle\Exception\NumberFormatException;
79
use Linio\DynamicFormBundle\FormlyMapper\FormlyField;
810

911
class BirthdayField extends FormlyField
@@ -24,9 +26,13 @@ protected function getFieldType()
2426
*/
2527
protected function buildFieldTypeConfiguration(): void
2628
{
27-
$this->formlyFieldConfiguration['templateOptions']['type'] = $this->getFieldType();
2829
$options = $this->fieldConfiguration['options'];
2930

31+
$this->validateNumberFormatForSpecificRangeOfYearsArray($options);
32+
$this->validateMaxAgeAllowedIsLargerThanMinAgeAllowed($options);
33+
34+
$this->formlyFieldConfiguration['templateOptions']['type'] = $this->getFieldType();
35+
3036
$order = isset($options['order']) && (strtolower($options['order']) == (self::ORDER_ASC || self::ORDER_DESC))
3137
? $options['order']
3238
: self::ORDER_DESC;
@@ -36,26 +42,49 @@ protected function buildFieldTypeConfiguration(): void
3642
if (!isset($options['minAgeAllowed']) || !isset($options['maxAgeAllowed'])
3743
|| !is_numeric($options['minAgeAllowed']) || !is_numeric($options['maxAgeAllowed'])
3844
|| $options['minAgeAllowed'] < 0 || $options['maxAgeAllowed'] < 0) {
39-
$this->cleanAgeAllowedInTemplateOptionsArray();
45+
$this->removeAgeAllowedFromTemplateOptions();
4046

4147
return;
4248
}
4349

44-
$minAgeAllowed = min($options['minAgeAllowed'], $options['maxAgeAllowed']);
45-
$maxAgeAllowed = max($options['minAgeAllowed'], $options['maxAgeAllowed']);
46-
47-
$yearsRange = range(date('Y') - $minAgeAllowed, date('Y') - $maxAgeAllowed);
50+
$yearsRange = range(date('Y') - $options['minAgeAllowed'], date('Y') - $options['maxAgeAllowed']);
4851

4952
$this->formlyFieldConfiguration['templateOptions']['years'] = ($order == self::ORDER_ASC)
5053
? array_reverse($yearsRange)
5154
: $yearsRange;
5255

53-
$this->cleanAgeAllowedInTemplateOptionsArray();
56+
$this->removeAgeAllowedFromTemplateOptions();
5457
}
5558

56-
private function cleanAgeAllowedInTemplateOptionsArray(): void
59+
private function removeAgeAllowedFromTemplateOptions(): void
5760
{
5861
unset($this->formlyFieldConfiguration['templateOptions']['minAgeAllowed']);
5962
unset($this->formlyFieldConfiguration['templateOptions']['maxAgeAllowed']);
6063
}
64+
65+
private function validateNumberFormatForSpecificRangeOfYearsArray(array $options): void
66+
{
67+
if (!isset($options['years'])) {
68+
return;
69+
}
70+
71+
foreach ($options['years'] as $key => $year) {
72+
if (!is_numeric($year)) {
73+
throw new NumberFormatException(sprintf('The year "%s" does not have a valid number format.', $year));
74+
}
75+
76+
$options['years'][$key] = (int) $year;
77+
}
78+
}
79+
80+
private function validateMaxAgeAllowedIsLargerThanMinAgeAllowed(array $options): void
81+
{
82+
if (!isset($options['minAgeAllowed']) || !isset($options['maxAgeAllowed'])) {
83+
return;
84+
}
85+
86+
if ($options['minAgeAllowed'] > $options['maxAgeAllowed']) {
87+
throw new InvalidConfigurationException(sprintf('The value of minAgeAllowed (%s) cannot be greater than maxAgeAllowed (%s).', $options['minAgeAllowed'], $options['maxAgeAllowed']));
88+
}
89+
}
6190
}

tests/FormlyMapper/FormlyField/BirthdayFieldTest.php

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Linio\DynamicFormBundle\Tests\FormlyMapper\FormlyField;
66

7+
use Linio\DynamicFormBundle\Exception\InvalidConfigurationException;
8+
use Linio\DynamicFormBundle\Exception\NumberFormatException;
79
use Linio\DynamicFormBundle\FormlyMapper\FormlyField\BirthdayField;
810
use PHPUnit\Framework\TestCase;
911

@@ -85,33 +87,24 @@ public function testIsAddingSpecificYears(array $fieldConfiguration, array $expe
8587
/**
8688
* @dataProvider basicDataProvider
8789
*/
88-
public function testWithNotNumberSpecificYears(array $fieldConfiguration, array $expected): void
90+
public function testIsThrowingExceptionWhenYearsArrayHasNonNumericalValues(array $fieldConfiguration, array $expected): void
8991
{
90-
$goodYears = [2000, 2001, 2002, 2003, 2004, 2005];
91-
$badYears = ['2000', 2001, '2002', 2003, 2004, 2005];
92+
$this->expectException(NumberFormatException::class);
9293

94+
$badYears = ['2000 ', 2001, '20O2', 2003, 2004, 2005];
9395
$fieldConfiguration['options']['years'] = $badYears;
9496

95-
$expected['templateOptions']['years'] = $goodYears;
96-
9797
$this->formlyField->setFieldConfiguration($fieldConfiguration);
98-
$actual = $this->formlyField->getFormlyFieldConfiguration();
99-
100-
$this->assertEquals($expected, $actual);
98+
$this->formlyField->getFormlyFieldConfiguration();
10199
}
102100

103101
/**
104102
* @dataProvider basicDataProvider
105103
*/
106-
public function testIsAddingYearsRangeInOrderByDefault(array $fieldConfiguration, array $expected): void
104+
public function testItDoesNotAllowNonNumericalValuesForAllowedAges(array $fieldConfiguration, array $expected): void
107105
{
108-
$minAgeAllowed = 18;
109-
$maxAgeAllowed = 120;
110-
111-
$fieldConfiguration['options']['minAgeAllowed'] = $minAgeAllowed;
112-
$fieldConfiguration['options']['maxAgeAllowed'] = $maxAgeAllowed;
113-
114-
$expected['templateOptions']['years'] = range(date('Y') - $minAgeAllowed, date('Y') - $maxAgeAllowed);
106+
$fieldConfiguration['options']['minAgeAllowed'] = '18a';
107+
$fieldConfiguration['options']['maxAgeAllowed'] = 120;
115108

116109
$this->formlyField->setFieldConfiguration($fieldConfiguration);
117110
$actual = $this->formlyField->getFormlyFieldConfiguration();
@@ -122,10 +115,15 @@ public function testIsAddingYearsRangeInOrderByDefault(array $fieldConfiguration
122115
/**
123116
* @dataProvider basicDataProvider
124117
*/
125-
public function testWithBadAgeRangeForWord(array $fieldConfiguration, array $expected): void
118+
public function testIsAddingYearsRangeInDefaultOrder(array $fieldConfiguration, array $expected): void
126119
{
127-
$fieldConfiguration['options']['minAgeAllowed'] = '18a';
128-
$fieldConfiguration['options']['maxAgeAllowed'] = 120;
120+
$minAgeAllowed = 18;
121+
$maxAgeAllowed = 120;
122+
123+
$fieldConfiguration['options']['minAgeAllowed'] = $minAgeAllowed;
124+
$fieldConfiguration['options']['maxAgeAllowed'] = $maxAgeAllowed;
125+
126+
$expected['templateOptions']['years'] = range(date('Y') - $minAgeAllowed, date('Y') - $maxAgeAllowed);
129127

130128
$this->formlyField->setFieldConfiguration($fieldConfiguration);
131129
$actual = $this->formlyField->getFormlyFieldConfiguration();
@@ -136,7 +134,7 @@ public function testWithBadAgeRangeForWord(array $fieldConfiguration, array $exp
136134
/**
137135
* @dataProvider basicDataProvider
138136
*/
139-
public function testWithBadAgeRangeForNegativeNumber(array $fieldConfiguration, array $expected): void
137+
public function testItDoesNotAllowNegativeValuesForAllowedAges(array $fieldConfiguration, array $expected): void
140138
{
141139
$fieldConfiguration['options']['minAgeAllowed'] = -2;
142140
$fieldConfiguration['options']['maxAgeAllowed'] = 120;
@@ -169,20 +167,15 @@ public function testWithRangeZero(array $fieldConfiguration, array $expected): v
169167
/**
170168
* @dataProvider basicDataProvider
171169
*/
172-
public function testWithMinAgeAllowedBiggerThanMaxAgeAllowed(array $fieldConfiguration, array $expected): void
170+
public function testIsThrowExceptionWhenMinAgeAllowedValueIsLargerThanMaxAgeAllowed(array $fieldConfiguration, array $expected): void
173171
{
174-
$minAgeAllowed = 18;
175-
$maxAgeAllowed = 120;
172+
$this->expectException(InvalidConfigurationException::class);
176173

177-
$fieldConfiguration['options']['minAgeAllowed'] = $maxAgeAllowed;
178-
$fieldConfiguration['options']['maxAgeAllowed'] = $minAgeAllowed;
179-
180-
$expected['templateOptions']['years'] = range(date('Y') - $minAgeAllowed, date('Y') - $maxAgeAllowed);
174+
$fieldConfiguration['options']['minAgeAllowed'] = 120;
175+
$fieldConfiguration['options']['maxAgeAllowed'] = 18;
181176

182177
$this->formlyField->setFieldConfiguration($fieldConfiguration);
183-
$actual = $this->formlyField->getFormlyFieldConfiguration();
184-
185-
$this->assertEquals($expected, $actual);
178+
$this->formlyField->getFormlyFieldConfiguration();
186179
}
187180

188181
/**

0 commit comments

Comments
 (0)