Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:

# old PHPUnit versions
- {php: 8.3, symfony: 7.4.*, phpunit: 9, database: mysql, use-phpunit-extension: 0}
- {php: 8.4, symfony: 7.4.*, phpunit: 10, database: mysql, use-phpunit-extension: 0}
- {php: 8.2, symfony: 7.4.*, phpunit: 10, database: mysql, use-phpunit-extension: 0}
- {php: 8.4, symfony: 7.4.*, phpunit: 11, database: mysql, use-phpunit-extension: 0}
- {php: 8.4, symfony: 7.4.*, phpunit: 11, database: mysql}
- {php: 8.4, symfony: 7.4.*, phpunit: 12, database: mysql, use-phpunit-extension: 0}
Expand Down
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
$csFixerConfig->setFinder(
$csFixerConfig->getFinder()
->notName('WebTestCaseWithBothTraitsInWrongOrderTest.php')
->notName('GenericFactoryUsingBeforeHooksAndResetDatabaseTraitTest.php')
->in(__DIR__.'/utils')
->in(__DIR__.'/config')
);
Expand Down
85 changes: 58 additions & 27 deletions src/Test/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,68 @@
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
trait Factories
{
/**
* @internal
* @before
*/
#[Before(5)]
public function _beforeHook(): void
if (!\method_exists(Before::class, '__construct')) { // @phpstan-ignore function.alreadyNarrowedType
trait Factories
{
if (FoundryExtension::isEnabled()) {
trigger_deprecation('zenstruck/foundry', '2.9', \sprintf('Trait %s is deprecated and will be removed in Foundry 3. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.9.md to upgrade.', Factories::class));
use CommonFactories;

return;
/**
* @internal
* @before
*/
#[Before]
public function _beforeHook(): void
{
$this->_bootFoundry();
}

if (FoundryExtension::shouldBeEnabled()) {
trigger_deprecation('zenstruck/foundry', '2.9', 'Not using Foundry\'s PHPUnit extension is deprecated and will throw an error in Foundry 3. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.9.md to upgrade.');
/**
* @internal
* @after
*/
#[After]
public static function _afterHook(): void
{
self::_shutdownFoundry();
}
}
} else {
trait Factories
{
use CommonFactories;

/** @internal */
#[Before(5)]
public function _beforeHook(): void
{
$this->_bootFoundry();
}

$this->_bootFoundry();
/** @internal */
#[After(5)]
public static function _afterHook(): void
{
self::_shutdownFoundry();
}
}
}

/**
* @internal
* @after
*/
#[After(5)]
public static function _shutdownFoundry(): void
/** @internal */
trait CommonFactories
{
/** @internal */
private function _bootFoundry(): void
{
if (FoundryExtension::isEnabled()) {
trigger_deprecation('zenstruck/foundry', '2.9', \sprintf('Trait %s is deprecated and will be removed in Foundry 3. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.9.md to upgrade.', Factories::class));

return;
}

Configuration::shutdown();
}
if (FoundryExtension::shouldBeEnabled()) {
trigger_deprecation('zenstruck/foundry', '2.9', 'Not using Foundry\'s PHPUnit extension is deprecated and will throw an error in Foundry 3. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.9.md to upgrade.');
}

/**
* @internal
*/
private function _bootFoundry(): void
{
if (!\is_subclass_of(static::class, KernelTestCase::class)) { // @phpstan-ignore function.impossibleType, function.alreadyNarrowedType
// unit test
Configuration::boot(UnitTestConfig::build());
Expand All @@ -77,4 +98,14 @@ private function _bootFoundry(): void
return static::getContainer()->get('.zenstruck_foundry.configuration'); // @phpstan-ignore staticMethod.notFound, return.type
});
}

/** @internal */
private static function _shutdownFoundry(): void
{
if (FoundryExtension::isEnabled()) {
return;
}

Configuration::shutdown();
}
}
82 changes: 52 additions & 30 deletions src/Test/ResetDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,37 @@
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
trait ResetDatabase
if (!\method_exists(Before::class, '__construct')) { // @phpstan-ignore function.alreadyNarrowedType
trait ResetDatabase
{
use CommonResetDatabase;

/**
* @internal
* @before
*/
#[Before]
public static function _resetDatabaseBeforeEachTest(): void
{
self::_commonResetDatabaseBeforeEachTest();
}
}
} else {
trait ResetDatabase
{
use CommonResetDatabase;

/** @internal */
#[Before(10)]
public static function _resetDatabaseBeforeEachTest(): void
{
self::_commonResetDatabaseBeforeEachTest();
}
}
}

/** @internal */
trait CommonResetDatabase
{
/**
* @internal
Expand All @@ -45,32 +75,7 @@ public static function _resetDatabaseBeforeFirstTest(): void
static::_shutdown(); // @phpstan-ignore staticClassAccess.privateMethod
}

/**
* @internal
* @before
*/
#[Before(10)]
public static function _resetDatabaseBeforeEachTest(): void
{
if (FoundryExtension::isEnabled()) {
return;
}

if (ResetDatabaseManager::canSkipSchemaReset()) {
// can fully skip booting the kernel
return;
}

$kernel = static::_boot(); // @phpstan-ignore staticClassAccess.privateMethod

ResetDatabaseManager::resetBeforeEachTest($kernel);

static::_shutdown(); // @phpstan-ignore staticClassAccess.privateMethod
}

/**
* @internal
*/
/** @internal */
private static function _boot(): KernelInterface
{
if (!\is_subclass_of(static::class, KernelTestCase::class)) { // @phpstan-ignore function.alreadyNarrowedType
Expand All @@ -88,9 +93,7 @@ private static function _boot(): KernelInterface
return $kernel;
}

/**
* @internal
*/
/** @internal */
private static function _shutdown(): void
{
if (!\is_subclass_of(static::class, KernelTestCase::class)) { // @phpstan-ignore function.alreadyNarrowedType
Expand All @@ -100,4 +103,23 @@ private static function _shutdown(): void
Configuration::shutdown();
static::ensureKernelShutdown();
}

/** @internal */
private static function _commonResetDatabaseBeforeEachTest(): void
{
if (FoundryExtension::isEnabled()) {
return;
}

if (ResetDatabaseManager::canSkipSchemaReset()) {
// can fully skip booting the kernel
return;
}

$kernel = static::_boot(); // @phpstan-ignore staticClassAccess.privateMethod

ResetDatabaseManager::resetBeforeEachTest($kernel);

static::_shutdown(); // @phpstan-ignore staticClassAccess.privateMethod
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static function provideContact(): iterable
#[DataProvider('provideContactWithLegacyProxy')]
#[AsInMemoryTest]
#[RequiresMethod(\Symfony\Component\VarExporter\LazyProxyTrait::class, 'createLazyProxy')]
#[IgnoreDeprecations('(p|P)roxy')]
#[IgnoreDeprecations]
public function it_can_create_in_memory_objects_in_data_provider_with_legacy_proxy(?Contact $contact = null): void
{
self::assertInstanceOf(Contact::class, $contact);
Expand Down
63 changes: 35 additions & 28 deletions tests/Integration/Faker/ResetFakerTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,50 @@
namespace Zenstruck\Foundry\Tests\Integration\Faker;

use PHPUnit\Framework\Attributes\AfterClass;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\BeforeClass;
use Zenstruck\Foundry\FakerAdapter;

trait ResetFakerTestTrait
{
private static ?string $savedServerSeed = null;
private static ?string $savedEnvSeed = null;
private static ?string $savedGetEnvSeed = null;

private static ?int $currentSeed = null;

#[BeforeClass(10)]
public static function __saveAndResetFakerSeed(): void
if (\method_exists(Before::class, '__construct')) { // @phpstan-ignore function.alreadyNarrowedType
trait ResetFakerTestTrait
{
self::$savedServerSeed = $_SERVER['FOUNDRY_FAKER_SEED'] ?? null;
self::$savedEnvSeed = $_ENV['FOUNDRY_FAKER_SEED'] ?? null;
self::$savedGetEnvSeed = \getenv('FOUNDRY_FAKER_SEED') ?: null;
private static ?string $savedServerSeed = null;
private static ?string $savedEnvSeed = null;
private static ?string $savedGetEnvSeed = null;

$_SERVER['FOUNDRY_FAKER_SEED'] = null;
$_ENV['FOUNDRY_FAKER_SEED'] = null;
\putenv('FOUNDRY_FAKER_SEED');
private static ?int $currentSeed = null;

FakerAdapter::resetFakerSeed();
}
#[BeforeClass(10)]
public static function __saveAndResetFakerSeed(): void
{
self::$savedServerSeed = $_SERVER['FOUNDRY_FAKER_SEED'] ?? null;
self::$savedEnvSeed = $_ENV['FOUNDRY_FAKER_SEED'] ?? null;
self::$savedGetEnvSeed = \getenv('FOUNDRY_FAKER_SEED') ?: null;

#[AfterClass(-10)]
public static function __restoreFakerSeed(): void
{
$_SERVER['FOUNDRY_FAKER_SEED'] = self::$savedServerSeed;
$_ENV['FOUNDRY_FAKER_SEED'] = self::$savedEnvSeed;
if (null === self::$savedGetEnvSeed) {
$_SERVER['FOUNDRY_FAKER_SEED'] = null;
$_ENV['FOUNDRY_FAKER_SEED'] = null;
\putenv('FOUNDRY_FAKER_SEED');
} else {
\putenv('FOUNDRY_FAKER_SEED='.self::$savedGetEnvSeed);

FakerAdapter::resetFakerSeed();
}

$savedValue = self::$savedServerSeed ?? self::$savedEnvSeed ?? self::$savedGetEnvSeed;
FakerAdapter::resetFakerSeed($savedValue ? (int) $savedValue : null);
#[AfterClass(-10)]
public static function __restoreFakerSeed(): void
{
$_SERVER['FOUNDRY_FAKER_SEED'] = self::$savedServerSeed;
$_ENV['FOUNDRY_FAKER_SEED'] = self::$savedEnvSeed;
if (null === self::$savedGetEnvSeed) {
\putenv('FOUNDRY_FAKER_SEED');
} else {
\putenv('FOUNDRY_FAKER_SEED='.self::$savedGetEnvSeed);
}

$savedValue = self::$savedServerSeed ?? self::$savedEnvSeed ?? self::$savedGetEnvSeed;
FakerAdapter::resetFakerSeed($savedValue ? (int) $savedValue : null);
}
}
} else {
trait ResetFakerTestTrait
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
* file that was distributed with this source code.
*/

namespace Integration\Persistence;
namespace Zenstruck\Foundry\Tests\Integration\Persistence;

use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
use Zenstruck\Foundry\Attribute\ResetDatabase;
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
use Zenstruck\Foundry\Tests\Integration\Persistence\GenericFactoryUsingBeforeHooksTestCase;

/**
* @author Nicolas PHILIPPE <nikophil@gmail.com>
Expand All @@ -26,4 +25,5 @@
#[ResetDatabase]
final class GenericFactoryUsingBeforeHooksAndResetDatabaseAttributeTest extends GenericFactoryUsingBeforeHooksTestCase
{
use GenericFactoryUsingBeforeHooksTrait;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

namespace Zenstruck\Foundry\Tests\Integration\Persistence;

use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

/**
* @author Nicolas PHILIPPE <nikophil@gmail.com>
* @group legacy
*/
#[IgnoreDeprecations]
final class GenericFactoryUsingBeforeHooksAndResetDatabaseTraitTest extends GenericFactoryUsingBeforeHooksTestCase
{
use Factories, ResetDatabase;
use GenericFactoryUsingBeforeHooksTrait, Factories, ResetDatabase;
}
Loading
Loading