|
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\CategoryFactory; |
|
9
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\CommentFactory; |
|
10
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory; |
|
11
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\TagFactory; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Kevin Bond <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
final class ModelFactoryTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
use ExpectDeprecationTrait, Factories; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @test |
|
22
|
|
|
*/ |
|
23
|
|
|
public function can_set_states_with_method(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$this->assertFalse(PostFactory::createOne()->isPublished()); |
|
|
|
|
|
|
26
|
|
|
$this->assertTrue(PostFactory::new()->published()->create()->isPublished()); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @test |
|
31
|
|
|
*/ |
|
32
|
|
|
public function can_set_state_via_new(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->assertFalse(PostFactory::createOne()->isPublished()); |
|
35
|
|
|
$this->assertTrue(PostFactory::new('published')->create()->isPublished()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @test |
|
40
|
|
|
*/ |
|
41
|
|
|
public function can_instantiate(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->assertSame('title', PostFactory::new()->create(['title' => 'title'])->getTitle()); |
|
|
|
|
|
|
44
|
|
|
$this->assertSame('title', PostFactory::createOne(['title' => 'title'])->getTitle()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @test |
|
49
|
|
|
* @group legacy |
|
50
|
|
|
*/ |
|
51
|
|
|
public function can_instantiate_many_legacy(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$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)); |
|
54
|
|
|
|
|
55
|
|
|
$objects = PostFactory::new(['body' => 'body'])->createMany(2, ['title' => 'title']); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertCount(2, $objects); |
|
58
|
|
|
$this->assertSame('title', $objects[0]->getTitle()); |
|
59
|
|
|
$this->assertSame('body', $objects[1]->getBody()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
*/ |
|
65
|
|
|
public function can_instantiate_many(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$objects = PostFactory::createMany(2, ['title' => 'title']); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertCount(2, $objects); |
|
70
|
|
|
$this->assertSame('title', $objects[0]->getTitle()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @test |
|
75
|
|
|
*/ |
|
76
|
|
|
public function create_with_many_to_one_relation(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$object = PostFactory::createOne(['category' => CategoryFactory::new(['name' => 'My Name'])]); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertNull($object->getId()); |
|
|
|
|
|
|
81
|
|
|
$this->assertNull($object->getCategory()->getId()); |
|
|
|
|
|
|
82
|
|
|
$this->assertSame('My Name', $object->getCategory()->getName()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @test |
|
87
|
|
|
*/ |
|
88
|
|
|
public function create_with_one_to_many_relation(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$post = PostFactory::new()->create([ |
|
91
|
|
|
'comments' => CommentFactory::new()->many(4), |
|
92
|
|
|
]); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertNull($post->getId()); |
|
95
|
|
|
$this->assertCount(4, $post->getComments()); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @test |
|
100
|
|
|
*/ |
|
101
|
|
|
public function create_with_many_to_many_relation(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$post = PostFactory::createOne([ |
|
104
|
|
|
'tags' => TagFactory::new()->many(3), |
|
105
|
|
|
]); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertNull($post->getId()); |
|
108
|
|
|
$this->assertCount(3, $post->getTags()); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths