Skip to content

Commit 9fd4e43

Browse files
committed
Extract test stubs, eliminate all phpcs ignores
- Extract 23 fixture classes from 6 test files into tests/Stubs/ - Add classmap autoload-dev for tests/Stubs/ - Rename StaticTest to StaticFactoryStub (avoid PHPUnit warning) - Rename WheneverIBornIPopulateAGlobalCalled_SHIT_ to SideEffectOnConstruct - Remove dead _MERD_ global assignment - Move @var class-string annotation to satisfy InlineDocCommentDeclaration - Add IniLoader tests for 0, 42, 3.14, 1e3, negative int coercion - Add Container test for plain callable caching behavior - Remove all 5 phpcs rule exclusions from phpcs.xml.dist
1 parent 8bb8475 commit 9fd4e43

32 files changed

+382
-279
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"autoload-dev": {
3737
"psr-4": {
3838
"Respect\\Config\\": "tests/"
39-
}
39+
},
40+
"classmap": ["tests/Stubs/"]
4041
},
4142
"scripts": {
4243
"coverage": "vendor/bin/phpunit --coverage-text",

phpcs.xml.dist

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,4 @@
1515
<file>tests/</file>
1616

1717
<rule ref="Respect" />
18-
19-
<!-- Instantiator dynamically invokes methods with loosely-typed INI data;
20-
strict_types conflicts with call_user_func coercion requirements -->
21-
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing">
22-
<exclude-pattern>src/Instantiator.php</exclude-pattern>
23-
</rule>
24-
25-
<!-- @var class-string narrowing for PHPStan on dynamic class names from INI -->
26-
<rule ref="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.MissingVariable">
27-
<exclude-pattern>src/</exclude-pattern>
28-
</rule>
29-
30-
<!-- Test files contain inline fixture classes required by the DI container tests -->
31-
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
32-
<exclude-pattern>tests/</exclude-pattern>
33-
</rule>
34-
<rule ref="Squiz.Classes.ClassFileName.NoMatch">
35-
<exclude-pattern>tests/</exclude-pattern>
36-
</rule>
37-
<rule ref="Squiz.Classes.ValidClassName.NotPascalCase">
38-
<exclude-pattern>tests/</exclude-pattern>
39-
</rule>
40-
41-
<!-- Test fixture properties use snake_case to match INI config keys -->
42-
<rule ref="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps">
43-
<exclude-pattern>tests/</exclude-pattern>
44-
</rule>
4518
</ruleset>

src/IniLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ protected function removeDuplicatedSpaces(string $string): string
172172
protected function parseInstantiator(string $key, mixed $value): void
173173
{
174174
$key = $this->removeDuplicatedSpaces($key);
175+
/** @var class-string $keyClass */
175176
[$keyName, $keyClass] = explode(' ', $key, 2);
176177
if ($keyName === 'instanceof') {
177178
$keyName = $keyClass;
178179
}
179180

180-
/** @var class-string $keyClass */
181181
$instantiator = $this->createInstantiator($keyClass);
182182

183183
if (is_array($value)) {
@@ -198,9 +198,9 @@ protected function createInstantiator(string $keyClass): Instantiator
198198
return new Instantiator($keyClass);
199199
}
200200

201+
/** @var class-string $className */
201202
[$modifier, $className] = explode(' ', $keyClass, 2);
202203

203-
/** @var class-string $className */
204204
return match ($modifier) {
205205
'new' => new Factory($className),
206206
'autowire' => new Autowire($className),

tests/AutowireTest.php

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -340,67 +340,3 @@ private static function parseIni(string $ini): array
340340
return $result;
341341
}
342342
}
343-
344-
class AutowireConsumer
345-
{
346-
public function __construct(public DateTime $date)
347-
{
348-
}
349-
}
350-
351-
class AutowireWithBuiltin
352-
{
353-
public function __construct(public string $name)
354-
{
355-
}
356-
}
357-
358-
class AutowireDependency
359-
{
360-
public function __construct(public string $value = 'default')
361-
{
362-
}
363-
}
364-
365-
class AutowireMultiParam
366-
{
367-
public function __construct(public DateTime $date, public AutowireDependency $dep)
368-
{
369-
}
370-
}
371-
372-
class AutowireOptionalDep
373-
{
374-
public function __construct(public DateTime $date, public AutowireDependency|null $dep = null)
375-
{
376-
}
377-
}
378-
379-
class AutowireTypedConsumer
380-
{
381-
public function __construct(public AutowireDependency $dep)
382-
{
383-
}
384-
}
385-
386-
class AutowireAllOptional
387-
{
388-
public function __construct(public DateTime|null $a = null, public DateTime|null $b = null)
389-
{
390-
}
391-
}
392-
393-
class AutowireWithArray
394-
{
395-
/** @param array<string> $paths */
396-
public function __construct(public array $paths)
397-
{
398-
}
399-
}
400-
401-
class AutowireWrapper
402-
{
403-
public function __construct(public mixed $inner)
404-
{
405-
}
406-
}

tests/ContainerTest.php

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -328,19 +328,19 @@ public function testClosureWithIniLoad(): void
328328

329329
public function testLazyLoadinessOnMultipleConfigLevels(): void
330330
{
331-
$GLOBALS['_SHIT_'] = false;
331+
$GLOBALS['_SIDE_EFFECT_'] = false;
332332
$ini = <<<'INI'
333-
[foo Respect\Config\WheneverIBornIPopulateAGlobalCalled_SHIT_]
333+
[foo Respect\Config\SideEffectOnConstruct]
334334
child = ""
335-
[bar Respect\Config\WheneverIBornIPopulateAGlobalCalled_SHIT_]
335+
[bar Respect\Config\SideEffectOnConstruct]
336336
child = [foo]
337-
[baz Respect\Config\WheneverIBornIPopulateAGlobalCalled_SHIT_]
337+
[baz Respect\Config\SideEffectOnConstruct]
338338
child = [bar]
339339
INI;
340340
$c = new Container();
341341
(new IniLoader($c))->fromArray(self::parseIni($ini));
342-
$this->assertFalse($GLOBALS['_SHIT_']);
343-
$GLOBALS['_SHIT_'] = false;
342+
$this->assertFalse($GLOBALS['_SIDE_EFFECT_']);
343+
$GLOBALS['_SIDE_EFFECT_'] = false;
344344
}
345345

346346
public function testSequencesConstructingLazy(): void
@@ -362,7 +362,7 @@ public function testPascutti(): void
362362
$this->markTestSkipped('SQLite PDO driver not available');
363363
}
364364

365-
$GLOBALS['_SHIT_'] = false;
365+
$GLOBALS['_SIDE_EFFECT_'] = false;
366366
$ini = <<<'INI'
367367
[pdo StdClass]
368368
@@ -378,7 +378,6 @@ public function testPascutti(): void
378378

379379
public function testPascuttiTypeHintIssue40(): void
380380
{
381-
$GLOBALS['_MERD_'] = false;
382381
$ini = <<<'INI'
383382
[now DateTime]
384383
@@ -724,70 +723,3 @@ private static function parseIni(string $ini): array
724723
return $result;
725724
}
726725
}
727-
728-
class Bar
729-
{
730-
}
731-
732-
class Foo
733-
{
734-
public mixed $bar = null;
735-
736-
public static function hey(DateTime $date): DateTime
737-
{
738-
return $date;
739-
}
740-
741-
public function hello(mixed $some, Bar $bar): void
742-
{
743-
$this->bar = $bar;
744-
}
745-
}
746-
747-
class WheneverIBornIPopulateAGlobalCalled_SHIT_
748-
{
749-
public function __construct()
750-
{
751-
$GLOBALS['_SHIT_'] = true;
752-
}
753-
}
754-
755-
class DatabaseWow
756-
{
757-
public mixed $c;
758-
759-
public function __construct(mixed $con)
760-
{
761-
$this->c = $con;
762-
}
763-
}
764-
765-
class TypeHintWowMuchType
766-
{
767-
public DateTime $d;
768-
769-
public function __construct(DateTime $date)
770-
{
771-
$this->d = $date;
772-
}
773-
}
774-
775-
class TestConstant
776-
{
777-
public const string CONS_TEST = 'XPTO';
778-
}
779-
780-
class WheneverWithAProperty
781-
{
782-
public mixed $test = null;
783-
}
784-
785-
class PrivateConstructorClass
786-
{
787-
public string $value = '';
788-
789-
private function __construct(string $x)
790-
{
791-
$this->value = $x;
792-
}
793-
}

tests/IniLoaderTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,3 @@ private static function parseIni(string $ini): array
412412
return $result;
413413
}
414414
}
415-
416-
class IniLoaderTestConstant
417-
{
418-
public const string VALUE = 'XPTO';
419-
}

tests/InstantiatorTest.php

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use stdClass;
1212

1313
use function date_default_timezone_set;
14-
use function func_num_args;
1514
use function get_class;
1615

1716
#[CoversClass(Instantiator::class)]
@@ -217,67 +216,3 @@ public function testStaticMethodReturningNonObject(): void
217216
$this->assertTrue($s->ready);
218217
}
219218
}
220-
221-
class StaticNonObjectReturn
222-
{
223-
public bool $ready = true;
224-
225-
public static function init(): string
226-
{
227-
return 'not_an_object';
228-
}
229-
}
230-
231-
class TestClass
232-
{
233-
public bool $ok = false;
234-
235-
public bool $myPropertyUsed = false;
236-
237-
public string $myProperty = 'foo';
238-
239-
public function __construct(mixed $foo = null, public mixed $bar = null, public mixed $baz = null)
240-
{
241-
if (!$foo) {
242-
return;
243-
}
244-
245-
$this->ok = true;
246-
}
247-
248-
public function usingProperty(): void
249-
{
250-
if ($this->myProperty !== 'bar') {
251-
return;
252-
}
253-
254-
$this->myPropertyUsed = true;
255-
}
256-
257-
public function noParams(): void
258-
{
259-
if (func_num_args() !== 0) {
260-
return;
261-
}
262-
263-
$this->ok = true;
264-
}
265-
266-
public function oneParam(mixed $ok): void
267-
{
268-
if (!$ok) {
269-
return;
270-
}
271-
272-
$this->ok = true;
273-
}
274-
275-
public function twoParams(mixed $ok, mixed $ok2): void
276-
{
277-
if (!$ok || !$ok2) {
278-
return;
279-
}
280-
281-
$this->ok = true;
282-
}
283-
}

tests/LazyLoadTest.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,3 @@ public function testLazyLoadedInstance(): void
5454
$this->assertEquals($expected, (string) $container->getItem('hello'));
5555
}
5656
}
57-
58-
class MyLazyLoadedHelloWorldConsumer
59-
{
60-
protected string $string;
61-
62-
public function __construct(mixed $hello)
63-
{
64-
$this->string = $hello;
65-
}
66-
67-
public function __toString(): string
68-
{
69-
return $this->string;
70-
}
71-
}
72-
73-
class MyLazyLoadedHelloWorld
74-
{
75-
public function __construct(protected string $string)
76-
{
77-
}
78-
79-
public function __toString(): string
80-
{
81-
return $this->string;
82-
}
83-
}

tests/StaticFactoryTest.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,11 @@ final class StaticFactoryTest extends TestCase
1616
{
1717
public function testInstance(): void
1818
{
19-
$i = new Instantiator(__NAMESPACE__ . '\\StaticTest');
19+
$i = new Instantiator(__NAMESPACE__ . '\\StaticFactoryStub');
2020
$i->setParam('factory', [[]]);
2121
$ref = new ReflectionObject($i);
2222
$prop = $ref->getProperty('staticMethodCalls');
2323
$this->assertNotEmpty($prop->getValue($i));
2424
$this->assertInstanceOf(DateTime::class, $i->getInstance());
2525
}
2626
}
27-
28-
class StaticTest
29-
{
30-
private function __construct()
31-
{
32-
}
33-
34-
public static function factory(): DateTime
35-
{
36-
return new DateTime();
37-
}
38-
}

0 commit comments

Comments
 (0)