Merge branch '4' into 5.0

This commit is contained in:
github-actions 2023-07-20 21:38:31 +00:00
commit d8b69c36d9
2 changed files with 66 additions and 21 deletions

View File

@ -396,13 +396,16 @@ abstract class DBSchemaManager
// Create custom fields // Create custom fields
if ($fieldSchema) { if ($fieldSchema) {
foreach ($fieldSchema as $fieldName => $fieldSpec) { foreach ($fieldSchema as $fieldName => $fieldSpec) {
$fieldSpec ??= '';
// convert Enum short array syntax to long array syntax to make parsing $arrayValue below easier
$fieldSpec = preg_replace('/^(enum\()\[(.*?)\]/i', '$1array($2)', $fieldSpec);
//Is this an array field? //Is this an array field?
$arrayValue = ''; $arrayValue = '';
if (strpos($fieldSpec ?? '', '[') !== false) { $pos = strpos($fieldSpec, '[');
if ($pos !== false) {
//If so, remove it and store that info separately //If so, remove it and store that info separately
$pos = strpos($fieldSpec ?? '', '['); $arrayValue = substr($fieldSpec, $pos);
$arrayValue = substr($fieldSpec ?? '', $pos ?? 0); $fieldSpec = substr($fieldSpec, 0, $pos);
$fieldSpec = substr($fieldSpec ?? '', 0, $pos);
} }
/** @var DBField $fieldObj */ /** @var DBField $fieldObj */

View File

@ -16,25 +16,67 @@ class DBEnumTest extends SapphireTest
protected $usesDatabase = true; protected $usesDatabase = true;
public function testDefault() /**
* @dataProvider provideParse
*
* nullifyEmpty is an option on DBString, which DBEnum extends
* Mainly used for testing that Enum short-array style syntax works while passing in options
*/
public function testParse(?string $expectedDefault, bool $expectedNullifyEmpty, string $spec)
{ {
/** @var DBEnum $enum1 */ /** @var DBEnum $enum */
$enum1 = DBField::create_field('Enum("A, B, C, D")', null); $enum = DBField::create_field($spec, null);
/** @var DBEnum $enum2 */ $this->assertEquals($expectedDefault, $enum->getDefaultValue());
$enum2 = DBField::create_field('Enum("A, B, C, D", "")', null); $this->assertEquals($expectedDefault, $enum->getDefault());
/** @var DBEnum $enum3 */ $this->assertEquals($expectedNullifyEmpty, $enum->getNullifyEmpty());
$enum3 = DBField::create_field('Enum("A, B, C, D", null)', null); }
/** @var DBEnum $enum4 */
$enum4 = DBField::create_field('Enum("A, B, C, D", 1)', null);
$this->assertEquals('A', $enum1->getDefaultValue()); public function provideParse()
$this->assertEquals('A', $enum1->getDefault()); {
$this->assertEquals(null, $enum2->getDefaultValue()); return [
$this->assertEquals(null, $enum2->getDefault()); // standard syntax - double quotes
$this->assertEquals(null, $enum3->getDefaultValue()); ['A', true, 'Enum("A, B, C, D")'],
$this->assertEquals(null, $enum3->getDefault()); ['B', true, 'Enum("A, B, C, D", "B")'],
$this->assertEquals('B', $enum4->getDefaultValue()); ['C', true, 'Enum("A, B, C, D", 2)'],
$this->assertEquals('B', $enum4->getDefault()); [null, true, 'Enum("A, B, C, D", "")'],
[null, true, 'Enum("A, B, C, D", null)'],
['B', false, 'Enum("A, B, C, D", "B", ["nullifyEmpty" => false])'],
// standard syntax - single quotes
['A', true, "Enum('A, B, C, D')"],
['B', true, "Enum('A, B, C, D', 'B')"],
['C', true, "Enum('A, B, C, D', 2)"],
[null, true, "Enum('A, B, C, D', '')"],
[null, true, "Enum('A, B, C, D', null)"],
['B', false, "Enum('A, B, C, D', 'B', ['nullifyEmpty' => false])"],
// long array syntax - double quotes
['A', true, 'Enum(array("A", "B", "C", "D"))'],
['B', true, 'Enum(array("A", "B", "C", "D"), "B")'],
['C', true, 'Enum(array("A", "B", "C", "D"), 2)'],
[null, true, 'Enum(array("A", "B", "C", "D"), "")'],
[null, true, 'Enum(array("A", "B", "C", "D"), null)'],
['B', false, 'Enum(array("A", "B", "C", "D"), "B", ["nullifyEmpty" => false])'],
// long array syntax - single quotes
['A', true, "Enum(array('A', 'B', 'C', 'D'))"],
['B', true, "Enum(array('A', 'B', 'C', 'D'), 'B')"],
['C', true, "Enum(array('A', 'B', 'C', 'D'), 2)"],
[null, true, "Enum(array('A', 'B', 'C', 'D'), '')"],
[null, true, "Enum(array('A', 'B', 'C', 'D'), null)"],
['B', false, "Enum(array('A', 'B', 'C', 'D'), 'B', ['nullifyEmpty' => false])"],
// short array syntax - double quotes
['A', true, 'Enum(["A", "B", "C", "D"])'],
['B', true, 'Enum(["A", "B", "C", "D"], "B")'],
['C', true, 'Enum(["A", "B", "C", "D"], 2)'],
[null, true, 'Enum(["A", "B", "C", "D"], "")'],
[null, true, 'Enum(["A", "B", "C", "D"], null)'],
['B', false, 'Enum(["A", "B", "C", "D"], "B", ["nullifyEmpty" => false])'],
// short array syntax - single quotes
['A', true, "Enum(['A', 'B', 'C', 'D'])"],
['B', true, "Enum(['A', 'B', 'C', 'D'], 'B')"],
['C', true, "Enum(['A', 'B', 'C', 'D'], 2)"],
[null, true, "Enum(['A', 'B', 'C', 'D'], '')"],
[null, true, "Enum(['A', 'B', 'C', 'D'], null)"],
['B', false, "Enum(['A', 'B', 'C', 'D'], 'B', ['nullifyEmpty' => false])"],
];
} }
public function testObsoleteValues() public function testObsoleteValues()