Skip to content

Commit 229c628

Browse files
committed
Added tests
1 parent b6ee8ed commit 229c628

File tree

7 files changed

+150
-8
lines changed

7 files changed

+150
-8
lines changed

lib/Doctrine/ODM/PHPCR/DocumentClassMapper.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
*/
3131
class DocumentClassMapper implements DocumentClassMapperInterface
3232
{
33-
const CLASS_PROPERTY = 'phpcr:class';
34-
3533
private function expandClassName(DocumentManagerInterface $dm, $className = null)
3634
{
3735
if (null === $className) {
@@ -52,8 +50,8 @@ public function getClassName(DocumentManagerInterface $dm, NodeInterface $node,
5250
{
5351
$className = $this->expandClassName($dm, $className);
5452

55-
if ($node->hasProperty(self::CLASS_PROPERTY)) {
56-
$nodeClassName = $node->getProperty(self::CLASS_PROPERTY)->getString();
53+
if ($node->hasProperty('phpcr:class')) {
54+
$nodeClassName = $node->getProperty('phpcr:class')->getString();
5755

5856
if (!empty($className)
5957
&& $nodeClassName !== $className

lib/Doctrine/ODM/PHPCR/Tools/Console/Command/DocumentMigrateClassCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7979
$mapper = $documentManager->getConfiguration()->getDocumentClassMapper();
8080

8181
$input->setOption('query', sprintf(
82-
'SELECT * FROM [nt:base] WHERE [phpcr:class] = "%s"',
82+
'SELECT * FROM [nt:base] WHERE [%s] = "%s"',
8383
$classname
8484
));
8585

tests/Doctrine/Tests/Models/CMS/CmsArticleFolder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ class CmsArticleFolder
1414
* @PHPCRODM\Id
1515
*/
1616
public $id;
17+
18+
/**
19+
* @PHPCRODM\Children()
20+
*/
21+
public $articles;
1722
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\Models\CMS;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
7+
8+
/**
9+
* @PHPCRODM\Document(requiredClasses={"Doctrine\Tests\Models\CMS\CmsBlogPost"})
10+
*/
11+
class CmsBlogFolder
12+
{
13+
/**
14+
* @PHPCRODM\Id()
15+
*/
16+
public $id;
17+
18+
/**
19+
* @PHPCRODM\Children()
20+
*/
21+
public $posts;
22+
}
23+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Documents;
4+
5+
namespace Doctrine\Tests\Models\CMS;
6+
7+
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
8+
use Doctrine\ODM\PHPCR\DocumentRepository;
9+
use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface;
10+
11+
/**
12+
* @PHPCRODM\Document()
13+
*/
14+
class CmsBlogInvalidChild
15+
{
16+
/** @PHPCRODM\Id(strategy="parent") */
17+
public $id;
18+
19+
/** @PHPCRODM\NodeName() */
20+
public $name;
21+
22+
/** @PHPCRODM\ParentDocument() */
23+
public $parent;
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Documents;
4+
5+
namespace Doctrine\Tests\Models\CMS;
6+
7+
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
8+
use Doctrine\ODM\PHPCR\DocumentRepository;
9+
use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface;
10+
11+
/**
12+
* @PHPCRODM\Document()
13+
*/
14+
class CmsBlogPost
15+
{
16+
/** @PHPCRODM\Id(strategy="parent") */
17+
public $id;
18+
19+
/** @PHPCRODM\NodeName() */
20+
public $name;
21+
22+
/** @PHPCRODM\ParentDocument() */
23+
public $parent;
24+
}

tests/Doctrine/Tests/ODM/PHPCR/Functional/UnitOfWorkTest.php

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
use Doctrine\Tests\Models\CMS\CmsArticleFolder;
1313
use Doctrine\Tests\Models\CMS\CmsGroup;
1414
use Doctrine\Tests\Models\CMS\CmsArticle;
15+
use Doctrine\Common\Collections\ArrayCollection;
16+
use Doctrine\Tests\Models\CMS\CmsBlogPost;
17+
use Doctrine\Tests\Models\CMS\CmsBlogInvalidChild;
18+
use Doctrine\Tests\Models\CMS\CmsBlogFolder;
1519

1620
/**
1721
* @group functional
@@ -193,7 +197,7 @@ public function testFetchingMultipleHierarchicalObjectsWithChildIdFirst()
193197
* @expectedException Doctrine\ODM\PHPCR\Exception\OutOfBoundsException
194198
* @expectedExceptionMessage Document "Doctrine\Tests\Models\CMS\CmsArticleFolder" does not allow children of type "Doctrine\Tests\Models\CMS\CmsGroup". Allowed child classes "Doctrine\Tests\Models\CMS\CmsArticle"
195199
*/
196-
public function testRestrictChildrenInvalidChildren()
200+
public function testRequiredClassesInvalidChildren()
197201
{
198202
$articleFolder = new CmsArticleFolder();
199203
$articleFolder->id = '/functional/articles';
@@ -207,7 +211,7 @@ public function testRestrictChildrenInvalidChildren()
207211
$this->dm->flush();
208212
}
209213

210-
public function testRestrictChildrenValidChildren()
214+
public function testRequiredClassesValidChildren()
211215
{
212216
$articleFolder = new CmsArticleFolder();
213217
$articleFolder->id = '/functional/articles';
@@ -226,7 +230,7 @@ public function testRestrictChildrenValidChildren()
226230
* @expectedException Doctrine\ODM\PHPCR\Exception\OutOfBoundsException
227231
* @expectedExceptionMessage Document "Doctrine\Tests\Models\CMS\CmsArticleFolder" does not allow children of type "Doctrine\Tests\Models\CMS\CmsGroup". Allowed child classes "Doctrine\Tests\Models\CMS\CmsArticle"
228232
*/
229-
public function testRestrictChildrenInvalidUpdate()
233+
public function testRequiredClassesInvalidUpdate()
230234
{
231235
$articleFolder = new CmsArticleFolder();
232236
$articleFolder->id = '/functional/articles';
@@ -242,4 +246,68 @@ public function testRestrictChildrenInvalidUpdate()
242246
$this->dm->move($article, '/functional/articles/address');
243247
$this->dm->flush();
244248
}
249+
250+
public function testRequiredClassesAddToChildrenValid()
251+
{
252+
$post = new CmsBlogPost();
253+
$post->name = 'hello';
254+
255+
$postFolder = new CmsBlogFolder();
256+
$postFolder->id = '/functional/posts';
257+
$postFolder->posts = new ArrayCollection([
258+
$post
259+
]);
260+
261+
$this->dm->persist($postFolder);
262+
$this->dm->flush();
263+
}
264+
265+
/**
266+
* @expectedException Doctrine\ODM\PHPCR\Exception\OutOfBoundsException
267+
* @expectedExceptionMessage Document "Doctrine\Tests\Models\CMS\CmsBlogFolder" does not allow children of type "Doctrine\Tests\Models\CMS\CmsBlogInvalidChild". Allowed child classes "Doctrine\Tests\Models\CMS\CmsBlogPost"
268+
*/
269+
public function testRequiredClassesAddToChildrenInvalid()
270+
{
271+
$post = new CmsBlogInvalidChild();
272+
$post->name = 'hello';
273+
274+
$postFolder = new CmsBlogFolder();
275+
$postFolder->id = '/functional/posts';
276+
$postFolder->posts = new ArrayCollection([
277+
$post
278+
]);
279+
280+
$this->dm->persist($postFolder);
281+
$this->dm->flush();
282+
}
283+
284+
/**
285+
* @expectedException Doctrine\ODM\PHPCR\Exception\OutOfBoundsException
286+
* @expectedExceptionMessage Document "Doctrine\Tests\Models\CMS\CmsBlogFolder" does not allow children of type "Doctrine\Tests\Models\CMS\CmsBlogInvalidChild". Allowed child classes "Doctrine\Tests\Models\CMS\CmsBlogPost"
287+
*/
288+
public function testRequiredClassesAddToChildrenInvalidOnUpdate()
289+
{
290+
$post = new CmsBlogPost();
291+
$post->name = 'hello';
292+
293+
$postFolder = new CmsBlogFolder();
294+
$postFolder->id = '/functional/posts';
295+
$postFolder->posts = new ArrayCollection([
296+
$post
297+
]);
298+
299+
$this->dm->persist($postFolder);
300+
$this->dm->flush();
301+
$this->dm->clear();
302+
303+
$postFolder = $this->dm->find(null, '/functional/posts');
304+
305+
$post = new CmsBlogInvalidChild();
306+
$post->name = 'wolrd';
307+
$postFolder->posts->add($post);
308+
309+
$this->dm->persist($postFolder);
310+
$this->dm->flush();
311+
$this->dm->clear();
312+
}
245313
}

0 commit comments

Comments
 (0)