1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivoaz ContentEditable bundle. |
5
|
|
|
* |
6
|
|
|
* (c) Ivo Azirjans <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivoaz\Bundle\ContentEditableBundle\Tests\Manager; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
16
|
|
|
use Doctrine\ORM\UnitOfWork; |
17
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Manager\ContentManager; |
18
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Exception\MissingLocaleException; |
19
|
|
|
use Ivoaz\Bundle\ContentEditableBundle\Model\Content; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
22
|
|
|
|
23
|
|
|
class ContentManagerTest extends \PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ObjectManager|\PHPUnit_Framework_MockObject_MockObject |
27
|
|
|
*/ |
28
|
|
|
private $om; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ObjectRepository|\PHPUnit_Framework_MockObject_MockObject |
32
|
|
|
*/ |
33
|
|
|
private $repository; |
34
|
|
|
|
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
$this->repository = $this->getMock(ObjectRepository::class); |
38
|
|
|
|
39
|
|
|
$this->om = $this->getMock(ObjectManager::class); |
40
|
|
|
$this->om->method('getRepository') |
41
|
|
|
->with(Content::class) |
42
|
|
|
->willReturn($this->repository); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testUpdateFlushesObject() |
46
|
|
|
{ |
47
|
|
|
$manager = new ContentManager($this->om); |
48
|
|
|
|
49
|
|
|
$content = new Content(); |
50
|
|
|
|
51
|
|
|
$this->om->method('contains') |
52
|
|
|
->with($content) |
53
|
|
|
->willReturn(true); |
54
|
|
|
|
55
|
|
|
$this->om->expects($this->once()) |
|
|
|
|
56
|
|
|
->method('flush') |
57
|
|
|
->with($content); |
58
|
|
|
|
59
|
|
|
$manager->update($content); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testUpdateDetachesObject() |
63
|
|
|
{ |
64
|
|
|
$manager = new ContentManager($this->om); |
65
|
|
|
|
66
|
|
|
$content = new Content(); |
67
|
|
|
$managedContent = new Content(); |
68
|
|
|
|
69
|
|
|
$this->om->method('contains') |
70
|
|
|
->with($content) |
71
|
|
|
->willReturn(false); |
72
|
|
|
|
73
|
|
|
$this->om->expects($this->at(1)) |
|
|
|
|
74
|
|
|
->method('merge') |
75
|
|
|
->with($content) |
76
|
|
|
->willReturn($managedContent); |
77
|
|
|
|
78
|
|
|
$this->om->expects($this->at(2)) |
79
|
|
|
->method('flush') |
80
|
|
|
->with($managedContent); |
81
|
|
|
|
82
|
|
|
$this->om->expects($this->at(3)) |
83
|
|
|
->method('detach') |
84
|
|
|
->with($managedContent); |
85
|
|
|
|
86
|
|
|
$manager->update($content); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testGetReturnsFoundContentByNameAndLocale() |
90
|
|
|
{ |
91
|
|
|
$manager = new ContentManager($this->om); |
92
|
|
|
|
93
|
|
|
$expectedContent = new Content(); |
94
|
|
|
|
95
|
|
|
$this->repository->method('findOneBy') |
96
|
|
|
->with(['name' => 'name', 'locale' => 'en']) |
97
|
|
|
->willReturn($expectedContent); |
98
|
|
|
|
99
|
|
|
$content = $manager->get('name', 'en', 'text'); |
100
|
|
|
|
101
|
|
|
$this->assertSame($expectedContent, $content); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testGetCreatesNewContent() |
105
|
|
|
{ |
106
|
|
|
$manager = new ContentManager($this->om); |
107
|
|
|
|
108
|
|
|
$expectedContent = new Content(); |
109
|
|
|
$expectedContent->setName('name') |
110
|
|
|
->setText('text') |
111
|
|
|
->setLocale('en'); |
112
|
|
|
|
113
|
|
|
$this->repository->method('findOneBy') |
114
|
|
|
->with(['name' => 'name', 'locale' => 'en']) |
115
|
|
|
->willReturn(null); |
116
|
|
|
|
117
|
|
|
$this->om->expects($this->once()) |
|
|
|
|
118
|
|
|
->method('persist') |
119
|
|
|
->with( |
120
|
|
|
$this->callback( |
121
|
|
|
function ($value) use ($expectedContent) { |
122
|
|
|
return $value == $expectedContent; |
123
|
|
|
} |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$this->om->expects($this->once()) |
|
|
|
|
128
|
|
|
->method('flush') |
129
|
|
|
->with( |
130
|
|
|
$this->callback( |
131
|
|
|
function ($value) use ($expectedContent) { |
132
|
|
|
return $value == $expectedContent; |
133
|
|
|
} |
134
|
|
|
) |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$content = $manager->get('name', 'en', 'text'); |
138
|
|
|
|
139
|
|
|
$this->assertEquals($expectedContent, $content); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testGetUsesNameForDefaultWhenNotSpecified() |
143
|
|
|
{ |
144
|
|
|
$manager = new ContentManager($this->om); |
145
|
|
|
|
146
|
|
|
$expectedContent = new Content(); |
147
|
|
|
$expectedContent->setName('name') |
148
|
|
|
->setText('name') |
149
|
|
|
->setLocale('en'); |
150
|
|
|
|
151
|
|
|
$this->repository->method('findOneBy') |
152
|
|
|
->with(['name' => 'name', 'locale' => 'en']) |
153
|
|
|
->willReturn(null); |
154
|
|
|
|
155
|
|
|
$content = $manager->get('name', 'en'); |
156
|
|
|
|
157
|
|
|
$this->assertEquals($expectedContent, $content); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testGetDetachesExistingObject() |
161
|
|
|
{ |
162
|
|
|
$manager = new ContentManager($this->om); |
163
|
|
|
|
164
|
|
|
$content = new Content(); |
165
|
|
|
|
166
|
|
|
$this->repository->method('findOneBy') |
167
|
|
|
->with(['name' => 'name', 'locale' => 'en']) |
168
|
|
|
->willReturn($content); |
169
|
|
|
|
170
|
|
|
$this->om->expects($this->once()) |
|
|
|
|
171
|
|
|
->method('detach') |
172
|
|
|
->with($content); |
173
|
|
|
|
174
|
|
|
$manager->get('name', 'en', 'text'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testGetDetachesNewObject() |
178
|
|
|
{ |
179
|
|
|
$manager = new ContentManager($this->om); |
180
|
|
|
|
181
|
|
|
$this->repository->method('findOneBy') |
182
|
|
|
->with(['name' => 'name', 'locale' => 'en']) |
183
|
|
|
->willReturn(null); |
184
|
|
|
|
185
|
|
|
$content = new Content(); |
186
|
|
|
$content->setName('name') |
187
|
|
|
->setText('text') |
188
|
|
|
->setLocale('en'); |
189
|
|
|
|
190
|
|
|
$this->om->expects($this->once()) |
|
|
|
|
191
|
|
|
->method('detach') |
192
|
|
|
->with( |
193
|
|
|
$this->callback( |
194
|
|
|
function ($value) use ($content) { |
195
|
|
|
return $value == $content; |
196
|
|
|
} |
197
|
|
|
) |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$manager->get('name', 'en', 'text'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testGetGuessesLocaleFromRequest() |
204
|
|
|
{ |
205
|
|
|
$requests = new RequestStack(); |
206
|
|
|
$request = new Request(); |
207
|
|
|
$request->setLocale('en'); |
208
|
|
|
$requests->push($request); |
209
|
|
|
|
210
|
|
|
$manager = new ContentManager($this->om, $requests); |
211
|
|
|
|
212
|
|
|
$content = $manager->get('name'); |
213
|
|
|
|
214
|
|
|
$this->assertSame('en', $content->getLocale()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function testGetThrowsMissingLocaleException() |
218
|
|
|
{ |
219
|
|
|
$this->setExpectedException(MissingLocaleException::class); |
|
|
|
|
220
|
|
|
|
221
|
|
|
$manager = new ContentManager($this->om); |
222
|
|
|
$manager->get('name'); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: