Skip to content

Commit ddb744b

Browse files
Tests defining enum values in various ways
1 parent c57b524 commit ddb744b

File tree

1 file changed

+58
-11
lines changed

1 file changed

+58
-11
lines changed

tests/Annotated/Unit/Attribute/ColumnTest.php

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
namespace Cycle\Annotated\Tests\Unit\Attribute;
66

77
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Tests\Unit\Attribute\SingleTable\StringEnum;
89
use PHPUnit\Framework\TestCase;
910

1011
class ColumnTest extends TestCase
1112
{
13+
public const ENUM_VALUE_A = 'a';
14+
public const ENUM_VALUE_B = 'b';
15+
1216
#[Column('integer', nullable: true, unsigned: true)]
1317
private $column1;
1418

@@ -21,42 +25,85 @@ class ColumnTest extends TestCase
2125
#[Column('string(32)', readonlySchema: true)]
2226
private mixed $column4;
2327

28+
#[Column(type: 'enum(a,b)')]
29+
private string $column5;
30+
31+
#[Column(
32+
type: 'enum',
33+
default: self::ENUM_VALUE_A,
34+
values: [self::ENUM_VALUE_A, self::ENUM_VALUE_B],
35+
)]
36+
private string $column6 = self::ENUM_VALUE_A;
37+
38+
#[Column(
39+
type: 'enum',
40+
default: 'a',
41+
typecast: StringEnum::class,
42+
values: StringEnum::class,
43+
)]
44+
private StringEnum $column7 = StringEnum::A;
45+
2446
public function testOneAttribute(): void
2547
{
26-
$attr = $this->getAttribute('column1');
48+
$column = $this->getColumn('column1');
2749

28-
$this->assertSame(['unsigned' => true], $attr->getAttributes());
50+
$this->assertSame(['unsigned' => true], $column->getAttributes());
2951
}
3052

3153
public function testTwoAttributes(): void
3254
{
33-
$attr = $this->getAttribute('column2');
55+
$column = $this->getColumn('column2');
3456

35-
$this->assertSame(['unsigned' => true, 'zerofill' => true], $attr->getAttributes());
57+
$this->assertSame(['unsigned' => true, 'zerofill' => true], $column->getAttributes());
3658
}
3759

3860
public function testCustomSizeAttribute(): void
3961
{
40-
$attr = $this->getAttribute('column3');
62+
$column = $this->getColumn('column3');
4163

42-
$this->assertSame(['size' => 128], $attr->getAttributes());
64+
$this->assertSame(['size' => 128], $column->getAttributes());
4365
}
4466

4567
public function testDefaultReadonlySchema(): void
4668
{
47-
$attr = $this->getAttribute('column1');
69+
$column = $this->getColumn('column1');
4870

49-
$this->assertFalse($attr->isReadonlySchema());
71+
$this->assertFalse($column->isReadonlySchema());
5072
}
5173

5274
public function testReadonlySchema(): void
5375
{
54-
$attr = $this->getAttribute('column4');
76+
$column = $this->getColumn('column4');
77+
78+
$this->assertTrue($column->isReadonlySchema());
79+
}
80+
81+
public function testEnumTypeString(): void
82+
{
83+
$column = $this->getColumn('column5');
84+
85+
$this->assertSame('enum(a,b)', $column->getType());
86+
}
87+
88+
public function testEnumTypeArray(): void
89+
{
90+
$column = $this->getColumn('column6');
91+
92+
$this->assertSame('enum(a,b)', $column->getType());
93+
$this->assertSame('a', $column->getDefault());
94+
$this->assertArrayNotHasKey('values', $column->getAttributes());
95+
}
96+
97+
public function testEnumTypeBackedEnum(): void
98+
{
99+
$column = $this->getColumn('column7');
55100

56-
$this->assertTrue($attr->isReadonlySchema());
101+
$this->assertSame('enum(a,b)', $column->getType());
102+
$this->assertSame('a', $column->getDefault());
103+
$this->assertArrayNotHasKey('values', $column->getAttributes());
57104
}
58105

59-
private function getAttribute(string $field): Column
106+
private function getColumn(string $field): Column
60107
{
61108
$ref = new \ReflectionClass(static::class);
62109
return $ref->getProperty($field)->getAttributes(Column::class)[0]->newInstance();

0 commit comments

Comments
 (0)