Passed
Pull Request — master (#185)
by Tobias
02:47
created

ModelFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 5
eloc 16
c 6
b 0
f 2
dl 0
loc 55
rs 10

5 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
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\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());
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

22
        $this->assertFalse(PostFactory::createOne()->/** @scrutinizer ignore-call */ isPublished());
Loading history...
23
        $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

23
        $this->assertTrue(PostFactory::new()->/** @scrutinizer ignore-call */ published()->create()->isPublished());
Loading history...
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());
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

40
        $this->assertSame('title', PostFactory::new()->create(['title' => 'title'])->/** @scrutinizer ignore-call */ getTitle());
Loading history...
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
The method createMany() 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

52
        $objects = PostFactory::new(['body' => 'body'])->/** @scrutinizer ignore-call */ createMany(2, ['title' => 'title']);
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
Bug introduced by
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 ignore-call  annotation

64
        /** @scrutinizer ignore-call */ 
65
        $objects = PostFactory::createMany(2, ['title' => 'title']);
Loading history...
65
66
        $this->assertCount(2, $objects);
67
        $this->assertSame('title', $objects[0]->getTitle());
68
    }
69
}
70