Skip to content

Commit

Permalink
Merge 0cdda4a into 4edfc49
Browse files Browse the repository at this point in the history
  • Loading branch information
radimvaculik committed Jan 11, 2022
2 parents 4edfc49 + 0cdda4a commit 2ecd7e5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/default.md
Expand Up @@ -12,7 +12,7 @@ First, define your entities. Entity definitions are quite short and pleasant to
* @property string $name
* @property DateTimeImmutable|null $born {default now}
* @property string $email
* @property OneHasMany|Book[] $books {1:m Book::$author, orderBy=[id, DESC], cascade=[persist, remove]}
* @property OneHasMany|Book[] $books {1:m Book::$author, orderBy=[id=DESC], cascade=[persist, remove]}
*/
class Author extends Entity {}

Expand Down
10 changes: 10 additions & 0 deletions src/Entity/Reflection/MetadataParser.php
Expand Up @@ -584,6 +584,16 @@ protected function processRelationshipOrder(PropertyMetadata $property, array &$

} elseif (is_array($args['orderBy']) && isset($args['orderBy'][0])) {
$order = [$args['orderBy'][0] => $args['orderBy'][1] ?? ICollection::ASC];
trigger_error(
sprintf(
'`orderBy=[%s, %s]` syntax is depracated. Use `orderBy=[%s=%s]` instead.',
$args['orderBy'][0],
$args['orderBy'][1],
$args['orderBy'][0],
$args['orderBy'][1]
),
E_USER_DEPRECATED
);

} else {
$order = $args['orderBy'];
Expand Down
Expand Up @@ -30,7 +30,7 @@ class Foo extends Entity
* @property mixed $test1 {m:m Foo::$property}
* @property mixed $test2 {m:m Foo::$property, isMain=true}
* @property mixed $test3 {m:m Foo::$property, orderBy=entity->id}
* @property mixed $test4 {m:m Foo::$property, isMain=true, orderBy=[id, DESC]}
* @property mixed $test4 {m:m Foo::$property, isMain=true, orderBy=[id=DESC]}
* @property mixed $test5 {m:m Foo::$property, orderBy=id}
* @property mixed $test6 {m:m Foo::$property, isMain=true, orderBy=id}
* @property mixed $test7 {m:m Foo::$property, orderBy=[id=ASC, name=DESC]}
Expand Down
Expand Up @@ -30,23 +30,22 @@ class Bar extends Entity
* @property ?int $id {primary}
* @property mixed $test1 {1:m Bar::$property}
* @property mixed $test2 {1:m Bar::$property, orderBy=entity->id}
* @property mixed $test3 {1:m Bar::$property, orderBy=[id,DESC]}
* @property mixed $test4 {1:m Bar::$property, orderBy=[id=DESC, entity->id=ASC]}
* @property OneHasMany&object[] $test5 {1:m Bar::$property}
* @property mixed $test3 {1:m Bar::$property, orderBy=[id=DESC, entity->id=ASC]}
* @property OneHasMany&object[] $test4 {1:m Bar::$property}
*/
class OneHasManyTestEntity extends Entity
{
}


/**
* @extends Repository<ManyHasManyTestEntity>
* @extends Repository<OneHasManyTestEntity>
*/
class BarRepository extends Repository
{
public static function getEntityClassNames(): array
{
return [ManyHasManyTestEntity::class];
return [OneHasManyTestEntity::class];
}
}

Expand Down Expand Up @@ -85,18 +84,11 @@ class MetadataParserParseOneHasManyTest extends TestCase
Assert::notNull($propertyMeta->relationship);
Assert::same(BarRepository::class, $propertyMeta->relationship->repository);
Assert::same('property', $propertyMeta->relationship->property);
Assert::same(['id' => ICollection::DESC], $propertyMeta->relationship->order);
Assert::same(PropertyRelationshipMetadata::ONE_HAS_MANY, $propertyMeta->relationship->type);

$propertyMeta = $metadata->getProperty('test4');
Assert::notNull($propertyMeta->relationship);
Assert::same(BarRepository::class, $propertyMeta->relationship->repository);
Assert::same('property', $propertyMeta->relationship->property);
Assert::same(['id' => ICollection::DESC, 'entity->id' => ICollection::ASC], $propertyMeta->relationship->order);
Assert::same(PropertyRelationshipMetadata::ONE_HAS_MANY, $propertyMeta->relationship->type);
Assert::same(['mixed' => true], $propertyMeta->types);

$propertyMeta = $metadata->getProperty('test5');
$propertyMeta = $metadata->getProperty('test4');
Assert::same([OneHasMany::class => true, 'array' => true], $propertyMeta->types);
}
}
Expand Down
Expand Up @@ -7,9 +7,11 @@
namespace NextrasTests\Orm\Entity\Reflection;


use Nextras\Orm\Entity\Entity;
use Nextras\Orm\Entity\Reflection\InvalidModifierDefinitionException;
use Nextras\Orm\Entity\Reflection\MetadataParser;
use Nextras\Orm\Exception\InvalidStateException;
use Nextras\Orm\Repository\Repository;
use NextrasTests\Orm\TestCase;
use Tester\Assert;

Expand Down Expand Up @@ -58,7 +60,7 @@ class EdgeCasesMetadataParserEntity7


/**
* @property foo $var {1:m Entity::$bar}
* @property foo $var {1:m Foo::$bar}
*/
class EdgeCasesMetadataParserEntity8
{
Expand All @@ -74,13 +76,35 @@ class EdgeCasesMetadataParserEntity9


/**
* @property foo $var {1:m Entity:$bar}
* @property foo $var {1:m Foo:$bar}
*/
class EdgeCasesMetadataParserEntity10
{
}


/**
* @property int $id {primary}
* @property foo $var {1:m Bar::$bar, orderBy=[id, DESC]}
*/
class EdgeCasesMetadataParserEntity11
{
}
class Bar extends Entity
{
}

/**
* @extends Repository<Bar>
*/
class BarRepository extends Repository
{
public static function getEntityClassNames(): array
{
return [Bar::class];
}
}

class MetadataParserExceptionsTest extends TestCase
{
public function testOneHasMany(): void
Expand All @@ -107,7 +131,13 @@ class MetadataParserExceptionsTest extends TestCase
}, InvalidModifierDefinitionException::class, 'Relationship {1:m} in NextrasTests\Orm\Entity\Reflection\EdgeCasesMetadataParserEntity7::$var has not defined target property name.');
Assert::throws(function () use ($parser): void {
$parser->parseMetadata(EdgeCasesMetadataParserEntity8::class, $dep);
}, InvalidModifierDefinitionException::class, 'Relationship {1:m} in NextrasTests\Orm\Entity\Reflection\EdgeCasesMetadataParserEntity8::$var points to unknown \'NextrasTests\Orm\Entity\Reflection\Entity\' entity. Don\'t forget to return it in IRepository::getEntityClassNames() and register its repository.');
}, InvalidModifierDefinitionException::class, 'Relationship {1:m} in NextrasTests\Orm\Entity\Reflection\EdgeCasesMetadataParserEntity8::$var points to unknown \'NextrasTests\Orm\Entity\Reflection\Foo\' entity. Don\'t forget to return it in IRepository::getEntityClassNames() and register its repository.');
Assert::error(function (): void {
$parser = new MetadataParser([ //@phpstan-ignore-line
Bar::class => BarRepository::class,
]);
$parser->parseMetadata(EdgeCasesMetadataParserEntity11::class, $dep);
}, E_USER_DEPRECATED, '`orderBy=[id, DESC]` syntax is depracated. Use `orderBy=[id=DESC]` instead.');
}


Expand Down
2 changes: 1 addition & 1 deletion tests/inc/model/author/Author.php
Expand Up @@ -15,7 +15,7 @@
* @property string $web {default "http://www.example.com"}
* @property Author|null $favoriteAuthor {m:1 Author::$favoredBy}
* @property OHM|Author[] $favoredBy {1:m Author::$favoriteAuthor}
* @property OHM|Book[] $books {1:m Book::$author, orderBy=[id, DESC], cascade=[persist, remove]}
* @property OHM|Book[] $books {1:m Book::$author, orderBy=[id=DESC], cascade=[persist, remove]}
* @property OHM|Book[] $translatedBooks {1:m Book::$translator}
* @property OHM|TagFollower[] $tagFollowers {1:m TagFollower::$author, cascade=[persist, remove]}
* @property-read int $age {virtual}
Expand Down

0 comments on commit 2ecd7e5

Please sign in to comment.