Output of the "Modifying a private class property at runtime" PHP snippet

Environment:

  • Strangebuzz Snippet VM: 2.2.1
  • PHP: 7.4

Code execution output:

> Request value object class: Symfony\Component\HttpFoundation\Request
> isHostValid: true
> isHostValid: false

 Back to the code  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  More on Stackoverflow   Read the doc  Random snippet

<?php declare(strict_types=1);

namespace App\Tests\Controller\Snippets;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;

/**
 * @covers Snippet74Trait
 */
final class Snippet61Test extends KernelTestCase
{
    /**
     * @covers Snippet61Trait::snippet61
     */
    public function testSnippet61(): void
    {
        $request = Request::create('/en/snippets/modifying-a-private-class-property-at-runtime');
        $property = (new \ReflectionClass($request))->getProperty('isHostValid');

        try {
            self::assertNull($property->getValue($request));
            self::fail('A ReflectionException should be raised!');
        } /** @noinspection PhpRedundantCatchClauseInspection */ /** @noinspection BadExceptionsProcessingInspection */
        catch (\ReflectionException $e) {
        }

        $property->setAccessible(true);
        self::assertTrue($property->getValue($request));
        $property->setValue($request, false);
        self::assertFalse($property->getValue($request));
    }
}