pastebin

Paste Search Dynamic
Recent pastes
relationships cyclic
  1. <?php declare(strict_types = 1);
  2.  
  3. /**
  4.  * @testCase
  5.  * @dataProvider ../../../sections.ini
  6.  */
  7.  
  8. namespace NextrasTestsOrmIntegrationRelationships;
  9.  
  10. use Mockery;
  11. use NextrasOrmInvalidStateException;
  12. use NextrasOrmModelIModel;
  13. use NextrasTestsOrmAuthor;
  14. use NextrasTestsOrmBook;
  15. use NextrasTestsOrmDataTestCase;
  16. use NextrasTestsOrmPhoto;
  17. use NextrasTestsOrmPhotoAlbum;
  18. use NextrasTestsOrmPublisher;
  19. use TesterAssert;
  20.  
  21. $dic = require_once __DIR__ . '/../../../bootstrap.php';
  22.  
  23.  
  24. class RelationshipCyclicTest extends DataTestCase
  25. {
  26.         public function testNotCycle()
  27.         {
  28.                 $publisher = new Publisher();
  29.                 $publisher->name = 'Jupiter Mining Corporation';
  30.  
  31.                 $author = new Author();
  32.                 $author->name = 'Arnold Judas Rimmer';
  33.  
  34.                 $translator = new Author();
  35.                 $translator->name = 'Dave Lister';
  36.                 $translator->favoredBy->add($author);
  37.  
  38.                 $book = new Book();
  39.                 $book->title = 'Better Than Life';
  40.                 $book->publisher = $publisher;
  41.                 $book->author = $author;
  42.                 $book->translator = $translator;
  43.  
  44.                 $this->orm->persist($author);
  45.  
  46.                 assert::true($publisher->isPersisted());
  47.                 assert::true($author->isPersisted());
  48.                 assert::true($translator->isPersisted());
  49.                 assert::true($book->isPersisted());
  50.         }
  51.  
  52.  
  53.         public function testCycleCheck()
  54.         {
  55.                 $album = new PhotoAlbum();
  56.                 $album->title = 'album 1';
  57.                 $photo1 = new Photo();
  58.                 $photo1->title = 'photo 1';
  59.                 $photo1->album = $album;
  60.                 $photo2 = new Photo();
  61.                 $photo2->title = 'photo 2';
  62.                 $photo2->album = $album;
  63.                 $photo3 = new Photo();
  64.                 $photo3->title = 'photo 3';
  65.                 $photo3->album = $album;
  66.                 $album->preview = $photo2;
  67.  
  68.                 assert::throws(function () use ($album) {
  69.                         $this->orm->persist($album);
  70.                 }, InvalidStateException::class, 'Persist cycle detected in NextrasTestsOrmPhoto::$album - NextrasTestsOrmPhotoAlbum::$preview. Use manual two phase persist.');
  71.  
  72.                 assert::throws(function () use ($photo2) {
  73.                         $this->orm->persist($photo2);
  74.                 }, InvalidStateException::class, 'Persist cycle detected in NextrasTestsOrmPhotoAlbum::$preview - NextrasTestsOrmPhoto::$album. Use manual two phase persist.');
  75.         }
  76.  
  77.  
  78.         public function testCycleManualPersist()
  79.         {
  80.                 $album = new PhotoAlbum();
  81.                 $album->title = 'album 1';
  82.                 $photo1 = new Photo();
  83.                 $photo1->title = 'photo 1';
  84.                 $photo1->album = $album;
  85.                 $photo2 = new Photo();
  86.                 $photo2->title = 'photo 2';
  87.                 $photo2->album = $album;
  88.                 $photo3 = new Photo();
  89.                 $photo3->title = 'photo 3';
  90.                 $photo3->album = $album;
  91.  
  92.                 $this->orm->persist($album);
  93.                 $album->preview = $photo2;
  94.                 $this->orm->persist($album);
  95.  
  96.                 assert::true($album->isPersisted());
  97.                 assert::true($photo1->isPersisted());
  98.                 assert::true($photo2->isPersisted());
  99.                 assert::true($photo3->isPersisted());
  100.         }
  101. }
  102.  
  103.  
  104. $test = new RelationshipCyclicTest($dic);
  105. $test->run();
  106. ?>
Parsed in 0.095 seconds