-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2325 from W0rma/route-attributes
Add support for native PHP8 Route attributes
- Loading branch information
Showing
23 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
Tests/Functional/Bundle/TestBundle/Controller/RouteAttributesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller; | ||
|
||
use FOS\RestBundle\Controller\AbstractFOSRestController; | ||
use FOS\RestBundle\Controller\Annotations as Rest; | ||
use FOS\RestBundle\View\View; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Controller to test native PHP8 Route attributes. | ||
*/ | ||
#[Rest\Route('/products')] | ||
class RouteAttributesController extends AbstractFOSRestController | ||
{ | ||
/** | ||
* @return View view instance | ||
* | ||
* @Rest\View() | ||
*/ | ||
#[Rest\Get(path: '/{page}', name: 'product_list', requirements: ['page' => '\d+'], defaults: ['_format' => 'json'])] | ||
public function listAction(int $page) | ||
{ | ||
$view = $this->view([ | ||
['name' => 'product1'], | ||
['name' => 'product2'], | ||
]); | ||
|
||
return $view; | ||
} | ||
|
||
/** | ||
* @return View view instance | ||
* | ||
* @Rest\View() | ||
*/ | ||
#[Rest\Post(path: '', name: 'product_create')] | ||
public function createAction(Request $request) | ||
{ | ||
$view = $this->view([ | ||
'name' => 'product1', | ||
], 201); | ||
|
||
return $view; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\RestBundle\Tests\Functional; | ||
|
||
/** | ||
* @requires PHP 8 | ||
*/ | ||
class RouteAttributesTest extends WebTestCase | ||
{ | ||
private const TEST_CASE = 'RouteAttributes'; | ||
private static $client; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
parent::setUpBeforeClass(); | ||
static::$client = static::createClient(['test_case' => self::TEST_CASE]); | ||
} | ||
|
||
public static function tearDownAfterClass(): void | ||
{ | ||
self::deleteTmpDir(self::TEST_CASE); | ||
parent::tearDownAfterClass(); | ||
} | ||
|
||
public function testGet() | ||
{ | ||
static::$client->request( | ||
'GET', | ||
'/products/1', | ||
[], | ||
[], | ||
['HTTP_ACCEPT' => 'application/json'] | ||
); | ||
|
||
$this->assertSame(200, static::$client->getResponse()->getStatusCode()); | ||
$this->assertJsonStringEqualsJsonString( | ||
'[{"name": "product1"},{"name": "product2"}]', | ||
static::$client->getResponse()->getContent() | ||
); | ||
} | ||
|
||
public function testPost() | ||
{ | ||
static::$client->request( | ||
'POST', | ||
'/products', | ||
[], | ||
[], | ||
['HTTP_ACCEPT' => 'application/json'] | ||
); | ||
|
||
$this->assertSame(201, static::$client->getResponse()->getStatusCode()); | ||
$this->assertJsonStringEqualsJsonString( | ||
'{"name": "product1"}', | ||
static::$client->getResponse()->getContent() | ||
); | ||
} | ||
|
||
public function testInvalidQueryParameter() | ||
{ | ||
static::$client->request( | ||
'GET', | ||
'/products/foo', | ||
[], | ||
[], | ||
['HTTP_ACCEPT' => 'application/json'] | ||
); | ||
|
||
$this->assertSame(404, static::$client->getResponse()->getStatusCode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
return [ | ||
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), | ||
new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), | ||
new \FOS\RestBundle\FOSRestBundle(), | ||
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(), | ||
new \Symfony\Bundle\TwigBundle\TwigBundle(), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
imports: | ||
- { resource: ../config/default.yml } | ||
- { resource: ../config/sensio_framework_extra.yml } | ||
|
||
framework: | ||
serializer: | ||
enabled: true | ||
|
||
fos_rest: | ||
view: | ||
view_response_listener: 'force' | ||
formats: | ||
xml: true | ||
json: true | ||
body_listener: true | ||
format_listener: | ||
rules: | ||
- { path: ^/, priorities: [ json, xml ], fallback_format: ~, prefer_extension: true } | ||
|
||
twig: | ||
exception_controller: ~ | ||
strict_variables: '%kernel.debug%' |