Repository: symfony/symfony
File: src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php (PHP)

Code before:
 */

namespace Symfony\Component\VarExporter\Tests;

use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\VarExporter\Exception\LogicException;
use Symfony\Component\VarExporter\ProxyHelper;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\RegularClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\AbstractHooked;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\AsymmetricVisibility;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\ConcreteReadOnlyClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\FinalPublicClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\Hooked;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\ReadOnlyClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\StringMagicGetClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestOverwritePropClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestUnserializeClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestWakeupClass;
use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject;

class LazyProxyTraitTest extends TestCase
{
    #[RequiresPhp('>=8.4')]
    public function testGetter()
    {
        $initCounter = 0;
        $proxy = $this->createLazyProxy(TestClass::class, function () use (&$initCounter) {
            ++$initCounter;

            return new TestClass((object) ['hello' => 'world']);
        });

        $this->assertInstanceOf(TestClass::class, $proxy);
        $this->assertSame(0, $initCounter);
        $this->assertFalse($proxy->isLazyObjectInitialized());

        $dep1 = $proxy->getDep();
        $this->assertTrue($proxy->isLazyObjectInitialized());
        $this->assertSame(1, $initCounter);

        $this->assertTrue($proxy->resetLazyObject());
        $this->assertSame(1, $initCounter);

        $dep2 = $proxy->getDep();
        $this->assertSame(2, $initCounter);
        $this->assertNotSame($dep1, $dep2);

Review comment (suggestion):
I removed `#[RequiresPhp('8.4')]` from the class and added `#[RequiresPhp(...)]` individually to each method. This is because one of the methods requires only PHP 8.3 (`#[RequiresPhp('>=8.3')])`, and if we annotate the whole class with `#[RequiresPhp('8.4')]`, that method would be skipped, even when running on PHP 8.3.

Code after:
 */

namespace Symfony\Component\VarExporter\Tests;

use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\VarExporter\Exception\LogicException;
use Symfony\Component\VarExporter\ProxyHelper;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\RegularClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\AbstractHooked;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\AsymmetricVisibility;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\ConcreteReadOnlyClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\FinalPublicClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\Hooked;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\ReadOnlyClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\StringMagicGetClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestOverwritePropClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestUnserializeClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestWakeupClass;
use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject;

#[RequiresPhp('>=8.4')]
class LazyProxyTraitTest extends TestCase
{
    public function testGetter()
    {
        $initCounter = 0;
        $proxy = $this->createLazyProxy(TestClass::class, function () use (&$initCounter) {
            ++$initCounter;

            return new TestClass((object) ['hello' => 'world']);
        });

        $this->assertInstanceOf(TestClass::class, $proxy);
        $this->assertSame(0, $initCounter);
        $this->assertFalse($proxy->isLazyObjectInitialized());

        $dep1 = $proxy->getDep();
        $this->assertTrue($proxy->isLazyObjectInitialized());
        $this->assertSame(1, $initCounter);

        $this->assertTrue($proxy->resetLazyObject());
        $this->assertSame(1, $initCounter);

        $dep2 = $proxy->getDep();
        $this->assertSame(2, $initCounter);
        $this->assertNotSame($dep1, $dep2);