Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerWarnings="true"
>
<coverage>
<report>
<clover outputFile="tests/_reports/logs/clover.xml"/>
Expand Down
24 changes: 19 additions & 5 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\Symfony\Set\SensiolabsSetList;
use Rector\Symfony\Set\SymfonySetList;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

return RectorConfig::configure()
->withPaths([
return static function (RectorConfig $rectorConfig): void {
// Paths for Rector to act upon
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPhpSets(true)
->withRules([
]);

$rectorConfig->sets([
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
SensiolabsSetList::ANNOTATIONS_TO_ATTRIBUTES,
]);
$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [
new AnnotationToAttribute(UniqueEntity::class),
]);
};
19 changes: 19 additions & 0 deletions src/Model/Resource/Attribute/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Undabot\SymfonyJsonApi\Model\Resource\Attribute;

use Attribute as PhpAttribute;

#[PhpAttribute(PhpAttribute::TARGET_PROPERTY | PhpAttribute::IS_REPEATABLE)]
class Attribute
{
public function __construct(
public string $name,
public ?string $description = null,
public ?string $example = null,
public ?string $format = null,
public bool $nullable = false
) {}
}
17 changes: 17 additions & 0 deletions src/Model/Resource/Attribute/Relationship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Undabot\SymfonyJsonApi\Model\Resource\Attribute;

abstract class Relationship
{
public function __construct(
public string $name,
public ?string $type = null,
public ?string $description = null,
public bool $nullable = false
) {}

abstract public function isToMany(): bool;
}
16 changes: 16 additions & 0 deletions src/Model/Resource/Attribute/ToMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Undabot\SymfonyJsonApi\Model\Resource\Attribute;

use Attribute as PhpAttribute;

#[PhpAttribute(PhpAttribute::TARGET_PROPERTY | PhpAttribute::IS_REPEATABLE)]
class ToMany extends Relationship
{
public function isToMany(): bool
{
return true;
}
}
16 changes: 16 additions & 0 deletions src/Model/Resource/Attribute/ToOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Undabot\SymfonyJsonApi\Model\Resource\Attribute;

use Attribute as PhpAttribute;

#[PhpAttribute(PhpAttribute::TARGET_PROPERTY | PhpAttribute::IS_REPEATABLE)]
class ToOne extends Relationship
{
public function isToMany(): bool
{
return false;
}
}
100 changes: 26 additions & 74 deletions tests/Bridge/OpenAPI/Model/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,44 @@

use Symfony\Component\Validator\Constraints as Assert;
use Undabot\SymfonyJsonApi\Model\ApiModel;
use Undabot\SymfonyJsonApi\Model\Resource\Annotation\Attribute;
use Undabot\SymfonyJsonApi\Model\Resource\Annotation\ToMany;
use Undabot\SymfonyJsonApi\Model\Resource\Annotation\ToOne;
use Undabot\SymfonyJsonApi\Model\Resource\Attribute\Attribute;
use Undabot\SymfonyJsonApi\Model\Resource\Attribute\ToMany;
use Undabot\SymfonyJsonApi\Model\Resource\Attribute\ToOne;
use Undabot\SymfonyJsonApi\Service\Resource\Validation\Constraint\ResourceType;

/** @ResourceType(type="article") */
#[ResourceType('article')]
class Article implements ApiModel
{
/**
* @var string
*/
private $id;
private string $id;

/**
* @var string
*
* @Attribute(nullable=false)
*/
private $slug;
#[Attribute('slug')]
private string $slug;

/**
* @var string
*
* @Attribute
*/
private $title;
#[Attribute('titlw')]
private string $title;

/**
* @var string
*
* @Attribute(name="eventAddress")
*/
private $address;
#[Attribute('eventAddress')]
private string $address;

/**
* @var string
*
* @Attribute(name="eventDate")
*/
private $date;
#[Attribute('eventDate')]
private string $date;

/**
* @var bool
*
* @Attribute
*/
private $enabled;
#[Attribute('enabled')]
private bool $enabled;

/**
* @var null|string
*
* @Attribute
*/
private $description;
#[Attribute('description', null, null, null, true)]
private ?string $description;

/**
* @var string
*
* @ToOne(name="category", type="category", nullable=true)
*
* @Assert\Type(type="string")
*/
private $categoryId;
#[ToOne('category', 'category', null, true)]
private string $categoryId;

/**
* @var string[]
*
* @ToMany(name="tags", type="tag", nullable=false)
*
* @Assert\Type(type="array")
*/
private $tagIds;
#[ToMany('tags', 'tag')]
#[Assert\Type('array')]
private array $tagIds;

/**
* @var string
*
* @Attribute(format="datetime", example="2001")
*
* @Assert\NotBlank
*/
private $createdAt;
#[Attribute('createdAt', null, null, 'datetime')]
private string $createdAt;

/**
* @var null|string
*
* @Attribute
*/
private $updatedAt;
#[Attribute('updatedAt')]
private ?string $updatedAt;
}
5 changes: 0 additions & 5 deletions tests/Unit/Http/Service/Factory/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,6 @@ public static function invalidRequestPrimaryDataProvider(): iterable
'Request data must be valid JSON',
];

yield 'Null data given' => [
null,
'Request data must be valid JSON',
];

yield 'Not array given' => [
'2',
'Request data must be parsable to a valid array',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static function provideFromArrayWillThrowExceptionGivenInvalidArgumentsPr

yield 'Resource array not valid type' => [
[$objectCollection, ResourceInterface::class],
null,
[],
null,
null,
'Class "' . \get_class($objectCollection) . '" was expected to be instanceof of "Undabot\JsonApi\Definition\Model\Resource\ResourceInterface" but is not.',
Expand Down