Passed
Pull Request — master (#38)
by Kevin
20:37
created

FunctionsTest::collection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Unit;
4
5
use Doctrine\Persistence\ManagerRegistry;
6
use Doctrine\Persistence\ObjectManager;
7
use Doctrine\Persistence\ObjectRepository;
8
use Zenstruck\Foundry\Proxy;
9
use Zenstruck\Foundry\RepositoryProxy;
10
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category;
11
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post;
12
use Zenstruck\Foundry\Tests\UnitTestCase;
13
use function Zenstruck\Foundry\create;
14
use function Zenstruck\Foundry\create_many;
15
use function Zenstruck\Foundry\faker;
16
use function Zenstruck\Foundry\instantiate;
17
use function Zenstruck\Foundry\instantiate_many;
18
use function Zenstruck\Foundry\repository;
19
20
/**
21
 * @author Kevin Bond <[email protected]>
22
 */
23
final class FunctionsTest extends UnitTestCase
24
{
25
    /**
26
     * @test
27
     */
28
    public function faker(): void
29
    {
30
        $this->assertIsString(faker()->name);
31
    }
32
33
    /**
34
     * @test
35
     */
36
    public function instantiate(): void
37
    {
38
        $proxy = instantiate(Post::class, ['title' => 'title', 'body' => 'body']);
39
40
        $this->assertInstanceOf(Post::class, $proxy->object());
41
        $this->assertFalse($proxy->isPersisted());
42
        $this->assertSame('title', $proxy->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

42
        $this->assertSame('title', $proxy->/** @scrutinizer ignore-call */ getTitle());
Loading history...
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function instantiate_many(): void
49
    {
50
        $objects = instantiate_many(3, Category::class);
51
52
        $this->assertCount(3, $objects);
53
        $this->assertInstanceOf(Category::class, $objects[0]->object());
54
        $this->assertFalse($objects[0]->isPersisted());
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function create(): void
61
    {
62
        $registry = $this->createMock(ManagerRegistry::class);
63
        $registry
64
            ->expects($this->exactly(2))
65
            ->method('getManagerForClass')
66
            ->with(Category::class)
67
            ->willReturn($this->createMock(ObjectManager::class))
68
        ;
69
70
        $this->configuration->setManagerRegistry($registry);
0 ignored issues
show
Bug introduced by
The method setManagerRegistry() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $this->configuration->/** @scrutinizer ignore-call */ 
71
                              setManagerRegistry($registry);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
72
        $object = create(Category::class);
73
74
        $this->assertInstanceOf(Proxy::class, $object);
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function create_many(): void
81
    {
82
        $registry = $this->createMock(ManagerRegistry::class);
83
        $registry
84
            ->expects($this->exactly(6))
85
            ->method('getManagerForClass')
86
            ->with(Category::class)
87
            ->willReturn($this->createMock(ObjectManager::class))
88
        ;
89
90
        $this->configuration->setManagerRegistry($registry);
91
92
        $objects = create_many(3, Category::class);
93
94
        $this->assertCount(3, $objects);
95
    }
96
97
    /**
98
     * @test
99
     */
100
    public function repository(): void
101
    {
102
        $registry = $this->createMock(ManagerRegistry::class);
103
        $registry
104
            ->expects($this->once())
105
            ->method('getRepository')
106
            ->with(Category::class)
107
            ->willReturn($this->createMock(ObjectRepository::class))
108
        ;
109
110
        $this->configuration->setManagerRegistry($registry);
111
112
        $this->assertInstanceOf(RepositoryProxy::class, repository(new Category()));
113
    }
114
115
    /**
116
     * @test
117
     */
118
    public function collection(): void
119
    {
120
        $this->markTestIncomplete();
121
    }
122
}
123