-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathModelFactoryTest.php
More file actions
178 lines (138 loc) · 4.91 KB
/
Copy pathModelFactoryTest.php
File metadata and controls
178 lines (138 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Tests\Tempest\Integration\Testing;
use PHPUnit\Framework\Attributes\Test;
use Tempest\Database\Migrations\CreateMigrationsTable;
use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable;
use Tests\Tempest\Fixtures\Migrations\CreateBookTable;
use Tests\Tempest\Fixtures\Migrations\CreatePublishersTable;
use Tests\Tempest\Fixtures\Modules\Books\Models\Author;
use Tests\Tempest\Fixtures\Modules\Books\Models\AuthorType;
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
use Tests\Tempest\Fixtures\Modules\Books\Models\Isbn;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use function Tempest\Framework\Testing\factory;
final class ModelFactoryTest extends FrameworkIntegrationTestCase
{
#[Test]
public function test_make(): void
{
$book = factory(Book::class)->make();
$this->assertInstanceOf(Book::class, $book);
// @phpstan-ignore-next-line
$this->assertNotNull($book->title);
}
#[Test]
public function test_make_with_data(): void
{
$author = new Author(name: 'Brent');
$factory = factory(Book::class)->with(author: $author);
$a = $factory->with(title: 'A')->make();
$b = $factory->with(title: 'B')->make();
$this->assertSame('A', $a->title);
$this->assertSame($author, $a->author);
$this->assertSame('B', $b->title);
$this->assertSame($author, $b->author);
}
#[Test]
public function test_with_is_immutable(): void
{
$factory = factory(Book::class)->with(title: 'A');
$factory->with(title: 'B');
$a = $factory->make();
$this->assertSame('A', $a->title);
}
#[Test]
public function test_with_nested_factories(): void
{
$bookFactory = factory(Book::class);
$authorFactory = factory(Author::class)->with(name: 'Brent');
$bookFactory = $bookFactory->with(author: $authorFactory);
$book = $bookFactory->make();
$this->assertSame('Brent', $book->author->name);
}
#[Test]
public function test_times(): void
{
$books = factory(Book::class)->times(3)->make();
$this->assertCount(3, $books);
}
#[Test]
public function test_times_with_items(): void
{
$books = factory(Book::class)->times([
['title' => 'A'],
['title' => 'B'],
['title' => 'C'],
])->make();
$this->assertCount(3, $books);
$this->assertSame('A', $books[0]->title);
$this->assertSame('B', $books[1]->title);
$this->assertSame('C', $books[2]->title);
}
#[Test]
public function test_times_with_nested_factories(): void
{
$authorFactory = factory(Author::class)->with(name: 'Brent');
$books = factory(Book::class)->times([
['author' => $authorFactory->with(name: 'Roose')],
['author' => $authorFactory],
['author' => $authorFactory],
])->make();
$this->assertSame('Roose', $books[0]->author->name);
$this->assertSame('Brent', $books[1]->author->name);
$this->assertSame('Brent', $books[2]->author->name);
}
#[Test]
public function test_save_to_the_database(): void
{
$this->database->migrate(
CreateMigrationsTable::class,
CreatePublishersTable::class,
CreateAuthorTable::class,
CreateBookTable::class,
);
$book = factory(Book::class)->save();
$this->database->assertTableHasRow('books', id: $book->id, title: $book->title);
}
#[Test]
public function test_save_to_the_database_with_nested_relation(): void
{
$this->database->migrate(
CreateMigrationsTable::class,
CreatePublishersTable::class,
CreateAuthorTable::class,
CreateBookTable::class,
);
factory(Book::class)->with(author: factory(Author::class)->with(name: 'Brent'))->save();
$this->database->assertTableHasRow('authors', name: 'Brent');
}
#[Test]
public function test_items_save_to_the_database(): void
{
$this->database->migrate(
CreateMigrationsTable::class,
CreatePublishersTable::class,
CreateAuthorTable::class,
CreateBookTable::class,
);
[$a, $b] = factory(Book::class)->times(2)->save();
$this->database->assertTableHasRow('books', id: $a->id, title: $a->title);
$this->database->assertTableHasRow('books', id: $b->id, title: $b->title);
}
#[Test]
public function test_make_with_nested_object(): void
{
$isbn = factory(Isbn::class)->make();
$this->assertInstanceOf(Book::class, $isbn->book);
}
#[Test]
public function test_make_with_enum(): void
{
$author = factory(AuthorWithEnum::class)->make();
$this->assertInstanceOf(AuthorType::class, $author->type);
}
}
class AuthorWithEnum
{
public AuthorType $type;
}