-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathFakerTest.php
More file actions
73 lines (59 loc) · 2.29 KB
/
Copy pathFakerTest.php
File metadata and controls
73 lines (59 loc) · 2.29 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
<?php
declare(strict_types=1);
use App\Entities\Dungeon;
use App\Entities\Monster;
use App\Models\DungeonModel;
use CodeIgniter\Test\Fabricator;
use Tests\Support\DatabaseTestCase;
use Tests\Support\Fakers\MonsterFaker;
/**
* We have already defined a special kind of Model in the _support/Fakers
* folder that contains the `fake()` method. This test shows off some
* of the ways you might use CodeIgniter's Fabricator to create and test
* different scenarios.
*
* @internal
*/
final class FakerTest extends DatabaseTestCase
{
private Fabricator $fabricator;
protected function setUp(): void
{
parent::setUp();
// Get an instance of Fabricator ready to use our Faker
$this->fabricator = new Fabricator(MonsterFaker::class);
// Let Fabricator know about the dungeons we already created in PlaygroundSeeder
Fabricator::setCount('dungeons', 3);
}
// Ensure that our Faker is returning a valid Monster
public function testMakesValidMonster()
{
// We can use make() to generate a random dataset defined in our Faker
$monster = $this->fabricator->make();
$this->assertInstanceOf(Monster::class, $monster);
$this->assertGreaterThanOrEqual(1, $monster->health);
}
// Since our Faker uses Fabricator counts for its dungeon_id we should always have a valid dungeon available
public function testMakesMonsterWithDungeon()
{
/** @var Monster $monster */
$monster = $this->fabricator->make();
$dungeon = model(DungeonModel::class)->find($monster->dungeon_id);
$this->assertInstanceOf(Dungeon::class, $dungeon);
}
public function testCreateAddsToDatabase()
{
// create() generates a random dataset just like make() but also adds it to the database for us
/** @var Monster $monster */
$monster = $this->fabricator->create();
$this->assertIsInt($monster->id);
$this->seeInDatabase('monsters', ['id' => $monster->id]);
}
public function testHelperUsesFaker()
{
// test_helper comes with the fake() method that does the same as above without all the set up
$monster = fake(MonsterFaker::class);
$this->assertIsInt($monster->id);
$this->seeInDatabase('monsters', ['id' => $monster->id]);
}
}