-
-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: throw an error if Factories trait is not used in a KernelTestCase #766
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Exception; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Test\Factories; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class FactoriesTraitNotUsed extends \LogicException | ||
{ | ||
/** | ||
* @param class-string<KernelTestCase> $class | ||
*/ | ||
private function __construct(string $class) | ||
{ | ||
parent::__construct( | ||
\sprintf('You must use the trait "%s" in "%s" in order to use Foundry.', Factories::class, $class) | ||
); | ||
} | ||
|
||
public static function throwIfComingFromKernelTestCaseWithoutFactoriesTrait(): void | ||
{ | ||
$backTrace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS); // @phpstan-ignore ekinoBannedCode.function | ||
|
||
foreach ($backTrace as $trace) { | ||
if ( | ||
'->' === ($trace['type'] ?? null) | ||
&& isset($trace['class']) | ||
&& KernelTestCase::class !== $trace['class'] | ||
&& \is_a($trace['class'], KernelTestCase::class, allow_string: true) | ||
&& !(new \ReflectionClass($trace['class']))->hasMethod('_bootFoundry') | ||
) { | ||
self::throwIfClassDoesNotHaveFactoriesTrait($trace['class']); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param class-string<KernelTestCase> $class | ||
*/ | ||
public static function throwIfClassDoesNotHaveFactoriesTrait(string $class): void | ||
{ | ||
if (!(new \ReflectionClass($class))->hasMethod('_bootFoundry')) { | ||
// throw new self($class); | ||
trigger_deprecation( | ||
'zenstruck/foundry', | ||
'2.4', | ||
'In order to use Foundry, you must use the trait "%s" in your "%s" tests. This will throw an exception in 3.0.', | ||
KernelTestCase::class, | ||
$class | ||
); | ||
} | ||
} | ||
} |
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 <[email protected]> | ||
* | ||
* 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\Attributes\RequiresPhpunit; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Test\ResetDatabase; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; | ||
|
||
#[RequiresPhpunit('>=11.0')] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only PhpUnit 11 handles correctly deprecation, so all tests in this namespace will only be ran with phpunit 11 |
||
final class KernelTestCaseWithBothTraitsInWrongOrderTest extends KernelTestCase | ||
{ | ||
use ResetDatabase, Factories; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in my different tests, I noticed that sometimes, the order of these traits could have an impact, so I wanted to test with both cases |
||
|
||
#[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()->get('.zenstruck_foundry.configuration'); | ||
|
||
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 <[email protected]> | ||
* | ||
* 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\Attributes\RequiresPhpunit; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Test\ResetDatabase; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; | ||
|
||
#[RequiresPhpunit('>=11.0')] | ||
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()->get('.zenstruck_foundry.configuration'); | ||
|
||
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 <[email protected]> | ||
* | ||
* 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\Attributes\RequiresPhpunit; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; | ||
|
||
#[RequiresPhpunit('>=11.0')] | ||
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()->get('.zenstruck_foundry.configuration'); | ||
|
||
Object1Factory::createOne(); | ||
|
||
$this->expectNotToPerformAssertions(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* 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\Attributes\RequiresPhpunit; | ||
use Zenstruck\Foundry\Test\ResetDatabase; | ||
|
||
#[RequiresPhpunit('>=11.0')] | ||
final class KernelTestCaseWithOnlyResetDatabaseTraitTest extends KernelTestCaseWithoutFactoriesTraitTestCase | ||
{ | ||
use ResetDatabase; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* 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\Attributes\RequiresPhpunit; | ||
|
||
#[RequiresPhpunit('>=11.0')] | ||
final class KernelTestCaseWithoutFactoriesTraitTest extends KernelTestCaseWithoutFactoriesTraitTestCase | ||
{ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at first I built everything to throw an error, but now I think we should not throw, it's a little bit too much violent I think 😆 🥊
But since everything is ready to throw, I propose to keep it like this in the exception