Skip to content

Commit

Permalink
test(Ernie, Pest, ConfigManagerTest): Refactor response handling and …
Browse files Browse the repository at this point in the history
…update test setup

- Refactor the response handling in Ernie.php to simplify the assignment of the response variable.
- Update the test setup in Pest.php to set the application version dynamically.
- Modify the skip method in ConfigManagerTest.php for better clarity in test grouping.
  • Loading branch information
ityaozm@gmail.com committed Sep 30, 2024
1 parent 9f28d27 commit d836cdc
Show file tree
Hide file tree
Showing 22 changed files with 208 additions and 48 deletions.
4 changes: 1 addition & 3 deletions app/Support/Ernie.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ static function (PendingRequest $pendingRequest) use (&$rowData, $writer): Pendi
));

if ($rowData && empty($response->body())) {
$response = new Response(
$response->toPsrResponse()->withBody(Utils::streamFor(self::sanitizeData($rowData)))
);
$response = new Response($response->toPsrResponse()->withBody(Utils::streamFor(self::sanitizeData($rowData))));
}

return tap($response->throw(), static function (Response $response): void {
Expand Down
30 changes: 29 additions & 1 deletion tests/Datasets/InvalidJsons.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand All @@ -10,7 +18,27 @@
* This source file is subject to the MIT license that is bundled.
*/

dataset('InvalidJsons', [
dataset('commit command parameters', [
[
'parameters' => [],
],
[
'parameters' => [
'--dry-run' => true,
],
],
[
'parameters' => [
'--diff' => <<<'DIFF'
tests/Pest.php | 1 +
tests/Unit/ConfigManagerTest.php | 2 +-
DIFF
,
],
],
]);

dataset('invalid jsons', [
[
'json' => '',
'expect' => '',
Expand Down
22 changes: 18 additions & 4 deletions tests/Feature/CommitCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand Down Expand Up @@ -60,7 +68,7 @@
'created' => 1677143178,
'model' => 'text-davinci-003',
'choices' => [
0 => [
[
'text' => 'invalid json', // 无效响应
'index' => 0,
'logprobs' => null,
Expand All @@ -87,7 +95,12 @@
->group(__DIR__, __FILE__)
->throws(TaskException::class, 'The generated commit message(');

it('can generate and commit message', function (): void {
it('can generate and commit message', function (array $parameters): void {
// 添加文件到暂存区
file_put_contents(repository_path('playground.random'), Str::random());
Process::fromShellCommandline('git rm -rf --cached repository/', fixtures_path())->run();
Process::fromShellCommandline('git add playground.random', repository_path())->mustRun();

// 设置 git 信息
Process::fromShellCommandline('git config user.email yaozm', repository_path())->mustRun();
Process::fromShellCommandline('git config user.name ityaozm@gmail.com', repository_path())->mustRun();
Expand All @@ -99,7 +112,7 @@
]);

$this
->artisan(CommitCommand::class, [
->artisan(CommitCommand::class, $parameters + [
'path' => repository_path(),
'--generator' => 'openai',
'--no-edit' => true,
Expand All @@ -117,12 +130,13 @@
->expectsConfirmation('Do you want to commit this message?', 'yes')
->assertSuccessful();
})
->with('commit command parameters')
->depends('it will throw TaskException(The generated commit message is an invalid JSON)')
->group(__DIR__, __FILE__);

afterAll(static function (): void {
// 清理 playground 仓库
Process::fromShellCommandline('git reset HEAD^', repository_path())->run();
Process::fromShellCommandline('git reset $(git rev-list --max-parents=0 HEAD)', repository_path())->run();
// Process::fromShellCommandline('git checkout -- .', repository_path())->run();
Process::fromShellCommandline('git checkout HEAD -- .', repository_path())->run();
Process::fromShellCommandline('git add tests/Fixtures/repository/', base_path())->mustRun();
Expand Down
10 changes: 9 additions & 1 deletion tests/Feature/ConfigCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand Down Expand Up @@ -62,7 +70,7 @@
])->assertSuccessful();
})
->group(__DIR__, __FILE__)
->with(['null', 'true', 'false', '0.0', '0', json_encode(['foo' => 'bar'])]);
->with(['null', 'true', 'false', '0.0', '0', json_encode(['foo' => 'bar'], JSON_THROW_ON_ERROR)]);

it('can get config', function (): void {
$this->artisan(ConfigCommand::class, [
Expand Down
8 changes: 8 additions & 0 deletions tests/Feature/ThanksCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand Down
17 changes: 14 additions & 3 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand All @@ -22,6 +30,7 @@
})
->beforeEach(function (): void {
// setup_http_fake();
config()->set('app.version', 'v'.config('app.version'));
})
->afterEach(function (): void {
})
Expand All @@ -40,8 +49,8 @@
|
*/

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

/*
Expand All @@ -57,6 +66,8 @@

/**
* @param object|string $class
*
* @throws \ReflectionException
*/
function class_namespace($class): string
{
Expand Down Expand Up @@ -165,7 +176,7 @@ function setup_http_fake(): void
'created' => 1677143178,
'model' => 'text-davinci-003',
'choices' => [
0 => [
[
'text' => $text,
'index' => 0,
'logprobs' => null,
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static function tearDownAfterClass(): void

/**
* This method is called before each test.
*
* @throws \JsonException
*/
protected function setUp(): void
{
Expand Down
10 changes: 9 additions & 1 deletion tests/Unit/ConfigManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand All @@ -26,7 +34,7 @@
->expects($this->once())
->willReturn(false);
expect(ConfigManager::localPath())->toBeString();
})->skip()->group(__DIR__, __FILE__);
})->group(__DIR__, __FILE__)->skip();

it('can put local config file', function (): void {
expect(ConfigManager::create())->putLocal()->toBeInt();
Expand Down
40 changes: 11 additions & 29 deletions tests/Unit/Exceptions/HandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

Expand All @@ -12,37 +18,13 @@
* This source file is subject to the MIT license that is bundled.
*/

use App\Exceptions\Handler;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Validation\Factory;
use Illuminate\Validation\ValidationException;
use Symfony\Component\Console\Output\OutputInterface;

it('can render exception for console', function (): void {
$this->app['env'] = 'production';
$output = Mockery::spy(OutputInterface::class);
/** @noinspection PhpVoidFunctionResultUsedInspection */
expect($this->app->get(Handler::class))
->renderForConsole(
$output,
new ValidationException(
$this->app->get(Factory::class)->make(
['foo' => 'bar'],
['foo' => 'int']
)
)
)->toBeNull()
->renderForConsole(
$output,
Mockery::spy(\Exception::class)
it('can map ValidationException', function (): void {
expect(app(ExceptionHandler::class))
->report(
new ValidationException($this->app->get(Factory::class)->make(['foo' => 'bar'], ['foo' => 'int']))
)->toBeNull();
})->group(__DIR__, __FILE__);

it('can report exception', function (): void {
$this->app['env'] = 'testing';
$exception = new Exception('foo');
$handler = $this->app->get(Handler::class);
expect($handler)->shouldReport($exception)->toBeTrue();

// $this->app['env'] = 'production';
// expect($handler)->shouldReport($exception)->toBeFalse();
})->group(__DIR__, __FILE__)->skip();
8 changes: 8 additions & 0 deletions tests/Unit/GeneratorManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Generators/BitoGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Generators/ErnieBotGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Generators/ErnieBotTurboGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Generators/MoonshotGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Generators/OpenAIChatGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Generators/OpenAIGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection JsonEncodingApiUsageInspection */
/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpUnused */
/** @noinspection PhpUnusedAliasInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
Expand Down
Loading

0 comments on commit d836cdc

Please sign in to comment.