Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Aug 12, 2024
1 parent 1a8ee08 commit d8b2d77
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 134 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/audit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ jobs:
os:
- "ubuntu-latest"
php:
- '8.0'
- 8.1
- 8.2
- 8.3
experimental:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: composer show -D

- name: Execute tests
run: vendor/bin/phpunit --testdox
run: composer run test

tests-on-laravel-10:
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -97,4 +97,4 @@ jobs:
run: composer show -D

- name: Execute tests
run: vendor/bin/phpunit --testdox
run: composer run test
27 changes: 17 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"autoload-dev": {
"psr-4": {
"NovaKit\\NovaQueuedExportAsCsv\\Tests\\": "tests/",
"Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
Expand All @@ -34,17 +34,19 @@
"guzzlehttp/guzzle": "^7.2",
"larastan/larastan": "^2.4",
"laravel/nova": "dev-develop",
"laravel/pint": "^1.6",
"laravel/nova-devtool": "^1.0",
"laravel/pint": "^1.17",
"orchestra/testbench": "^8.20 || ^9.0",
"laravel/nova-devtool": "^1.0"
"phpstan/phpstan": "^1.11",
"orchestra/pest-plugin-testbench": "^2.0"
},
"conflict": {
"laravel/nova": "<5.0.0 || >=6.0.0"
},
"repositories": [
{
"type": "composer",
"url": "https://nova.laravel.com"
"type": "vcs",
"url": "https://github.com/laravel/nova"
},
{
"type": "vcs",
Expand All @@ -56,6 +58,11 @@
"dev-develop": "2.x-dev"
}
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"scripts": {
"post-autoload-dump": [
"@clear",
Expand All @@ -67,14 +74,14 @@
"serve": [
"Composer\\Config::disableProcessTimeout",
"@build",
"@php vendor/bin/testbench serve"
"@php vendor/bin/testbench serve --ansi"
],
"lint": [
"@php vendor/bin/pint",
"@php vendor/bin/phpstan analyse"
"@php vendor/bin/pint --ansi",
"@php vendor/bin/phpstan analyse --verbose"
],
"test": [
"@php vendor/bin/phpunit"
"@php vendor/bin/pest"
],
"ci": [
"@lint",
Expand All @@ -83,4 +90,4 @@
},
"prefer-stable": true,
"minimum-stability": "dev"
}
}
6 changes: 4 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ parameters:
# The level 8 is the highest level
level: 6

checkMissingIterableValueType: false
ignoreErrors:
- identifier: missingType.generics
- identifier: missingType.iterableValue

noUnnecessaryCollectionCall: false
checkModelProperties: false
reportUnmatchedIgnoredErrors: false
checkGenericClassInNonGenericObjectType: false
2 changes: 0 additions & 2 deletions src/Actions/QueuedExportAsCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public function withStorageDisk(?string $storageDisk)

/**
* Perform the action request using custom dispatch handler.
*
* @return \Laravel\Nova\Actions\Response
*/
#[\Override]
protected function dispatchRequestUsing(ActionRequest $request, Response $response, ActionFields $fields): Response
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/QueuedExportAsCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function handle(): void
'nova-actions-export-as-csv', new File($exportedFilename), $filename, 'public'
);

(new Filesystem())->delete($exportedFilename);
(new Filesystem)->delete($exportedFilename);

/** @var \Illuminate\Contracts\Auth\Authenticatable&\Illuminate\Database\Eloquent\Model $user */
$user = $userModel::findOrFail($this->userId);
Expand Down
87 changes: 38 additions & 49 deletions tests/Feature/QueuedExportAsCsvActionTest.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,44 @@
<?php

namespace NovaKit\NovaQueuedExportAsCsv\Tests\Feature;

use Illuminate\Support\Facades\Queue;
use NovaKit\NovaQueuedExportAsCsv\Actions\QueuedExportAsCsv as QueuedExportAsCsvAction;
use NovaKit\NovaQueuedExportAsCsv\Jobs\QueuedExportAsCsv as QueuedExportAsCsvJob;
use NovaKit\NovaQueuedExportAsCsv\Tests\TestCase;
use Orchestra\Testbench\Factories\UserFactory;
use PHPUnit\Framework\Attributes\Test;

class QueuedExportAsCsvActionTest extends TestCase
{
#[Test]
public function it_can_generate_queued_action()
{
Queue::fake();

$user = UserFactory::new()->create();

$response = $this->withoutMix()
->actingAs($user)
->post('/nova-api/users/action?action='.(new QueuedExportAsCsvAction)->uriKey(), [
'resources' => 'all',
]);

$response->assertOk()
->assertJson(['message' => 'The action was executed successfully.']);

Queue::assertPushed(function (QueuedExportAsCsvJob $job) use ($user) {
return $job->userId === $user->id;
});
}

#[Test]
public function it_can_generate_queued_action_with_custom_response()
{
Queue::fake();

$user = UserFactory::new()->create();

$response = $this->withoutMix()
->actingAs($user)
->post('/nova-api/subscribers/action?action='.(new QueuedExportAsCsvAction)->uriKey(), [
'resources' => 'all',
]);

$response->assertOk()
->assertJson(['message' => 'Action has been queued!']);

Queue::assertPushed(function (QueuedExportAsCsvJob $job) use ($user) {
return $job->userId === $user->id;
});
}
}

it('can generate queued action', function () {
Queue::fake();

$user = UserFactory::new()->create();

$response = $this->withoutMix()
->actingAs($user)
->post('/nova-api/users/action?action='.(new QueuedExportAsCsvAction)->uriKey(), [
'resources' => 'all',
]);

$response->assertOk()
->assertJson(['message' => 'The action was executed successfully.']);

Queue::assertPushed(function (QueuedExportAsCsvJob $job) use ($user) {
return $job->userId === $user->id;
});
});

it('can generate queued action with custom response', function () {
Queue::fake();

$user = UserFactory::new()->create();

$response = $this->withoutMix()
->actingAs($user)
->post('/nova-api/subscribers/action?action='.(new QueuedExportAsCsvAction)->uriKey(), [
'resources' => 'all',
]);

$response->assertOk()
->assertJson(['message' => 'Action has been queued!']);

Queue::assertPushed(function (QueuedExportAsCsvJob $job) use ($user) {
return $job->userId === $user->id;
});
});
53 changes: 22 additions & 31 deletions tests/Feature/QueuedExportAsCsvJobTest.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
<?php

namespace NovaKit\NovaQueuedExportAsCsv\Tests\Feature;

use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;
use NovaKit\NovaQueuedExportAsCsv\Events\QueuedCsvExported;
use NovaKit\NovaQueuedExportAsCsv\Jobs\QueuedExportAsCsv as QueuedExportAsCsvJob;
use NovaKit\NovaQueuedExportAsCsv\Tests\TestCase;
use Orchestra\Testbench\Factories\UserFactory;
use PHPUnit\Framework\Attributes\Test;

use function Laravie\SerializesQuery\serialize;

class QueuedExportAsCsvJobTest extends TestCase
{
#[Test]
public function it_can_generate_export_file_from_job()
{
Event::fake();

$user = UserFactory::new()->create();
UserFactory::new()->times(10)->create();

$query = serialize(User::query());
$options = [
'storageDisk' => 'local',
'filename' => 'users.csv',
];

dispatch(new QueuedExportAsCsvJob($query, $user->id, null, $options));

Event::assertDispatched(function (QueuedCsvExported $event) use ($user) {
return $event->user->id === $user->id
&& $event->filename === 'nova-actions-export-as-csv/users.csv'
&& $event->storageDisk === 'local';
});

$this->assertTrue(Storage::disk('local')->exists('nova-actions-export-as-csv/users.csv'));
}
}
it('can generate export file from job', function () {
Event::fake();

$user = UserFactory::new()->create();
UserFactory::new()->times(10)->create();

$query = serialize(User::query());
$options = [
'storageDisk' => 'local',
'filename' => 'users.csv',
];

dispatch(new QueuedExportAsCsvJob($query, $user->id, null, $options));

Event::assertDispatched(function (QueuedCsvExported $event) use ($user) {
return $event->user->id === $user->id
&& $event->filename === 'nova-actions-export-as-csv/users.csv'
&& $event->storageDisk === 'local';
});

$this->assertTrue(Storage::disk('local')->exists('nova-actions-export-as-csv/users.csv'));
});
45 changes: 45 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
28 changes: 6 additions & 22 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
<?php

namespace NovaKit\NovaQueuedExportAsCsv\Tests;
namespace Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Orchestra\Testbench\Attributes\WithMigration;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Laravel\Nova\Testing\Concerns\InteractsWithNova;
use Orchestra\Testbench\Concerns\WithWorkbench;

#[WithMigration]
class TestCase extends \Orchestra\Testbench\TestCase
abstract class TestCase extends \Orchestra\Testbench\TestCase
{
use RefreshDatabase;
use InteractsWithNova;
use LazilyRefreshDatabase;
use WithWorkbench;

/**
* Setup the test environment.
*/
#[\Override]
protected function setUp(): void
{
$this->afterApplicationCreated(function () {
Http::fake([
'nova.laravel.com/*' => Http::response([], 200),
]);
});

parent::setUp();
}
}
1 change: 0 additions & 1 deletion workbench/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
Expand Down
Loading

0 comments on commit d8b2d77

Please sign in to comment.