Sortie du snippet PHP "Modification d'une propriété privée de classe à l'exécution"

Environement :

  • Strangebuzz Snippet VM : 2.1.0
  • PHP : 7.4

Sortie du code exécuté :

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

 Retour au code  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  Plus sur Stackoverflow   Lire la doc  Snippet aléatoire

<?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 {
            $this->assertNull($property->getValue($request));
            $this->fail('A ReflectionException should be raised!');
        } /** @noinspection PhpRedundantCatchClauseInspection */ /** @noinspection BadExceptionsProcessingInspection */
        catch (\ReflectionException $e) {
        }

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