Skip to content

Commit

Permalink
minor: add unproxy function (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored Jan 3, 2025
1 parent e228f35 commit dcf09e8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion phpunit.dama.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ini name="error_reporting" value="-1"/>
<env name="KERNEL_CLASS" value="Zenstruck\Foundry\Tests\Fixtures\Kernel"/>
<env name="USE_DAMA_DOCTRINE_TEST_BUNDLE" value="1"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0&amp;quiet[]=indirect&amp;quiet[]=other"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=99999&amp;max[direct]=99999&amp;quiet[]=indirect&amp;quiet[]=other"/>
<env name="SHELL_VERBOSITY" value="0"/>
</php>

Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<php>
<ini name="error_reporting" value="-1"/>
<env name="KERNEL_CLASS" value="Zenstruck\Foundry\Tests\Fixtures\Kernel"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0&amp;quiet[]=indirect&amp;quiet[]=other"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=99999&amp;max[direct]=99999&amp;quiet[]=indirect&amp;quiet[]=other"/>
<env name="SHELL_VERBOSITY" value="0"/>
</php>

Expand Down
16 changes: 16 additions & 0 deletions src/Persistence/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,19 @@ function enable_persisting(): void
{
Factory::configuration()->enablePersist();
}

/**
* Recursively unwrap all proxies.
*/
function unproxy(mixed $what): mixed
{
if ($what instanceof Proxy) {
return $what->_real();
}

if (\is_array($what)) {
return \array_map('Zenstruck\Foundry\Persistence\unproxy', $what);
}

return $what;
}
22 changes: 22 additions & 0 deletions tests/Unit/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category;
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post;

use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory;

use function Zenstruck\Foundry\create;
use function Zenstruck\Foundry\create_many;
use function Zenstruck\Foundry\faker;
Expand All @@ -32,6 +34,7 @@
use function Zenstruck\Foundry\object;
use function Zenstruck\Foundry\Persistence\disable_persisting;
use function Zenstruck\Foundry\Persistence\enable_persisting;
use function Zenstruck\Foundry\Persistence\unproxy;
use function Zenstruck\Foundry\repository;

/**
Expand Down Expand Up @@ -164,4 +167,23 @@ public function enable_or_disable_persisting_can_be_called_without_doctrine(): v
enable_persisting();
disable_persisting();
}

/**
* @test
*/
public function it_can_unproxy_all_the_stuff(): void
{
self::assertInstanceOf(Proxy::class, $post = PostFactory::createOne());
self::assertInstanceOf(Post::class, unproxy($post));
self::assertInstanceOf(Post::class, unproxy(unproxy($post)));

$posts = PostFactory::createMany(2);
foreach (unproxy($posts) as $post) {
self::assertInstanceOf(Post::class, $post);
}

$stdClass = new \stdClass();
self::assertSame($stdClass, unproxy($stdClass));
self::assertSame(1, unproxy(1));
}
}

0 comments on commit dcf09e8

Please sign in to comment.