-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(faker): load faker seed from env var
- Loading branch information
Showing
14 changed files
with
293 additions
and
37 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
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,2 @@ | ||
parameters: | ||
env(FOUNDRY_FAKER_SEED): 1234 |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
tests/Integration/Faker/FakerSeedAutomaticallySetKernelTest.php
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\Integration\Faker; | ||
|
||
use PHPUnit\Framework\Attributes\Depends; | ||
use PHPUnit\Framework\Attributes\RequiresPhpunit; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Configuration; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Test\ResetDatabase; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* @requires PHPUnit >=11.0 | ||
*/ | ||
#[RequiresPhpunit('>=11.0')] | ||
final class FakerSeedAutomaticallySetKernelTest extends KernelTestCase | ||
{ | ||
use Factories, ResetDatabase, FakerTestTrait; | ||
|
||
#[Test] | ||
public function faker_seed_does_not_change(): void | ||
{ | ||
self::$currentSeed = Configuration::fakerSeed(); | ||
|
||
self::assertSame(self::$currentSeed, Configuration::fakerSeed()); | ||
} | ||
|
||
#[Test] | ||
#[Depends('faker_seed_does_not_change')] | ||
public function faker_seed_does_not_change_between_tests(): void | ||
{ | ||
self::assertSame(self::$currentSeed, Configuration::fakerSeed()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
tests/Integration/Faker/FakerSeedAutomaticallySetUnitTest.php
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\Integration\Faker; | ||
|
||
use PHPUnit\Framework\Attributes\Depends; | ||
use PHPUnit\Framework\Attributes\RequiresPhpunit; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
use Zenstruck\Foundry\Configuration; | ||
use Zenstruck\Foundry\Test\Factories; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* @requires PHPUnit >=11.0 | ||
*/ | ||
#[RequiresPhpunit('>=11.0')] | ||
final class FakerSeedAutomaticallySetUnitTest extends TestCase | ||
{ | ||
use Factories, FakerTestTrait; | ||
|
||
#[Test] | ||
public function faker_seed_does_not_change(): void | ||
{ | ||
self::$currentSeed = Configuration::fakerSeed(); | ||
|
||
self::assertSame(self::$currentSeed, Configuration::fakerSeed()); | ||
} | ||
|
||
#[Test] | ||
#[Depends('faker_seed_does_not_change')] | ||
public function faker_seed_does_not_change_between_tests(): void | ||
{ | ||
self::assertSame(self::$currentSeed, Configuration::fakerSeed()); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/Integration/Faker/FakerSeedSetFromEnvVarKernelTest.php
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\Integration\Faker; | ||
|
||
use PHPUnit\Framework\Attributes\Depends; | ||
use PHPUnit\Framework\Attributes\RequiresPhpunit; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Configuration; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Test\ResetDatabase; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Address\AddressFactory; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* @requires PHPUnit >=11.0 | ||
*/ | ||
#[RequiresPhpunit('>=11.0')] | ||
final class FakerSeedSetFromEnvVarKernelTest extends KernelTestCase | ||
{ | ||
use Factories, ResetDatabase, FakerTestTrait; | ||
|
||
#[Test] | ||
public function faker_seed_can_be_set_by_environment_variable(): void | ||
{ | ||
// let's fake, we're starting from a fresh kernel | ||
$this->tearDown(); | ||
Configuration::shutdown(); | ||
Configuration::resetFakerSeed(); | ||
|
||
self::bootKernel(['environment' => 'faker_seed_env_var']); | ||
|
||
self::assertSame(1234, Configuration::fakerSeed()); | ||
|
||
self::assertSame('Baileyshire', AddressFactory::createOne()->getCity()); | ||
} | ||
|
||
#[Test] | ||
#[Depends('faker_seed_can_be_set_by_environment_variable')] | ||
public function faker_seed_is_already_set(): void | ||
{ | ||
self::assertSame(1234, Configuration::fakerSeed()); | ||
|
||
self::assertSame('Baileyshire', AddressFactory::createOne()->getCity()); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
tests/Integration/Faker/FakerSeedSetFromEnvVarUnitTest.php
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\Integration\Faker; | ||
|
||
use PHPUnit\Framework\Attributes\AfterClass; | ||
use PHPUnit\Framework\Attributes\Before; | ||
use PHPUnit\Framework\Attributes\BeforeClass; | ||
use PHPUnit\Framework\Attributes\Depends; | ||
use PHPUnit\Framework\Attributes\RequiresPhpunit; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
use Zenstruck\Foundry\Configuration; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Address\AddressFactory; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* @requires PHPUnit >=11.0 | ||
*/ | ||
#[RequiresPhpunit('>=11.0')] | ||
final class FakerSeedSetFromEnvVarUnitTest extends TestCase | ||
{ | ||
use Factories, FakerTestTrait; | ||
|
||
#[Before(10)] | ||
public static function __setEnv(): void | ||
{ | ||
$_ENV['FOUNDRY_FAKER_SEED'] = $_SERVER['FOUNDRY_FAKER_SEED'] = '1234'; | ||
} | ||
|
||
#[Test] | ||
public function faker_seed_is_set_from_env_var(): void | ||
{ | ||
self::assertSame(1234, Configuration::fakerSeed()); | ||
|
||
self::assertSame('Baileyshire', AddressFactory::createOne()->getCity()); | ||
} | ||
|
||
#[Test] | ||
#[Depends('faker_seed_is_set_from_env_var')] | ||
public function faker_seed_does_not_change(): void | ||
{ | ||
self::assertSame(1234, Configuration::fakerSeed()); | ||
|
||
self::assertSame('Baileyshire', AddressFactory::createOne()->getCity()); | ||
} | ||
|
||
#[AfterClass(-9)] // @phpstan-ignore argument.type (negative priority should be allowed) | ||
public static function __resetFakerSeedEnv(): void | ||
{ | ||
unset($_ENV['FOUNDRY_FAKER_SEED'], $_SERVER['FOUNDRY_FAKER_SEED']); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/Integration/Faker/FakerSeedSetFromLegacyConfigKernelTest.php
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\Integration\Faker; | ||
|
||
use PHPUnit\Framework\Attributes\Depends; | ||
use PHPUnit\Framework\Attributes\IgnoreDeprecations; | ||
use PHPUnit\Framework\Attributes\RequiresPhpunit; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Configuration; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Test\ResetDatabase; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Address\AddressFactory; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* @requires PHPUnit >=11.0 | ||
*/ | ||
#[RequiresPhpunit('>=11.0')] | ||
final class FakerSeedSetFromLegacyConfigKernelTest extends KernelTestCase | ||
{ | ||
use Factories, ResetDatabase, FakerTestTrait; | ||
|
||
#[Test] | ||
#[IgnoreDeprecations] | ||
public function test_faker_seed_by_configuration_is_deprecated(): void | ||
{ | ||
// let's fake, we're starting from a fresh kernel | ||
$this->tearDown(); | ||
Configuration::shutdown(); | ||
Configuration::resetFakerSeed(); | ||
|
||
self::bootKernel(['environment' => 'faker_seed_legacy_config']); | ||
|
||
self::expectUserDeprecationMessageMatches( | ||
'/The "faker.seed" configuration is deprecated and will be removed in 3.0/' | ||
); | ||
|
||
self::assertSame(1234, Configuration::fakerSeed()); | ||
|
||
self::assertSame('Baileyshire', AddressFactory::createOne()->getCity()); | ||
} | ||
|
||
#[Test] | ||
#[Depends('test_faker_seed_by_configuration_is_deprecated')] | ||
public function faker_seed_is_already_set(): void | ||
{ | ||
self::assertSame(1234, Configuration::fakerSeed()); | ||
|
||
self::assertSame('Baileyshire', AddressFactory::createOne()->getCity()); | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\Integration\Faker; | ||
|
||
use PHPUnit\Framework\Attributes\AfterClass; | ||
use PHPUnit\Framework\Attributes\BeforeClass; | ||
use Zenstruck\Foundry\Configuration; | ||
|
||
trait FakerTestTrait | ||
{ | ||
private static int|null $currentSeed = null; | ||
private static int|null $savedSeed = null; | ||
|
||
#[BeforeClass(10)] | ||
public static function __saveAndResetFakerSeed(): void | ||
{ | ||
self::$savedSeed = Configuration::fakerSeed(); | ||
|
||
self::$currentSeed = null; | ||
Configuration::resetFakerSeed(); | ||
} | ||
|
||
#[AfterClass(-10)] // @phpstan-ignore argument.type (negative priority should be allowed) | ||
public static function __restoreSeed(): void | ||
{ | ||
Configuration::resetFakerSeed(); | ||
Configuration::fakerSeed(self::$savedSeed); | ||
} | ||
} |