Skip to content

Commit a1d990d

Browse files
committed
refactor(test): Improve env_explode test case
- Updated the test case for env_explode function in HeplersTest.php - Changed the expectation for env_explode('ENV_EXPLODE_EMPTY') to an empty array
1 parent fbc5b00 commit a1d990d

File tree

10 files changed

+89
-30
lines changed

10 files changed

+89
-30
lines changed

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ parameters:
55
count: 1
66
path: src/Collectors/ApplicationCollector.php
77

8+
-
9+
message: "#^Call to an undefined method Illuminate\\\\Contracts\\\\Foundation\\\\Application\\:\\:terminating\\(\\)\\.$#"
10+
count: 1
11+
path: src/Commands/TestCommand.php
12+
813
-
914
message: "#^Method Guanguans\\\\LaravelExceptionNotify\\\\DefaultNotifyClientExtender\\:\\:__invoke\\(\\) should return Guanguans\\\\Notify\\\\Foundation\\\\Client but returns Guanguans\\\\Notify\\\\Foundation\\\\Concerns\\\\HasHttpClient\\.$#"
1015
count: 1

phpunit.xml.dist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
</testsuites>
2626
<php>
2727
<env name="APP_KEY" value="base64:e2ASw7JCNMYR6PWInGGQrzUzisuDvv8bhNl14XGbUi8="/>
28-
<env name="ENV_EXPLODE_STRING" value="log,null"/>
29-
<env name="ENV_EXPLODE_TRUE" value="true"/>
30-
<env name="ENV_EXPLODE_FALSE" value="false"/>
3128
<env name="ENV_EXPLODE_EMPTY" value="empty"/>
29+
<env name="ENV_EXPLODE_FALSE" value="false"/>
3230
<env name="ENV_EXPLODE_NULL" value="null"/>
31+
<env name="ENV_EXPLODE_STRING" value="log,null"/>
32+
<env name="ENV_EXPLODE_TRUE" value="true"/>
3333
</php>
3434
<!--<listeners>
3535
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener">

rector.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
])
164164
->withSkip([
165165
EncapsedStringsToSprintfRector::class,
166+
ExplicitBoolCompareRector::class,
166167
ExplicitReturnNullRector::class,
167168
LogicalToBooleanRector::class,
168169
NewlineAfterStatementRector::class,
@@ -174,20 +175,14 @@
174175
DisallowedEmptyRuleFixerRector::class => [
175176
// __DIR__.'/src/Support/QueryAnalyzer.php',
176177
],
177-
ExplicitBoolCompareRector::class => [
178-
// __DIR__.'/src/JavascriptRenderer.php',
179-
],
180178
RemoveExtraParametersRector::class => [
181179
// __DIR__.'/src/Macros/QueryBuilderMacro.php',
182180
],
183181
RenameForeachValueVariableToMatchExprVariableRector::class => [
184182
// __DIR__.'/src/OutputManager.php',
185183
],
186-
StaticArrowFunctionRector::class => [
187-
__DIR__.'/tests/ExceptionNotifyManagerTest.php',
188-
],
189-
StaticClosureRector::class => [
190-
__DIR__.'/src/ReportUsingCreator.php',
184+
StaticArrowFunctionRector::class => $staticClosureSkipPaths = [
191185
__DIR__.'/tests',
192186
],
187+
StaticClosureRector::class => $staticClosureSkipPaths,
193188
]);

src/Commands/TestCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TestCommand extends Command
2525
protected $signature = 'exception-notify:test {--c|channels=* : Specify channels to test}';
2626
protected $description = 'Test for exception-notify';
2727

28-
public function handle(ExceptionNotifyManager $exceptionNotifyManager)
28+
public function handle(ExceptionNotifyManager $exceptionNotifyManager): int
2929
{
3030
$this->output->info('Test for exception-notify start.');
3131

src/Support/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function env_explode(string $key, $default = null, string $delimiter = ',', int
7272
$env = env($key, $default);
7373

7474
if (\is_string($env)) {
75-
return explode($delimiter, $env, $limit);
75+
return $env ? explode($delimiter, $env, $limit) : [];
7676
}
7777

7878
return $env;

tests/Commands/TestCommandTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,29 @@
1818
use Symfony\Component\Console\Command\Command;
1919
use function Pest\Laravel\artisan;
2020

21-
it('can test for exception-notify', function (): void {
21+
afterEach(function (): void {
22+
app()->terminate();
23+
});
24+
25+
it('can test for exception-notify when is not enabled', function (): void {
2226
config()->set('exception-notify.enabled', false);
23-
artisan(TestCommand::class)->assertExitCode(Command::SUCCESS);
24-
})->group(__DIR__, __FILE__)->skip();
27+
artisan(TestCommand::class)->assertExitCode(Command::INVALID);
28+
})->group(__DIR__, __FILE__);
29+
30+
it('can test for exception-notify when default channels is empty', function (): void {
31+
config()->set('exception-notify.defaults', []);
32+
artisan(TestCommand::class)->assertExitCode(Command::INVALID);
33+
})->group(__DIR__, __FILE__);
34+
35+
it('can test for exception-notify when should not report', function (): void {
36+
\Guanguans\LaravelExceptionNotify\Facades\ExceptionNotify::skipWhen(
37+
static fn (\Throwable $throwable): bool => $throwable instanceof RuntimeException
38+
);
39+
artisan(TestCommand::class)->assertExitCode(Command::INVALID);
40+
})->group(__DIR__, __FILE__);
2541

2642
it('will throws RuntimeException', function (): void {
27-
artisan(TestCommand::class);
43+
artisan(TestCommand::class, [
44+
'--channels' => ['bark', 'log'],
45+
]);
2846
})->group(__DIR__, __FILE__)->throws(RuntimeException::class, 'Test for exception-notify.');

tests/ExceptionNotifyManagerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@
6666
->report(new RuntimeException)->toBeNull();
6767
})->group(__DIR__, __FILE__);
6868

69-
it('should report', function (): void {
70-
expect(app(ExceptionNotifyManager::class))->shouldReport(new RuntimeException)->toBeTrue();
71-
})->group(__DIR__, __FILE__);
72-
7369
it('should not report', function (): void {
7470
config()->set('exception-notify.enabled', false);
7571
expect(app(ExceptionNotifyManager::class))->shouldReport(new RuntimeException)->toBeFalse();
@@ -88,7 +84,11 @@
8884
);
8985
});
9086
expect(app(ExceptionNotifyManager::class))->shouldReport(new RuntimeException)->toBeFalse();
91-
})->group(__DIR__, __FILE__)->depends('it should report');
87+
})->group(__DIR__, __FILE__);
88+
89+
it('should report', function (): void {
90+
expect(app(ExceptionNotifyManager::class))->shouldReport(new RuntimeException)->toBeTrue();
91+
})->group(__DIR__, __FILE__);
9292

9393
it('can get default driver', function (): void {
9494
expect(app(ExceptionNotifyManager::class))

tests/Pest.php

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
/** @noinspection PhpUnused */
4+
/** @noinspection PhpUndefinedClassInspection */
35
/** @noinspection AnonymousFunctionStaticInspection */
46
/** @noinspection StaticClosureCanBeUsedInspection */
57

@@ -16,19 +18,57 @@
1618

1719
namespace Tests;
1820

21+
use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;
1922
use Guanguans\LaravelExceptionNotify\Tests\TestCase;
2023

2124
uses(TestCase::class)
2225
->beforeAll(function (): void {})
23-
->beforeEach(function (): void {})
26+
->beforeEach(function (): void {
27+
(fn (): array => self::$skipCallbacks = [])->call(app(ExceptionNotifyManager::class));
28+
})
2429
->afterEach(function (): void {})
2530
->afterAll(function (): void {})
2631
->in(__DIR__);
2732

28-
expect()->extend('between', function (int $min, $max) {
29-
expect($this->value)
30-
->toBeGreaterThanOrEqual($min)
31-
->toBeLessThanOrEqual($max);
33+
/*
34+
|--------------------------------------------------------------------------
35+
| Expectations
36+
|--------------------------------------------------------------------------
37+
|
38+
| When you're writing tests, you often need to check that values meet certain conditions. The
39+
| "expect()" function gives you access to a set of "expectations" methods that you can use
40+
| to assert different things. Of course, you may extend the Expectation API at any time.
41+
|
42+
*/
43+
44+
expect()->extend('toBetween', fn (int $min, int $max): Expectation => expect($this->value)
45+
->toBeGreaterThanOrEqual($min)
46+
->toBeLessThanOrEqual($max));
47+
48+
/*
49+
|--------------------------------------------------------------------------
50+
| Functions
51+
|--------------------------------------------------------------------------
52+
|
53+
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
54+
| project that you don't want to repeat in every file. Here you can also expose helpers as
55+
| global functions to help you to reduce the number of lines of code in your test files.
56+
|
57+
*/
58+
59+
/**
60+
* @param object|string $class
61+
*
62+
* @throws ReflectionException
63+
*/
64+
function class_namespace($class): string
65+
{
66+
$class = \is_object($class) ? \get_class($class) : $class;
67+
68+
return (new ReflectionClass($class))->getNamespaceName();
69+
}
3270

33-
return $this;
34-
});
71+
function fixtures_path(string $path = ''): string
72+
{
73+
return __DIR__.\DIRECTORY_SEPARATOR.'fixtures'.($path ? \DIRECTORY_SEPARATOR.$path : $path);
74+
}

tests/Support/HeplersTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
it('can explode env', function (): void {
2929
expect(env_explode('ENV_EXPLODE_STRING'))->toBeArray()->toBeTruthy()
30-
->and(env_explode('ENV_EXPLODE_EMPTY'))->toBe([''])
30+
->and(env_explode('ENV_EXPLODE_EMPTY'))->toBe([])
3131
->and(env_explode('ENV_EXPLODE_NOT_EXIST'))->toBeNull();
3232
// ->and(env_explode('ENV_EXPLODE_TRUE'))->toBeTrue()
3333
// ->and(env_explode('ENV_EXPLODE_FALSE'))->toBeFalse()

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ protected function defineEnvironment($app): void
7272
{
7373
config()->set('exception-notify.job.queue', 'exception-notify');
7474
config()->set('exception-notify.channels.bark.authenticator.token', $this->faker()->uuid());
75+
config()->set('exception-notify.channels.bark.client.http_options', []);
7576
config()->set('exception-notify.channels.bark.client.extender', static fn (Client $client): Client => $client->mock([
7677
(new HttpFactory)->createResponse(200, '{"code":200,"message":"success","timestamp":1708331409}'),
7778
]));

0 commit comments

Comments
 (0)