Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to specify a url generation strategy #3198

Merged
merged 2 commits into from Jun 24, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 77 additions & 1 deletion features/bootstrap/DoctrineContext.php
Expand Up @@ -11,6 +11,8 @@

declare(strict_types=1);

use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\AbsoluteUrlDummy as AbsoluteUrlDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\AbsoluteUrlRelationDummy as AbsoluteUrlRelationDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Address as AddressDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Answer as AnswerDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeItem as CompositeItemDocument;
Expand Down Expand Up @@ -52,6 +54,8 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\FourthLevel as FourthLevelDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Greeting as GreetingDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\MaxDepthDummy as MaxDepthDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\NetworkPathDummy as NetworkPathDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\NetworkPathRelationDummy as NetworkPathRelationDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Order as OrderDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Person as PersonDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\PersonToPet as PersonToPetDocument;
Expand All @@ -69,6 +73,8 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ThirdLevel as ThirdLevelDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\UrlEncodedId as UrlEncodedIdDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\AbsoluteUrlDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\AbsoluteUrlRelationDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Address;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
Expand Down Expand Up @@ -113,6 +119,9 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Greeting;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\InternalUser;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\NetworkPathDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\NetworkPathRelationDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Node;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Order;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Person;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\PersonToPet;
Expand Down Expand Up @@ -1533,7 +1542,7 @@ public function thereAreDummyMercureObjects(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$relatedDummy = $this->buildRelatedDummy();
$relatedDummy->setName('RelatedDummy #'.$i);
$relatedDummy->setName('RelatedDummy #' . $i);

$dummyMercure = $this->buildDummyMercure();
$dummyMercure->name = "Dummy Mercure #$i";
Expand All @@ -1547,6 +1556,40 @@ public function thereAreDummyMercureObjects(int $nb)
$this->manager->flush();
}

/**
* @Given there are :nb absoluteUrlDummy objects with a related absoluteUrlRelationDummy
*/
public function thereAreAbsoluteUrlDummies(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$absoluteUrlRelationDummy = $this->buildAbsoluteUrlRelationDummy();
$absoluteUrlDummy = $this->buildAbsoluteUrlDummy();
$absoluteUrlDummy->absoluteUrlRelationDummy = $absoluteUrlRelationDummy;

$this->manager->persist($absoluteUrlRelationDummy);
$this->manager->persist($absoluteUrlDummy);
}

$this->manager->flush();
}

/**
* @Given there are :nb networkPathDummy objects with a related networkPathRelationDummy
*/
public function thereAreNetworkPathDummies(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$networkPathRelationDummy = $this->buildNetworkPathRelationDummy();
$networkPathDummy = $this->buildNetworkPathDummy();
$networkPathDummy->networkPathRelationDummy = $networkPathRelationDummy;

$this->manager->persist($networkPathRelationDummy);
$this->manager->persist($networkPathDummy);
}

$this->manager->flush();
}

private function isOrm(): bool
{
return null !== $this->schemaTool;
Expand Down Expand Up @@ -1931,5 +1974,38 @@ private function buildConvertedRelated()
private function buildDummyMercure()
{
return $this->isOrm() ? new DummyMercure() : new DummyMercureDocument();

}

/**
* @return AbsoluteUrlDummyDocument|AbsoluteUrlDummy
*/
private function buildAbsoluteUrlDummy()
{
return $this->isOrm() ? new AbsoluteUrlDummy() : new AbsoluteUrlDummyDocument();
}

/**
* @return AbsoluteUrlRelationDummyDocument|AbsoluteUrlRelationDummy
*/
private function buildAbsoluteUrlRelationDummy()
{
return $this->isOrm() ? new AbsoluteUrlRelationDummy() : new AbsoluteUrlRelationDummyDocument();
}

/**
* @return NetworkPathDummyDocument|NetworkPathDummy
*/
private function buildNetworkPathDummy()
{
return $this->isOrm() ? new NetworkPathDummy() : new NetworkPathDummyDocument();
}

/**
* @return NetworkPathRelationDummyDocument|NetworkPathRelationDummy
*/
private function buildNetworkPathRelationDummy()
{
return $this->isOrm() ? new NetworkPathRelationDummy() : new NetworkPathRelationDummyDocument();
}
}
1 change: 0 additions & 1 deletion features/doctrine/search_filter.feature
Expand Up @@ -805,7 +805,6 @@ Feature: Search filter on collections
When I send a "GET" request to "/converted_owners?name_converted.name_converted=Converted 3"
Then the response status code should be 200
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
Then print last JSON response
And the JSON should be valid according to this schema:
"""
{
Expand Down
119 changes: 119 additions & 0 deletions features/hal/absolute_url.feature
@@ -0,0 +1,119 @@
Feature: IRI should contain Absolute URL
In order to add detail to IRIs
Include the absolute url

@createSchema
Scenario: I should be able to GET a collection of Objects with Absolute Urls
Given there are 1 absoluteUrlDummy objects with a related absoluteUrlRelationDummy
And I add "Accept" header equal to "application/hal+json"
And I send a "GET" request to "/absolute_url_dummies"
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "http://example.com/absolute_url_dummies"
},
"item": [
{
"href": "http://example.com/absolute_url_dummies/1"
}
]
},
"totalItems": 1,
"itemsPerPage": 3,
"_embedded": {
"item": [
{
"_links": {
"self": {
"href": "http://example.com/absolute_url_dummies/1"
},
"absoluteUrlRelationDummy": {
"href": "http://example.com/absolute_url_relation_dummies/1"
}
},
"id": 1
}
]
}
}
"""

Scenario: I should be able to POST an object using an Absolute Url
Given I add "Accept" header equal to "application/hal+json"
And I add "Content-Type" header equal to "application/json"
And I send a "POST" request to "/absolute_url_relation_dummies" with body:
"""
{
"absolute_url_dummies": "http://example.com/absolute_url_dummies/1"
}
"""
Then the response status code should be 201
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "http://example.com/absolute_url_relation_dummies/2"
}
},
"id": 2
}
"""

Scenario: I should be able to GET an Item with Absolute Urls
Given I add "Accept" header equal to "application/hal+json"
And I add "Content-Type" header equal to "application/json"
And I send a "GET" request to "/absolute_url_dummies/1"
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "http://example.com/absolute_url_dummies/1"
},
"absoluteUrlRelationDummy": {
"href": "http://example.com/absolute_url_relation_dummies/1"
}
},
"id": 1
}
"""

Scenario: I should be able to GET subresources with Absolute Urls
Given I add "Accept" header equal to "application/hal+json"
And I add "Content-Type" header equal to "application/json"
And I send a "GET" request to "/absolute_url_relation_dummies/1/absolute_url_dummies"
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "http://example.com/absolute_url_relation_dummies/1/absolute_url_dummies"
},
"item": [
{
"href": "http://example.com/absolute_url_dummies/1"
}
]
},
"totalItems": 1,
"itemsPerPage": 3,
"_embedded": {
"item": [
{
"_links": {
"self": {
"href": "http://example.com/absolute_url_dummies/1"
},
"absoluteUrlRelationDummy": {
"href": "http://example.com/absolute_url_relation_dummies/1"
}
},
"id": 1
}
]
}
}
"""
117 changes: 117 additions & 0 deletions features/hal/network_path.feature
@@ -0,0 +1,117 @@
Feature: IRI should contain network path
In order to add detail to IRIs
Include the network path

@createSchema
Scenario: I should be able to GET a collection of objects with network paths
Given there are 1 networkPathDummy objects with a related networkPathRelationDummy
And I add "Accept" header equal to "application/hal+json"
And I send a "GET" request to "/network_path_dummies"
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "//example.com/network_path_dummies"
},
"item": [
{
"href": "//example.com/network_path_dummies/1"
}
]
},
"totalItems": 1,
"itemsPerPage": 3,
"_embedded": {
"item": [
{
"_links": {
"self": {
"href": "//example.com/network_path_dummies/1"
},
"networkPathRelationDummy": {
"href": "//example.com/network_path_relation_dummies/1"
}
},
"id": 1
}
]
}
}
"""

Scenario: I should be able to POST an object using a network path
Given I add "Accept" header equal to "application/hal+json"
And I add "Content-Type" header equal to "application/json"
And I send a "POST" request to "/network_path_relation_dummies" with body:
"""
{
"network_path_dummies": "//example.com/network_path_dummies/1"
}
"""
Then the response status code should be 201
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "//example.com/network_path_relation_dummies/2"
}
},
"id": 2
}
"""

Scenario: I should be able to GET an Item with network paths
Given I add "Accept" header equal to "application/hal+json"
And I send a "GET" request to "/network_path_dummies/1"
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "//example.com/network_path_dummies/1"
},
"networkPathRelationDummy": {
"href": "//example.com/network_path_relation_dummies/1"
}
},
"id": 1
}
"""

Scenario: I should be able to GET subresources with network paths
Given I add "Accept" header equal to "application/hal+json"
And I send a "GET" request to "/network_path_relation_dummies/1/network_path_dummies"
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "//example.com/network_path_relation_dummies/1/network_path_dummies"
},
"item": [
{
"href": "//example.com/network_path_dummies/1"
}
]
},
"totalItems": 1,
"itemsPerPage": 3,
"_embedded": {
"item": [
{
"_links": {
"self": {
"href": "//example.com/network_path_dummies/1"
},
"networkPathRelationDummy": {
"href": "//example.com/network_path_relation_dummies/1"
}
},
"id": 1
}
]
}
}
"""