Skip to content

Commit

Permalink
Merge pull request #111 from RonasIT/feat/support-phpunit-11
Browse files Browse the repository at this point in the history
feat: implemented supporting of PHPUnit v11
  • Loading branch information
DenTray authored Dec 2, 2024
2 parents e270f60 + 704f473 commit 208a337
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 206 deletions.
133 changes: 24 additions & 109 deletions stubs/nova_test.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
@php($shouldUseStatus = version_compare(app()->version(), '7', '<'))
namespace App\Tests;

use App\Models\{{$entity}};
use PHPUnit\Framework\Attributes\DataProvider;
use RonasIT\Support\Tests\ModelTestState;
use RonasIT\Support\Traits\NovaTestTrait;
@if($shouldUseStatus)
use Symfony\Component\HttpFoundation\Response;
@endif

class Nova{{$entity}}Test extends TestCase
{
Expand All @@ -31,27 +28,19 @@ public function testCreate(): void

$response = $this->novaActingAs(self::$user)->novaCreateResourceAPICall({{$entity}}::class, $data);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_CREATED);
@else
$response->assertCreated();
@endif

$this->assertEqualsFixture('create_{{$snake_entity}}_response.json', $response->json());

// TODO: Need to remove after first successful start
// TODO: Need to remove last argument after first successful start
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('create_{{$lower_entities}}_state.json', true);
}

public function testCreateNoAuth(): void
{
$response = $this->novaCreateResourceAPICall({{$entity}}::class);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_UNAUTHORIZED);
@else
$response->assertUnauthorized();
@endif

self::${{$dromedary_entity}}State->assertNotChanged();
}
Expand All @@ -60,13 +49,9 @@ public function testCreateValidationError(): void
{
$response = $this->novaActingAs(self::$user)->novaCreateResourceAPICall({{$entity}}::class);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
@else
$response->assertUnprocessable();
@endif

// TODO: Need to remove after first successful start
// TODO: Need to remove last argument after first successful start
$this->assertEqualsFixture('create_validation_response.json', $response->json(), true);

self::${{$dromedary_entity}}State->assertNotChanged();
Expand All @@ -78,13 +63,9 @@ public function testUpdate(): void

$response = $this->novaActingAs(self::$user)->novaUpdateResourceAPICall({{$entity}}::class, 1, $data);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_NO_CONTENT);
@else
$response->assertNoContent();
@endif

// TODO: Need to remove after first successful start
// TODO: Need to remove last argument after first successful start
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('update_{{$lower_entities}}_state.json', true);
}

Expand All @@ -94,150 +75,102 @@ public function testUpdateNotExists(): void

$response = $this->novaActingAs(self::$user)->novaUpdateResourceAPICall({{$entity}}::class, 0, $data);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_NOT_FOUND);
@else
$response->assertNotFound();
@endif
}

public function testUpdateNoAuth(): void
{
$response = $this->novaUpdateResourceAPICall({{$entity}}::class, 1);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_UNAUTHORIZED);
@else
$response->assertUnauthorized();
@endif
}

public function testUpdateValidationError(): void
{
$response = $this->novaActingAs(self::$user)->novaUpdateResourceAPICall({{$entity}}::class, 4);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
@else
$response->assertUnprocessable();
@endif

// TODO: Need to remove after first successful start
// TODO: Need to remove last argument after first successful start
$this->assertEqualsFixture('update_validation_response.json', $response->json(), true);
}

public function testGetUpdatableFields(): void
{
$response = $this->novaActingAs(self::$user)->novaGetUpdatableFieldsAPICall({{$entity}}::class, 1);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
@else
$response->assertOk();
@endif

// TODO: Need to remove after first successful start
// TODO: Need to remove last argument after first successful start
$this->assertEqualsFixture('get_updatable_fields_response.json', $response->json(), true);
}

public function testDelete(): void
{
$response = $this->novaActingAs(self::$user)->novaDeleteResourceAPICall({{$entity}}::class, [1, 2]);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
@else
$response->assertOk();
@endif

// TODO: Need to remove after first successful start
// TODO: Need to remove last argument after first successful start
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('delete_{{$lower_entities}}_state.json', true);
}

public function testDeleteNotExists(): void
{
$response = $this->novaActingAs(self::$user)->novaDeleteResourceAPICall({{$entity}}::class, [0]);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_NOT_FOUND);
@else
$response->assertNotFound();
@endif
}

public function testDeleteNoAuth(): void
{
$response = $this->novaDeleteResourceAPICall({{$entity}}::class, [1, 2]);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_UNAUTHORIZED);
@else
$response->assertUnauthorized();
@endif
}

public function testGet(): void
{
$response = $this->novaActingAs(self::$user)->novaGetResourceAPICall({{$entity}}::class, 1);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
@else
$response->assertOk();
@endif

// TODO: Need to remove after first successful start
// TODO: Need to remove last argument after first successful start
$this->assertEqualsFixture('get_{{$snake_entity}}_response.json', $response->json(), true);
}

public function testGetNotExists(): void
{
$response = $this->novaActingAs(self::$user)->novaGetResourceAPICall({{$entity}}::class, 0);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_NOT_FOUND);
@else
$response->assertNotFound();
@endif
}

public function testGetNoAuth(): void
{
$response = $this->novaGetResourceAPICall({{$entity}}::class, 1);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_UNAUTHORIZED);
@else
$response->assertUnauthorized();
@endif
}

public function testSearchUnauthorized(): void
{
$response = $this->novaSearchResourceAPICall({{$entity}}::class);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_UNAUTHORIZED);
@else
$response->assertUnauthorized();
@endif
}

public function testGetFieldsVisibleOnCreate(): void
{
$response = $this->novaActingAs(self::$user)->novaGetCreationFieldsAPICall({{$entity}}::class);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
@else
$response->assertOk();
@endif

// TODO: Need to remove after first successful start
// TODO: Need to remove last argument after first successful start
$this->assertEqualsFixture('get_fields_visible_on_create_response.json', $response->json(), true);
}

public function getRun{{$entity}}ActionsData(): array
public static function getRun{{$entity}}ActionsData(): array
{
return [
@foreach($actions as $action)
Expand All @@ -252,82 +185,64 @@ public function getRun{{$entity}}ActionsData(): array
];
}

/**
* @dataProvider getRun{{$entity}}ActionsData
*/
#[DataProvider('getRun{{$entity}}ActionsData')]
public function testRun{{$entity}}Actions($action, $request, $state): void
{
$response = $this->novaActingAs(self::$user)->novaRunActionAPICall({{$entity}}::class, $action, $request);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
@else
$response->assertOk();
@endif

$this->assertEmpty($response->getContent());

// TODO: Need to remove after first successful start
// TODO: Need to remove last argument after first successful start
self::${{$dromedary_entity}}State->assertChangesEqualsFixture($state, true);
}

public function get{{$entity}}ActionsData(): array
public static function get{{$entity}}ActionsData(): array
{
return [
@foreach($actions as $action)
[
'resources' => [1, 2],
'response_fixture' => 'get_{{$snake_entity}}_actions_{{$action['fixture']}}.json',
'fixture' => 'get_{{$snake_entity}}_actions_{{$action['fixture']}}.json',
],
@endforeach
];
}

/**
* @dataProvider get{{$entity}}ActionsData
*/
public function testGet{{$entity}}Actions(array $resources, string $responseFixture): void
#[DataProvider('get{{$entity}}ActionsData')]
public function testGet{{$entity}}Actions(array $resources, string $fixture): void
{
$response = $this->novaActingAs(self::$user)->novaGetActionsAPICall({{$entity}}::class, $resources);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
@else
$response->assertOk();
@endif

// TODO: Need to remove after first successful start
$this->assertEqualsFixture($responseFixture, $response->json(), true);
// TODO: Need to remove last argument after first successful start
$this->assertEqualsFixture($fixture, $response->json(), true);
}

public function get{{$entity}}FiltersData(): array
public static function get{{$entity}}FiltersData(): array
{
return [
@foreach($filters as $filter)
[
'request' => [
'{{$filter['name']}}' => $this->novaSearchParams(['search term']),
],
'response_fixture' => 'filter_{{$snake_entity}}_by_{{$filter['fixture_name']}}.json',
'fixture' => 'filter_{{$snake_entity}}_by_{{$filter['fixture_name']}}.json',
],
@endforeach
];
}

/**
* @dataProvider get{{$entity}}FiltersData
*/
public function testFilter{{$entity}}(array $request, string $responseFixture): void
#[DataProvider('get{{$entity}}FiltersData')]
public function testFilter{{$entity}}(array $request, string $fixture): void
{
$response = $this->novaActingAs(self::$user)->novaSearchResourceAPICall({{$entity}}::class, $request);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
@else
$response->assertOk();
@endif

// TODO: Need to remove after first successful start
$this->assertEqualsFixture($responseFixture, $response->json(), true);
// TODO: Need to remove last argument after first successful start
$this->assertEqualsFixture($fixture, $response->json(), true);
}
}
Loading

0 comments on commit 208a337

Please sign in to comment.