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 PHPUnit\Framework\TestCase; |
9
|
|
|
use Zenstruck\Foundry\PersistenceManager; |
10
|
|
|
use Zenstruck\Foundry\Proxy; |
11
|
|
|
use Zenstruck\Foundry\RepositoryProxy; |
12
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category; |
13
|
|
|
use Zenstruck\Foundry\Tests\ResetGlobals; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Kevin Bond <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class PersistenceManagerTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
use ResetGlobals; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @test |
24
|
|
|
*/ |
25
|
|
|
public function can_get_repository_for_object(): void |
26
|
|
|
{ |
27
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
28
|
|
|
$registry |
29
|
|
|
->expects($this->once()) |
30
|
|
|
->method('getRepository') |
31
|
|
|
->with(Category::class) |
32
|
|
|
->willReturn($this->createMock(ObjectRepository::class)) |
33
|
|
|
; |
34
|
|
|
|
35
|
|
|
PersistenceManager::register($registry); |
36
|
|
|
|
37
|
|
|
$this->assertInstanceOf(RepositoryProxy::class, PersistenceManager::repositoryFor(new Category())); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @test |
42
|
|
|
*/ |
43
|
|
|
public function can_get_repository_for_class(): void |
44
|
|
|
{ |
45
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
46
|
|
|
$registry |
47
|
|
|
->expects($this->once()) |
48
|
|
|
->method('getRepository') |
49
|
|
|
->with(Category::class) |
50
|
|
|
->willReturn($this->createMock(ObjectRepository::class)) |
51
|
|
|
; |
52
|
|
|
|
53
|
|
|
PersistenceManager::register($registry); |
54
|
|
|
|
55
|
|
|
$this->assertInstanceOf(RepositoryProxy::class, PersistenceManager::repositoryFor(Category::class)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
*/ |
61
|
|
|
public function can_get_repository_for_object_proxy(): void |
62
|
|
|
{ |
63
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
64
|
|
|
$registry |
65
|
|
|
->expects($this->once()) |
66
|
|
|
->method('getRepository') |
67
|
|
|
->with(Category::class) |
68
|
|
|
->willReturn($this->createMock(ObjectRepository::class)) |
69
|
|
|
; |
70
|
|
|
|
71
|
|
|
PersistenceManager::register($registry); |
72
|
|
|
|
73
|
|
|
$proxy = (new Proxy(new Category()))->withoutAutoRefresh(); |
74
|
|
|
|
75
|
|
|
$this->assertInstanceOf(RepositoryProxy::class, PersistenceManager::repositoryFor($proxy)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
*/ |
81
|
|
|
public function can_persist_object(): void |
82
|
|
|
{ |
83
|
|
|
$category = new Category(); |
84
|
|
|
|
85
|
|
|
$manager = $this->createMock(ObjectManager::class); |
86
|
|
|
$manager->expects($this->once())->method('persist')->with($category); |
87
|
|
|
$manager->expects($this->once())->method('flush'); |
88
|
|
|
|
89
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
90
|
|
|
$registry |
91
|
|
|
->expects($this->once()) |
92
|
|
|
->method('getManagerForClass') |
93
|
|
|
->with(Category::class) |
94
|
|
|
->willReturn($manager) |
95
|
|
|
; |
96
|
|
|
|
97
|
|
|
PersistenceManager::register($registry); |
98
|
|
|
|
99
|
|
|
$object = PersistenceManager::persist($category); |
100
|
|
|
|
101
|
|
|
$this->assertSame($category, $object); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @test |
106
|
|
|
*/ |
107
|
|
|
public function proxying_a_proxy_returns_the_proxy(): void |
108
|
|
|
{ |
109
|
|
|
$proxy = PersistenceManager::proxy(new Category()); |
110
|
|
|
|
111
|
|
|
$this->assertSame($proxy, PersistenceManager::proxy($proxy)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @test |
116
|
|
|
*/ |
117
|
|
|
public function exception_thrown_if_no_manager_registry_registered(): void |
118
|
|
|
{ |
119
|
|
|
$this->expectException(\RuntimeException::class); |
120
|
|
|
$this->expectExceptionMessage('ManagerRegistry not registered...'); |
121
|
|
|
|
122
|
|
|
PersistenceManager::objectManagerFor(Category::class); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @test |
127
|
|
|
*/ |
128
|
|
|
public function exception_thrown_if_manager_does_not_manage_object(): void |
129
|
|
|
{ |
130
|
|
|
PersistenceManager::register($this->createMock(ManagerRegistry::class)); |
131
|
|
|
|
132
|
|
|
$this->expectException(\RuntimeException::class); |
133
|
|
|
$this->expectExceptionMessage(\sprintf('No object manager registered for "%s".', Category::class)); |
134
|
|
|
|
135
|
|
|
PersistenceManager::objectManagerFor(Category::class); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|