33namespace Gedmo \Tree ;
44
55use Doctrine \Common \EventManager ;
6+ use Gedmo \Tree \Entity \Repository \NestedTreeRepository ;
67use Tool \BaseTestCaseORM ;
8+ use Tree \Fixture \ForeignRootCategory ;
9+ use Tree \Fixture \RootAssociationCategory ;
710use Tree \Fixture \RootCategory ;
811
912/**
@@ -25,7 +28,7 @@ protected function setUp()
2528 $ evm ->addEventSubscriber (new TreeListener ());
2629
2730 $ this ->getMockSqliteEntityManager ($ evm );
28- $ this ->populate ();
31+ // $this->populate();
2932 }
3033
3134 /**
@@ -320,13 +323,186 @@ public function testRemoval()
320323 $ this ->assertEquals (4 , $ node ->getRight ());
321324 }
322325
326+
327+ /**
328+ * @throws \Doctrine\ORM\OptimisticLockException
329+ */
330+ public function testTreeWithRootPointingAtAnotherTable ()
331+ {
332+ // depopulate, i don't want the other stuff in db
333+ /** @var NestedTreeRepository $repo */
334+ $ repo = $ this ->em ->getRepository (ForeignRootCategory::class);
335+ $ all = $ repo ->findAll ();
336+ foreach ($ all as $ one ) {
337+ $ this ->em ->remove ($ one );
338+ }
339+ $ this ->em ->flush ();
340+
341+ $ fiction = new ForeignRootCategory ();
342+ $ fiction ->setTitle ('Fiction Books ' );
343+ $ fiction ->setRoot (1 ); // Lets pretend this points to another table, and root id 1 is "Books"
344+
345+ $ fact = new ForeignRootCategory ();
346+ $ fact ->setTitle ('Fact Books ' );
347+ $ fact ->setRoot (1 );
348+
349+ $ action = new ForeignRootCategory ();
350+ $ action ->setTitle ('Action ' );
351+ $ action ->setRoot (2 ); // Lets pretend this points to another table, and root id 2 is "Movies"
352+
353+ $ comedy = new ForeignRootCategory ();
354+ $ comedy ->setTitle ('Comedy ' );
355+ $ comedy ->setRoot (2 );
356+
357+ $ horror = new ForeignRootCategory ();
358+ $ horror ->setTitle ('Horror ' );
359+ $ horror ->setRoot (2 );
360+
361+ // Child categories now
362+ $ lotr = new ForeignRootCategory ();
363+ $ lotr ->setTitle ('Lord of the Rings ' );
364+ $ lotr ->setParent ($ fiction );
365+ $ lotr ->setRoot (1 );
366+
367+ $ warlock = new ForeignRootCategory ();
368+ $ warlock ->setTitle ('The Warlock of Firetop Mountain ' );
369+ $ warlock ->setParent ($ fiction );
370+ $ warlock ->setRoot (1 );
371+
372+ $ php = new ForeignRootCategory ();
373+ $ php ->setTitle ('PHP open source development ' );
374+ $ php ->setParent ($ fact );
375+ $ php ->setRoot (1 );
376+
377+ $ dracula = new ForeignRootCategory ();
378+ $ dracula ->setTitle ('Hammer Horror Dracula ' );
379+ $ dracula ->setParent ($ horror );
380+ $ dracula ->setRoot (2 );
381+
382+ $ frankenstein = new ForeignRootCategory ();
383+ $ frankenstein ->setTitle ('Hammer Horror Frankenstein ' );
384+ $ frankenstein ->setParent ($ horror );
385+ $ frankenstein ->setRoot (2 );
386+
387+ $ this ->em ->persist ($ fact );
388+ $ this ->em ->persist ($ fiction );
389+ $ this ->em ->persist ($ comedy );
390+ $ this ->em ->persist ($ horror );
391+ $ this ->em ->persist ($ action );
392+ $ this ->em ->persist ($ lotr );
393+ $ this ->em ->persist ($ warlock );
394+ $ this ->em ->persist ($ php );
395+ $ this ->em ->persist ($ dracula );
396+ $ this ->em ->persist ($ frankenstein );
397+ $ this ->em ->flush ();
398+
399+ $ this ->assertEquals (1 , $ fact ->getLeft ());
400+ $ this ->assertEquals (4 , $ fact ->getRight ());
401+ $ this ->assertEquals (0 , $ fact ->getLevel ());
402+ $ this ->assertEquals (1 , $ fact ->getRoot ());
403+ $ this ->assertNull ($ fact ->getParent ());
404+
405+ $ this ->assertEquals (5 , $ fiction ->getLeft ());
406+ $ this ->assertEquals (10 , $ fiction ->getRight ());
407+ $ this ->assertEquals (0 , $ fiction ->getLevel ());
408+ $ this ->assertEquals (1 , $ fiction ->getRoot ());
409+ $ this ->assertNull ($ fiction ->getParent ());
410+
411+ $ this ->assertEquals (6 , $ lotr ->getLeft ());
412+ $ this ->assertEquals (7 , $ lotr ->getRight ());
413+ $ this ->assertEquals (1 , $ lotr ->getLevel ());
414+ $ this ->assertEquals (1 , $ lotr ->getRoot ());
415+ $ this ->assertEquals ($ fiction , $ lotr ->getParent ());
416+
417+ $ this ->assertEquals (8 , $ warlock ->getLeft ());
418+ $ this ->assertEquals (9 , $ warlock ->getRight ());
419+ $ this ->assertEquals (1 , $ warlock ->getLevel ());
420+ $ this ->assertEquals (1 , $ warlock ->getRoot ());
421+ $ this ->assertEquals ($ fiction , $ warlock ->getParent ());
422+
423+ $ this ->assertEquals (2 , $ php ->getLeft ());
424+ $ this ->assertEquals (3 , $ php ->getRight ());
425+ $ this ->assertEquals (1 , $ php ->getLevel ());
426+ $ this ->assertEquals (1 , $ php ->getRoot ());
427+ $ this ->assertEquals ($ fact , $ php ->getParent ());
428+
429+ $ this ->assertEquals (1 , $ comedy ->getLeft ());
430+ $ this ->assertEquals (2 , $ comedy ->getRight ());
431+ $ this ->assertEquals (0 , $ comedy ->getLevel ());
432+ $ this ->assertEquals (2 , $ comedy ->getRoot ());
433+ $ this ->assertNull ($ comedy ->getParent ());
434+
435+ $ this ->assertEquals (3 , $ horror ->getLeft ());
436+ $ this ->assertEquals (8 , $ horror ->getRight ());
437+ $ this ->assertEquals (0 , $ horror ->getLevel ());
438+ $ this ->assertEquals (2 , $ horror ->getRoot ());
439+ $ this ->assertNull ($ horror ->getParent ());
440+
441+ $ this ->assertEquals (9 , $ action ->getLeft ());
442+ $ this ->assertEquals (10 , $ action ->getRight ());
443+ $ this ->assertEquals (0 , $ action ->getLevel ());
444+ $ this ->assertEquals (2 , $ action ->getRoot ());
445+ $ this ->assertNull ($ action ->getParent ());
446+
447+ $ this ->assertEquals (4 , $ dracula ->getLeft ());
448+ $ this ->assertEquals (5 , $ dracula ->getRight ());
449+ $ this ->assertEquals (1 , $ dracula ->getLevel ());
450+ $ this ->assertEquals (2 , $ dracula ->getRoot ());
451+ $ this ->assertEquals ($ horror , $ dracula ->getParent ());
452+
453+ $ this ->assertEquals (6 , $ frankenstein ->getLeft ());
454+ $ this ->assertEquals (7 , $ frankenstein ->getRight ());
455+ $ this ->assertEquals (1 , $ frankenstein ->getLevel ());
456+ $ this ->assertEquals (2 , $ frankenstein ->getRoot ());
457+ $ this ->assertEquals ($ horror , $ frankenstein ->getParent ());
458+
459+ // Now move the action movie category up
460+ $ repo ->moveUp ($ action );
461+
462+ $ this ->assertEquals (1 , $ comedy ->getLeft ());
463+ $ this ->assertEquals (2 , $ comedy ->getRight ());
464+ $ this ->assertEquals (0 , $ comedy ->getLevel ());
465+ $ this ->assertEquals (2 , $ comedy ->getRoot ());
466+ $ this ->assertNull ($ comedy ->getParent ());
467+
468+ $ this ->assertEquals (3 , $ action ->getLeft ());
469+ $ this ->assertEquals (4 , $ action ->getRight ());
470+ $ this ->assertEquals (0 , $ action ->getLevel ());
471+ $ this ->assertEquals (2 , $ action ->getRoot ());
472+ $ this ->assertNull ($ action ->getParent ());
473+
474+ $ this ->assertEquals (5 , $ horror ->getLeft ());
475+ $ this ->assertEquals (10 , $ horror ->getRight ());
476+ $ this ->assertEquals (0 , $ horror ->getLevel ());
477+ $ this ->assertEquals (2 , $ horror ->getRoot ());
478+ $ this ->assertNull ($ horror ->getParent ());
479+
480+ $ this ->assertEquals (6 , $ dracula ->getLeft ());
481+ $ this ->assertEquals (7 , $ dracula ->getRight ());
482+ $ this ->assertEquals (1 , $ dracula ->getLevel ());
483+ $ this ->assertEquals (2 , $ dracula ->getRoot ());
484+ $ this ->assertEquals ($ horror , $ dracula ->getParent ());
485+
486+ $ this ->assertEquals (8 , $ frankenstein ->getLeft ());
487+ $ this ->assertEquals (9 , $ frankenstein ->getRight ());
488+ $ this ->assertEquals (1 , $ frankenstein ->getLevel ());
489+ $ this ->assertEquals (2 , $ frankenstein ->getRoot ());
490+ $ this ->assertEquals ($ horror , $ frankenstein ->getParent ());
491+
492+ $ this ->em ->clear ();
493+ }
494+
323495 protected function getUsedEntityFixtures ()
324496 {
325497 return array (
326498 self ::CATEGORY ,
499+ ForeignRootCategory::class,
327500 );
328501 }
329502
503+ /**
504+ * @throws \Doctrine\ORM\OptimisticLockException
505+ */
330506 private function populate ()
331507 {
332508 $ root = new RootCategory ();
0 commit comments