Passed
Push — master ( 1dccda...06b24d )
by Kevin
03:24
created

ModelFactoryTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 8
eloc 28
c 7
b 0
f 2
dl 0
loc 93
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A can_instantiate() 0 4 1
A can_instantiate_many() 0 6 1
A can_instantiate_many_legacy() 0 9 1
A can_set_state_via_new() 0 4 1
A can_set_states_with_method() 0 4 1
A create_with_one_to_many_relation() 0 8 1
A create_with_many_to_many_relation() 0 8 1
A create_with_many_to_one_relation() 0 7 1
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Unit;
4
5
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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());
0 ignored issues
show
Bug introduced by
The method isPublished() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, 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 ignore-call  annotation

25
        $this->assertFalse(PostFactory::createOne()->/** @scrutinizer ignore-call */ isPublished());
Loading history...
26
        $this->assertTrue(PostFactory::new()->published()->create()->isPublished());
0 ignored issues
show
Bug introduced by
The method published() does not exist on Zenstruck\Foundry\ModelFactory. Since you implemented __call, 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 ignore-call  annotation

26
        $this->assertTrue(PostFactory::new()->/** @scrutinizer ignore-call */ published()->create()->isPublished());
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method getTitle() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, 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 ignore-call  annotation

43
        $this->assertSame('title', PostFactory::new()->create(['title' => 'title'])->/** @scrutinizer ignore-call */ getTitle());
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, 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 ignore-call  annotation

80
        $this->assertNull($object->/** @scrutinizer ignore-call */ getId());
Loading history...
81
        $this->assertNull($object->getCategory()->getId());
0 ignored issues
show
Bug introduced by
The method getCategory() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, 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 ignore-call  annotation

81
        $this->assertNull($object->/** @scrutinizer ignore-call */ getCategory()->getId());
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method getComments() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, 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 ignore-call  annotation

95
        $this->assertCount(4, $post->/** @scrutinizer ignore-call */ getComments());
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method getTags() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, 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 ignore-call  annotation

108
        $this->assertCount(3, $post->/** @scrutinizer ignore-call */ getTags());
Loading history...
109
    }
110
}
111