Skip to content

Commit a3df067

Browse files
committed
Fixed check constraint result (removed backticks) & added tests
1 parent 4d931cd commit a3df067

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/Command/SqlGeneratorCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ private static function getCheck(\ReflectionProperty $property, string $beanName
403403
foreach ($property->getAttributes(\CoolBeans\Attribute\CheckConstraint::class) as $index => $attribute) {
404404
$constraintName = 'check_' . $beanName . '_' . $property->getName() . '_' . $index;
405405

406-
$return[] = self::INDENTATION . 'CONSTRAINT `' . $constraintName . '` CHECK (`' . $attribute->newInstance()->expression . '`)';
406+
$return[] = self::INDENTATION . 'CONSTRAINT `' . $constraintName . '` CHECK (' . $attribute->newInstance()->expression . ')';
407407
}
408408

409409
return $return;

tests/Unit/Command/SqlGeneratorCommandTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function testSimple() : void
2525
`col8` DATETIME NOT NULL,
2626
`col9` DATETIME NOT NULL,
2727
28+
CONSTRAINT `check_SimpleBean2_0` CHECK (IF(`col3` = 'abc', `col4` IS NOT NULL, TRUE)),
29+
CONSTRAINT `check_SimpleBean2_col3_0` CHECK (CHAR_LENGTH(`col3`) > 3),
30+
2831
PRIMARY KEY (`id`)
2932
)
3033
CHARSET = `utf8mb4`
@@ -78,7 +81,16 @@ public function testSimple() : void
7881
)
7982
CHARSET = `utf8mb4`
8083
COLLATE = `utf8mb4_general_ci`;
81-
84+
85+
CREATE TABLE `two_primary_keys_bean`(
86+
`col1` INT(11) NOT NULL,
87+
`col2` INT(11) NOT NULL,
88+
89+
PRIMARY KEY (`col1`, `col2`)
90+
)
91+
CHARSET = `utf8mb4`
92+
COLLATE = `utf8mb4_general_ci`;
93+
8294
CREATE TABLE `attribute_bean`(
8395
`col2` DATE NOT NULL DEFAULT NOW(),
8496
`col3` TIME NOT NULL DEFAULT NOW(),

tests/Unit/TestBean/SimpleBean2.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
//@phpcs:disable SlevomatCodingStandard.Classes.ClassStructure.IncorrectGroupOrder
88
//@phpcs:disable SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedProperty
9+
#[\CoolBeans\Attribute\ClassCheckConstraint('IF(`col3` = \'abc\', `col4` IS NOT NULL, TRUE)')]
910
final class SimpleBean2 extends \CoolBeans\Bean
1011
{
1112
private int $col1;
1213
protected string $col2 = 'default';
1314
public \CoolBeans\PrimaryKey\IntPrimaryKey $id;
15+
#[\CoolBeans\Attribute\CheckConstraint('CHAR_LENGTH(`col3`) > 3')]
1416
public string $col3;
1517
public ?string $col4;
1618
public ?string $col5 = null;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace CoolBeans\Tests\Unit\TestBean;
6+
7+
#[\CoolBeans\Attribute\PrimaryKey('col1', 'col2')]
8+
final class TwoPrimaryKeysBean extends \CoolBeans\Bean
9+
{
10+
public int $col1;
11+
public int $col2;
12+
}

0 commit comments

Comments
 (0)