Skip to content

Commit

Permalink
refactor(test): Improve env_explode test case
Browse files Browse the repository at this point in the history
- Updated the test case for env_explode function in HeplersTest.php
- Changed the expectation for env_explode('ENV_EXPLODE_EMPTY') to an empty array
  • Loading branch information
guanguans committed May 10, 2024
1 parent fbc5b00 commit d430333
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 29 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ parameters:
count: 1
path: src/Collectors/ApplicationCollector.php

-
message: "#^Call to an undefined method Illuminate\\\\Contracts\\\\Foundation\\\\Application\\:\\:terminating\\(\\)\\.$#"
count: 1
path: src/Commands/TestCommand.php

-
message: "#^Method Guanguans\\\\LaravelExceptionNotify\\\\DefaultNotifyClientExtender\\:\\:__invoke\\(\\) should return Guanguans\\\\Notify\\\\Foundation\\\\Client but returns Guanguans\\\\Notify\\\\Foundation\\\\Concerns\\\\HasHttpClient\\.$#"
count: 1
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
</testsuites>
<php>
<env name="APP_KEY" value="base64:e2ASw7JCNMYR6PWInGGQrzUzisuDvv8bhNl14XGbUi8="/>
<env name="ENV_EXPLODE_STRING" value="log,null"/>
<env name="ENV_EXPLODE_TRUE" value="true"/>
<env name="ENV_EXPLODE_FALSE" value="false"/>
<env name="ENV_EXPLODE_EMPTY" value="empty"/>
<env name="ENV_EXPLODE_FALSE" value="false"/>
<env name="ENV_EXPLODE_NULL" value="null"/>
<env name="ENV_EXPLODE_STRING" value="log,null"/>
<env name="ENV_EXPLODE_TRUE" value="true"/>
</php>
<!--<listeners>
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener">
Expand Down
11 changes: 3 additions & 8 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
])
->withSkip([
EncapsedStringsToSprintfRector::class,
ExplicitBoolCompareRector::class,
ExplicitReturnNullRector::class,
LogicalToBooleanRector::class,
NewlineAfterStatementRector::class,
Expand All @@ -174,20 +175,14 @@
DisallowedEmptyRuleFixerRector::class => [
// __DIR__.'/src/Support/QueryAnalyzer.php',
],
ExplicitBoolCompareRector::class => [
// __DIR__.'/src/JavascriptRenderer.php',
],
RemoveExtraParametersRector::class => [
// __DIR__.'/src/Macros/QueryBuilderMacro.php',
],
RenameForeachValueVariableToMatchExprVariableRector::class => [
// __DIR__.'/src/OutputManager.php',
],
StaticArrowFunctionRector::class => [
__DIR__.'/tests/ExceptionNotifyManagerTest.php',
],
StaticClosureRector::class => [
__DIR__.'/src/ReportUsingCreator.php',
StaticArrowFunctionRector::class => $staticClosureSkipPaths = [
__DIR__.'/tests',
],
StaticClosureRector::class => $staticClosureSkipPaths,
]);
2 changes: 1 addition & 1 deletion src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function env_explode(string $key, $default = null, string $delimiter = ',', int
$env = env($key, $default);

if (\is_string($env)) {
return explode($delimiter, $env, $limit);
return $env ? explode($delimiter, $env, $limit) : [];
}

return $env;
Expand Down
22 changes: 18 additions & 4 deletions tests/Commands/TestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@
use Symfony\Component\Console\Command\Command;
use function Pest\Laravel\artisan;

it('can test for exception-notify', function (): void {
it('can test for exception-notify when is not enabled', function (): void {
config()->set('exception-notify.enabled', false);
artisan(TestCommand::class)->assertExitCode(Command::SUCCESS);
})->group(__DIR__, __FILE__)->skip();
artisan(TestCommand::class)->assertExitCode(Command::INVALID);
})->group(__DIR__, __FILE__);

it('can test for exception-notify when default channels is empty', function (): void {
config()->set('exception-notify.defaults', []);
artisan(TestCommand::class)->assertExitCode(Command::INVALID);
})->group(__DIR__, __FILE__);

it('can test for exception-notify when should not report', function (): void {
\Guanguans\LaravelExceptionNotify\Facades\ExceptionNotify::skipWhen(
static fn (\Throwable $throwable): bool => $throwable instanceof RuntimeException
);
artisan(TestCommand::class)->assertExitCode(Command::INVALID);
})->group(__DIR__, __FILE__);

it('will throws RuntimeException', function (): void {
artisan(TestCommand::class);
artisan(TestCommand::class, [
'--channels' => ['bark', 'log'],
]);
})->group(__DIR__, __FILE__)->throws(RuntimeException::class, 'Test for exception-notify.');
10 changes: 5 additions & 5 deletions tests/ExceptionNotifyManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@
->report(new RuntimeException)->toBeNull();
})->group(__DIR__, __FILE__);

it('should report', function (): void {
expect(app(ExceptionNotifyManager::class))->shouldReport(new RuntimeException)->toBeTrue();
})->group(__DIR__, __FILE__);

it('should not report', function (): void {
config()->set('exception-notify.enabled', false);
expect(app(ExceptionNotifyManager::class))->shouldReport(new RuntimeException)->toBeFalse();
Expand All @@ -88,7 +84,11 @@
);
});
expect(app(ExceptionNotifyManager::class))->shouldReport(new RuntimeException)->toBeFalse();
})->group(__DIR__, __FILE__)->depends('it should report');
})->group(__DIR__, __FILE__);

it('should report', function (): void {
expect(app(ExceptionNotifyManager::class))->shouldReport(new RuntimeException)->toBeTrue();
})->group(__DIR__, __FILE__);

it('can get default driver', function (): void {
expect(app(ExceptionNotifyManager::class))
Expand Down
54 changes: 47 additions & 7 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/** @noinspection PhpUnused */
/** @noinspection PhpUndefinedClassInspection */
/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

Expand All @@ -16,19 +18,57 @@

namespace Tests;

use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;
use Guanguans\LaravelExceptionNotify\Tests\TestCase;

uses(TestCase::class)
->beforeAll(function (): void {})
->beforeEach(function (): void {})
->beforeEach(function (): void {
(fn (): array => self::$skipCallbacks = [])->call(app(ExceptionNotifyManager::class));
})
->afterEach(function (): void {})
->afterAll(function (): void {})
->in(__DIR__);

expect()->extend('between', function (int $min, $max) {
expect($this->value)
->toBeGreaterThanOrEqual($min)
->toBeLessThanOrEqual($max);
/*
|--------------------------------------------------------------------------
| 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('toBetween', fn (int $min, int $max): Expectation => expect($this->value)
->toBeGreaterThanOrEqual($min)
->toBeLessThanOrEqual($max));

/*
|--------------------------------------------------------------------------
| 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.
|
*/

/**
* @param object|string $class
*
* @throws ReflectionException
*/
function class_namespace($class): string
{
$class = \is_object($class) ? \get_class($class) : $class;

return (new ReflectionClass($class))->getNamespaceName();
}

return $this;
});
function fixtures_path(string $path = ''): string
{
return __DIR__.\DIRECTORY_SEPARATOR.'fixtures'.($path ? \DIRECTORY_SEPARATOR.$path : $path);
}
2 changes: 1 addition & 1 deletion tests/Support/HeplersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

it('can explode env', function (): void {
expect(env_explode('ENV_EXPLODE_STRING'))->toBeArray()->toBeTruthy()
->and(env_explode('ENV_EXPLODE_EMPTY'))->toBe([''])
->and(env_explode('ENV_EXPLODE_EMPTY'))->toBe([])
->and(env_explode('ENV_EXPLODE_NOT_EXIST'))->toBeNull();
// ->and(env_explode('ENV_EXPLODE_TRUE'))->toBeTrue()
// ->and(env_explode('ENV_EXPLODE_FALSE'))->toBeFalse()
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ protected function defineEnvironment($app): void
{
config()->set('exception-notify.job.queue', 'exception-notify');
config()->set('exception-notify.channels.bark.authenticator.token', $this->faker()->uuid());
config()->set('exception-notify.channels.bark.client.http_options', []);
config()->set('exception-notify.channels.bark.client.extender', static fn (Client $client): Client => $client->mock([
(new HttpFactory)->createResponse(200, '{"code":200,"message":"success","timestamp":1708331409}'),
]));
Expand Down

0 comments on commit d430333

Please sign in to comment.