From 624d2da178f9962339d93a315f2165ba72cc9c4c Mon Sep 17 00:00:00 2001 From: guanguans Date: Thu, 22 Aug 2024 11:32:15 +0800 Subject: [PATCH] test: Refactor tests and skip unimplemented tests - Skip tests that are not implemented yet in `TestCommandTest.php` and `Pest.php`. - Change test cases to return success and error JSON responses instead of reporting exceptions in `FeatureTest.php`. - Add new `ArchTest.php` file for architecture checks. - Update `ApiResponseServiceProviderTest.php` to skip the test for provides. --- composer.json | 1 + src/ApiResponse.php | 2 +- tests/ArchTest.php | 32 ++++++++++++++++++++++++++++++ tests/Commands/TestCommandTest.php | 4 ++++ tests/FeatureTest.php | 19 ++++++------------ tests/Pest.php | 2 +- tests/TestCase.php | 15 ++++---------- 7 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 tests/ArchTest.php diff --git a/composer.json b/composer.json index d91045c..67ce54d 100644 --- a/composer.json +++ b/composer.json @@ -54,6 +54,7 @@ "phpstan/phpstan": "^1.11", "phpstan/phpstan-deprecation-rules": "^1.2", "rector/rector": "^1.2", + "spatie/pest-plugin-snapshots": "^1.1 || ^2.0", "spaze/phpstan-disallowed-calls": "^3.4" }, "minimum-stability": "stable", diff --git a/src/ApiResponse.php b/src/ApiResponse.php index 869707f..66444c4 100644 --- a/src/ApiResponse.php +++ b/src/ApiResponse.php @@ -40,9 +40,9 @@ */ class ApiResponse implements ApiResponseContract { + // use Conditionable; // use Dumpable; use ConcreteHttpStatusMethods; - use Conditionable; use HasExceptionMap; use HasPipes; use Macroable; diff --git a/tests/ArchTest.php b/tests/ArchTest.php new file mode 100644 index 0000000..6645b31 --- /dev/null +++ b/tests/ArchTest.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + * + * @see https://github.com/guanguans/laravel-api-response + */ +// arch('will not use debugging functions') +// ->expect([ +// 'dd', +// 'die', +// 'dump', +// 'echo', +// 'env', +// 'env_explode', +// 'env_getcsv', +// 'exit', +// 'print', +// 'print_r', +// 'printf', +// 'ray', +// 'trap', +// 'var_dump', +// 'var_export', +// 'vprintf', +// ]) +// ->each->not->toBeUsed(); diff --git a/tests/Commands/TestCommandTest.php b/tests/Commands/TestCommandTest.php index 569a378..f254633 100644 --- a/tests/Commands/TestCommandTest.php +++ b/tests/Commands/TestCommandTest.php @@ -19,6 +19,10 @@ use Symfony\Component\Console\Command\Command; use function Pest\Laravel\artisan; +beforeEach(function (): void { + $this->markTestSkipped('Not implemented yet.'); +}); + afterEach(function (): void { app()->terminate(); }); diff --git a/tests/FeatureTest.php b/tests/FeatureTest.php index 8aa5e65..bb53310 100644 --- a/tests/FeatureTest.php +++ b/tests/FeatureTest.php @@ -14,24 +14,17 @@ * @see https://github.com/guanguans/laravel-api-response */ -use Illuminate\Http\UploadedFile; - -it('can report exception', function (): void { +it('can return success json response', function (): void { $this - ->post('report-exception?foo=bar', [ + ->post('success', [ + 'foo' => 'bar', 'bar' => 'baz', - 'password' => 'password', - 'file' => new UploadedFile(__FILE__, basename(__FILE__)), ]) ->assertOk(); })->group(__DIR__, __FILE__); -it('can auto report exception', function (): void { +it('can return error json response', function (): void { $this - ->post('exception?foo=bar', [ - 'bar' => 'baz', - 'password' => 'password', - 'file' => new UploadedFile(__FILE__, basename(__FILE__)), - ]) - ->assertStatus(500); + ->post('error') + ->assertOk(); })->group(__DIR__, __FILE__); diff --git a/tests/Pest.php b/tests/Pest.php index e2a43f2..2e9c1ca 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -21,7 +21,7 @@ uses(TestCase::class) ->beforeAll(function (): void {}) ->beforeEach(function (): void { - $this->markTestSkipped('Not implemented yet.'); + // $this->markTestSkipped('Not implemented yet.'); }) ->afterEach(function (): void {}) ->afterAll(function (): void {}) diff --git a/tests/TestCase.php b/tests/TestCase.php index 18706a6..cdb5941 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -15,6 +15,7 @@ use Guanguans\LaravelApiResponse\ApiResponseServiceProvider; use Guanguans\LaravelApiResponse\Facades\ApiResponse; +use Illuminate\Http\Request; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Symfony\Component\VarDumper\Test\VarDumperTestTrait; @@ -43,19 +44,11 @@ protected function getPackageProviders($app): array ]; } - protected function defineEnvironment($app): void - { - // config()->set('exception-notify.job.queue', 'exception-notify'); - } + protected function defineEnvironment($app): void {} protected function defineRoutes($router): void { - $router->any('report-exception', static fn () => tap(response('report-exception'), static function (): void { - ApiResponse::report(new \Guanguans\LaravelApiResponse\Exceptions\RuntimeException('What happened?'), ['dump', 'log', 'bark', 'lark']); - })); - - $router->any('exception', static fn () => tap(response('exception'), static function (): void { - throw new \Guanguans\LaravelApiResponse\Exceptions\RuntimeException('What happened?'); - })); + $router->any('success', static fn (Request $request) => ApiResponse::success($request->input())); + $router->any('error', static fn (Request $request) => ApiResponse::error()); } }