2828- Invoke inaccessible methods to expand testing coverage.
2929
3030✅ ** Cross-Platform String Assertions**
31- - Eliminate false positives/ negatives caused by Windows vs. Unix line- ending differences.
31+ - Avoid false positives and negatives caused by Windows vs. Unix line ending differences.
3232- Normalize line endings for consistent string comparisons across platforms.
3333
3434✅ ** File System Test Management**
5050Install the extension.
5151
5252``` bash
53- composer require --dev --prefer-dist php-forge/support:^0.1
53+ composer require --dev --prefer-dist php-forge/support:^0.2
5454```
5555
5656#### Method 2: Manual installation
@@ -60,7 +60,7 @@ Add to your `composer.json`.
6060``` json
6161{
6262 "require-dev" : {
63- "php-forge/support" : " ^0.1 "
63+ "php-forge/support" : " ^0.2 "
6464 }
6565}
6666```
@@ -77,94 +77,133 @@ composer update
7777
7878``` php
7979<?php
80-
8180declare(strict_types=1);
8281
83- use PHPForge\Support\Assert;
82+ use PHPForge\Support\TestSupport;
83+ use PHPUnit\Framework\TestCase;
8484
85- $object = new class () {
86- private string $secretValue = 'hidden';
87- } ;
85+ final class AccessPrivatePropertyTest extends TestCase
86+ {
87+ use TestSupport ;
8888
89- // access private properties for testing
90- $value = Assert::inaccessibleProperty($object, 'secretValue');
89+ public function testInaccessibleProperty(): void
90+ {
91+ $object = new class () {
92+ private string $secretValue = 'hidden';
93+ };
9194
92- self::assertSame('hidden', $value);
95+ $value = self::inaccessibleProperty($object, 'secretValue');
96+
97+ self::assertSame('hidden', $value, "Should access the private property and return its value.");
98+ }
99+ }
93100```
94101
95- ### Equals without line ending
102+ ### Invoking protected methods
96103
97104``` php
98105<?php
99-
100106declare(strict_types=1);
101107
102- use PHPForge\Support\Assert;
108+ use PHPForge\Support\TestSupport;
109+ use PHPUnit\Framework\TestCase;
110+
111+ final class InvokeProtectedMethodTest extends TestCase
112+ {
113+ use TestSupport;
114+
115+ public function testInvokeMethod(): void
116+ {
117+ $object = new class () {
118+ protected function calculate(int $a, int $b): int
119+ {
120+ return $a + $b;
121+ }
122+ };
103123
104- // normalize line endings for consistent comparisons
105- Assert::equalsWithoutLE(
106- "Foo\r\nBar",
107- "Foo\nBar",
108- "Should match regardless of line ending style"
109- );
124+ $result = self::invokeMethod($object, 'calculate', [5, 3]);
125+
126+ self::assertSame(8, $result, "Should invoke the protected method and return the correct sum.");
127+ }
128+ }
110129```
111130
112- ### Invoking protected methods
131+ ### Normalize line endings
113132
114133``` php
115134<?php
116-
117135declare(strict_types=1);
118136
119- use PHPForge\Support\Assert;
137+ use PHPForge\Support\TestSupport;
138+ use PHPUnit\Framework\TestCase;
120139
121- $object = new class () {
122- protected function calculate(int $a, int $b): int
140+ final class NormalizeLineEndingsTest extends TestCase
141+ {
142+ use TestSupport;
143+
144+ public function testNormalizedComparison(): void
123145 {
124- return $a + $b;
146+ self::assertSame(
147+ self::normalizeLineEndings("Foo\r\nBar"),
148+ self::normalizeLineEndings("Foo\nBar"),
149+ "Should match regardless of line ending style",
150+ );
125151 }
126- };
127-
128- // test protected method behavior
129- $result = Assert::invokeMethod($object, 'calculate', [5, 3]);
130-
131- self::assertSame(8, $result);
152+ }
132153```
133154
134155### Remove files from directory
135156
136157``` php
137158<?php
138-
139159declare(strict_types=1);
140160
141- use PHPForge\Support\Assert;
161+ use PHPForge\Support\TestSupport;
162+ use PHPUnit\Framework\TestCase;
163+
164+ final class RemoveFilesFromDirectoryTest extends TestCase
165+ {
166+ use TestSupport;
167+
168+ public function testCleanup(): void
169+ {
170+ $testDir = dirname(__DIR__) . '/runtime';
171+ // clean up test artifacts (preserves '.gitignore' and '.gitkeep')
142172
143- $testDir = dirname(__DIR__) . '/runtime' ;
173+ self::removeFilesFromDirectory($testDir) ;
144174
145- // clean up test artifacts (preserves '.gitignore' and '.gitkeep')
146- Assert::removeFilesFromDirectory($testDir);
175+ self::assertTrue(true, "Should remove all files in the test directory while preserving Git-tracked files.");
176+ }
177+ }
147178```
148179
149180### Set inaccessible property
150181
151182``` php
152183<?php
153-
154184declare(strict_types=1);
155185
156- use PHPForge\Support\Assert;
186+ use PHPForge\Support\TestSupport;
187+ use PHPUnit\Framework\TestCase;
188+
189+ final class SetInaccessiblePropertyTest extends TestCase
190+ {
191+ use TestSupport;
157192
158- $object = new class () {
159- private string $config = 'default';
160- };
193+ public function testSetProperty(): void
194+ {
195+ $object = new class () {
196+ private string $config = 'default';
197+ };
161198
162- // set private property for testing scenarios
163- Assert ::setInaccessibleProperty($object, 'config', 'test-mode');
199+ // set private property for testing scenarios
200+ self ::setInaccessibleProperty($object, 'config', 'test-mode');
164201
165- $newValue = Assert ::inaccessibleProperty($object, 'config');
202+ $newValue = self ::inaccessibleProperty($object, 'config');
166203
167- self::assertSame('test-mode', $newValue);
204+ self::assertSame('test-mode', $newValue, "Should set the inaccessible property to 'test-mode'.");
205+ }
206+ }
168207```
169208
170209## Documentation
0 commit comments