Skip to content

Commit

Permalink
feat: throw an error if Factories trait is not used in a KernelTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Dec 21, 2024
1 parent 9810cbb commit b28e7cb
Show file tree
Hide file tree
Showing 15 changed files with 342 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions phpunit-10.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
<exclude>tests/Fixture</exclude>
</testsuite>
<testsuite name="migrate">
<file>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</file>
Expand Down
1 change: 1 addition & 0 deletions phpunit-paratest.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
<exclude>tests/Fixture</exclude>
</testsuite>
</testsuites>
<source ignoreSuppressionOfDeprecations="true">
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
<exclude>tests/Fixture</exclude>
</testsuite>
<testsuite name="migrate">
<file>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</file>
Expand Down
20 changes: 20 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <kevinbond@gmail.com>
Expand Down Expand Up @@ -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;
}

Expand All @@ -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']));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* 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
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* 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();
}
}
Loading

0 comments on commit b28e7cb

Please sign in to comment.