Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(laravel): call factories, debug is now false by default #6702

Open
wants to merge 1 commit into
base: 4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/GraphQl/Type/TypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,14 @@ private function getResourceType(Type $type, bool $input, Operation $rootOperati
try {
$operation = $resourceMetadataCollection->getOperation($operationName);
} catch (OperationNotFoundException) {
$operation = $resourceMetadataCollection->getOperation($isCollection ? 'collection_query' : 'item_query');
try {
$operation = $resourceMetadataCollection->getOperation($isCollection ? 'collection_query' : 'item_query');
} catch (OperationNotFoundException) {
throw new OperationNotFoundException(\sprintf('A GraphQl operation named "%s" should exist on the type "%s" as we reference this type in another query.', $isCollection ? 'collection_query' : 'item_query', $resourceClass));
}
}
if (!$operation instanceof Operation) {
throw new OperationNotFoundException();
throw new OperationNotFoundException(\sprintf('A GraphQl operation named "%s" should exist on the type "%s" as we reference this type in another query.', $operationName, $resourceClass));
}

return $this->typeBuilder->getResourceObjectType($resourceMetadataCollection, $operation, $propertyMetadata, [
Expand Down
21 changes: 20 additions & 1 deletion src/Laravel/Tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,35 @@
namespace ApiPlatform\Laravel\Tests;

use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\Database\Factories\UserFactory;

class AuthTest extends TestCase
{
use ApiTestAssertionsTrait;
use RefreshDatabase;
use WithWorkbench;

/**
* @param Application $app
*/
protected function defineEnvironment($app): void
{
tap($app['config'], function (Repository $config): void {
$config->set('api-platform.graphql.enabled', true);
$config->set('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF');
});
}

protected function afterRefreshingDatabase(): void
{
UserFactory::new()->create();
}

public function testGetCollection(): void
{
$response = $this->get('/api/vaults', ['accept' => ['application/ld+json']]);
Expand All @@ -44,7 +63,7 @@ public function testAuthenticatedPolicy(): void
{
$response = $this->post('/tokens/create');
$token = $response->json()['token'];
$response = $this->post('/api/vaults', [], ['accept' => ['application/ld+json'], 'content-type' => ['application/ld+json'], 'authorization' => 'Bearer '.$token]);
$response = $this->postJson('/api/vaults', [], ['accept' => ['application/ld+json'], 'content-type' => ['application/ld+json'], 'authorization' => 'Bearer '.$token]);
$response->assertStatus(403);
}

Expand Down
34 changes: 33 additions & 1 deletion src/Laravel/Tests/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\Database\Factories\AuthorFactory;
use Workbench\Database\Factories\BookFactory;
use Workbench\Database\Factories\WithAccessorFactory;

class EloquentTest extends TestCase
{
Expand All @@ -26,6 +29,8 @@ class EloquentTest extends TestCase

public function testSearchFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$book = $response->json()['member'][0];

Expand All @@ -35,18 +40,24 @@ public function testSearchFilter(): void

public function testValidateSearchFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

$response = $this->get('/api/books?isbn=a', ['Accept' => ['application/ld+json']]);
$this->assertSame($response->json()['detail'], 'The isbn field must be at least 2 characters.');
}

public function testSearchFilterRelation(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

$response = $this->get('/api/books?author=1', ['Accept' => ['application/ld+json']]);
$this->assertSame($response->json()['member'][0]['author'], '/api/authors/1');
}

public function testPropertyFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$book = $response->json()['member'][0];

Expand All @@ -60,6 +71,8 @@ public function testPropertyFilter(): void

public function testPartialSearchFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$book = $response->json()['member'][0];

Expand All @@ -76,6 +89,8 @@ public function testPartialSearchFilter(): void

public function testDateFilterEqual(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$book = $response->json()['member'][0];
$updated = $this->patchJson(
Expand All @@ -93,6 +108,8 @@ public function testDateFilterEqual(): void

public function testDateFilterIncludeNull(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$book = $response->json()['member'][0];
$updated = $this->patchJson(
Expand All @@ -110,6 +127,8 @@ public function testDateFilterIncludeNull(): void

public function testDateFilterExcludeNull(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$book = $response->json()['member'][0];
$updated = $this->patchJson(
Expand All @@ -127,6 +146,8 @@ public function testDateFilterExcludeNull(): void

public function testDateFilterGreaterThan(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();

$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$bookBefore = $response->json()['member'][0];
$updated = $this->patchJson(
Expand Down Expand Up @@ -155,9 +176,10 @@ public function testDateFilterGreaterThan(): void

public function testDateFilterLowerThanEqual(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$bookBefore = $response->json()['member'][0];
$updated = $this->patchJson(
$this->patchJson(
$bookBefore['@id'],
['publicationDate' => '0001-02-18 00:00:00'],
[
Expand All @@ -184,6 +206,7 @@ public function testDateFilterLowerThanEqual(): void

public function testDateFilterBetween(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$book = $response->json()['member'][0];
$updated = $this->patchJson(
Expand Down Expand Up @@ -223,6 +246,7 @@ public function testDateFilterBetween(): void

public function testSearchFilterWithPropertyPlaceholder(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->get('/api/authors', ['Accept' => ['application/ld+json']])->json();
$author = $response['member'][0];

Expand All @@ -235,12 +259,14 @@ public function testSearchFilterWithPropertyPlaceholder(): void

public function testOrderFilterWithPropertyPlaceholder(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$res = $this->get('/api/authors?order[id]=desc', ['Accept' => ['application/ld+json']])->json();
$this->assertSame($res['member'][0]['id'], 10);
}

public function testOrFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']])->json()['member'];
$book = $response[0];
$book2 = $response[1];
Expand All @@ -251,6 +277,7 @@ public function testOrFilter(): void

public function testRangeLowerThanFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$bookBefore = $response->json()['member'][0];
$this->patchJson(
Expand Down Expand Up @@ -279,6 +306,7 @@ public function testRangeLowerThanFilter(): void

public function testRangeLowerThanEqualFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$bookBefore = $response->json()['member'][0];
$this->patchJson(
Expand Down Expand Up @@ -308,6 +336,7 @@ public function testRangeLowerThanEqualFilter(): void

public function testRangeGreaterThanFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$bookBefore = $response->json()['member'][0];
$updated = $this->patchJson(
Expand Down Expand Up @@ -336,6 +365,7 @@ public function testRangeGreaterThanFilter(): void

public function testRangeGreaterThanEqualFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
$bookBefore = $response->json()['member'][0];
$updated = $this->patchJson(
Expand Down Expand Up @@ -365,12 +395,14 @@ public function testRangeGreaterThanEqualFilter(): void

public function testWrongOrderFilter(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$res = $this->get('/api/authors?order[name]=something', ['Accept' => ['application/ld+json']]);
$this->assertEquals($res->getStatusCode(), 422);
}

public function testWithAccessor(): void
{
WithAccessorFactory::new()->create();
$res = $this->get('/api/with_accessors/1', ['Accept' => ['application/ld+json']]);
$this->assertArraySubset(['name' => 'test'], $res->json());
}
Expand Down
14 changes: 14 additions & 0 deletions src/Laravel/Tests/GraphQlAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,30 @@
use Orchestra\Testbench\Attributes\DefineEnvironment;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\Database\Factories\AuthorFactory;
use Workbench\Database\Factories\BookFactory;
use Workbench\Database\Factories\UserFactory;
use Workbench\Database\Factories\VaultFactory;

class GraphQlAuthTest extends TestCase
{
use ApiTestAssertionsTrait;
use RefreshDatabase;
use WithWorkbench;

protected function afterRefreshingDatabase(): void
{
UserFactory::new()->create();
}

/**
* @param Application $app
*/
protected function defineEnvironment($app): void
{
tap($app['config'], function (Repository $config): void {
$config->set('api-platform.routes.middleware', ['auth:sanctum']);
$config->set('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF');
$config->set('api-platform.graphql.enabled', true);
});
}
Expand All @@ -46,6 +56,7 @@ public function testUnauthenticated(): void

public function testAuthenticated(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->post('/tokens/create');
$token = $response->json()['token'];
$response = $this->get('/api/graphql', ['accept' => ['text/html'], 'authorization' => 'Bearer '.$token]);
Expand All @@ -64,6 +75,7 @@ public function testAuthenticated(): void

public function testPolicy(): void
{
VaultFactory::new()->count(10)->create();
$response = $this->post('/tokens/create');
$token = $response->json()['token'];
$response = $this->postJson('/api/graphql', ['query' => 'mutation {
Expand All @@ -86,13 +98,15 @@ protected function useProductionMode($app): void
tap($app['config'], function (Repository $config): void {
$config->set('api-platform.routes.middleware', ['auth:sanctum']);
$config->set('api-platform.graphql.enabled', true);
$config->set('app.key', 'AckfSECXIvnK5r28GVIWUAxmbBSjTsmF');
$config->set('app.debug', false);
});
}

#[DefineEnvironment('useProductionMode')]
public function testProductionError(): void
{
VaultFactory::new()->count(10)->create();
$response = $this->post('/tokens/create');
$token = $response->json()['token'];
$response = $this->postJson('/api/graphql', ['query' => 'mutation {
Expand Down
5 changes: 4 additions & 1 deletion src/Laravel/Tests/GraphQlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
namespace ApiPlatform\Laravel\Tests;

use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\Database\Factories\AuthorFactory;
use Workbench\Database\Factories\BookFactory;

class GraphQlTest extends TestCase
{
Expand All @@ -38,6 +40,7 @@ protected function defineEnvironment($app): void

public function testGetBooks(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->postJson('/api/graphql', ['query' => '{books { edges { node {id, name, publicationDate, author {id, name }}}}}'], ['accept' => ['application/json']]);
$response->assertStatus(200);
$data = $response->json();
Expand Down
6 changes: 6 additions & 0 deletions src/Laravel/Tests/HalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\App\Models\Book;
use Workbench\Database\Factories\AuthorFactory;
use Workbench\Database\Factories\BookFactory;

class HalTest extends TestCase
{
Expand All @@ -35,6 +37,7 @@ protected function defineEnvironment($app): void
tap($app['config'], function (Repository $config): void {
$config->set('api-platform.formats', ['jsonhal' => ['application/hal+json']]);
$config->set('api-platform.docs_formats', ['jsonhal' => ['application/hal+json']]);
$config->set('app.debug', true);
});
}

Expand All @@ -61,6 +64,7 @@ public function testGetEntrypoint(): void

public function testGetCollection(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$response = $this->get('/api/books', ['accept' => 'application/hal+json']);
$response->assertStatus(200);
$response->assertHeader('content-type', 'application/hal+json; charset=utf-8');
Expand All @@ -79,6 +83,7 @@ public function testGetCollection(): void

public function testGetBook(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$book = Book::first();
$iri = $this->getIriFromResource($book);
$response = $this->get($iri, ['accept' => ['application/hal+json']]);
Expand All @@ -103,6 +108,7 @@ public function testGetBook(): void

public function testDeleteBook(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
$book = Book::first();
$iri = $this->getIriFromResource($book);
$response = $this->delete($iri, headers: ['accept' => 'application/hal+json']);
Expand Down
Loading
Loading