Skip to content

Commit d882fe4

Browse files
vjikroxblnfk
andauthored
Support more types as value of SingleTable attribute (#103)
Co-authored-by: Aleksei Gagarin <roxblnfk@ya.ru>
1 parent 7dad356 commit d882fe4

7 files changed

Lines changed: 81 additions & 3 deletions

File tree

‎.github/workflows/main.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Setup DB services
1717
run: |
1818
cd tests
19-
docker-compose up -d
19+
docker compose up -d
2020
cd ..
2121
- name: Setup PHP ${{ matrix.php-version }}
2222
uses: shivammathur/setup-php@v2

‎src/Annotation/Inheritance/SingleTable.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
1616
class SingleTable extends Inheritance
1717
{
18+
protected ?string $value;
19+
1820
public function __construct(
19-
protected ?string $value = null
21+
string|int|float|\Stringable|\BackedEnum|null $value = null
2022
) {
23+
$this->value = $value === null
24+
? null
25+
: (string) ($value instanceof \BackedEnum ? $value->value : $value);
2126
parent::__construct('single');
2227
}
2328

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Unit\Attribute\SingleTable;
6+
7+
enum IntegerEnum: int
8+
{
9+
case ONE = 1;
10+
case TWO = 2;
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Unit\Attribute\SingleTable;
6+
7+
use Cycle\Annotated\Annotation\Inheritance\SingleTable;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class SingleTableTest extends TestCase
12+
{
13+
public static function dataBase(): iterable
14+
{
15+
yield [null, null];
16+
yield ['test', 'test'];
17+
yield ['42', 42];
18+
yield ['36.6', 36.6];
19+
yield ['test', new StringableObject('test')];
20+
yield ['1', IntegerEnum::ONE];
21+
yield ['a', StringEnum::A];
22+
}
23+
24+
#[DataProvider('dataBase')]
25+
public function testBase1(?string $expected, mixed $value): void
26+
{
27+
$attribute = new SingleTable($value);
28+
29+
$this->assertSame($expected, $attribute->getValue());
30+
}
31+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Unit\Attribute\SingleTable;
6+
7+
enum StringEnum: string
8+
{
9+
case A = 'a';
10+
case B = 'b';
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Unit\Attribute\SingleTable;
6+
7+
use Stringable;
8+
9+
final class StringableObject implements Stringable
10+
{
11+
public function __construct(
12+
private string $value,
13+
) {
14+
}
15+
16+
public function __toString(): string
17+
{
18+
return $this->value;
19+
}
20+
}

‎tests/docker-compose.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ services:
1111
ACCEPT_EULA: "Y"
1212

1313
mysql_latest:
14-
image: mysql:latest
14+
image: mysql:8.0
1515
restart: always
1616
command: --default-authentication-plugin=mysql_native_password
1717
ports:

0 commit comments

Comments
 (0)