PHP code example of eerison / pest-plugin-api-platform
1. Go to this page and download the library: Download eerison/pest-plugin-api-platform library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
eerison / pest-plugin-api-platform example snippets
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
uses(ApiTestCase::class)->beforeEach(fn() => static::bootKernel())->in('Feature');
it('is checking the body structure')
->group('response')
->get('/foo/response/200')
->expectResponseContent()
->json()
->toHaveKey('name', 'Erison')
->toHaveKey('company.name', 'Fake company');
use function Eerison\PestPluginApiPlatform\get;
it('is checking the body structure using context.', function () {
$responseContent = get('/foo/response/200')->getContent();
expect($responseContent)
->json()
->toHaveKey('company.address')
;
});
use function Eerison\PestPluginApiPlatform\{get, findIriBy};
it('can use findIriBy', function () {
$iri = findIriBy(YourEntity::class, ['yourField' => 'yourValue']);
$responseContent = get($iri)->getContent();
expect($responseContent)
->json()
->toHaveKey('company.address')
;
});
it('can be used with snapshot')
->group('response')
->get('/foo/response/200')
->expectResponseContent()
->json()
->toMatchJsonSnapshot();
// api/tests/BooksTest.php
namespace App\Tests;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Book;
class BooksTest extends ApiTestCase
{
// This trait provided by AliceBundle will take care of refreshing the database content to a known state before each test
use RefreshDatabaseTrait;
public function testGetCollection(): void
{
// The client implements Symfony HttpClient's `HttpClientInterface`, and the response `ResponseInterface`
$response = static::createClient()->request('GET', '/books');
$this->assertResponseIsSuccessful();
// Asserts that the returned JSON is a superset of this one
$this->assertJsonContains([
'@context' => '/contexts/Book',
'@id' => '/books',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 100,
'hydra:view' => [
'@id' => '/books?page=1',
'@type' => 'hydra:PartialCollectionView',
'hydra:first' => '/books?page=1',
'hydra:last' => '/books?page=4',
'hydra:next' => '/books?page=2',
],
]);
// Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
// This generated JSON Schema is also used in the OpenAPI spec!
$this->assertMatchesResourceCollectionJsonSchema(Book::class);
}
use App\Entity\Book;
use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait;
uses(RefreshDatabaseTrait::class);
it('can get a collection')
->get('/books')
->assertResponseIsSuccessful()
->expectResponseContent()
->json()
->toHaveKey('@context', '/contexts/Book')
->toHaveKey('@id', '/books')
->toHaveKey('@type', 'hydra:Collection')
->toHaveKey('hydra:totalItems', 100)
->toHaveKey('hydra:view.@id', '/books?page=1')
->toHaveKey('hydra:view.@type', 'hydra:PartialCollectionView')
->toHaveKey('hydra:first', '/books?page=1')
->toHaveKey('hydra:last', '/books?page=4')
->toHaveKey('hydra:next', '/books?page=2')
->toMatchesResourceCollectionJsonSchema(Book::class)
;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.