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