-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: allow to create objects in dataProvider thanks to lazy proxy
- Loading branch information
Showing
21 changed files
with
485 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\PHPUnit; | ||
|
||
use App\Infrastructure\Common\Symfony\Kernel; | ||
use PHPUnit\Event; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Zenstruck\Foundry\Configuration; | ||
use Zenstruck\Foundry\Test\UnitTestConfig; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class BootFoundryOnDataProviderMethodCalled implements Event\Test\DataProviderMethodCalledSubscriber | ||
{ | ||
public function __construct( | ||
private KernelInterface $kernel | ||
) | ||
{ | ||
} | ||
|
||
public function notify(Event\Test\DataProviderMethodCalled $event): void | ||
{ | ||
if (is_a($event->testMethod()->className(), KernelTestCase::class, allow_string: true)) { | ||
static $kernelIsBooted = false; | ||
|
||
if (!$kernelIsBooted) { | ||
$this->kernel->boot(); | ||
$kernelIsBooted = true; | ||
} | ||
|
||
Configuration::boot( | ||
fn() => $this->kernel->getContainer()->get('.zenstruck_foundry.configuration'), | ||
bootForDataProvider: true | ||
); | ||
} else { | ||
Configuration::boot(UnitTestConfig::build(), bootForDataProvider: true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\PHPUnit; | ||
|
||
use PHPUnit\Metadata\Version\ConstraintRequirement; | ||
use PHPUnit\Runner; | ||
use PHPUnit\TextUI; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Zenstruck\Foundry\Configuration; | ||
|
||
final class Extension implements Runner\Extension\Extension | ||
{ | ||
const MIN_PHPUNIT_VERSION = '11.4'; | ||
|
||
public function bootstrap( | ||
TextUI\Configuration\Configuration $configuration, | ||
Runner\Extension\Facade $facade, | ||
Runner\Extension\ParameterCollection $parameters | ||
): void { | ||
if (!ConstraintRequirement::from(self::MIN_PHPUNIT_VERSION)->isSatisfiedBy(Runner\Version::id())) { | ||
throw new \LogicException( | ||
sprintf( | ||
'Your PHPUnit version (%s) is not compatible with the minimum version (%s) needed to use this extension.', | ||
Runner\Version::id(), | ||
self::MIN_PHPUNIT_VERSION | ||
) | ||
); | ||
} | ||
|
||
// shutdown Foundry if for some reason it has been booted before | ||
if (Configuration::isBooted()) { | ||
Configuration::shutdown(); | ||
} | ||
|
||
$kernel = $this->createKernel(); | ||
|
||
$facade->registerSubscribers( | ||
new BootFoundryOnDataProviderMethodCalled($kernel), | ||
new ShutdownKernelOnTestSuiteLoaded($kernel) | ||
); | ||
} | ||
|
||
/** | ||
* This logic was shamelessly stolen from Symfony's KernelTestCase | ||
*/ | ||
private function createKernel(): KernelInterface | ||
{ | ||
if (!isset($_SERVER['KERNEL_CLASS']) && !isset($_ENV['KERNEL_CLASS'])) { | ||
throw new \LogicException('You must set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel in phpunit.xml / phpunit.xml.dist.'); | ||
} | ||
|
||
if (!class_exists($class = $_ENV['KERNEL_CLASS'] ?? $_SERVER['KERNEL_CLASS'])) { | ||
throw new \RuntimeException( | ||
sprintf( | ||
'Class "%s" doesn\'t exist or cannot be autoloaded. Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel.', | ||
$class | ||
) | ||
); | ||
} | ||
|
||
$env = $options['environment'] ?? $_ENV['APP_ENV'] ?? $_SERVER['APP_ENV'] ?? 'test'; | ||
$debug = $options['debug'] ?? $_ENV['APP_DEBUG'] ?? $_SERVER['APP_DEBUG'] ?? true; | ||
|
||
return new $class($env, $debug); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\PHPUnit; | ||
|
||
use PHPUnit\Event; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Zenstruck\Foundry\Configuration; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class ShutdownKernelOnTestSuiteLoaded implements Event\TestSuite\LoadedSubscriber | ||
{ | ||
public function __construct( | ||
private KernelInterface $kernel | ||
) { | ||
} | ||
|
||
public function notify(Event\TestSuite\Loaded $event): void | ||
{ | ||
$this->kernel->shutdown(); | ||
Configuration::shutdown(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,14 @@ | |
use Symfony\Component\VarExporter\LazyObjectInterface; | ||
use Symfony\Component\VarExporter\LazyProxyTrait; | ||
use Symfony\Component\VarExporter\ProxyHelper; | ||
use Zenstruck\Foundry\Factory; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
* @internal | ||
* | ||
* @phpstan-import-type Attributes from Factory | ||
*/ | ||
final class ProxyGenerator | ||
{ | ||
|
@@ -43,6 +46,19 @@ public static function wrap(object $object): Proxy | |
return self::generateClassFor($object)::createLazyProxy(static fn() => $object); // @phpstan-ignore-line | ||
} | ||
|
||
/** | ||
* @template T of object | ||
* | ||
* @param PersistentProxyObjectFactory<T> $factory | ||
* @phpstan-param Attributes $attributes | ||
* | ||
* @return T&Proxy<T> | ||
*/ | ||
public static function wrapFactory(PersistentProxyObjectFactory $factory, callable|array $attributes): Proxy | ||
{ | ||
return self::generateClassFor($factory)::createLazyProxy(static fn() => $factory->create($attributes)); // @phpstan-ignore-line | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
|
@@ -76,8 +92,8 @@ public static function unwrap(mixed $what): mixed | |
*/ | ||
private static function generateClassFor(object $object): string | ||
{ | ||
/** @var class-string $class */ | ||
$class = $object instanceof DoctrineProxy ? \get_parent_class($object) : $object::class; | ||
$class = self::extractClassName($object); | ||
|
||
$proxyClass = self::proxyClassNameFor($class); | ||
|
||
/** @var class-string<LazyObjectInterface&Proxy<T>&T> $proxyClass */ | ||
|
@@ -108,4 +124,16 @@ public static function proxyClassNameFor(string $class): string | |
{ | ||
return \str_replace('\\', '', $class).'Proxy'; | ||
} | ||
|
||
/** | ||
* @return class-string | ||
*/ | ||
private static function extractClassName(object $object): string | ||
{ | ||
if ($object instanceof PersistentProxyObjectFactory) { | ||
return $object::class(); | ||
} | ||
|
||
return $object instanceof DoctrineProxy ? \get_parent_class($object) : $object::class; // @phpstan-ignore return.type | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.