Skip to content

Commit

Permalink
test: Refactor tests and skip unimplemented tests
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
guanguans committed Aug 22, 2024
1 parent c7436a5 commit 624d2da
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 26 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
*/
class ApiResponse implements ApiResponseContract
{
// use Conditionable;
// use Dumpable;
use ConcreteHttpStatusMethods;
use Conditionable;
use HasExceptionMap;
use HasPipes;
use Macroable;
Expand Down
32 changes: 32 additions & 0 deletions tests/ArchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2024 guanguans<ityaozm@gmail.com>
*
* 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();
4 changes: 4 additions & 0 deletions tests/Commands/TestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
19 changes: 6 additions & 13 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
15 changes: 4 additions & 11 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}

0 comments on commit 624d2da

Please sign in to comment.