Skip to content

Commit

Permalink
FIX Short-array syntax for Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jul 20, 2023
1 parent 8c3ba81 commit 7daa3fd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 22 deletions.
11 changes: 7 additions & 4 deletions src/ORM/Connect/DBSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,16 @@ public function requireTable(
// Create custom fields
if ($fieldSchema) {
foreach ($fieldSchema as $fieldName => $fieldSpec) {
$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?
$arrayValue = '';
if (strpos($fieldSpec ?? '', '[') !== false) {
$pos = strpos($fieldSpec, '[');
if ($pos !== false) {
//If so, remove it and store that info separately
$pos = strpos($fieldSpec ?? '', '[');
$arrayValue = substr($fieldSpec ?? '', $pos ?? 0);
$fieldSpec = substr($fieldSpec ?? '', 0, $pos);
$arrayValue = substr($fieldSpec, $pos);
$fieldSpec = substr($fieldSpec, 0, $pos);
}

/** @var DBField $fieldObj */
Expand Down
78 changes: 60 additions & 18 deletions tests/php/ORM/DBEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,67 @@ class DBEnumTest extends SapphireTest

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 */
$enum1 = DBField::create_field('Enum("A, B, C, D")', null);
/** @var DBEnum $enum2 */
$enum2 = DBField::create_field('Enum("A, B, C, D", "")', null);
/** @var DBEnum $enum3 */
$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());
$this->assertEquals('A', $enum1->getDefault());
$this->assertEquals(null, $enum2->getDefaultValue());
$this->assertEquals(null, $enum2->getDefault());
$this->assertEquals(null, $enum3->getDefaultValue());
$this->assertEquals(null, $enum3->getDefault());
$this->assertEquals('B', $enum4->getDefaultValue());
$this->assertEquals('B', $enum4->getDefault());
/** @var DBEnum $enum */
$enum = DBField::create_field($spec, null);
$this->assertEquals($expectedDefault, $enum->getDefaultValue());
$this->assertEquals($expectedDefault, $enum->getDefault());
$this->assertEquals($expectedNullifyEmpty, $enum->getNullifyEmpty());
}

public function provideParse()
{
return [
// standard 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])'],
// 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()
Expand Down

0 comments on commit 7daa3fd

Please sign in to comment.