zenstruck /
foundry
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Zenstruck\Foundry\Tests\Unit; |
||||||
| 4 | |||||||
| 5 | use PHPUnit\Framework\TestCase; |
||||||
| 6 | use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; |
||||||
| 7 | use Zenstruck\Foundry\Test\Factories; |
||||||
| 8 | use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory; |
||||||
| 9 | |||||||
| 10 | /** |
||||||
| 11 | * @author Kevin Bond <[email protected]> |
||||||
| 12 | */ |
||||||
| 13 | final class ModelFactoryTest extends TestCase |
||||||
| 14 | { |
||||||
| 15 | use ExpectDeprecationTrait, Factories; |
||||||
| 16 | |||||||
| 17 | /** |
||||||
| 18 | * @test |
||||||
| 19 | */ |
||||||
| 20 | public function can_set_states_with_method(): void |
||||||
| 21 | { |
||||||
| 22 | $this->assertFalse(PostFactory::createOne()->isPublished()); |
||||||
| 23 | $this->assertTrue(PostFactory::new()->published()->create()->isPublished()); |
||||||
| 24 | } |
||||||
| 25 | |||||||
| 26 | /** |
||||||
| 27 | * @test |
||||||
| 28 | */ |
||||||
| 29 | public function can_set_state_via_new(): void |
||||||
| 30 | { |
||||||
| 31 | $this->assertFalse(PostFactory::createOne()->isPublished()); |
||||||
| 32 | $this->assertTrue(PostFactory::new('published')->create()->isPublished()); |
||||||
| 33 | } |
||||||
| 34 | |||||||
| 35 | /** |
||||||
| 36 | * @test |
||||||
| 37 | */ |
||||||
| 38 | public function can_instantiate(): void |
||||||
| 39 | { |
||||||
| 40 | $this->assertSame('title', PostFactory::new()->create(['title' => 'title'])->getTitle()); |
||||||
| 41 | $this->assertSame('title', PostFactory::createOne(['title' => 'title'])->getTitle()); |
||||||
| 42 | } |
||||||
| 43 | |||||||
| 44 | /** |
||||||
| 45 | * @test |
||||||
| 46 | * @group legacy |
||||||
| 47 | */ |
||||||
| 48 | public function can_instantiate_many_legacy(): void |
||||||
| 49 | { |
||||||
| 50 | $this->expectDeprecation(\sprintf('Since zenstruck/foundry 1.7: Calling instance method "%1$s::createMany()" is deprecated and will be removed in 2.0, use the static "%1$s:createMany()" method instead.', PostFactory::class)); |
||||||
| 51 | |||||||
| 52 | $objects = PostFactory::new(['body' => 'body'])->createMany(2, ['title' => 'title']); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 53 | |||||||
| 54 | $this->assertCount(2, $objects); |
||||||
| 55 | $this->assertSame('title', $objects[0]->getTitle()); |
||||||
| 56 | $this->assertSame('body', $objects[1]->getBody()); |
||||||
| 57 | } |
||||||
| 58 | |||||||
| 59 | /** |
||||||
| 60 | * @test |
||||||
| 61 | */ |
||||||
| 62 | public function can_instantiate_many(): void |
||||||
| 63 | { |
||||||
| 64 | $objects = PostFactory::createMany(2, ['title' => 'title']); |
||||||
|
0 ignored issues
–
show
The method
createMany() does not exist on Zenstruck\Foundry\Tests\...s\Factories\PostFactory. Since you implemented __callStatic, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 65 | |||||||
| 66 | $this->assertCount(2, $objects); |
||||||
| 67 | $this->assertSame('title', $objects[0]->getTitle()); |
||||||
| 68 | } |
||||||
| 69 | } |
||||||
| 70 |