Skip to content

Commit

Permalink
fix: make tests pass for current guides (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacquesDurand authored Feb 3, 2023
1 parent c4811bd commit 8095999
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/doc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ jobs:
- name: Global require pdg
run: |
cd $(composer -n config --global home)
echo "{\"repositories\":[{\"type\":\"vcs\",\"url\":\"https://github.com/JacquesDurand/php-documentation-generator\"}]}" > composer.json
echo "{\"repositories\":[{\"type\":\"vcs\",\"url\":\"https://github.com/php-documentation-generator/php-documentation-generator\"}]}" > composer.json
composer global config --no-plugins allow-plugins.symfony/runtime true
composer global require api-platform/pdg:dev-main
composer global require php-documentation-generator/php-documentation-generator:dev-main
- name: Cache dependencies
uses: actions/cache@v3
with:
Expand Down
8 changes: 3 additions & 5 deletions docs/guide/creating-a-custom-doctrine-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,15 @@ public function down(Schema $schema): void
namespace App\Tests {
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Book;
use PhpDocumentGenerator\Playground\TestGuideTrait;

final class BookTest extends ApiTestCase
{
protected function setUp(): void
{
static::createKernel()->executeMigrations();
}
use TestGuideTrait;

public function testAsAnonymousICanAccessTheDocumentation(): void
{
$resp = static::createClient()->request('GET', '/books.jsonld?regexp_title=^[Found]');
static::createClient()->request('GET', '/books.jsonld?regexp_title=^[Found]');

$this->assertResponseIsSuccessful();
$this->assertMatchesResourceCollectionJsonSchema(Book::class, '_api_/books{._format}_get_collection');
Expand Down
23 changes: 22 additions & 1 deletion docs/guide/doctrine-orm-and-mongodb-odm-attribute-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,39 @@ function request(): Request
}
}

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, author VARCHAR(255) NOT NULL)');
}

public function down(Schema $schema): void
{
$this->addSql('DROP TABLE book');
}
}
}

namespace App\Tests {
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Book;
use PhpDocumentGenerator\Playground\TestGuideTrait;

final class BookTest extends ApiTestCase
{
use TestGuideTrait;

public function testAsAnonymousICanAccessTheDocumentation(): void
{
static::createClient()->request('GET', '/books.jsonld');

$this->assertResponseIsSuccessful();
$this->assertMatchesResourceCollectionJsonSchema(Book::class, '_api_/books.{_format}_get_collection', 'jsonld');
$this->assertMatchesResourceCollectionJsonSchema(Book::class, '_api_/books{._format}_get_collection', 'jsonld');
$this->assertJsonContains([
'hydra:search' => [
'@type' => 'hydra:IriTemplate',
Expand Down
33 changes: 29 additions & 4 deletions docs/guide/doctrine-orm-and-mongodb-odm-service-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ class Book
}
}

namespace App\Configurator {
namespace App\DependencyInjection {
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

function configure(ContainerConfigurator $configurator) {
$configurator->services()
->set('book.search_filter')
->parent('api_platform.doctrine.orm.search_filter')
->args(['title' => null])
->tag('api_platform.filter');
->args([['title' => null]])
->tag('api_platform.filter')
->autowire(false)
->autoconfigure(false)
->public(false)
;
}
}

Expand All @@ -51,18 +55,39 @@ function request(): Request
}
}

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)');
}

public function down(Schema $schema): void
{
$this->addSql('DROP TABLE book');
}
}
}

namespace App\Tests {
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Book;
use PhpDocumentGenerator\Playground\TestGuideTrait;

final class BookTest extends ApiTestCase
{
use TestGuideTrait;

public function testAsAnonymousICanAccessTheDocumentation(): void
{
static::createClient()->request('GET', '/books.jsonld');

$this->assertResponseIsSuccessful();
$this->assertMatchesResourceCollectionJsonSchema(Book::class, '_api_/books.{_format}_get_collection', 'jsonld');
$this->assertMatchesResourceCollectionJsonSchema(Book::class, '_api_/books{._format}_get_collection', 'jsonld');
$this->assertJsonContains([
'hydra:search' => [
'@type' => 'hydra:IriTemplate',
Expand Down
7 changes: 2 additions & 5 deletions docs/guide/handle-a-pagination-on-a-custom-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,11 @@ public function load(ObjectManager $manager): void
namespace App\Tests {
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Book;
use PhpDocumentGenerator\Playground\TestGuideTrait;

final class BookTest extends ApiTestCase
{
protected function setUp(): void
{
static::createKernel()->executeMigrations();
static::createKernel()->loadFixtures();
}
use TestGuideTrait;

public function testTheCustomCollectionIsPaginated(): void
{
Expand Down
47 changes: 18 additions & 29 deletions docs/guide/using-doctrine-orm-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function getId(): ?int
* Each Book is related to a User, supposedly allowed to authenticate.
*/
#[ApiResource]
#[ApiFilter(UserFilter::class)]
#[ORM\Entity]
/*
* This entity is restricted by current user: only current user books will be shown (cf. UserFilter).
Expand Down Expand Up @@ -171,6 +170,7 @@ function configure(ContainerConfigurator $configurator) {
$services = $configurator->services();
$services->set(UserAwareEventSubscriber::class)
->args([service('doctrine.orm.default_entity_manager')])
->tag('kernel.event_subscriber')
;
$configurator->extension('doctrine', [
'orm' => [
Expand All @@ -186,24 +186,6 @@ function configure(ContainerConfigurator $configurator) {
}
}

namespace App\Configurator {
use App\Filter\UserFilter;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

function configure(ContainerConfigurator $configurator) {
$configurator->extension('doctrine', [
'orm' => [
'filters' => [
'user_filter' => [
'class' => UserFilter::class,
'enabled' => true,
],
],
],
]);
}
}

namespace App\Playground {
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -223,7 +205,7 @@ final class Migration extends AbstractMigration
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username VARCHAR(255) NOT NULL)');
$this->addSql('CREATE TABLE book (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title VARCHAR(255) NOT NULL, user_id INTEGER NOT NULL)');
$this->addSql('CREATE TABLE book (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title VARCHAR(255) NOT NULL, user_id INTEGER NOT NULL, FOREIGN KEY (user_id) REFERENCES user (id))');
}
}
}
Expand All @@ -239,31 +221,38 @@ final class BookFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$johnDoe = AnonymousFactory::new(User::class)::createOne(['username' => 'john.doe']);
$janeDoe = AnonymousFactory::new(User::class)::createOne(['username' => 'jane.doe']);
AnonymousFactory::new(Book::class)::createMany(10, ['user' => $johnDoe]);
AnonymousFactory::new(Book::class)::createMany(10, ['user' => $janeDoe]);
$userFactory = AnonymousFactory::new(User::class);
$johnDoe = $userFactory->create(['username' => 'john.doe']);
$janeDoe = $userFactory->create(['username' => 'jane.doe']);

$bookFactory = AnonymousFactory::new(Book::class);
$bookFactory->many(10)->create([
'title' => 'title',
'user' => $johnDoe
]);
$bookFactory->many(10)->create([
'title' => 'title',
'user' => $janeDoe
]);
}
}
}

namespace App\Tests {
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Book;
use PhpDocumentGenerator\Playground\TestGuideTrait;

final class BookTest extends ApiTestCase
{
protected function setUp(): void
{
static::createKernel()->executeMigrations();
}
use TestGuideTrait;

public function testAsAnonymousICanAccessTheDocumentation(): void
{
$response = static::createClient()->request('GET', '/books.jsonld');

$this->assertResponseIsSuccessful();
$this->assertMatchesResourceCollectionJsonSchema(Book::class, '_api_/books.{_format}_get_collection', 'jsonld');
$this->assertMatchesResourceCollectionJsonSchema(Book::class, '_api_/books{._format}_get_collection', 'jsonld');
$this->assertNotSame(0, $response->toArray(false)['hydra:totalItems'], 'The collection is empty.');
$this->assertJsonContains([
'hydra:totalItems' => 10,
Expand Down
6 changes: 2 additions & 4 deletions docs/guide/using-messenger-with-an-input-object.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,11 @@ function request(): Request
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Book;
use Symfony\Component\HttpFoundation\Response;
use PhpDocumentGenerator\Playground\TestGuideTrait;

final class BookTest extends ApiTestCase
{
protected function setUp(): void
{
static::createKernel()->executeMigrations();
}
use TestGuideTrait;

public function testAsAnonymousICanAccessTheDocumentation(): void
{
Expand Down

0 comments on commit 8095999

Please sign in to comment.