Skip to content

Commit dc7bd97

Browse files
feat: add TNR
1 parent 88d4799 commit dc7bd97

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

features/main/operation.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ Feature: Operation support
8383
"id": 1
8484
}
8585
"""
86+
87+
Scenario: Call a non API Platform route
88+
When I send a "GET" request to "/common/custom/object"
89+
Then the response status code should be 200
90+
And the response should be in JSON
91+
And the JSON should be equal to:
92+
"""
93+
{
94+
"id": 1,
95+
"text": "Lorem ipsum dolor sit amet"
96+
}
97+
"""
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Controller\Common;
15+
16+
use ApiPlatform\Tests\Fixtures\TestBundle\Model\CustomObject;
17+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
18+
use Symfony\Component\HttpFoundation\JsonResponse;
19+
use Symfony\Component\Routing\Annotation\Route;
20+
21+
/**
22+
* Custom Controller.
23+
*
24+
* @author Kévin Dunglas <[email protected]>
25+
*/
26+
class CustomController extends AbstractController
27+
{
28+
public function customAction(int $id): JsonResponse
29+
{
30+
return new JsonResponse(sprintf('This is a custom action for %d.', $id), 200, ['Content-Type' => 'application/ld+json; charset=utf-8']);
31+
}
32+
33+
/**
34+
* Custom route for a non API Platform route.
35+
*
36+
* @Route(methods={"GET"}, name="custom_external_route", path="/common/custom/object")
37+
*/
38+
public function __invoke(): CustomObject
39+
{
40+
return new CustomObject(1, 'Lorem ipsum dolor sit amet');
41+
}
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\EventSubscriber;
15+
16+
use ApiPlatform\Tests\Fixtures\TestBundle\Model\CustomObject;
17+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18+
use Symfony\Component\HttpFoundation\JsonResponse;
19+
use Symfony\Component\HttpKernel\Event\ViewEvent;
20+
21+
final class CustomObjectSerializeEventSubscriber implements EventSubscriberInterface
22+
{
23+
public static function getSubscribedEvents(): array
24+
{
25+
return [
26+
ViewEvent::class => 'onKernelView',
27+
];
28+
}
29+
30+
public function onKernelView(ViewEvent $event): void
31+
{
32+
$result = $event->getControllerResult();
33+
if (!$result instanceof CustomObject) {
34+
return;
35+
}
36+
37+
$event->setResponse(new JsonResponse(['id' => $result->id, 'text' => $result->text]));
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Model;
15+
16+
class CustomObject
17+
{
18+
public $id;
19+
20+
public $text;
21+
22+
public function __construct($id, $text)
23+
{
24+
$this->id = $id;
25+
$this->text = $text;
26+
}
27+
}

tests/Fixtures/app/config/config_common.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ services:
9696
autowire: true
9797
autoconfigure: true
9898

99+
ApiPlatform\Tests\Fixtures\TestBundle\EventSubscriber\:
100+
resource: '../../TestBundle/EventSubscriber'
101+
autowire: true
102+
autoconfigure: true
103+
99104
ApiPlatform\Tests\Fixtures\TestBundle\State\AttributeResourceProvider:
100105
class: 'ApiPlatform\Tests\Fixtures\TestBundle\State\AttributeResourceProvider'
101106
public: false

tests/Fixtures/app/config/routing_common.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
_main:
22
resource: routing.yml
33

4+
controller:
5+
resource: '@TestBundle/Controller/Common'
6+
type: annotation
7+
48
relation_embedded.custom_get:
59
path: '/relation_embedders/{id}/custom'
610
methods: ['GET', 'HEAD']

0 commit comments

Comments
 (0)