forked from zenstruck/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: throw an error if Factories trait is not used in a KernelTestCase
- Loading branch information
Showing
13 changed files
with
384 additions
and
1 deletion.
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
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 <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\Exception; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Test\Factories; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <nikophil@gmail.com> | ||
*/ | ||
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 | ||
); | ||
} | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithBothTraitsInWrongOrderTest.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,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 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 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()->get('.zenstruck_foundry.configuration'); | ||
|
||
Object1Factory::createOne(); | ||
|
||
$this->expectNotToPerformAssertions(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithBothTraitsTest.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,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 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(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithFactoriesTraitTest.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,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 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(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithOnlyResetDatabaseTraitTest.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,50 @@ | ||
<?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\Attributes\IgnoreDeprecations; | ||
use PHPUnit\Framework\Attributes\RequiresPhpunit; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Test\ResetDatabase; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; | ||
|
||
#[RequiresPhpunit('>=11.0')] | ||
final class KernelTestCaseWithOnlyResetDatabaseTraitTest extends KernelTestCase | ||
{ | ||
use ResetDatabase; | ||
|
||
#[Test] | ||
public function not_using_foundry_should_not_throw(): void | ||
{ | ||
$this->expectNotToPerformAssertions(); | ||
} | ||
|
||
#[Test] | ||
public function not_using_foundry_should_not_throw_even_when_container_is_used(): void | ||
{ | ||
self::getContainer()->get('.zenstruck_foundry.configuration'); | ||
|
||
$this->expectNotToPerformAssertions(); | ||
} | ||
|
||
#[Test] | ||
#[IgnoreDeprecations] | ||
public function using_foundry_should_throw(): void | ||
{ | ||
$this->expectUserDeprecationMessageMatches('/In order to use Foundry, you must use the trait/'); | ||
|
||
Object1Factory::createOne(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
tests/Integration/ForceFactoriesTraitUsage/KernelTestCaseWithoutFactoriesTraitTest.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,52 @@ | ||
<?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\Attributes\IgnoreDeprecations; | ||
use PHPUnit\Framework\Attributes\RequiresPhpunit; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Object1Factory; | ||
|
||
#[RequiresPhpunit('>=11.0')] | ||
final class KernelTestCaseWithoutFactoriesTraitTest extends KernelTestCase | ||
{ | ||
#[Test] | ||
#[IgnoreDeprecations] | ||
public function using_foundry_without_trait_should_throw(): void | ||
{ | ||
$this->expectUserDeprecationMessageMatches('/In order to use Foundry, you must use the trait/'); | ||
|
||
Object1Factory::createOne(); | ||
} | ||
|
||
#[Test] | ||
#[IgnoreDeprecations] | ||
public function using_foundry_without_trait_should_throw_even_when_kernel_is_booted(): void | ||
{ | ||
$this->expectUserDeprecationMessageMatches('/In order to use Foundry, you must use the trait/'); | ||
|
||
self::getContainer()->get('.zenstruck_foundry.configuration'); | ||
|
||
Object1Factory::createOne(); | ||
} | ||
|
||
#[Test] | ||
public function booting_kernel_without_using_foundry_and_without_factories_trait_should_not_throw(): void | ||
{ | ||
self::getContainer()->get('.zenstruck_foundry.configuration'); | ||
|
||
$this->expectNotToPerformAssertions(); | ||
} | ||
} |
Oops, something went wrong.