Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions tests/Feature/Common/Schema/RegistryModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Cycle\ORM\Schema\GeneratedField;
use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Definition\Entity;
use Cycle\Schema\Definition\Field;
use Cycle\Schema\Registry;
use Cycle\Testing\Database\DatabaseTestCase;
use Ramsey\Uuid\Uuid;
Expand Down Expand Up @@ -273,6 +274,26 @@ public function testExistingFieldOfSameTypeIsReused(string $method): void
Assert::same($column->getName(), 'value_column');
}

#[Test]
#[DataSet(['addIntegerColumn'], 'integer')]
#[DataSet(['addBigIntegerColumn'], 'big integer')]
#[DataSet(['addDatetimeColumn'], 'datetime')]
#[DataSet(['addStringColumn'], 'string')]
#[DataSet(['addSnowflakeColumn'], 'snowflake')]
#[DataSet(['addUlidColumn'], 'ulid')]
#[DataSet(['addUuidColumn'], 'uuid')]
public function testExistingFieldOfSameTypeOnOtherColumnIsRejected(string $method): void
{
$this->modifier->{$method}('value_column', 'value');

Expect::exception(BehaviorCompilationException::class)->withMessage(
'Ambiguous column name definition. '
. 'The `value` field already linked with the `value_column` column but the behavior expects `other_column`.',
);

$this->modifier->{$method}('other_column', 'value');
}

#[Test]
#[DataSet(['addIntegerColumn', 'Field value must be of type integer.'], 'integer')]
#[DataSet(['addStringColumn', 'Field value must be of type string.'], 'string')]
Expand Down Expand Up @@ -327,6 +348,28 @@ public function testCustomTypecastNotOverridden(): void
Assert::same($field->getTypecast(), ['foo', 'bar']);
}

#[Test]
#[DataSet(['datetime', 'datetime', 'datetime'], 'datetime field over a datetime column')]
#[DataSet(['datetime', 'string', 'datetime'], 'datetime field over a string column')]
#[DataSet(['int', 'integer', 'int'], 'integer column')]
#[DataSet(['uuid', 'uuid', 'uuid'], 'uuid field over a uuid column')]
#[DataSet(['uuid', 'string', 'uuid'], 'uuid field over a string column')]
#[DataSet(['string', 'string', 'string'], 'any other type')]
public function testDeprecatedIsTypeMatches(string $type, string $columnType, string $fieldType): void
{
Assert::true($this->checkType($type, $columnType, $fieldType));
}

#[Test]
#[DataSet(['datetime', 'string', 'string'], 'datetime')]
#[DataSet(['int', 'string', 'int'], 'integer')]
#[DataSet(['uuid', 'string', 'string'], 'uuid')]
#[DataSet(['string', 'integer', 'string'], 'any other type')]
public function testDeprecatedIsTypeMismatches(string $type, string $columnType, string $fieldType): void
{
Assert::false($this->checkType($type, $columnType, $fieldType));
}

#[BeforeTest]
protected function setUp(): void
{
Expand All @@ -338,4 +381,23 @@ protected function setUp(): void

$this->modifier = new RegistryModifier($this->registry, self::ROLE_TEST);
}

/**
* Calls the deprecated protected `isType()` the way a subclass would, on a field linked to a column of its name.
*/
private function checkType(string $type, string $columnType, string $fieldType): bool
{
$entity = $this->registry->getEntity(self::ROLE_TEST);
$this->registry->getTableSchema($entity)->column('value')->type($columnType);
$entity->getFields()->set('value', (new Field())->setColumn('value')->setType($fieldType));

$modifier = new class($this->registry, self::ROLE_TEST) extends RegistryModifier {
public function check(string $type, string $fieldName, string $columnName): bool
{
return $this->isType($type, $fieldName, $columnName);
}
};

return $modifier->check($type, 'value', 'value');
}
}
Loading