diff --git a/.gitignore b/.gitignore index 6d5aba94..58efde94 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ /.phpunit.cache /vendor/ /bin/tools/*/vendor/ -/bin/tools/php-cs-fixer/composer.lock +/bin/tools/csfixer /build/ /.php-cs-fixer.cache /.phpunit.result.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 5fca00a6..12b58602 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -7,6 +7,12 @@ \file_get_contents('https://raw.githubusercontent.com/zenstruck/.github/main/.php-cs-fixer.dist.php') ); +$finder = PhpCsFixer\Finder::create() + ->in([__DIR__.'/src', __DIR__.'/tests']) + ->notName('KernelTestCaseWithOnlyResetDatabaseTraitTest.php') + ->notName('KernelTestCaseWithBothTraitsInWrongOrderTest.php') +; + try { return require $file; } finally { diff --git a/phpunit-10.xml.dist b/phpunit-10.xml.dist index b9758154..2ec36faf 100644 --- a/phpunit-10.xml.dist +++ b/phpunit-10.xml.dist @@ -18,6 +18,7 @@ tests tests/Integration/Migration/ResetDatabaseWithMigrationTest.php + tests/Fixture tests/Integration/Migration/ResetDatabaseWithMigrationTest.php diff --git a/phpunit-paratest.xml.dist b/phpunit-paratest.xml.dist index 387352e8..33027af9 100644 --- a/phpunit-paratest.xml.dist +++ b/phpunit-paratest.xml.dist @@ -17,6 +17,7 @@ tests tests/Integration/Migration/ResetDatabaseWithMigrationTest.php + tests/Fixture diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c6b9f26c..bee2840a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -20,6 +20,7 @@ tests tests/Integration/Migration/ResetDatabaseWithMigrationTest.php + tests/Fixture tests/Integration/Migration/ResetDatabaseWithMigrationTest.php diff --git a/src/Configuration.php b/src/Configuration.php index 1dae202a..3ac9ddc8 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -12,10 +12,12 @@ namespace Zenstruck\Foundry; use Faker; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Zenstruck\Foundry\Exception\FoundryNotBooted; use Zenstruck\Foundry\Exception\PersistenceDisabled; use Zenstruck\Foundry\Exception\PersistenceNotAvailable; use Zenstruck\Foundry\Persistence\PersistenceManager; +use Zenstruck\Foundry\Test\Factories; /** * @author Kevin Bond @@ -96,6 +98,8 @@ public static function isBooted(): bool /** @param \Closure():self|self $configuration */ public static function boot(\Closure|self $configuration): void { + self::throwIfComingFromKernelTestCaseWithoutFactoriesTrait(); + self::$instance = $configuration; } @@ -111,4 +115,20 @@ public static function shutdown(): void StoryRegistry::reset(); self::$instance = null; } + + private static function throwIfComingFromKernelTestCaseWithoutFactoriesTrait(): void + { + $backTrace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS); // @phpstan-ignore ekinoBannedCode.function + + foreach ($backTrace as $trace) { + if ( + isset($trace['class']) + && KernelTestCase::class !== $trace['class'] + && \is_a($trace['class'], KernelTestCase::class, true) + && !(new \ReflectionClass($trace['class']))->hasMethod('_bootFoundry') + ) { + throw new \LogicException(\sprintf('You must use the trait "%s" in "%s" in order to use Foundry.', Factories::class, $trace['class'])); + } + } + } } diff --git a/tests/Fixture/ForceFactoriesTraitUsage/KernelTestCaseWithOnlyResetDatabaseTraitTest.php b/tests/Fixture/ForceFactoriesTraitUsage/KernelTestCaseWithOnlyResetDatabaseTraitTest.php new file mode 100644 index 00000000..1cd1b3d7 --- /dev/null +++ b/tests/Fixture/ForceFactoriesTraitUsage/KernelTestCaseWithOnlyResetDatabaseTraitTest.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Foundry\Tests\Fixture\ForceFactoriesTraitUsage; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Zenstruck\Foundry\Test\ResetDatabase; + +final class KernelTestCaseWithOnlyResetDatabaseTraitTest extends KernelTestCase +{ + use ResetDatabase; + + public function test_using_foundry_without_trait_should_throw(): void + { + } +} diff --git a/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithBothTraitsInWrongOrderTest.php b/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithBothTraitsInWrongOrderTest.php new file mode 100644 index 00000000..6ac322d8 --- /dev/null +++ b/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithBothTraitsInWrongOrderTest.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Foundry\Tests\Integration\ForceFactoriesTraitUsage; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Zenstruck\Foundry\Test\Factories; +use Zenstruck\Foundry\Test\ResetDatabase; +use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; + +final class KernelTestCaseWithBothTraitsInWrongOrderTest extends KernelTestCase +{ + use ResetDatabase, Factories; + + /** + * @test + */ + public function should_not_throw(): void + { + Object1Factory::createOne(); + + $this->expectNotToPerformAssertions(); + } + + /** + * @test + */ + public function should_not_throw_even_when_kernel_is_booted(): void + { + self::getContainer(); + + Object1Factory::createOne(); + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithBothTraitsTest.php b/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithBothTraitsTest.php new file mode 100644 index 00000000..9324d38b --- /dev/null +++ b/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithBothTraitsTest.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Foundry\Tests\Integration\ForceFactoriesTraitUsage; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Zenstruck\Foundry\Test\Factories; +use Zenstruck\Foundry\Test\ResetDatabase; +use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; + +final class KernelTestCaseWithBothTraitsTest extends KernelTestCase +{ + use Factories, ResetDatabase; + + /** + * @test + */ + public function should_not_throw(): void + { + Object1Factory::createOne(); + + $this->expectNotToPerformAssertions(); + } + + /** + * @test + */ + public function should_not_throw_even_when_kernel_is_booted(): void + { + self::getContainer(); + + Object1Factory::createOne(); + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithFactoriesTraitTest.php b/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithFactoriesTraitTest.php new file mode 100644 index 00000000..9d7f90d5 --- /dev/null +++ b/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithFactoriesTraitTest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Foundry\Tests\Integration\ForceFactoriesTraitUsage; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Zenstruck\Foundry\Test\Factories; +use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; + +final class KernelTestCaseWithFactoriesTraitTest extends KernelTestCase +{ + use Factories; + + /** + * @test + */ + public function should_not_throw(): void + { + Object1Factory::createOne(); + + $this->expectNotToPerformAssertions(); + } + + /** + * @test + */ + public function should_not_throw_even_when_kernel_is_booted(): void + { + self::getContainer(); + + Object1Factory::createOne(); + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithoutFactoriesTraitTest.php b/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithoutFactoriesTraitTest.php new file mode 100644 index 00000000..829f80a1 --- /dev/null +++ b/tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithoutFactoriesTraitTest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Foundry\Tests\Integration\ForceFactoriesTraitUsage; + +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Zenstruck\Foundry\Exception\FoundryNotBooted; +use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; + +final class KernelTestCaseWithoutFactoriesTraitTest extends KernelTestCase +{ + /** + * @test + */ + public function using_foundry_without_trait_should_throw(): void + { + $this->expectException(FoundryNotBooted::class); + + Object1Factory::createOne(); + } + + /** + * @test + */ + public function using_foundry_without_trait_should_throw_even_when_kernel_is_booted(): void + { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('You must use the trait "Zenstruck\Foundry\Test\Factories"'); + + self::getContainer(); + + Object1Factory::createOne(); + } +} diff --git a/tests/Integration/ForceFactoriesTraitUsage/UnitTestCaseWithFactoriesTraitTest.php b/tests/Integration/ForceFactoriesTraitUsage/UnitTestCaseWithFactoriesTraitTest.php new file mode 100644 index 00000000..34077b9d --- /dev/null +++ b/tests/Integration/ForceFactoriesTraitUsage/UnitTestCaseWithFactoriesTraitTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Foundry\Tests\Integration\ForceFactoriesTraitUsage; + +use PHPUnit\Framework\TestCase; +use Zenstruck\Foundry\Test\Factories; +use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; + +final class UnitTestCaseWithFactoriesTraitTest extends TestCase +{ + use Factories; + + /** + * @test + */ + public function should_not_throw(): void + { + Object1Factory::createOne(); + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/Integration/ForceFactoriesTraitUsage/UnitTestCaseWithoutFactoriesTraitTest.php b/tests/Integration/ForceFactoriesTraitUsage/UnitTestCaseWithoutFactoriesTraitTest.php new file mode 100644 index 00000000..76bd41b7 --- /dev/null +++ b/tests/Integration/ForceFactoriesTraitUsage/UnitTestCaseWithoutFactoriesTraitTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Zenstruck\Foundry\Tests\Integration\ForceFactoriesTraitUsage; + +use PHPUnit\Framework\TestCase; +use Zenstruck\Foundry\Exception\FoundryNotBooted; +use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; + +final class UnitTestCaseWithoutFactoriesTraitTest extends TestCase +{ + /** + * @test + */ + public function should_throw(): void + { + $this->expectException(FoundryNotBooted::class); + + Object1Factory::createOne(); + } +} diff --git a/tests/Integration/ForceFactoriesTraitUsage/kernel_test_case_with_only_reset_database_trait.phpt b/tests/Integration/ForceFactoriesTraitUsage/kernel_test_case_with_only_reset_database_trait.phpt new file mode 100644 index 00000000..51327f0b --- /dev/null +++ b/tests/Integration/ForceFactoriesTraitUsage/kernel_test_case_with_only_reset_database_trait.phpt @@ -0,0 +1,38 @@ +--TEST-- +phpunit --version +--FILE-- +run($_SERVER['argv']); +?> +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Runtime: PHP %s + +E 1 / 1 (100%) + +Time: %s, Memory: %s MB + +There was 1 error: + +1) Zenstruck\Foundry\Tests\Fixture\ForceFactoriesTraitUsage\KernelTestCaseWithOnlyResetDatabaseTraitTest +LogicException: You must use the trait "Zenstruck\Foundry\Test\Factories" in "Zenstruck\Foundry\Tests\Fixture\ForceFactoriesTraitUsage\KernelTestCaseWithOnlyResetDatabaseTraitTest" in order to use Foundry. + +%s/src/Configuration.php:%d +%s/src/Configuration.php:%d +%s/src/ZenstruckFoundryBundle.php:%d +%s/vendor/symfony/http-kernel/Kernel.php:%d +%s/vendor/symfony/framework-bundle/Test/KernelTestCase.php:%d +%s/src/Test/ResetDatabase.php:%d +%s/src/Persistence/ResetDatabase/ResetDatabaseManager.php:%d +%s/src/Test/ResetDatabase.php:%d + +ERRORS! +Tests: 1, Assertions: 0, Errors: 1. diff --git a/tests/Integration/Maker/MakerTestCase.php b/tests/Integration/Maker/MakerTestCase.php index 1e830d3d..2984dd64 100644 --- a/tests/Integration/Maker/MakerTestCase.php +++ b/tests/Integration/Maker/MakerTestCase.php @@ -14,6 +14,7 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\String\Slugger\AsciiSlugger; +use Zenstruck\Foundry\Test\Factories; /** * @author Kevin Bond @@ -21,6 +22,8 @@ */ abstract class MakerTestCase extends KernelTestCase { + use Factories; + /** * @before */