forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
192 additions
and
551 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
when@test: | ||
framework: | ||
test: true | ||
framework: | ||
test: true | ||
api_platform: | ||
formats: | ||
jsonld: ['application/ld+json'] | ||
json: ['application/json'] | ||
defaults: | ||
extraProperties: | ||
standard_put: true | ||
formats: | ||
jsonld: ['application/ld+json'] | ||
json: ['application/json'] | ||
event_listeners_backward_compatibility_layer: false | ||
keep_legacy_inflector: false | ||
defaults: | ||
extra_properties: | ||
rfc_7807_compliant_errors: true | ||
standard_put: true |
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 |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
// position: 1 | ||
// executable: true | ||
// tags: design | ||
// homepage: true | ||
// --- | ||
|
||
// # Declare a Resource | ||
|
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,110 @@ | ||
<?php | ||
// --- | ||
// slug: doctrine-entity-as-resource | ||
// name: Doctrine entity as API resource | ||
// position: 4 | ||
// tags: doctrine | ||
// executable: true | ||
// --- | ||
|
||
// API Platform is compatible with [Doctrine ORM](https://www.doctrine-project.org), all we need is to declare an | ||
// API Resource on a Doctrine Entity. | ||
namespace App\Entity { | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\ApiFilter; | ||
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
// When an ApiResource is declared on an ORM\Entity we have access to [Doctrine filters](https://api-platform.com/docs/core/filters/). | ||
#[ApiResource] | ||
#[ApiFilter(OrderFilter::class)] | ||
#[ORM\Entity] | ||
class Book | ||
{ | ||
#[ORM\Id, ORM\Column, ORM\GeneratedValue] | ||
private ?int $id = null; | ||
|
||
#[ORM\Column] | ||
public ?string $title = null; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
} | ||
} | ||
|
||
namespace App\Playground { | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
function request(): Request | ||
{ | ||
// Persistence is automatic, you can try to create or read data: | ||
return Request::create('/books?order[id]=desc', 'GET'); | ||
return Request::create('/books/1', 'GET'); | ||
return Request::create(uri: '/books', method: 'POST', server: ['CONTENT_TYPE' => 'application/ld+json'], content: json_encode(['id' => 1, 'title' => 'API Platform rocks.'])); | ||
} | ||
} | ||
|
||
namespace DoctrineMigrations { | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
final class Migration extends AbstractMigration | ||
{ | ||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('CREATE TABLE book (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title VARCHAR(255) NOT NULL)'); | ||
} | ||
} | ||
} | ||
|
||
namespace App\Fixtures { | ||
|
||
use App\Entity\Book; | ||
use Doctrine\Bundle\FixturesBundle\Fixture; | ||
use Doctrine\Persistence\ObjectManager; | ||
use function Zenstruck\Foundry\anonymous; | ||
use function Zenstruck\Foundry\repository; | ||
|
||
final class BookFixtures extends Fixture | ||
{ | ||
public function load(ObjectManager $manager): void | ||
{ | ||
$bookFactory = anonymous(Book::class); | ||
if (repository(Book::class)->count()) { | ||
return; | ||
} | ||
|
||
$bookFactory->many(10)->create([ | ||
'title' => 'title' | ||
]); | ||
} | ||
} | ||
} | ||
|
||
namespace App\Tests { | ||
|
||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; | ||
use ApiPlatform\Playground\Test\TestGuideTrait; | ||
|
||
final class BookTest extends ApiTestCase | ||
{ | ||
use TestGuideTrait; | ||
|
||
public function testGet(): void | ||
{ | ||
static::createClient()->request('GET', '/books.jsonld'); | ||
$this->assertResponseIsSuccessful(); | ||
} | ||
|
||
public function testGetOne(): void | ||
{ | ||
static::createClient()->request('GET', '/books/1.jsonld'); | ||
$this->assertResponseIsSuccessful(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.