Skip to content

Commit b6024f2

Browse files
committed
test(ArchTest): enforce prohibition of debugging functions usage
- Replace commented out expectation with active architecture test to forbid debugging functions - Specify a comprehensive list of disallowed debugging functions such as dd, die, dump, echo, print, var_dump, and others - Group the test by directory and file for better organization - Add ignoring rules for ComposerScripts and WithDumpable classes to prevent false positives - Include copyright and license information in the test file header for compliance Signed-off-by: guanguans <[email protected]>
1 parent ecd2502 commit b6024f2

17 files changed

+188
-117
lines changed

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"nette/utils": "^4.0",
5757
"pestphp/pest": "^2.36 || ^3.0 || ^4.0",
5858
"pestphp/pest-plugin-arch": "^2.7 || ^3.0 || ^4.0",
59-
"pestphp/pest-plugin-faker": "^2.0 || ^3.0 || ^4.0",
6059
"pestphp/pest-plugin-profanity": "^1.7 || ^2.0 || ^3.0 || ^4.0",
6160
"php-mock/php-mock-phpunit": "^2.14",
6261
"phpbench/phpbench": "^1.4",
@@ -184,8 +183,6 @@
184183
],
185184
"checks:optional": [
186185
"@putenv:xdebug-off",
187-
"@composer:normalize-dry-run",
188-
"@composer-dependency-analyser",
189186
"@class-leak:check",
190187
"@detect-collisions",
191188
"@jack:open-versions-dry-run",
@@ -209,6 +206,8 @@
209206
],
210207
"checks:required": [
211208
"@putenv:xdebug-off",
209+
"@composer:normalize-dry-run",
210+
"@composer-dependency-analyser",
212211
"@php-cs-fixer:fix-dry-run",
213212
"@soar:check-binary",
214213
"@soar:dump-config",
@@ -307,6 +306,7 @@
307306
"@putenv:xdebug-off"
308307
],
309308
"pest:bail": "@pest --bail",
309+
"pest:compact": "@pest --compact",
310310
"pest:coverage": "@pest --coverage-html=.build/phpunit/ --coverage-clover=.build/phpunit/clover.xml",
311311
"pest:disable-coverage-ignore": "@pest --disable-coverage-ignore",
312312
"pest:highest": [
@@ -318,8 +318,9 @@
318318
"pest:migrate-configuration": "@pest --migrate-configuration",
319319
"pest:parallel": "@pest --parallel",
320320
"pest:profanity": "@pest --profanity --language=en",
321+
"pest:profile": "@pest --profile",
321322
"pest:type-coverage": "@pest --type-coverage",
322-
"pest:update-snapshots": "@pest -d --update-snapshots",
323+
"pest:update-snapshots": "@pest --update-snapshots",
323324
"php-cs-fixer": "@php vendor/bin/php-cs-fixer --config=.php-cs-fixer.php --ansi -v",
324325
"php-cs-fixer:check": "@php-cs-fixer check --show-progress=dots --diff",
325326
"php-cs-fixer:describe": "@php-cs-fixer describe",

phpunit.xml.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
colors="true"
77
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
88
>
9-
<!--printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer"-->
10-
<!--printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printers\DefaultPrinter"-->
11-
<!--printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printers\ReportablePrinter"-->
129
<testsuites>
1310
<testsuite name="Guanguans Test Suite">
1411
<directory>tests/</directory>

rector.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Guanguans\MonorepoBuilderWorker\Support\Rectors\RemoveNamespaceRector;
2222
use Guanguans\SoarPHP\Contracts\Throwable;
2323
use Guanguans\SoarPHP\Support\Rectors\AddDocCommentForHasOptionsRector;
24+
use Illuminate\Support\Str;
2425
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
2526
use Rector\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector;
2627
use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
@@ -39,7 +40,12 @@
3940
use Rector\Php80\ValueObject\AnnotationToAttribute;
4041
use Rector\PHPUnit\Set\PHPUnitSetList;
4142
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
43+
use Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector;
44+
use Rector\Transform\ValueObject\StaticCallToFuncCall;
4245
use Rector\ValueObject\PhpVersion;
46+
use Rector\ValueObject\Visibility;
47+
use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector;
48+
use Rector\Visibility\ValueObject\ChangeMethodVisibility;
4349
use function Guanguans\SoarPHP\Support\classes;
4450

4551
return RectorConfig::configure()
@@ -123,11 +129,32 @@
123129
'phpstan-ignore-next-line',
124130
'psalm-suppress',
125131
])
132+
->withConfiguredRule(StaticCallToFuncCallRector::class, [
133+
new StaticCallToFuncCall(Str::class, 'of', 'str'),
134+
])
135+
->withConfiguredRule(
136+
ChangeMethodVisibilityRector::class,
137+
classes(static fn (string $class, string $file): bool => str_starts_with($class, 'Guanguans\SoarPHP'))
138+
->filter(static fn (ReflectionClass $reflectionClass): bool => $reflectionClass->isTrait())
139+
->map(
140+
static fn (ReflectionClass $reflectionClass): array => collect($reflectionClass->getMethods(ReflectionMethod::IS_PRIVATE))
141+
->reject(static fn (ReflectionMethod $reflectionMethod): bool => $reflectionMethod->isFinal() || $reflectionMethod->isInternal())
142+
->map(static fn (ReflectionMethod $reflectionMethod): ChangeMethodVisibility => new ChangeMethodVisibility(
143+
$reflectionClass->getName(),
144+
$reflectionMethod->getName(),
145+
Visibility::PROTECTED
146+
))
147+
->all()
148+
)
149+
->flatten()
150+
// ->dd()
151+
->all(),
152+
)
126153
->withConfiguredRule(
127154
RenameFunctionRector::class,
128155
[
129156
'Pest\Faker\fake' => 'fake',
130-
'Pest\Faker\faker' => 'faker',
157+
'Pest\Faker\faker' => 'fake',
131158
'faker' => 'fake',
132159
'test' => 'it',
133160
] + array_reduce(

src/Support/ComposerScripts.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ public static function dumpSoarPHPConfig(Event $event): int
121121
*/
122122
public static function resolveSoarHelp(): Collection
123123
{
124-
return Str::of(self::resolveSoarHelpContent())
124+
return str(self::resolveSoarHelpContent())
125125
->explode(\PHP_EOL)
126126
->filter(static fn (string $option): bool => str_starts_with($option, ' '))
127127
->map(static fn (string $option): string => trim($option))
128128
->chunk(2)
129129
->reduce(
130130
static function (Collection $options, Collection $option): Collection {
131-
[$name, $type] = Str::of($option->firstOrFail())
131+
[$name, $type] = (array) str($option->firstOrFail())
132132
->explode(' ')
133133
->pad(2, 'bool')
134134
->pipe(static function (Collection $collection): array {
@@ -140,7 +140,7 @@ static function (Collection $options, Collection $option): Collection {
140140
}];
141141
});
142142

143-
$default = Str::of($option->last())
143+
$default = str($option->last())
144144
/** @lang PhpRegExp */
145145
->match('/\\(default .*\\)/')
146146
->pipe(
@@ -163,7 +163,7 @@ static function (Collection $options, Collection $option): Collection {
163163
'name' => $help,
164164
'type' => 'bool',
165165
'default' => null,
166-
'description' => Str::of($help)->headline()->toString(),
166+
'description' => str($help)->headline()->toString(),
167167
])
168168
->tap(static function (Collection $collection): void {
169169
$asserter = static function (Collection $collection): void {
@@ -182,9 +182,9 @@ static function (Collection $options, Collection $option): Collection {
182182
->map(static function (array $option, string $name): array {
183183
$config = self::resolveSoarConfig();
184184

185-
$option['default'] = $config->has($snakedName = Str::of($name)->ltrim('-')->snake('-')->toString())
185+
$option['default'] = $config->has($snakedName = str($name)->ltrim('-')->snake('-')->toString())
186186
? $config->get($snakedName)
187-
: $config->get(Str::of($name)->ltrim('-')->snake()->toString(), $option['default']);
187+
: $config->get(str($name)->ltrim('-')->snake()->toString(), $option['default']);
188188

189189
$option['type'] = collect([$option['type'], \gettype($option['default'])])
190190
->map(static fn (string $type): string => match ($type = strtolower($type)) {

src/Support/helpers.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
* @noinspection RedundantDocCommentTagInspection
3737
* @noinspection PhpUndefinedNamespaceInspection
3838
*
39+
* @internal
40+
*
3941
* @param null|(callable(class-string, string): bool) $filter
4042
*
4143
* @return \Illuminate\Support\Collection<class-string, \ReflectionClass>

tests/__snapshots__/HasOptionsTest__it_can_normalize_options__1.json renamed to tests/.pest/snapshots/Concerns/HasOptionsTest/it_can_normalize_options.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@
7575
"-unique-key-prefix": "-unique-key-prefix=uk_",
7676
"-verbose": "-verbose=false",
7777
"-version": "-version"
78-
}
78+
}

tests/__snapshots__/SoarTest__it_can_get_help_snapshot__1.txt renamed to tests/.pest/snapshots/SoarTest/it_can_get_help_snapshot.snap

File renamed without changes.

tests/__snapshots__/SoarTest__it_can_get_version_snapshot__1.txt renamed to tests/.pest/snapshots/SoarTest/it_can_get_version_snapshot.snap

File renamed without changes.

tests/ArchTest.php

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,42 @@
1717
*
1818
* @see https://github.com/guanguans/soar-php
1919
*/
20-
// arch('will not use debugging functions')
21-
// ->expect([
22-
// 'echo',
23-
// 'print',
24-
// 'die',
25-
// 'exit',
26-
// 'printf',
27-
// 'vprintf',
28-
// 'var_dump',
29-
// 'dump',
30-
// 'dd',
31-
// 'ray',
32-
// 'print_r',
33-
// 'var_export',
34-
// ])
35-
// ->each->not->toBeUsed();
20+
21+
use Guanguans\SoarPHP\Concerns\WithDumpable;
22+
use Guanguans\SoarPHP\Support\ComposerScripts;
23+
24+
/**
25+
* Copyright (c) 2019-2025 guanguans<[email protected]>.
26+
*
27+
* For the full copyright and license information, please view
28+
* the LICENSE file that was distributed with this source code.
29+
*
30+
* @see https://github.com/guanguans/soar-php
31+
*/
32+
arch('will not use debugging functions')
33+
// ->throwsNoExceptions()
34+
->group(__DIR__, __FILE__)
35+
->expect([
36+
'dd',
37+
'die',
38+
'dump',
39+
'echo',
40+
'env',
41+
'env_explode',
42+
'env_getcsv',
43+
'exit',
44+
'print',
45+
'print_r',
46+
'printf',
47+
'ray',
48+
'trap',
49+
'var_dump',
50+
'var_export',
51+
'vprintf',
52+
])
53+
// ->each
54+
->not->toBeUsed()
55+
->ignoring([
56+
ComposerScripts::class,
57+
WithDumpable::class,
58+
]);

tests/Concerns/HasOptionsTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ public function __toString(): string
112112
],
113113
]));
114114

115-
expect($normalizedOptions)->each->toBeString();
116-
$this->assertMatchesJsonSnapshot($normalizedOptions);
115+
expect($normalizedOptions)->toMatchSnapshot()->each->toBeString();
117116
})->group(__DIR__, __FILE__);
118117

119118
it('will throw InvalidOptionException when normalize invalid dsn', function (): void {

0 commit comments

Comments
 (0)