Skip to content

Commit

Permalink
Support more types as value of SingleTable attribute (#103)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksei Gagarin <roxblnfk@ya.ru>
  • Loading branch information
vjik and roxblnfk committed Sep 3, 2024
1 parent 7dad356 commit d882fe4
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup DB services
run: |
cd tests
docker-compose up -d
docker compose up -d
cd ..
- name: Setup PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
Expand Down
7 changes: 6 additions & 1 deletion src/Annotation/Inheritance/SingleTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
class SingleTable extends Inheritance
{
protected ?string $value;

public function __construct(
protected ?string $value = null
string|int|float|\Stringable|\BackedEnum|null $value = null
) {
$this->value = $value === null
? null
: (string) ($value instanceof \BackedEnum ? $value->value : $value);
parent::__construct('single');
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Annotated/Unit/Attribute/SingleTable/IntegerEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Unit\Attribute\SingleTable;

enum IntegerEnum: int
{
case ONE = 1;
case TWO = 2;
}
31 changes: 31 additions & 0 deletions tests/Annotated/Unit/Attribute/SingleTable/SingleTableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Unit\Attribute\SingleTable;

use Cycle\Annotated\Annotation\Inheritance\SingleTable;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class SingleTableTest extends TestCase
{
public static function dataBase(): iterable
{
yield [null, null];
yield ['test', 'test'];
yield ['42', 42];
yield ['36.6', 36.6];
yield ['test', new StringableObject('test')];
yield ['1', IntegerEnum::ONE];
yield ['a', StringEnum::A];
}

#[DataProvider('dataBase')]
public function testBase1(?string $expected, mixed $value): void
{
$attribute = new SingleTable($value);

$this->assertSame($expected, $attribute->getValue());
}
}
11 changes: 11 additions & 0 deletions tests/Annotated/Unit/Attribute/SingleTable/StringEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Unit\Attribute\SingleTable;

enum StringEnum: string
{
case A = 'a';
case B = 'b';
}
20 changes: 20 additions & 0 deletions tests/Annotated/Unit/Attribute/SingleTable/StringableObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Unit\Attribute\SingleTable;

use Stringable;

final class StringableObject implements Stringable
{
public function __construct(
private string $value,
) {
}

public function __toString(): string
{
return $this->value;
}
}
2 changes: 1 addition & 1 deletion tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
ACCEPT_EULA: "Y"

mysql_latest:
image: mysql:latest
image: mysql:8.0
restart: always
command: --default-authentication-plugin=mysql_native_password
ports:
Expand Down

0 comments on commit d882fe4

Please sign in to comment.