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: 5 additions & 0 deletions src/Annotation/ApiSubresource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*
* @Annotation
* @Target({"METHOD", "PROPERTY"})
* @Attributes(
* @Attribute("maxDepth", type="int"),
* )
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)]
final class ApiSubresource
Expand All @@ -36,6 +39,8 @@ public function __construct($maxDepth = null)
{
if (!\is_array($maxDepth)) { // @phpstan-ignore-line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we modify the PHPDoc?

Copy link
Member

@soyuka soyuka Jan 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really as we want to keep the auto documentation as we did in https://github.com/api-platform/core/blob/main/src/Annotation/ApiResource.php this happens only when using this class as a php 8 attribute

$this->maxDepth = $maxDepth;
} else {
$this->maxDepth = $maxDepth['maxDepth'] ?? null;
}
}
}
53 changes: 53 additions & 0 deletions tests/Annotation/ApiSubresourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Annotation;

use ApiPlatform\Core\Annotation\ApiSubresource;
use PHPUnit\Framework\TestCase;

/**
* @author Cody Banman <crbanman@gmail.com>
*/
class ApiSubresourceTest extends TestCase
{
public function testAssignation()
{
$property = new ApiSubresource();
$property->maxDepth = 1;

$this->assertEquals(1, $property->maxDepth);
}

public function testConstruct()
{
$property = new ApiSubresource([ // @phpstan-ignore-line
'maxDepth' => 1,
]);
$this->assertEquals(1, $property->maxDepth);
}

/**
* @requires PHP 8.0
*/
public function testConstructAttribute()
{
$property = eval(<<<'PHP'
return new \ApiPlatform\Core\Annotation\ApiSubresource(
maxDepth: 1
);
PHP
);
$this->assertEquals(1, $property->maxDepth);
}
}